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

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

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private WebSocketService webSocketService;

    public JsonResult ignore(String id) {
//        log.info("进入应急忽略事故业务");
        User user = CurrentUserUtil.getUserInfo();
        String userId = user.getId();
        String tenantId = user.getTenantId();
        JsonResult result = this.check(tenantId, id);
        if (0 != result.getCode()) {
            log.info("忽略应急事故业务：校验失败：{}", result.getMsg());
            return result;
        }
        Accident accident = (Accident) result.getData();

        this.updateAccident(userId, accident);

        webSocketService.accidentNotice(accident.getTenantId());

        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @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::getTenantId, tenantId)
                .eq(Accident::getId, id)
                .eq(Accident::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(accident)) {
            throw new ServiceException("应急事故不存在");
        }
        if (AccidentStatusEnum.UNTREATED.getCode() != accident.getStatus()) {
            throw new ServiceException("未处理的事故才能忽略");
        }
        return JsonResult.success(accident);
    }

    /**
     * 修改预警记录信息
     *
     * @param userId
     * @param userId
     */
    private void updateAccident(String userId, Accident accident) {
        Accident accidentUpdate = new Accident();
        accidentUpdate.setId(accident.getId());
        accidentUpdate.setStatus(AccidentStatusEnum.IGNORED.getCode());
        accidentUpdate.setUpdateUser(userId);
        accidentUpdate.setUpdateTime(DateUtils.now());
        Integer count = accidentMapper.updateById(accidentUpdate);
        if (count <= 0) {
            log.info("事故忽略，修改预警信息失败");
        }
    }
}
