package com.tuoheng.admin.service.workorder.generate;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.enums.InspectionFileStatusEnum;
import com.tuoheng.admin.enums.WorkOrderStatusEnum;
import com.tuoheng.admin.enums.code.workorder.GenerateWorkorderCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 生成工单业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-07
 */
@Slf4j
@Service
public class GenerateWorkorderByAutoService {

    @Autowired
    private InspectionMapper inspectionMapper;
    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private WorkOrderFileMapper workOrderFileMapper;

    /**
     * 产生问题时自动生成工单
     *
     * @param inspection
     * @param inspectionFileList
     *
     * @return
     */
    @Transactional
    public JsonResult generate(Inspection inspection, List<InspectionFile> inspectionFileList) {
        log.info("进入自动生成工单业务");

        List<String> inspectionFileIdList = inspectionFileList.stream().map(o -> o.getId()).collect(Collectors.toList());

        WorkOrder workOrder = this.insertWorkOrder(inspection);

        this.insertWorkOrderFile(workOrder, inspectionFileIdList);

        this.updateInspectionFileStatus(inspectionFileIdList);

        return JsonResult.success();
    }

    /**
     *
     * 添加工单
     *
     */
    private WorkOrder insertWorkOrder(Inspection inspection) {
        WorkOrder workOrder = new WorkOrder();
        String code = DateUtils.generateCode("XJGD");
        workOrder.setCode(code);
        workOrder.setTenantId(inspection.getTenantId());
        workOrder.setDeptId(inspection.getDeptId());
        workOrder.setStatus(WorkOrderStatusEnum.WAIT.getCode());
        workOrder.setDistributionId(""); // 分配人员
        workOrder.setCreateUser("");
        workOrder.setCreateTime(DateUtils.now());
        Integer rowCount = workOrderMapper.insert(workOrder);
        if (rowCount <= 0) {
            log.info("添加工单表失败");
        }
        return workOrder;
    }

    /**
     *
     * 添加工单子表
     *
     */
    private void insertWorkOrderFile(WorkOrder workOrder, List<String> idList) {
        List<WorkOrderFile> workOrderFileList = new ArrayList<>();
        WorkOrderFile workOrderFile;
        for (String id : idList) {
            workOrderFile = new WorkOrderFile();
            workOrderFile.setTenantId(workOrder.getTenantId());
            workOrderFile.setWorkOrderId(workOrder.getId());
            workOrderFile.setInspectionFileId(id);
            workOrderFileList.add(workOrderFile);
        }

        if (CollectionUtil.isEmpty(workOrderFileList)) {
           return;
        }
        Integer rowCount = workOrderFileMapper.insertBatch(workOrderFileList);
        if (rowCount <= 0) {
            log.info("添加工单子表失败");
        }
    }

    /**
     *
     * 问题状态，改为已生成工单
     *
     * @param idList
     */
    private JsonResult updateInspectionFileStatus(List<String> idList) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", InspectionFileStatusEnum.GENERATE_ORDER.getCode());
        map.put("updateUser", "");
        map.put("updateTime", DateUtils.now());
        map.put("idList", idList);

        Integer rowCount = inspectionFileMapper.updateByIdList(map);
        if (rowCount <= 0) {
            log.info("修改任务问题状态， 确认失败");
            return JsonResult.error(GenerateWorkorderCodeEnum.GENERATE_IS_FAILED.getCode(), GenerateWorkorderCodeEnum.GENERATE_IS_FAILED.getMsg());
        }
        return JsonResult.success();
    }
}
