package com.tuoheng.admin.service.inspectionfile.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.entity.Inspection;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.QuestionType;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionPageListCodeEnum;
import com.tuoheng.admin.enums.code.inspectionfile.QueryInspectionFilePageListByInspectionIdCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.request.inspectionfile.QueryInspectionFilePageListByInspectionIdRequest;
import com.tuoheng.admin.vo.inspection.InspectionFilePageListByInspectionIdVo;
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.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 根据任务ID查询巡检任务问题分页列表业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-05
 */
@Slf4j
@Service
public class QueryInspectionFilePageListByInspectionIdService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    public JsonResult getPageListByInspectionId(QueryInspectionFilePageListByInspectionIdRequest request) {
        log.info("进入根据任务ID查询巡检任务问题分页列表业务, inspectionId:{}, questionId:{}", request.getInspectionId(), request.getQuestionCode());
        JsonResult result = this.check(request);
        if (0 != result.getCode()) {
            log.info("进入根据任务ID查询巡检任务问题分页列表业务：校验失败：{}", result.getMsg());
            return result;
        }

        // 设置分页参数
        IPage<InspectionFile> page = new Page<>(request.getPage(), request.getLimit());
        // 查询结果
        IPage<InspectionFile> pageData = inspectionFileMapper.selectPageListByInspectionId(page, request);
        if (null == pageData || pageData.getTotal() == 0) {
            log.info("获取任务分页列表为空");
            return JsonResult.success(null, QueryInspectionPageListCodeEnum.DATA_IS_FAILED.getMsg());
        }

        // 构造返回结果对象
        List<InspectionFilePageListByInspectionIdVo> inspectionFilePageListVoList = this.buildInspectionFilePageListVoList(pageData.getRecords());

        // 重写返回结果对象
        IPage<InspectionFilePageListByInspectionIdVo> inspectionFilePageListVoPageData = new Page<>();
        inspectionFilePageListVoPageData.setPages(pageData.getPages());
        inspectionFilePageListVoPageData.setCurrent(pageData.getCurrent());
        inspectionFilePageListVoPageData.setSize(pageData.getSize());
        inspectionFilePageListVoPageData.setTotal(pageData.getTotal());
        inspectionFilePageListVoPageData.setRecords(inspectionFilePageListVoList);

        return JsonResult.success(inspectionFilePageListVoPageData);
    }

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

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

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

    /**
     *  1）、查找问题类型字段
     *  2）、拼接缩略图路径
     *
     * @param inspectionFileList
     * @return
     */
    private List<InspectionFilePageListByInspectionIdVo> buildInspectionFilePageListVoList(List<InspectionFile> inspectionFileList) {
        List<InspectionFilePageListByInspectionIdVo> inspectionFilePageListVoList = InspectionFileConverMapper.INSTANCE.fromInspectionFileListToInspectionFilePageByInspectionIdListVoList(inspectionFileList);
        for (InspectionFilePageListByInspectionIdVo inspectionFilePageListVo : inspectionFilePageListVoList) {
            inspectionFilePageListVo.setFileImage(CommonConfig.imageURL + inspectionFilePageListVo.getFileImage());
            inspectionFilePageListVo.setFileThumbnail(CommonConfig.imageURL + inspectionFilePageListVo.getFileThumbnail());
            inspectionFilePageListVo.setFileOriginal(CommonConfig.imageURL + inspectionFilePageListVo.getFileOriginal());
        }
        return inspectionFilePageListVoList;
    }
}
