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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.conver.InspectionFileConverMapper;
import com.tuoheng.admin.conver.ReportConverMapper;
import com.tuoheng.admin.entity.*;
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.ReportMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.request.report.QueryReportPageListRequest;
import com.tuoheng.admin.utils.ShiroUtils;
import com.tuoheng.admin.vo.InspectionFileHandleVo;
import com.tuoheng.admin.vo.InspectionFileReportVo;
import com.tuoheng.admin.vo.InspectionReportVo;
import com.tuoheng.admin.vo.ReportPageListVo;
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;

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

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private ReportMapper reportMapper;

    public JsonResult getInspectionReport(String id) {
        log.info("进入查看巡检报告业务");

        String userId = ShiroUtils.getUserId();
        String tenantId = ShiroUtils.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())
                                                    .eq(InspectionFile::getMark, 1));

        List<InspectionFileReportVo> inspectionFileHandleVoList = InspectionFileConverMapper.INSTANCE.fromInspectionFileListToInspectionFileHandleVoList(inspectionFileList);
        inspectionReportVo.setInspectionFileReportVoList(inspectionFileHandleVoList);
        return inspectionReportVo;
    }
}
