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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.conver.InspectionFileConverMapper;
import com.tuoheng.admin.conver.ReportConverMapper;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.InspectionFileHandle;
import com.tuoheng.admin.entity.Report;
import com.tuoheng.admin.enums.code.report.QueryInspectionReportCodeEnum;
import com.tuoheng.admin.mapper.InspectionFileHandleMapper;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.ReportMapper;
import com.tuoheng.admin.utils.ShiroUtils;
import com.tuoheng.admin.vo.InspectionFileHandleReportVo;
import com.tuoheng.admin.vo.InspectionFileReportVo;
import com.tuoheng.admin.vo.InspectionReportVo;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

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

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private InspectionFileHandleMapper inspectionFileHandleMapper;

    public JsonResult getInspectionHandleReport(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);

        Map<String, InspectionFileHandle> inspectionFileHandleMap = this.getInspectionFileHandleMap(inspectionFileList);
        if (null != inspectionFileHandleMap) {
            InspectionFileHandle inspectionFileHandle;
            InspectionFileHandleReportVo inspectionFileHandleReportVo;
            List<String> handlerImageList = null;
            for (InspectionFileReportVo inspectionFileReportVo : inspectionFileHandleVoList) {
                inspectionFileHandle = inspectionFileHandleMap.get(inspectionFileReportVo.getId());
                if (null != inspectionFileHandle) {
                    handlerImageList = new ArrayList<>();
                    String[] arr = inspectionFileHandle.getHandlerImage().split(",");
                    List<String> list = Arrays.stream(arr).map(String::toString).collect(Collectors.toList());
                    for (String str : list) {
                        handlerImageList.add(CommonConfig.imageURL + str);
                    }
                    inspectionFileHandleReportVo = new InspectionFileHandleReportVo();
                    inspectionFileHandleReportVo.setHandlerUser(inspectionFileHandle.getHandlerUser());
                    inspectionFileHandleReportVo.setHandlerResult(inspectionFileHandle.getHandlerResult());
                    inspectionFileHandleReportVo.setHandlerTime(inspectionFileHandle.getHandlerTime());
                    inspectionFileHandleReportVo.setHandlerImageList(handlerImageList);
                    inspectionFileReportVo.setInspectionFileHandleReportVo(inspectionFileHandleReportVo);
                }
            }
        }
        inspectionReportVo.setInspectionFileReportVoList(inspectionFileHandleVoList);

        return inspectionReportVo;
    }

    private Map<String, InspectionFileHandle> getInspectionFileHandleMap(List<InspectionFile> inspectionFileList) {
        List<String> inspectionFileIdList = inspectionFileList.stream().map(o -> o.getId()).collect(Collectors.toList());
        List<InspectionFileHandle> inspectionFileHandleList = null;
        if (!CollectionUtil.isEmpty(inspectionFileIdList)) {
            inspectionFileHandleList = inspectionFileHandleMapper.selectList(Wrappers.<InspectionFileHandle>lambdaQuery()
                    .in(InspectionFileHandle::getInspectionFileId, inspectionFileIdList)
                    .eq(InspectionFileHandle::getMark, 1));

        }
        if (CollectionUtil.isEmpty(inspectionFileIdList)) {
            return null;
        }
        Map<String, InspectionFileHandle> inspectionFileHandleMap = inspectionFileHandleList.stream().collect(Collectors.toMap(InspectionFileHandle::getInspectionFileId, Function.identity()));
        return inspectionFileHandleMap;
    }
}
