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


import com.alibaba.fastjson.JSON;
import com.tofly.api.UserTaskClient;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.*;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.audit.IAuditHiddenTroubleServer;
import com.tofly.gpsboot.service.mq.IAuditMessageService;
import com.tofly.gpsboot.service.mq.IPlanMessageService;
import com.tofly.gpsboot.service.paln.CheckhistoryService;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.gpsboot.service.process.PointtempService;
import com.tofly.gpsboot.service.trouble.ToubleplanService;
import com.tofly.gpsboot.service.trouble.TroubleService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.message.Usertask;
import com.tofly.pojo.plan.Plan;
import com.tofly.pojo.plan.dto.PlanDto;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.pojo.trouble.Trouble;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/**
 * 隐患 审核
 *
 * @author Zev
 */
@Service("AUDIT_TYPE_HIDDEN_TROUBLE")
public class AuditHiddenTroubleServiceImpl implements IAuditHiddenTroubleServer {

    @Autowired
    private IAuditMessageService iAuditMessageService;

    @Autowired
    private IPlanMessageService iPlanMessageService;

    @Autowired
    private CheckhistoryService checkhistoryService;

    @Autowired
    private TroubleService troubleService;

    @Autowired
    private PlanService planService;

    @Autowired
    private PointtempService pointtempService;

    @Autowired
    private ToubleplanService toubleplanService;

    @Autowired
    private UserTaskClient userTaskClient;

    @Override
    public void audit(ReportAuditDto dto) throws BusinessException {
        Long id = dto.getId();
        Trouble info = troubleService.getById(id);
        if (AuditSateEnum.AUDIT_YES.getCode().equals(info.getAuditResult())) {
            throw new BusinessException(Constant.AUDIT_NOT_UNCOMMITTED_CODE, Constant.AUDIT_NOT_UNCOMMITTED);
        }
        String auditResult = dto.getAuditResult();
        Long auditId = dto.getAuditId();
        String auditNotes = dto.getAuditNotes();
        String isrespect = dto.getIsrespect();
        Date date = DateTimeUtil.convertLDTToDate(LocalDateTime.now());
        // 修改对象审核状态 审核时间
        info.setAuditTime(date);
        info.setAuditUser(auditId);
        info.setAuditResult(auditResult);
        info.setAuditNotes(auditNotes);
        info.setIsrespect(isrespect);
        info.setState(isrespect);
        // 存入当前计划审批历史信息
        checkhistoryService.recordHis(AuditTypeEnum.AUDIT_TYPE_HIDDEN_TROUBLE.getCode(), id, auditResult, auditNotes, auditId, date);


        // 发送mq消息(计划审核成功/失败)给上报对象到消息提示列表
        // 通知审核对象mq消息
        iAuditMessageService.auditTypeHiddenTrouble(JSON.toJSONString(dto),info.getSubmitUser());
        // 当前审批类型为月计划 审核通过发布任务
        if (AuditSateEnum.AUDIT_YES.getCode().equals(auditResult) && IsRespectEnum.IS_RESPECT_YES.getCode().equals(isrespect)) {
            // 新增计划

            PlanDto planDto = dto.getPlanDto();
            if (null != planDto) {
                planDto.setPlanType(PlanTypeEnum.PLAN_TYPE_TROUBLE.getCode());
                if(StringUtils.isEmpty(planDto.getInspectUsers())){
                  planDto.setInspectUsers(auditId+"");
                }
                List<Plan> planList = planService.savaPlanInfo(planDto);
                planList.forEach(po -> po.setPlanState(PlanStateEnum.PLAN_STATE_UNENFORCEABLE.getCode()));
                // 当前计划与隐患关联
                toubleplanService.saveToublePlanRelation(planList,id);
            } else {
                throw new BusinessException(Constant.TASK_IS_PLAN_ADD_NULL_CODE, Constant.TASK_IS_PLAN_ADD_NULL);
            }
        }
      /*  else {
            // 审核不通过,且需要人员监护 上报状态再设置为 待上报
//            if(IsRespectEnum.IS_RESPECT_YES.getCode().equals(isrespect)){
                info.setIsSubmit(ReportSateEnum.REPORT_UNCOMMITTED.getCode());
//            }


        }*/
        if (AuditSateEnum.AUDIT_NO.getCode().equals(auditResult)){ //不通过
            info.setIsSubmit(ReportSateEnum.REPORT_UNCOMMITTED.getCode());
        }


        troubleService.updateById(info);
        // 待办事项处理状态置为完成
        // 待办事项处理
        Usertask usertask = new Usertask();
        usertask.setTableName(MessageTopicEnum.REPORT_HIDDEN_TROUBLE.getTableName());
        usertask.setHandleUser(SecurityUtils.getUserId());
        usertask.setTableId(id);
        List<Usertask> result = userTaskClient.getUserTaskInfo(usertask).getResult();
        if(null != result && result.size()>0){
            result.forEach(task->{
                task.setHandleTime(DateTimeUtil.convertLDTToDate(LocalDateTime.now()));
                task.setHandleUser(SecurityUtils.getUserId());
                task.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_COMPLETED.getCode());
                userTaskClient.updateById(task);
            });

        }
    }
}
