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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.InspectionFileConverMapper;
import com.tuoheng.admin.conver.ReportConverMapper;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.InspectionFileStatusEnum;
import com.tuoheng.admin.enums.InspectionTypeEnum;
import com.tuoheng.admin.enums.code.report.QueryInspectionReportCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.QuestionTypeMapper;
import com.tuoheng.admin.mapper.ReportMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.inspection.InspectionFileReportVo;
import com.tuoheng.admin.vo.inspection.InspectionReportVo;
import com.tuoheng.admin.vo.inspection.InspectionResultVo;
import com.tuoheng.common.core.config.common.CommonConfig;
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.ArrayList;
import java.util.List;

/**
 * 查询巡检报告业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-12
 */
@Slf4j
@Service
public class QueryInspectionReportService {

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

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

        Report report = (Report) result.getData();

        InspectionReportVo inspectionReportVo = this.buildInspectionReportVo(report);

        return JsonResult.success(inspectionReportVo);
    }

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

        // 判断报告是否存在
        Report report = reportMapper.selectOne(new LambdaQueryWrapper<Report>()
                .eq(Report::getTenantId, tenantId)
                .eq(Report::getId, id)
                .eq(Report::getMark, 1));
        if (null == report) {
            return JsonResult.error(QueryInspectionReportCodeEnum.REPORT_IS_NOT_EXIST.getCode(), QueryInspectionReportCodeEnum.REPORT_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(report);
    }

    /**
     * 构造返回的数据列
     *
     * @param report
     * @return
     */
    private InspectionReportVo buildInspectionReportVo(Report report) {
        InspectionReportVo inspectionReportVo = ReportConverMapper.INSTANCE.fromReportToInspectionReportVo(report);
        List<InspectionFile> inspectionFileList = inspectionFileMapper.selectList(new LambdaQueryWrapper<InspectionFile>()
                                                    .eq(InspectionFile::getInspectionId, report.getInspectionId())
                                                    .in(InspectionFile::getStatus, GetInspectionFileStatusList.getStatusList())
                                                    .eq(InspectionFile::getMark, 1));
        String deptName = this.getDeptName(report.getDeptId());
        List<InspectionResultVo> inspectionResultVoList = this.buildInspectionResult(report);
        List<InspectionFileReportVo> inspectionFileHandleVoList = InspectionFileConverMapper.INSTANCE.fromInspectionFileListToInspectionFileHandleVoList(inspectionFileList);
        if (CollectionUtil.isNotEmpty(inspectionFileHandleVoList)) {
            for (InspectionFileReportVo inspectionFileReportVo : inspectionFileHandleVoList) {
                inspectionFileReportVo.setFileImage(CommonConfig.imageURL + inspectionFileReportVo.getFileImage());
                inspectionFileReportVo.setFileThumbnail(CommonConfig.imageURL + inspectionFileReportVo.getFileThumbnail());
                inspectionFileReportVo.setFileOriginal(CommonConfig.imageURL + inspectionFileReportVo.getFileOriginal());
            }
        }
        // 机场任务没有设备名称，暂定使用机场名称作为设备名称
        if (InspectionTypeEnum.AIRPORT.getCode() == report.getInspectionType()) {
            inspectionReportVo.setEquipmentName(report.getAirportName());
        }
        inspectionReportVo.setDeptName(deptName);
        inspectionReportVo.setInspectionResultVoList(inspectionResultVoList);
        inspectionReportVo.setInspectionFileReportVoList(inspectionFileHandleVoList);
        return inspectionReportVo;
    }

    /**
     * 获取部门路名称
     */
    private String getDeptName(String deptId) {
        if (StringUtils.isEmpty(deptId)) {
            return "";
        }
        Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
                .eq(Dept::getId, deptId)
                .eq(Dept::getMark, 1));
        if (null != dept) {
            return dept.getName();
        }
        return "";
    }

    private List<InspectionResultVo> buildInspectionResult(Report report) {
        List<InspectionResultVo> inspectionResultVoList = new ArrayList<>();
        List<QuestionType> questionTypeList = questionTypeMapper.selectList(new LambdaQueryWrapper<QuestionType>()
                                                .eq(QuestionType::getMark, 1));
        QuestionType questionType;
        InspectionResultVo inspectionResultVo;
        for (int i = 0; i < questionTypeList.size(); i++) {
            inspectionResultVo = new InspectionResultVo();
            questionType = questionTypeList.get(i);
            Integer count = this.getCount(report.getInspectionId(), questionType.getCode());
            inspectionResultVo.setQuestionTypeContent(questionType.getContent());
            inspectionResultVo.setCount(count);
            inspectionResultVoList.add(inspectionResultVo);
        }
        return inspectionResultVoList;
    }

    private Integer getCount(String inspectionId, String questionCode) {
        Integer count = inspectionFileMapper.selectCount(new LambdaQueryWrapper<InspectionFile>()
                        .eq(InspectionFile::getInspectionId, inspectionId)
                        .in(InspectionFile::getStatus, GetInspectionFileStatusList.getStatusList())
                        .eq(InspectionFile::getQuestionCode, questionCode)
                        .eq(InspectionFile::getMark, 1));
        return count;
    }

}
