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

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.entity.request.warning.WarningConfirmRequest;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.WarningStatusEnum;
import com.tuoheng.admin.mapper.WarningMapper;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.DateUtils;
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;

import java.util.HashMap;
import java.util.Map;

/**
 * 预警确认业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-02-07
 */
@Slf4j
@Service
public class WarningConfirmService {

    @Autowired
    private WarningMapper warningMapper;

    public JsonResult confirm(WarningConfirmRequest request) {
        log.info("进入预警确认业务, request:{}", request);
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();
        JsonResult result = this.check(tenantId, request);
        if (0 != result.getCode()) {
            log.info("进入任务问题确认业务：校验失败：{}", result.getMsg());
            return result;
        }
        // 确认预警
        result = this.updateStatus(user, request);

        return result;
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult check(Integer tenantId, WarningConfirmRequest request) {
        // 判断预警id是否为空
        if (ObjectUtil.isNull(request.getId())) {
            throw new ServiceException("预警ID为空");
        }
        Warning warning = warningMapper.selectOne(new LambdaQueryWrapper<Warning>()
                            .eq(Warning::getId, request.getId())
                            .eq(Warning::getTenantId, tenantId)
                            .eq(Warning::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(warning)) {
            throw new ServiceException("预警信息不存在");
        }
        if (!tenantId.equals(warning.getTenantId())) {
            throw new ServiceException("该租户没有查询该预警信息的权限");
        }
        if (WarningStatusEnum.CONFIRM.getCode() == warning.getStatus()) {
            throw new ServiceException("该预警已确认，请不要重复确认");
        }
        if (WarningStatusEnum.IGNORE.getCode() == warning.getStatus()) {
            throw new ServiceException("该预警已忽略，不能在确认");
        }
        return JsonResult.success(warning);
    }

    /**
     * 预警状态，改为确认
     *
     * @param user
     * @param request
     */
    private JsonResult updateStatus(User user, WarningConfirmRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", request.getId());
        map.put("status", WarningStatusEnum.CONFIRM.getCode());
        map.put("updateUser", user.getId());
        map.put("updateTime", DateUtils.now());
        map.put("checkUser", user.getId());
        map.put("checkTime", DateUtils.now());
        map.put("checkResult", request.getCheckResult());
        Integer rowCount = warningMapper.confirm(map);
        if (rowCount <= 0) {
            log.info("修改预警状态， 确认失败");
            throw new ServiceException("修改预警状态， 确认失败");
        }
        return JsonResult.success();
    }
}
