package com.tofly.gpsboot.service.mq.impl;

import com.alibaba.fastjson.JSON;

import com.tofly.api.UserClent;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.system.AuditSateEnum;
import com.tofly.gpsboot.common.system.MessageSateEnum;
import com.tofly.gpsboot.common.system.MessageTopicEnum;
import com.tofly.gpsboot.common.system.MessageTypeEnum;
import com.tofly.gpsboot.mapper.maintain.RepairorderMapper;
import com.tofly.pojo.User;
import com.tofly.pojo.message.Messagerecipient;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.gpsboot.service.mq.IAuditMessageService;
import com.tofly.gpsboot.service.mq.IMessageRecipientService;
import com.tofly.pojo.message.Messagepush;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 审核消息处理 实现类
 *
 * @author Zev
 */
@Service("AUDIT_MESSAGE")
public class AuditMessageServiceImpl implements IAuditMessageService {

    @Autowired
    private IMessageRecipientService messageRecipientService;
    @Value("${gps.audit.web.inspectionYear}")
    private String auditWebInspectionYearRouteIp;

    @Value("${gps.audit.web.inspectionMonth}")
    private String auditWebInspectionMonthRouteIp;

    @Value("${gps.audit.web.inspection}")
    private String auditWebInspectionRouteIp;

    @Value("${gps.audit.web.selRepairOrder}")
    private String auditWebSelRepairOrderRouteIp;

    @Value("${gps.audit.web.selRepairOrderYear}")
    private String auditWebSelRepairOrderYearRouteIp;

    @Value("${gps.audit.web.selConstructionSite}")
    private String auditWebSelConstructionSiteRouteIp;

    @Value("${gps.audit.web.selHiddenTrouble}")
    private String auditWebSelHiddenTroubleRouteIp;

    @Value("${gps.audit.app.inspection}")
    private String auditAppInspectionRouteIp;

    @Value("${gps.audit.app.selRepairOrder}")
    private String auditAppSelRepairOrderRouteIp;

    @Value("${gps.audit.app.selConstructionSite}")
    private String auditAppSelConstructionSiteRouteIp;

    @Value("${gps.audit.app.selHiddenTrouble}")
    private String auditAppSelHiddenTroubleRouteIp;

    @Value("${gps.audit.app.selPlanMt}")
    private String auditAppSelSelPlanMtIp;

    @Value("${gps.audit.app.selEmergencyDis}")
    private String auditAppselEmergencyDisIp;

    @Autowired
    private UserClent userClent;

    @Autowired
    private RepairorderMapper repairorderMapper;


    @Override
    public void auditTypeYearPlan(ReportAuditDto dto, Long messageUserId) {
        Long id = dto.getId();
        AtomicReference<Long> messagePushId = new AtomicReference<>(repairorderMapper.getPlanTempMaxId("TF_YWPN_MESSAGEPUSH_W"));
        AtomicReference<Long> messageRecipientId = new AtomicReference<>(repairorderMapper.getPlanTempMaxId("TF_YWPN_MESSAGERECIPIENT_W"));
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        Long message1 = messagePushId.get();
        Long recipient1 = messageRecipientId.get();
        messagepush.setId(message1);
        messagepush.setTopic(MessageTopicEnum.AUDIT_YEAR_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_YEAR_PLAN.getTableName());
        messagepush.setTableId(id);
        messagepush.setCreateTime(new Date());
        messagepush.setCreateUser(SecurityUtils.getUserId());
        // 跳转到年计划查看页面
        messagepush.setRouteIp(auditWebInspectionYearRouteIp);
        messagepush.setRouteIpApp("");
        User resUser = userClent.getById(dto.getAuditId()).getResult();
        if (null != resUser) {
            messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());

        }

        Messagerecipient messagerecipient = new Messagerecipient();
        messagerecipient.setId(recipient1);
        messagerecipient.setMessageId(message1);
        messagerecipient.setRecipient(messageUserId);
        messagerecipient.setFlag(MessageSateEnum.MESSAGE_SATE_NO.getCode());

