package cn.iocoder.yudao.module.pms.message;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.pms.constant.PmsConstants;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotopeationfill.IotOpeationFillDO;
import cn.iocoder.yudao.module.pms.service.iotopeationfill.IotOpeationFillService;
import cn.iocoder.yudao.module.system.api.dingtalk.DingtalkSendApi;
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class PmsMessage {
    @Autowired
    private NotifyMessageSendApi notifyMessageSendApi;
    @Resource
    private DingtalkSendApi dingtalkSendApi;
    @Resource
    private IotOpeationFillService iotOpeationFillService;
    @Value("${system.url}")
    private String systemUrl;

    public void sendMessage(Long businessId, String name, String businessType, Long userId, String mobile){

        IotOpeationFillDO fill = new IotOpeationFillDO();
        fill.setId(businessId);
        IotOpeationFillDO fillDO = iotOpeationFillService.getFillById(fill);

        Map<String, Object> templateParams = new HashMap<>();

        if(PmsConstants.GENERATE_OPERATION.equals(businessType)){
            templateParams.put("name", name);
            templateParams.put("businessType", businessType);
            templateParams.put("businessId", businessId);
            templateParams.put("deptId",fillDO.getDeptId());
            templateParams.put("userId",userId);
            templateParams.put("orderStatus",fillDO.getOrderStatus());
            ZoneId zoneId = ZoneId.of("UTC");
            ZonedDateTime zonedDateTime = fillDO.getCreateTime().atZone(zoneId);
            Instant instant = zonedDateTime.toInstant();
            long timestamp = instant.toEpochMilli();
            templateParams.put("createTime",timestamp);
            templateParams.put("userName",fillDO.getUserName());
            templateParams.put("orderName",fillDO.getOrderName());

        }else{
            templateParams.put("name", name);
            templateParams.put("businessType", businessType);
            templateParams.put("businessId", businessId);
            if (PmsConstants.ALARM_MESSAGE.equals(businessType)) {
                String[] split = name.split(",");
                templateParams.put("name", split[0]);
                templateParams.put("identifier", split[1]);
            }
        }

        notifyMessageSendApi.sendSingleMessageToAdmin(new NotifySendSingleToUserReqDTO()
                .setUserId(userId).setTemplateCode(businessType).setTemplateParams(templateParams));
        String title = "DeepOil-PMS消息提醒";
        if (StrUtil.isNotBlank(mobile)) {
            //发送钉钉消息
            String msg = "您有新的巡检工单待处理，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            if (PmsConstants.MATERIAL_REQUISITION.equals(businessType)) {
                msg = "物料领用单 " + name + " 已生成，请知悉。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.GENERATE_MAINTAIN.equals(businessType)) {
                msg = "您有新的维修工单 " + name + "，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.GENERATE_MAINTENANCE.equals(businessType)) {
                msg = "您有新的保养工单 " + name + "，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.DAILY_REPORT.equals(businessType)) {
                msg = "您有新的生产日报 " + name + "，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.DAILY_REPORT_APPROVAL.equals(businessType)) {
                msg = "您有新的生产日报 " + name + "需要审批，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.DAILY_REPORT_DETAIL.equals(businessType)) {
                msg = "生产日报 " + name + "已经更新，请查阅。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.RH_DAILY_REPORT_APPROVAL.equals(businessType)) {
                msg = "您有新的生产日报 " + name + "需要审批，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.RH_DAILY_REPORT.equals(businessType)) {
                msg = "您有新的生产日报 " + name + "需要填报，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            // 瑞鹰钻井日报
            if (PmsConstants.RY_DAILY_REPORT_APPROVAL.equals(businessType)) {
                msg = "您有新的钻井生产日报 " + name + "需要审批，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.RY_DAILY_REPORT.equals(businessType)) {
                msg = "您有新的钻井生产日报 " + name + "需要填报，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            // 瑞鹰修井日报
            if (PmsConstants.RY_XJ_DAILY_REPORT_APPROVAL.equals(businessType)) {
                msg = "您有新的修井生产日报 " + name + "需要审批，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.RY_XJ_DAILY_REPORT.equals(businessType)) {
                msg = "您有新的修井生产日报 " + name + "需要填报，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }

            // 瑞鹰项目启动设备整改
            if (PmsConstants.RY_IMPROVE_DAILY_REPORT.equals(businessType)) {
                msg = "您有新的项目启动设备整改日报 " + name + "需要填报，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.RY_IMPROVE_DAILY_REPORT_APPROVAL.equals(businessType)) {
                msg = "您有新的项目启动设备整改日报 " + name + "需要审批，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }

            if (PmsConstants.GENERATE_OPERATION.equals(businessType)) {
                msg = "您有新的运行记录工单 " + name + "，请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.FAILURE_NO_PASS.equals(businessType)) {
                msg = name+"故障上报流程审批完成，没有生成维修工单，将设备的状态调整为观察使用。";
            }
            if (PmsConstants.ALARM_MESSAGE.equals(businessType)) {
                String[] split = name.split(",");
                msg = split[0]+"的"+split[1]+"数采的数值近十分钟内不在设定区间内，请前往系统查看。";
            }
            if (PmsConstants.GENERATE_QHSE_CERT.equals(businessType)) {
                msg = "您有QHSE相关证书"+name+"已过期,请处理。"+"["+ DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss")+"]";
            }
            if (PmsConstants.QHSE_ACCIDENT.equals(businessType)) {
                msg = "【事故事件上报】:"+name;
                title = "DeepOil-QHSE消息提醒";
            }
            if (PmsConstants.WEEK_ALARM_MESSAGE.equals(businessType)) {
                msg = name;
                title = "DeepOil-QHSE消息提醒";
            }
            String url = "";

            if(PmsConstants.GENERATE_OPERATION.equals(businessType)){
                ZoneId zoneId = ZoneId.of("UTC");
                ZonedDateTime zonedDateTime = fillDO.getCreateTime().atZone(zoneId);
                Instant instant = zonedDateTime.toInstant();
                long timestamp = instant.toEpochMilli();
                url=systemUrl + "dingding?deptId="+fillDO.getDeptId()+"&userId="+userId
                        +"&createTime="+timestamp+"&id="
                        +businessId+"&orderStatus="+fillDO.getOrderStatus()+"&type="+businessType+"&userName="+fillDO.getUserName()+"&orderName="+fillDO.getOrderName();
            }else{
                url=systemUrl + "dingding?id="+businessId+"&type="+businessType+"&userId="+userId;
            }

            if (PmsConstants.FAILURE_NO_PASS.equals(businessType)||PmsConstants.ALARM_MESSAGE.equals(businessType)||PmsConstants.GENERATE_QHSE_CERT.equals(businessType)||PmsConstants.WEEK_ALARM_MESSAGE.equals(businessType)) {
                //通知设备状态变更为观察使用
                dingtalkSendApi.send(mobile, msg, "text",url,  title);
            } else {
                dingtalkSendApi.send(mobile, msg,"link", url, title);
            }
        }
    }
}
