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

import com.alibaba.fastjson.JSON;
import com.tofly.api.UserTaskClient;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.system.MessageTopicEnum;
import com.tofly.gpsboot.common.system.MessageTypeEnum;
import com.tofly.gpsboot.common.system.UserTaskStateEnumEnum;
import com.tofly.pojo.message.Usertask;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.gpsboot.service.mq.IMessageRecipientService;
import com.tofly.gpsboot.service.mq.IReportMessageService;
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.Date;


/**
 * 上报消息处理 实现类
 *
 * @author Zev
 */
@Service("REPORT_MESSAGE")
public class ReportMessageServiceImpl implements IReportMessageService {

    @Autowired
    private IMessageRecipientService messageRecipientService;

    @Autowired
    private UserTaskClient userTaskClient;

    @Value("${gps.dispatching.web.auditWebRouteIp}")
    private String auditWebRouteIp;

    @Value("${gps.dispatching.app.auditAppRouteIp}")
    private String auditAppRouteIp;

    @Value("${gps.dispatching.web.auditWebPollingRouteIp}")
    private String auditWebPollingRouteIp;

    @Value("${gps.dispatching.app.auditFillMonthAppIp}")
    private String auditFillMonthAppIp;

    @Override
    public void reportTypeYearPlan(ReportAuditDto dto,Long auditId) {
        // 获取传过来的年计划id
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.REPORT_YEAR_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_YEAR_PLAN.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, auditId);
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_YEAR_PLAN.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_YEAR_PLAN.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(auditId);
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }


    @Override
    public void reportTypeMonthlyPlan(ReportAuditDto dto,Long auditId) {
        // 获取传过来的月计划id
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转
        messagepush.setTopic(MessageTopicEnum.REPORT_MONTHLY_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_MONTHLY_PLAN.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, auditId);
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_MONTHLY_PLAN.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_MONTHLY_PLAN.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(auditId);
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    /**
     * 隐患上报
     *
     * @param data
     */
    @Override
    public void reportTypeHiddenPlan(String data) {
        // 获取传过来的隐患id和隐患申请内容
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        String troubleNotes = dto.getTroubleNotes();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_HIDDEN_TROUBLE.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_HIDDEN_TROUBLE.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        messagepush.setMessage(troubleNotes);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_HIDDEN_TROUBLE.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_HIDDEN_TROUBLE.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(dto.getAuditId());
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    /**
     * 隐患消除上报
     *
     * @param data
     */
    @Override
    public void reportTypeHiddenTroubleElimination(String data) {
        // 获取传过来的消除隐患id 和消除隐患申请内容
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        String troubleClearNotes = dto.getTroubleClearNotes();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_HIDDEN_TROUBLE_ELIMINATION.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_HIDDEN_TROUBLE_ELIMINATION.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        messagepush.setMessage(troubleClearNotes);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_HIDDEN_TROUBLE_ELIMINATION.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_TROUBLE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_HIDDEN_TROUBLE_ELIMINATION.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(dto.getAuditId());
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    /**
     * 工地上报
     *
     * @param data
     */
    @Override
    public void reportTypeConstructionSite(String data) {
        // 获取传过来的工地上报id 和工地上报申请内容
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        String buildNotes = dto.getBuildNotes();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_CONSTRUCTION_SITE.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_CONSTRUCTION_SITE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_CONSTRUCTION_SITE.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        messagepush.setMessage(buildNotes);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_CONSTRUCTION_SITE.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_CONSTRUCTION_SITE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_CONSTRUCTION_SITE.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(dto.getAuditId());
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    /**
     * 工地监护消除上报
     *
     * @param data
     */
    @Override
    public void reportTypeConstructionSiteElimination(String data) {
        // 获取传过来的工地监护消除id 和工地监护消除申请内容
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        String buildClearNotes = dto.getBuildClearNotes();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_CONSTRUCTION_SITE_ELIMINATION.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_CONSTRUCTION_SITE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_CONSTRUCTION_SITE_ELIMINATION.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        messagepush.setMessage(buildClearNotes);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_CONSTRUCTION_SITE_ELIMINATION.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_CONSTRUCTION_SITE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_CONSTRUCTION_SITE_ELIMINATION.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(dto.getAuditId());
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    /**
     * 巡检计划任务暂停发送
     *
     * @param data
     */
    @Override
    public void reportPlanStop(String data) {
        // 获取传过来的任务暂停id 和任务暂停申请内容
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        String planStopNotes = dto.getPlanStopNotes();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_PLAN_STOP.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_PLAN_STOP.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        messagepush.setMessage(planStopNotes);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_PLAN_STOP.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_PLAN_STOP.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(dto.getAuditId());
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    /**
     * 巡检计划任务作废发送
     *
     * @param data
     */
    @Override
    public void reportPlanDisuse(String data) {
        // 获取传过来的任务废除id 和任务废除申请内容
        ReportAuditDto dto = JSON.parseObject(data, ReportAuditDto.class);
        Long id = dto.getId();
        String planDisuseNotes = dto.getPlanDisuseNotes();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_PLAN_DISUSE.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_PLAN_DISUSE.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebPollingRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        messagepush.setMessage(planDisuseNotes);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, dto.getAuditId());
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_PLAN_DISUSE.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_INSPECTION.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_PLAN_DISUSE.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebPollingRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(dto.getAuditId());
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    @Override
    public void reportRepairYearPlan(ReportAuditDto dto, Long messageUserId) {
        // 获取传过来的抢修年计划id
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_REPAIR_YEAR_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_REPAIR_YEAR_PLAN.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_REPAIR_YEAR_PLAN.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_REPAIR_YEAR_PLAN.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(messageUserId);
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    @Override
    public void reportRepairMonthPlan(ReportAuditDto dto, Long messageUserId) {
        // 获取传过来的抢修年计划id
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_REPAIR_MONTH_PLAN.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_REPAIR_MONTH_PLAN.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_REPAIR_MONTH_PLAN.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_REPAIR_MONTH_PLAN.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(messageUserId);
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    @Override
    public void reportRepairRecord(ReportAuditDto dto, Long messageUserId) {
        Long id = dto.getId();
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_REPAIR_RECORD.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_REPAIR_RECORD.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp(auditWebRouteIp);
        messagepush.setRouteIpApp(auditAppRouteIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_REPAIR_RECORD.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_ROB_MAINTENANCE.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_REPAIR_RECORD.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp(auditWebRouteIp);
        usertask.setRouteAppIp(auditAppRouteIp);
        usertask.setHandleUser(messageUserId);
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }

    @Override
    public void reportFillMonth(Long id, Long messageUserId) {
        Messagepush messagepush = new Messagepush();
        // 消息主题/消息类型/消息表名/消息表中id/路由地址跳转/消息详情
        messagepush.setTopic(MessageTopicEnum.REPORT_REPAIR_FILL_MONTH.getTopic());
        messagepush.setType(MessageTypeEnum.MESSAGE_TYPE_FILL_REPORT.getCode());
        messagepush.setTableName(MessageTopicEnum.REPORT_REPAIR_FILL_MONTH.getTableName());
        messagepush.setTableId(id);
        // 跳转到审核页面
        messagepush.setRouteIp("");
        messagepush.setRouteIpApp(auditFillMonthAppIp);
        // 消息与接收人信息处理
        messageRecipientService.setMessageRecipient(messagepush, messageUserId);
        // 新增代办任务
        Usertask usertask = new Usertask();
        usertask.setStartTime(new Date());
        usertask.setTknm(MessageTopicEnum.REPORT_REPAIR_FILL_MONTH.getTopic());
        usertask.setTkty(MessageTypeEnum.MESSAGE_TYPE_FILL_REPORT.getCode());
        usertask.setTableName(MessageTopicEnum.REPORT_REPAIR_FILL_MONTH.getTableName());
        usertask.setTableId(id);
        usertask.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_PENDING.getCode());
        usertask.setRouteIp("");
        usertask.setRouteAppIp(auditFillMonthAppIp);
        usertask.setHandleUser(messageUserId);
        usertask.setCreateUser(SecurityUtils.getUserId());
        usertask.setCreateTime(new Date());
        userTaskClient.save(usertask);
    }


}
