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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.QuestionType;
import com.tuoheng.admin.enums.InspectionFileStatusEnum;
import com.tuoheng.admin.enums.code.inspectionfile.EditInspectionFileQuestionTypeCodeEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.QuestionTypeMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.DateUtils;
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;

/**
 * 修改任务问题类型业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-16
 */
@Slf4j
@Service
public class UpdateInspectionFileQuestionTypeService {

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    /**
     * 修改任务问题类型
     *
     * @return
     */
    public JsonResult update(String id, String questionCode) {
        log.info("进入修改任务问题类型业务接口");
        String userId = CurrentUserUtil.getUserId();
        JsonResult result = this.check(id, questionCode);
        if (0 != result.getCode()) {
            log.info("修改任务问题类型业务接口：校验失败：{}", result.getMsg());
            return result;
        }
        QuestionType questionType = (QuestionType) result.getData();
        InspectionFile inspectionFile = new InspectionFile();
        inspectionFile.setId(id);
        inspectionFile.setQuestionCode(questionType.getCode());
        inspectionFile.setQuestionName(questionType.getContent());
        inspectionFile.setQuestionId(questionType.getId());
        inspectionFile.setStatus(InspectionFileStatusEnum.CONFIRMED.getCode());
        inspectionFile.setUpdateUser(userId);
        inspectionFile.setUpdateTime(DateUtils.now());
        inspectionFile.setCheckUser(userId);
        inspectionFile.setCheckTime(DateUtils.now());
        Integer rowId = inspectionFileMapper.updateById(inspectionFile);
        if (rowId <= 0) {
            log.info("修改任务问题类型业务接口：修改任务失败：{}", result.getMsg());
            return JsonResult.error(EditInspectionFileQuestionTypeCodeEnum.EDIT_IS_FAILED.getCode(), EditInspectionFileQuestionTypeCodeEnum.EDIT_IS_FAILED.getMsg());
        }
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param id
     * @param questionCode
     * @return
     */
    private JsonResult check(String id, String questionCode) {
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(EditInspectionFileQuestionTypeCodeEnum.ID_IS_NULL.getCode(), EditInspectionFileQuestionTypeCodeEnum.ID_IS_NULL.getMsg());
        }

        if (ObjectUtil.isNull(questionCode)) {
            return JsonResult.error(EditInspectionFileQuestionTypeCodeEnum.QUESTION_NAME_IS_NULL.getCode(), EditInspectionFileQuestionTypeCodeEnum.QUESTION_NAME_IS_NULL.getMsg());
        }

        InspectionFile inspectionFile = inspectionFileMapper.selectOne(Wrappers.<InspectionFile>lambdaQuery()
                .eq(InspectionFile::getId, id)
                .eq(InspectionFile::getMark, 1));

        if (ObjectUtil.isNull(inspectionFile)) {
            return JsonResult.error(EditInspectionFileQuestionTypeCodeEnum.INSPECTION_FILE_IS_NOT_EXIST.getCode(), EditInspectionFileQuestionTypeCodeEnum.INSPECTION_FILE_IS_NOT_EXIST.getMsg());
        }

        QuestionType questionType = questionTypeMapper.selectOne(Wrappers.<QuestionType>lambdaQuery()
                .eq(QuestionType::getCode, questionCode)
                .eq(QuestionType::getMark, 1));
        if (ObjectUtil.isNull(questionType)) {
            return JsonResult.error(EditInspectionFileQuestionTypeCodeEnum.QUESTION_TYPE_IS_NOT_EXIST.getCode(), EditInspectionFileQuestionTypeCodeEnum.QUESTION_TYPE_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(questionType);
    }

}
