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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
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 2023-03-03
 */
@Slf4j
@Service
public class QueryAccidentByIdService {

    @Autowired
    private AccidentMapper accidentMapper;

    public JsonResult getInfoById(String id) {
//        log.info("进入根据ID查询事件业务");
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, id);
        if (0 != result.getCode()) {
            log.info("进入根据ID查询事件业务：校验失败：{}", result.getMsg());
            return result;
        }

       Accident accident = (Accident) result.getData();

        return JsonResult.success(accident);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param id
     * @return
     */
    private JsonResult check(String tenantId, String id) {
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("事故ID为空");
        }
        Accident accident = accidentMapper.selectOne(new LambdaQueryWrapper<Accident>()
                .eq(Accident::getId, id)
                .eq(Accident::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(accident)) {
            throw new ServiceException("事故不存在");
        }
        return JsonResult.success(accident);
    }

}
