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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.domain.Warning;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.QuestionMapper;
import com.tuoheng.admin.mapper.WarningMapper;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 根据Id查询预警信息业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-02-07
 */
@Slf4j
@Service
public class QueryWarningByIdService {

    @Autowired
    private WarningMapper warningMapper;

    @Autowired
    private QuestionMapper questionMapper;

    public JsonResult getOneById(Integer id) {
//        log.info("进入根据查询Id查询预警信息业务");
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();
        JsonResult result = this.check(tenantId, id);
        if (0 != result.getCode()) {
            log.info("根据Id查询预警信息业务：校验失败：{}", result.getMsg());
            return result;
        }
        Warning warning = (Warning) result.getData();
        return JsonResult.success(warning);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param id
     * @return
     */
    private JsonResult check(Integer tenantId, Integer id) {
        // 判断预警id是否为空
        if (ObjectUtil.isNotNull(id)) {
            throw new ServiceException("预警ID为空");
        }
        Warning warning = warningMapper.selectOne(new LambdaQueryWrapper<Warning>()
                .eq(Warning::getId, id)
                .eq(Warning::getTenantId, tenantId)
                .eq(Warning::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(warning)) {
            throw new ServiceException("预警信息不存在");
        }
        return JsonResult.success(warning);
    }

}
