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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tofly.api.UserTaskClient;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.MessageTopicEnum;
import com.tofly.gpsboot.common.system.OrderStateEnum;
import com.tofly.gpsboot.common.system.UserTaskStateEnumEnum;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.maintain.dto.RepairorderDto;
import com.tofly.pojo.maintain.dto.RepairuserChildDto;
import com.tofly.pojo.message.Usertask;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.audit.IAuditRepairPlanServer;
import com.tofly.gpsboot.service.maintain.RepairorderService;
import com.tofly.gpsboot.service.maintain.RepairuserService;
import com.tofly.gpsboot.service.mq.IPlanMessageService;
import com.tofly.pojo.maintain.Repairorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("AUDIT_TYPE_REPAIR_PLAN")
public class AuditRepairPlanServerImpl implements IAuditRepairPlanServer {
    @Autowired
    private RepairuserService repairuserService;
    @Autowired
    private IPlanMessageService iPlanMessageService;
    @Autowired
    private RepairorderService repairorderService;
    @Autowired
    private UserTaskClient userTaskClient;
    @Override
    public void audit(ReportAuditDto dto) throws BusinessException {
        RepairorderDto parDto = dto.getRepairorderDto();
        if(null == parDto){
            throw new BusinessException(Constant.PLAN_DISPATCHING_ERROR_CODE,Constant.PLAN_DISPATCHING_ERROR);
        }
        Repairorder repairorder = JSONObject.parseObject(JSON.toJSONString(parDto), Repairorder.class);
        // 当前工单任务状态为 0 未派工
        if(OrderStateEnum.ORDER_STATE_NOT_WORK.getCode().equals(repairorder.getState())){
            // 派工处理
            // 1.新增到 抢维修工单个人任务信息表
            List<RepairuserChildDto> repairUserChildDtoList = parDto.getRepairUserChildDtoList();
            if(null!=repairUserChildDtoList && repairUserChildDtoList.size()>0){
                repairorderService.addRepairUserList(repairorder,repairUserChildDtoList,true);
            }else {
                throw new BusinessException(Constant.TASK_REPAIR_ORDER_RESP_USER_NOT_NULL_CODE,Constant.TASK_REPAIR_ORDER_RESP_USER_NOT_NULL);
            }
            repairorder.setState(OrderStateEnum.ORDER_STATE_NOT_START.getCode());
            repairorderService.updateById(repairorder);
            // 消息处理
            repairorderService.dispatching(repairorder);
            // 待办事项处理
            Usertask usertask = new Usertask();
            usertask.setTableName(MessageTopicEnum.TASK_PLAN_MAINTENANCE_ORDER.getTableName());
            usertask.setHandleUser(SecurityUtils.getUserId());
            usertask.setTableId(repairorder.getId());
            List<Usertask> result = userTaskClient.getUserTaskInfo(usertask).getResult();
            if(null != result && result.size()>0){
                Usertask task = result.get(0);
                task.setHandleTime(DateTimeUtil.convertLDTToDate(LocalDateTime.now()));
                task.setHandleUser(SecurityUtils.getUserId());
                task.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_COMPLETED.getCode());
                userTaskClient.updateById(task);
            }
        }else {
            throw new BusinessException(Constant.PLAN_DISPATCHING_AUDIT_YES_ERROR_CODE,Constant.PLAN_DISPATCHING_AUDIT_YES_ERROR);

        }

    }
}
