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

import com.alibaba.fastjson.JSON;

import com.tofly.api.UserClent;
import com.tofly.gpsboot.common.system.AuditSateEnum;
import com.tofly.gpsboot.common.system.MessageTopicEnum;
import com.tofly.gpsboot.common.system.MessageTypeEnum;
import com.tofly.pojo.User;
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.Set;

/**
 * 审核消息处理 实现类
 *
 * @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;

    @Autowired
    private UserClent userClent;


    @Override
    public void auditTypeYearPlan(ReportAuditDto dto, Long messageUserId) {

        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.AUDIT_YEAR_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_YEAR_PLAN.getTableName());
        messagepush.setTableId(dto.getId());
        User resUser = userClent.getById(messageUserId).getResult();
        if (null != resUser) {
            messagepush.setMessage("审核人: " + resUser.getRealName() + ",审核意见: " + AuditSateEnum.getCodeForValue(dto.getAuditResult()) + ",审核备注: " + dto.getAuditNotes());

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

    }


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

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

    }

    /**
     * 隐患审核
     *
     * @param data
     */
    @Override
    public void auditTypeHiddenTrouble(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.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        messagepush.setTableName(MessageTopicEnum.AUDIT_HIDDEN_TROUBLE.getTableName());
        messagepush.setTableId(id);
        // 跳转到隐患查看页面
        messagepush.setRouteIp(auditWebSelHiddenTroubleRouteIp);
        messagepush.setRouteIpApp(auditAppSelHiddenTroubleRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getReportId());
    }

    /**
     * 隐患消除审核
     *
     * @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);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
    }

    /**
     * 工地监护审核
     *
     * @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);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
    }

    /**
     * 工地监护消除审核
     *
     * @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);
        });

    }
}
