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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionInfoByIdCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
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;

/**
 *  根据Id查询巡检任务业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-25
 */
@Slf4j
@Service
public class QueryInspectionByIdService {

    @Autowired
    private InspectionMapper inspectionMapper;

    public JsonResult getInspectionInfo(String id) {
        log.info("进入查询巡检任务信息业务");
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, id);
        if (0 != result.getCode()) {
            log.info("进入查询巡检任务信息业务：校验失败：{}", result.getMsg());
            return result;
        }

        Inspection inspection = (Inspection) result.getData();

        return JsonResult.success(inspection);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(String tenantId, String id) {
        // 判断任务id是否为空
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryInspectionInfoByIdCodeEnum.INSPECTION_ID_IS_NULL.getCode(), QueryInspectionInfoByIdCodeEnum.INSPECTION_ID_IS_NULL.getMsg());
        }
        // 判断任务是否存在
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                                    .eq(Inspection::getTenantId, tenantId)
                                    .eq(Inspection::getId, id)
                                    .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        if (null == inspection) {
            return JsonResult.error(QueryInspectionInfoByIdCodeEnum.INSPECTION_IS_NOT_EXIST.getCode(), QueryInspectionInfoByIdCodeEnum.INSPECTION_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(inspection);
    }

}
