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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.api.UserTaskClient;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.system.*;
import com.tofly.pojo.message.Usertask;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.comm.IAuditService;
import com.tofly.gpsboot.service.maintain.RepairorderService;
import com.tofly.gpsboot.service.maintain.RepairrecordService;
import com.tofly.gpsboot.service.maintain.RepairuserService;
import com.tofly.gpsboot.service.mq.IReportMessageService;
import com.tofly.gpsboot.service.report.IReportRepairRecordServer;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.comm.Audit;
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.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportRepairRecordServerImpl implements IReportRepairRecordServer {
    @Autowired
    private RepairrecordService repairrecordService;
    @Autowired
    private IReportMessageService iReportMessageService;
    @Autowired
    private RepairorderService repairorderService;
    @Autowired
    private IAuditService iAuditService;
    @Autowired
    private RepairuserService repairuserService;
    @Autowired
    private UserTaskClient userTaskClient;

    @Override
    public void report(ReportAuditDto dto) throws BusinessException {
        Long id = dto.getId();
        Repairorder info = repairorderService.getById(id);
        List<Audit> auditList = JSONArray.parseArray(JSON.toJSONString(dto.getAuditDtoList()), Audit.class);
        // 审批链处理
        List<Audit> collect = auditList.stream().filter(a -> 0 == a.getAuditLevel()).collect(Collectors.toList());
        Audit audit = collect.get(0);
        // (获取一级审批的审批人默认为待审批)
        audit.setAuditState(AuditSateEnum.AUDIT_UNCOMMITTED.getCode());
        // 修改当前上报状态为 已上报,上报时间,修改审核状态为 未审批
        Date date = DateTimeUtil.convertLDTToDate(LocalDateTime.now());
        // 工单状态改为 1 未审核
        info.setState(OrderStateEnum.ORDER_STATE_NOT_AUDIT.getCode());

        // 清记录->落库
        iAuditService.remove(new QueryWrapper<Audit>().lambda().eq(Audit::getAuditPlanId, id).eq(Audit::getAuditType,AuditTypeEnum.AUDIT_TYPE_REPAIR_RECORD.getCode()));
        auditList.forEach(t -> {
            t.setAuditPlanId(id);
//            t.setAuditTime(date);
            t.setAuditType(AuditTypeEnum.AUDIT_TYPE_REPAIR_RECORD.getCode());
            iAuditService.save(t);
        });
        repairorderService.updateById(info);

        // 待办事项处理状态置为完成,强维修任务
        // 待办事项处理
        Usertask usertask = new Usertask();
        usertask.setTableName(MessageTopicEnum.TASK_PLAN_MAINTENANCE_ORDER.getTableName());
        usertask.setTableId(id);
        List<Usertask> result = userTaskClient.getUserTaskInfo(usertask).getResult();
        if(null != result && result.size()>0){
            for (Usertask task:result
                 ) {
                if(StringUtil.isNotEmpty(task.getIshandle()) && task.getIshandle().equals(UserTaskStateEnumEnum.IS_HANDLER_COMPLETED)){
                     continue;
                }
                 task.setHandleTime(DateTimeUtil.convertLDTToDate(LocalDateTime.now()));
                task.setHandleUser(SecurityUtils.getUserId());
                task.setIshandle(UserTaskStateEnumEnum.IS_HANDLER_COMPLETED.getCode());
                userTaskClient.updateById(task);
                }
            }


        // 通知审核对象mq消息
        iReportMessageService.reportRepairRecord(dto, audit.getAuditUser());


    }
}
