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


import com.alibaba.fastjson.JSON;
import com.tofly.api.UserTaskClient;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.*;
import com.tofly.pojo.message.Usertask;
import com.tofly.pojo.plan.dto.PlanDto;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.audit.IAuditConstructionSiteServer;
import com.tofly.gpsboot.service.build.BuildService;
import com.tofly.gpsboot.service.build.BuildplanService;
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.util.DateTimeUtil;
import com.tofly.pojo.build.Build;
import com.tofly.pojo.plan.Plan;
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_CONSTRUCTION_SITE")
public class AuditConstructionSiteServiceImpl implements IAuditConstructionSiteServer {
    @Autowired
    private IAuditMessageService iAuditMessageService;

    @Autowired
    private IPlanMessageService iPlanMessageService;

    @Autowired
    private BuildplanService buildplanService;

    @Autowired
    private CheckhistoryService checkhistoryService;

    @Autowired
    private BuildService buildService;

    @Autowired
    private PlanService planService;

    @Autowired
    private UserTaskClient userTaskClient;


    @Override
    public void audit(ReportAuditDto dto) throws BusinessException {
        Long id = dto.getId();

        Build info = buildService.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();
        String auditNotes = dto.getAuditNotes();
        Date date = DateTimeUtil.convertLDTToDate(LocalDateTime.now());
        String isrespect = dto.getIsrespect();
        Long auditId = dto.getAuditId();
        // 修改对象审核状态 审核时间
        info.setAuditTime(date);
        info.setAuditUser(auditId);
        // 审核状态
        info.setAuditResult(auditResult);
        // 审核意见
        info.setAuditNotes(auditNotes);
        info.setIsrespect(isrespect);
        info.setState(isrespect);
        // 存入当前计划审批历史信息
        checkhistoryService.recordHis(AuditTypeEnum.AUDIT_TYPE_CONSTRUCTION_SITE.getCode(), id, auditResult, auditNotes, auditId, date);


        // 发送mq消息(计划审核成功/失败)给上报对象到消息提示列表
        // 通知审核对象mq消息
        iAuditMessageService.auditTypeConstructionSite(JSON.toJSONString(dto));
        // 审核通过发布任务
        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_BUILD.getCode());
                List<Plan> planList = planService.savaPlanInfo(planDto);
                planList.forEach(po -> po.setPlanState(PlanStateEnum.PLAN_STATE_UNENFORCEABLE.getCode()));
                // 当前计划与工地关联
                buildplanService.saveBuildPlanRelation(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());
            }
        }
        buildService.updateById(info);
        // 待办事项处理
        Usertask usertask = new Usertask();
        usertask.setTableName(MessageTopicEnum.REPORT_CONSTRUCTION_SITE.getTableName());
        usertask.setTableId(info.getId());
        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);
            });

        }
    }
}
