package com.tuoheng.admin.service.inspectionfile.handle;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.InspectionFileHandleConverMapper;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.InspectionFileHandle;
import com.tuoheng.admin.entity.QuestionType;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspectionfilehandle.QueryInspectionFileHandleByInspectionFileIdCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.vo.inspection.InspectionFileHandleVo;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 根据任务问题ID查询任务问题处理业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-06
 */
@Slf4j
@Service
public class QueryInspectionFileHandleByInspectionFileIdService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private InspectionFileHandleMapper inspectionFileHandleMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    public JsonResult handle(String id) {
        log.info("进入根据任务问题ID查询任务问题处理业务, id:{}", id);
        JsonResult result = this.check(id);
        if (0 != result.getCode()) {
            log.info("进入根据任务ID查询巡检任务问题分页列表业务：校验失败：{}", result.getMsg());
            return result;
        }
        InspectionFile inspectionFile = (InspectionFile) result.getData();

        InspectionFileHandle inspectionFileHandle = inspectionFileHandleMapper.selectOne(new LambdaQueryWrapper<InspectionFileHandle>()
                .eq(InspectionFileHandle::getInspectionFileId, id)
                .eq(InspectionFileHandle::getMark, MarkEnum.VALID.getCode()));

        InspectionFileHandleVo inspectionFileHandleVo = this.buildInspectionFileHandleVo(inspectionFile, inspectionFileHandle);

        return JsonResult.success(inspectionFileHandleVo);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(String id) {
        // 判断任务问题id是否为空
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryInspectionFileHandleByInspectionFileIdCodeEnum.INSPECTION_FILE_ID_IS_NULL.getCode(), QueryInspectionFileHandleByInspectionFileIdCodeEnum.INSPECTION_FILE_ID_IS_NULL.getMsg());
        }
        // 判断任务是否存在
        InspectionFile inspectionFile = inspectionFileMapper.selectOne(new LambdaQueryWrapper<InspectionFile>()
                .eq(InspectionFile::getId, id)
                .eq(InspectionFile::getMark, MarkEnum.VALID.getCode()));

        if (null == inspectionFile) {
            return JsonResult.error(QueryInspectionFileHandleByInspectionFileIdCodeEnum.INSPECTION_FILE_IS_NOT_EXIST.getCode(), QueryInspectionFileHandleByInspectionFileIdCodeEnum.INSPECTION_FILE_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(inspectionFile);
    }

    /**
     * 处理字段
     */
    private InspectionFileHandleVo buildInspectionFileHandleVo(InspectionFile inspectionFile, InspectionFileHandle inspectionFileHandle) {
        InspectionFileHandleVo inspectionFileHandleVo;
        String handlerImages = "";
        if (null != inspectionFileHandle) {
            inspectionFileHandleVo = InspectionFileHandleConverMapper.INSTANCE.fromInspectionFileHandleToInspectionFileHandleVo(inspectionFileHandle);
            if (!StringUtils.isEmpty(inspectionFileHandle.getHandlerImage())) {
                String[] arr = inspectionFileHandle.getHandlerImage().split(",");
                List<String> list = Arrays.stream(arr).map(String::toString).collect(Collectors.toList());
                for (String str : list) {
                    handlerImages += CommonConfig.imageURL + str + ",";
                }
            }
        } else {
            inspectionFileHandleVo = new InspectionFileHandleVo();
        }
        if (StringUtils.isNotEmpty(handlerImages)) {
            handlerImages = handlerImages.substring(0, handlerImages.length() - 1);
            inspectionFileHandleVo.setHandlerImages(handlerImages);
        }
        QuestionType questionType = questionTypeMapper.selectOne(new LambdaQueryWrapper<QuestionType>()
                .eq(QuestionType::getCode, inspectionFile.getQuestionCode())
                .eq(QuestionType::getMark, MarkEnum.VALID.getCode()));
        if (null != questionType) {
            inspectionFileHandleVo.setQuestionName(questionType.getContent());
        }
        if (StringUtils.isNotEmpty(inspectionFileHandleVo.getHandlerUser())) {
            User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getId, inspectionFileHandleVo.getHandlerUser())
                    .eq(User::getMark, MarkEnum.VALID.getCode()));
            if (ObjectUtil.isNotNull(user)) {
                inspectionFileHandleVo.setHandlerUsername(user.getRealname());
            }
        }
        inspectionFileHandleVo.setQuestionCode(inspectionFile.getQuestionCode());
        inspectionFileHandleVo.setFileImage(CommonConfig.imageURL + inspectionFile.getFileImage());
        inspectionFileHandleVo.setFileThumbnail(CommonConfig.imageURL + inspectionFile.getFileThumbnail());
        inspectionFileHandleVo.setFileOriginal(CommonConfig.imageURL + inspectionFile.getFileOriginal());
        return inspectionFileHandleVo;
    }
}
