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

import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.AccidentStatusEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.websocket.WebSocketService;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UpdateAccidentStatusService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private WebSocketService webSocketService;

    /**
     * 修改应急事故状态
     *
     * @param user
     * @param accident
     */
    public void update(User user, Accident accident) {
        log.info("修改应急事故状态, accidentId={}", accident.getId());
        accident.setStatus(AccidentStatusEnum.PROCESSING.getCode());
        accident.setUpdateUser(user.getId());
        accident.setUpdateTime(DateUtils.now());
        Integer count = accidentMapper.updateById(accident);
        if (count <= 0) {
            log.info("修改应急事故状态失败");
            throw new ServiceException("修改应急事故状态失败");
        }

        webSocketService.accidentNotice(accident.getTenantId());
    }

}
