package com.tuoheng.admin.service.inspection.query;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.InspectionConverMapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.InspectionHistory;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionDetailsByIdCodeEnum;
import com.tuoheng.admin.mapper.InspectionHistoryMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.inspection.InspectionDetailsVo;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 根据Id查询巡检任务详情业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-25
 */
@Slf4j
@Service
public class QueryInspectionDetailsByIdService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private InspectionHistoryMapper inspectionHistoryMapper;

    public JsonResult getInspectionDetails(String id) {
        log.info("进入查询巡检任务详情业务");
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, id);
        if (0 != result.getCode()) {
            log.info("进入查询巡检任务详情业务：校验失败：{}", result.getMsg());
            return result;
        }

        Inspection inspection = (Inspection) result.getData();

        InspectionDetailsVo inspectionDetailsVo = InspectionConverMapper.INSTANCE.fromInspectionToInspectionDetailsVo(inspection);

        // 任务失败，查询任务失败记录
        if (InspectionStatusEnum.FLIGHT_FAILED.getCode() == inspection.getStatus()) {
            List<InspectionHistory> inspectionHistoryList = this.getInspectionHistoryList(id);
            if (CollectionUtil.isEmpty(inspectionHistoryList)) {
                log.info("该任务没有失败记录");
            } else {
                inspectionDetailsVo.setInspectionHistoryList(inspectionHistoryList);
            }
        }
        return JsonResult.success(inspectionDetailsVo);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(String tenantId, String id) {
        // 判断任务id是否为空
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryInspectionDetailsByIdCodeEnum.INSPECTION_ID_IS_NULL.getCode(), QueryInspectionDetailsByIdCodeEnum.INSPECTION_ID_IS_NULL.getMsg());
        }

        // 判断任务是否存在
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getTenantId, tenantId)
                .eq(Inspection::getId, id)
                .eq(Inspection::getMark, MarkEnum.VALID.getCode()));

        if (null == inspection) {
            return JsonResult.error(QueryInspectionDetailsByIdCodeEnum.INSPECTION_IS_NOT_EXIST.getCode(), QueryInspectionDetailsByIdCodeEnum.INSPECTION_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(inspection);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private List<InspectionHistory> getInspectionHistoryList(String id) {
        List<InspectionHistory> inspectionHistoryList = inspectionHistoryMapper.selectList(new LambdaQueryWrapper<InspectionHistory>()
                .eq(InspectionHistory::getInspectionId, id)
                .eq(InspectionHistory::getMark, MarkEnum.VALID.getCode()));
        return inspectionHistoryList;
    }

}
