package com.tuoheng.admin.service.inspectionfile.confirm;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.enums.InspectionFileStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspectionfile.InspectionFileConfirmCodeEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务问题确认业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-05
 */
@Slf4j
@Service
public class InspectionFileConfirmService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    public JsonResult confirm(List<String> idList) {
        log.info("进入任务问题确认业务, id:{}", idList.toString());
        String userId = CurrentUserUtil.getUserId();
        JsonResult result = this.check(idList);
        if (0 != result.getCode()) {
            log.info("进入任务问题确认业务：校验失败：{}", result.getMsg());
            return result;
        }

        // 确认任务问题
        result = this.updateInspectionFileStatus(userId, idList);

        return result;

    }

    /**
     * 检查参数
     *
     * @param idList
     * @return
     */
    private JsonResult check(List<String> idList) {
        // 判断任务id是否为空
        if (CollectionUtil.isEmpty(idList)) {
            return JsonResult.error(InspectionFileConfirmCodeEnum.INSPECTION_FILE_ID_IS_NULL.getCode(), InspectionFileConfirmCodeEnum.INSPECTION_FILE_ID_IS_NULL.getMsg());
        }

        // 判断任务是否存在
        List<InspectionFile> inspectionFileList = inspectionFileMapper.selectList(new LambdaQueryWrapper<InspectionFile>()
                .in(InspectionFile::getId, idList)
                .eq(InspectionFile::getMark, MarkEnum.VALID.getCode()));

        if (CollectionUtil.isEmpty(inspectionFileList)) {
            return JsonResult.error(InspectionFileConfirmCodeEnum.INSPECTION_FILE_IS_NOT_EXIST.getCode(), InspectionFileConfirmCodeEnum.INSPECTION_FILE_IS_NOT_EXIST.getMsg());
        }

//        for (InspectionFile inspectionFile : inspectionFileList) {
//            if (InspectionFileStatusEnum.WAIT_CONFIRMED.getCode() != inspectionFile.getStatus()) {
//                return JsonResult.error(InspectionFileConfirmCodeEnum.NOT_CONFIRM_INSPECTION_FILE.getCode(), InspectionFileConfirmCodeEnum.NOT_CONFIRM_INSPECTION_FILE.getMsg());
//            }
//        }

        return JsonResult.success();
    }

    /**
     *
     * 问题状态，改为已经确认
     *
     * @param userId
     * @param idList
     */
    private JsonResult updateInspectionFileStatus(String userId, List<String> idList) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", InspectionFileStatusEnum.CONFIRMED.getCode());
        map.put("updateUser", userId);
        map.put("updateTime", DateUtils.now());
        map.put("checkUser", userId);
        map.put("checkTime", DateUtils.now());
        map.put("idList", idList);

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