package com.zcloud.service.bus.impl;

import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.inspection.SafetyEnvironmentalMapper;
import com.zcloud.service.bus.NoticeCorpUtil;
import com.zcloud.service.bus.NoticeRegulatoryService;
import com.zcloud.service.hidden.HiddenService;
import com.zcloud.service.notice.NoticeCorpService;
import com.zcloud.service.system.UsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NoticeCorpUtils implements NoticeCorpUtil {

    @Resource
    private NoticeCorpService noticeCorpService;

    @Resource
    private UsersService usersService;

    @Resource
    private SafetyEnvironmentalMapper safetyEnvironmentalMapper;

    @Resource
    private HiddenService hiddenService;

    @Resource
    private NoticeRegulatoryService noticeRegulatoryService;

    /**
     *  description:通知编码/safetyenvironmental_001
     *
     * @param information
     * @throws Exception
     */
    @Override
    public void SE_OneStepUtil(PageData information) throws Exception{
        PageData mes = new PageData();
        mes.put("TEMPLATE_CODE","SMS_225910619");
        mes.put("SENDER_ID", information.get("CREATOR"));	// 发送人员ID
        PageData userInfo = new PageData();
        userInfo.put("USER_ID",information.get("CREATOR"));
        userInfo = usersService.findById(userInfo);
        if (userInfo != null){
            mes.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
            mes.put("SYNOPSIS", "安全环保审批待处理");	// 站内信标题
            mes.put("CORPINFO_ID", information.get("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", information.get("INSPECTION_USER_ID"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "检查人核实管理");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
    }

    @Override
    public void SE_returnOneStepUtil(PageData information) throws Exception {
        PageData condition = new PageData();
        condition.put("INSPECTION_ID",information.getString("INSPECTION_ID"));
        condition = safetyEnvironmentalMapper.findById(condition);
        PageData mes = new PageData();
        mes.put("TEMPLATE_CODE","SMS_225910620");
        mes.put("SENDER_ID", information.get("OPERATOR"));	// 发送人员ID
        mes.put("USER_ID", information.get("OPERATOR")); //查询发送人名字
        PageData name = usersService.findById(mes);
        mes.put("SENDER_NAME", name.getString("NAME"));	// 发送人员姓名
        PageData userInfo = new PageData();
        userInfo.put("USER_ID",information.get("CREATOR"));  //接收人员ID
        userInfo = usersService.findById(userInfo);
        if (userInfo != null){
            mes.put("SYNOPSIS", "安全环保确认待处理");	// 站内信标题
            mes.put("CORPINFO_ID", information.get("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", information.get("CREATOR"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "安全环保检查");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
        if(userInfo == null) {
            PageData regulaUser = new PageData();
            PageData message = new PageData();
            regulaUser.put("USER_ID",information.get("OPERATOR"));
            PageData regulatoryuser = usersService.findByRegulatoryId(regulaUser); //检查人是企业端 发起人是监管端
            if(regulatoryuser == null){
                regulaUser.put("USER_ID",information.get("OPERATOR"));
                PageData corpuser = usersService.findById(regulaUser); //查询发送人名称
                message.put("SENDER_NAME", corpuser.getString("NAME"));	// 发送人员姓名

            } else {
                message.put("SENDER_NAME", regulatoryuser.getString("NAME"));	// 发送人员姓名
            }
            message.put("SENDER_ID", information.get("OPERATOR"));	// 发送人员ID
            message.put("SYNOPSIS", "安全环保审批待处理");	// 站内信标题
            message.put("RECEIVER_ID", information.get("CREATOR"));	// 接收人员ID
            message.put("CONTENT", "有一条数据被打回在安全环保检查中待编辑");// 站内信内容
            noticeRegulatoryService.sendMessage(message);
        }
    }

    @Override
    public void SE_TwoStepUtil(PageData information) throws Exception {
        PageData condition = new PageData();
        condition.put("INSPECTION_ID",information.getString("INSPECTION_ID"));
        condition = safetyEnvironmentalMapper.findById(condition);
        PageData mes = new PageData();
        mes.put("TEMPLATE_CODE","SMS_225910620");
        mes.put("SENDER_ID", information.get("inspector"));	// 发送人员ID
        PageData userInfo = new PageData();
        userInfo.put("USER_ID",information.get("inspector"));
        userInfo = usersService.findById(userInfo);
        if(userInfo != null){
            mes.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
            mes.put("SYNOPSIS", "安全环保隐患待确认提醒");	// 站内信标题
            mes.put("CORPINFO_ID", information.get("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", condition.get("INSPECTED_SITEUSER_ID"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "安全环保检查");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
    }

    @Override
    public void SE_ThreeStepUtil(PageData information) throws Exception {
        PageData condition = new PageData();
        condition.put("INSPECTION_ID",information.getString("INSPECTION_ID"));
        condition = safetyEnvironmentalMapper.findById(condition);
        PageData mes = new PageData();
        mes.put("TEMPLATE_CODE","SMS_215800731");
        mes.put("SENDER_ID", information.get("OPERATOR"));	// 发送人员ID
        PageData userInfo = new PageData();
        userInfo.put("USER_ID",information.get("OPERATOR"));
        userInfo = usersService.findById(userInfo);
        PageData hidden_condition = new PageData();
        hidden_condition.put("HIDDEN_ID",information.getString("HIDDEN_vector"));
        hidden_condition = hiddenService.findById(hidden_condition);  //查隐患数据
        hidden_condition.put("USER_ID",hidden_condition.getString("CREATOR")); //获取发现人ID
        PageData corpuser = usersService.findById(hidden_condition);  //根据隐患发现人查询是企业端用户还是监管端用户
        if (corpuser != null){
            mes.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
            mes.put("SYNOPSIS", "隐患指派与验收");	// 站内信标题
            mes.put("CORPINFO_ID", userInfo.get("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", corpuser.getString("USER_ID"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "隐患指派与验收");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
        if(corpuser == null) {
            PageData regulatoryuser = usersService.findByRegulatoryId(hidden_condition); //根据隐患发现人查询是企业端用户还是监管端用户
            if( regulatoryuser != null){
                PageData message = new PageData();
                message.put("SENDER_ID", information.get("OPERATOR"));	// 发送人员ID
                message.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
                message.put("SYNOPSIS", "友情提示：有一条安全环保检查验收数据");	// 站内信标题
                message.put("RECEIVER_ID", regulatoryuser.getString("USER_ID"));	// 接收人员ID
                message.put("CONTENT", "有一条安全环保检查验收数据在“隐患指派与验收”，等您验收");// 站内信内容
                noticeRegulatoryService.sendMessage(message);
            }
        }
    }

    @Override
    public void SE_Repulse(PageData information) throws Exception {
        PageData condition = new PageData();
        condition.put("INSPECTION_ID",information.getString("INSPECTION_ID"));
        condition = safetyEnvironmentalMapper.findById(condition);
        PageData mes = new PageData();
        mes.put("BIANMA","safetyenvironmental_001");
        mes.put("SENDER_ID", information.get("SENDER_ID"));	// 发送人员ID
        PageData userInfo = new PageData();
        userInfo.put("USER_ID",condition.get("INSPECTED_SITEUSER_ID"));
        userInfo = usersService.findById(userInfo);
        if (userInfo != null){
            mes.put("SENDER_NAME", information.getString("SENDER_NAME"));	// 发送人员姓名
            mes.put("SYNOPSIS", "安全环保审批待处理");	// 站内信标题
            mes.put("CORPINFO_ID", information.get("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", condition.get("INSPECTED_SITEUSER_ID"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "隐患指派与验收需要继续指派确认人");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
    }

    @Override
    public void SE_PleadingStepUtil(PageData information) throws Exception {  //申辩发站内信
        PageData condition = new PageData();
        condition.put("INSPECTION_ID",information.getString("INSPECTION_ID"));
        condition = safetyEnvironmentalMapper.findById(condition);
        PageData mes = new PageData();
        mes.put("TEMPLATE_CODE","SMS_225910621");
        mes.put("SENDER_ID", information.get("OPERATOR"));	// 发送人员ID
        PageData userInfo = new PageData();
        PageData hidden_condition = new PageData();
        hidden_condition.put("USER_ID",condition.getString("INSPECTION_ORIGINATOR_ID")); //获取发现人ID
        PageData corpuser = usersService.findById(hidden_condition);  //根据隐患发现人查询是企业端用户还是监管端用户
        if ("3".equals(information.getString("INSPECTION_STATUS"))){
            mes.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
            mes.put("SYNOPSIS", "安全环保检查待指派整改");	// 站内信标题
            mes.put("CORPINFO_ID", condition.getString("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", corpuser.getString("USER_ID"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "安全环保检查发起");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
        if("-2".equals(information.getString("INSPECTION_STATUS"))) {
            PageData regulatoryuser = usersService.findById(hidden_condition); //根据隐患发现人查询是企业端用户还是监管端用户
            if( regulatoryuser != null){
                PageData message = new PageData();
                message.put("TEMPLATE_CODE","SMS_225910625");
                message.put("SENDER_ID", information.get("OPERATOR"));	// 发送人员ID
                message.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
                message.put("SYNOPSIS", "友情提示：安全环保检查提交申辩");	// 站内信标题
                message.put("RECEIVER_ID", regulatoryuser.getString("USER_ID"));	// 接收人员ID
                PageData content = new PageData();
                content.put("params", "有一条安全环保检查申辩数据在“检查发起列表”，等您处理");
//                message.put("CONTENT", "有一条安全环保检查申辩数据在“检查发起列表”，等您处理");// 站内信内容
//                noticeRegulatoryService.sendMessage(message);
                message.put("CONTENT", content);
                noticeCorpService.sendNotice(message);
            }
        }
    }

    @Override
    public void FailPleadind(PageData information) throws Exception {  //申辩不通过
        PageData condition = new PageData();
        condition.put("INSPECTION_ID",information.getString("INSPECTION_ID"));
        condition = safetyEnvironmentalMapper.findById(condition);
        PageData mes = new PageData();
        mes.put("BIANMA","safetyenvironmental_001");
        mes.put("SENDER_ID", information.get("inspector"));	// 发送人员ID
        PageData userInfo = new PageData();
        userInfo.put("USER_ID",information.get("inspector"));
        userInfo = usersService.findById(userInfo);
        if(userInfo != null){
            PageData USER = new PageData();
            mes.put("SENDER_NAME", userInfo.getString("NAME"));	// 发送人员姓名
            mes.put("SYNOPSIS", "申辩处理没有通过且需要为隐患指派隐患确认人");	// 站内信标题
            mes.put("CORPINFO_ID", information.get("CORPINFO_ID"));// 企业id
            mes.put("RECEIVER_ID", condition.getString("INSPECTED_SITEUSER_ID"));	// 接收人员ID
            PageData content = new PageData();
            content.put("params", "隐患指派与验收");	// 作业名称
            mes.put("CONTENT", content);// 站内信内容
            noticeCorpService.sendNotice(mes);
        }
    }
}