        List<Messagepush> messagePushList = new ArrayList<>();
        messagePushList.add(messagepush);
        List<Messagerecipient> messageRecipientList = new ArrayList<>();
        messageRecipientList.add(messagerecipient);
        repairorderMapper.addMessageList(messagePushList);
        repairorderMapper.addMessageRecipientList(messageRecipientList);
    }


    @Override
    public void auditTypeMonthlyPlan(ReportAuditDto dto, Long messageUserId) {
        // 获取传过来的月计划id
        Long id = dto.getId();
        AtomicReference<Long> messagePushId = new AtomicReference<>(repairorderMapper.getPlanTempMaxId("TF_YWPN_MESSAGEPUSH_W"));
        AtomicReference<Long> messageRecipientId = new AtomicReference<>(repairorderMapper.getPlanTempMaxId("TF_YWPN_MESSAGERECIPIENT_W"));
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        Long message1 = messagePushId.get();
        Long recipient1 = messageRecipientId.get();
        messagepush.setId(message1);
        messagepush.setTopic(MessageTopicEnum.AUDIT_MONTHLY_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_MONTHLY_PLAN.getTableName());
        messagepush.setTableId(id);
        messagepush.setCreateTime(new Date());
        messagepush.setCreateUser(SecurityUtils.getUserId());
        // 跳转到计划任务页面
        messagepush.setRouteIp(auditWebInspectionMonthRouteIp);
        messagepush.setRouteIpApp("");
        User resUser = userClent.getById(dto.getAuditId()).getResult();
        if (null != resUser) {
            messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());

        }

        Messagerecipient messagerecipient = new Messagerecipient();
        messagerecipient.setId(recipient1);
        messagerecipient.setMessageId(message1);
        messagerecipient.setRecipient(messageUserId);
        messagerecipient.setFlag(MessageSateEnum.MESSAGE_SATE_NO.getCode());

        List<Messagepush> messagePushList = new ArrayList<>();
        messagePushList.add(messagepush);
        List<Messagerecipient> messageRecipientList = new ArrayList<>();
        messageRecipientList.add(messagerecipient);
        repairorderMapper.addMessageList(messagePushList);
        repairorderMapper.addMessageRecipientList(messageRecipientList);

    }

    /**
     * 隐患审核
     *
     * @param data
     */
    @Override
    public void auditTypeHiddenTrouble(String data, Long messageUserId) {
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        // 获取传过来的月计划id
        Long id = dto.getId();
        AtomicReference<Long> messagePushId = new AtomicReference<>(repairorderMapper.getPlanTempMaxId("TF_YWPN_MESSAGEPUSH_W"));
        AtomicReference<Long> messageRecipientId = new AtomicReference<>(repairorderMapper.getPlanTempMaxId("TF_YWPN_MESSAGERECIPIENT_W"));
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        Long message1 = messagePushId.get();
        Long recipient1 = messageRecipientId.get();
        messagepush.setId(message1);
        messagepush.setTopic(MessageTopicEnum.AUDIT_HIDDEN_TROUBLE.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_HIDDEN_TROUBLE.getTableName());
        messagepush.setTableId(id);
        messagepush.setCreateTime(new Date());
        messagepush.setCreateUser(SecurityUtils.getUserId());
        // 跳转到计划任务页面
        // 跳转到隐患查看页面
        messagepush.setRouteIp(auditWebSelHiddenTroubleRouteIp);
        messagepush.setRouteIpApp(auditAppSelHiddenTroubleRouteIp);
        User resUser = userClent.getById(dto.getAuditId()).getResult();
        if (null != resUser) {
            messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());

        }

        Messagerecipient messagerecipient = new Messagerecipient();
        messagerecipient.setId(recipient1);
        messagerecipient.setMessageId(message1);
        messagerecipient.setRecipient(messageUserId);
        messagerecipient.setFlag(MessageSateEnum.MESSAGE_SATE_NO.getCode());

        List<Messagepush> messagePushList = new ArrayList<>();
        messagePushList.add(messagepush);
        List<Messagerecipient> messageRecipientList = new ArrayList<>();
        messageRecipientList.add(messagerecipient);
        repairorderMapper.addMessageList(messagePushList);
        repairorderMapper.addMessageRecipientList(messageRecipientList);
    }

    /**
     * 隐患消除审核
     *
     * @param data
     */
    @Override
    public void auditTypeHiddenTroubleElimination(String data) {
        // 获取传过来的月计划id
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_HIDDEN_TROUBLE_ELIMINATION.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_HIDDEN_TROUBLE_ELIMINATION.getTableName());
        messagepush.setTableId(id);
        // 跳转到隐患消除查看页面
        messagepush.setRouteIp(auditWebSelHiddenTroubleRouteIp);
        messagepush.setRouteIpApp(auditAppSelHiddenTroubleRouteIp);
       String AuditResuleName= AuditSateEnum.getCodeForValue(dto.getAuditResult());
        messagepush.setMessage("隐患审核结果："+AuditResuleName+"；审核意见："+dto.getAuditNotes());
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getReportId());
    }

    /**
     * 工地监护审核
     *
     * @param data
     */
    @Override
    public void auditTypeConstructionSite(String data) {
        // 获取传过来的月计划id
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_CONSTRUCTION_SITE.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_CONSTRUCTION_SITE.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_CONSTRUCTION_SITE.getTableName());
        messagepush.setTableId(id);
        // 跳转到工地查看页面
        messagepush.setRouteIp(auditWebSelConstructionSiteRouteIp);
        messagepush.setRouteIpApp(auditAppSelConstructionSiteRouteIp);
        String auDitResult=AuditSateEnum.getCodeForValue(dto.getAuditResult());
        messagepush.setMessage("工地监护审核结果:"+auDitResult+"；审核意见："+dto.getAuditNotes());

        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getReportId());
    }

    /**
     * 工地监护消除审核
     *
     * @param data
     */
    @Override
    public void auditTypeConstructionSiteElimination(String data) {
        // 获取传过来的月计划id
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_CONSTRUCTION_SITE_ELIMINATION.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_CONSTRUCTION_SITE.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_CONSTRUCTION_SITE_ELIMINATION.getTableName());
        messagepush.setTableId(id);
        // 跳转到工地消除查看页面
        messagepush.setRouteIp(auditWebSelConstructionSiteRouteIp);
        messagepush.setRouteIpApp(auditAppSelConstructionSiteRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
    }

    /**
     * 巡检任务申请暂停审核
     *
     * @param data
     */
    @Override
    public void auditTypePlanStop(String data) {
        // 获取传过来的月计划id
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_PLAN_STOP.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_PLAN_STOP.getTableName());
        messagepush.setTableId(id);
        // 跳转到任务暂停查看页面
        messagepush.setRouteIp(auditWebInspectionRouteIp);
        messagepush.setRouteIpApp(auditAppInspectionRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
    }

    /**
     * 巡检任务申请作废审核
     *
     * @param data
     */
    @Override
    public void auditTypePlanDisuse(String data) {
        // 获取传过来的月计划id
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_PLAN_DISUSE.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_PLAN_DISUSE.getTableName());
        messagepush.setTableId(id);
        // 跳转到任务申请作废查看页面
        messagepush.setRouteIp(auditWebInspectionRouteIp);
        messagepush.setRouteIpApp(auditAppInspectionRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
    }


    @Override
    public void auditTypeRepairYearPlan(ReportAuditDto dto, Long messageUserId) {
        // 获取传过来的抢维修年计划id
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_REPAIR_YEAR_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_REPAIR_YEAR_PLAN.getTableName());
        messagepush.setTableId(id);
        User resUser = userClent.getById(dto.getAuditId()).getResult();
        if (null != resUser) {
            messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());

        }
        // 跳转到抢维修年计划查看页面
        messagepush.setRouteIp(auditWebSelRepairOrderYearRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, messageUserId);

    }

    @Override
    public void auditTypeRepairRecord(ReportAuditDto dto, Set<Long> userIdSet) {
        // 获取传过来的抢维修年计划id
        Long id = dto.getId();
        userIdSet.forEach(messageUserId -> {
            Messagepush messagepush = new Messagepush();
            // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
            messagepush.setTopic(MessageTopicEnum.AUDIT_REPAIR_RECORD.getTopic());
            messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
            messagepush.setTableName(MessageTopicEnum.AUDIT_REPAIR_RECORD.getTableName());
            messagepush.setTableId(id);
            User resUser = userClent.getById(dto.getAuditId()).getResult();
            if (null != resUser) {
                messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());

            }
            // 跳转到抢维修计划查看页面
            messagepush.setRouteIp(auditWebSelRepairOrderRouteIp);
            messagepush.setRouteIpApp(auditAppSelRepairOrderRouteIp);
            // 消息与接收人信息处理
            messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        });

    }

    @Override
    public void auditPlanMt(ReportAuditDto dto,Set<Long> userIdSet) {
        Long id = dto.getId();
        userIdSet.forEach(messageUserId -> {
            Messagepush messagepush = new Messagepush();
            // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
            messagepush.setTopic(MessageTopicEnum.AUDIT_PLAN_MT.getTopic());
            messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
            messagepush.setTableName(MessageTopicEnum.AUDIT_PLAN_MT.getTableName());
            messagepush.setTableId(id);
            User resUser = userClent.getById(dto.getAuditId()).getResult();
            if (null != resUser) {
                messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());
            }
            // 跳转到维护计划查看页面
            messagepush.setRouteIp("");
            messagepush.setRouteIpApp(auditAppSelSelPlanMtIp);
            // 消息与接收人信息处理
            messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        });
    }


    @Override
    public void auditEmergencyDis(ReportAuditDto dto, Set<Long> userIdSet) {
        Long id = dto.getId();
        userIdSet.forEach(messageUserId -> {
            Messagepush messagepush = new Messagepush();
            // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
            messagepush.setTopic(MessageTopicEnum.AUDIT_EMERGENCY_DIS.getTopic());
            messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
            messagepush.setTableName(MessageTopicEnum.AUDIT_EMERGENCY_DIS.getTableName());
            messagepush.setTableId(id);
            User resUser = userClent.getById(dto.getAuditId()).getResult();
            if (null != resUser) {
                messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());
            }
            // 跳转到应急查看页面
            messagepush.setRouteIp("");
            messagepush.setRouteIpApp(auditAppselEmergencyDisIp);
            // 消息与接收人信息处理
            messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        });
    }
}
