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

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.service.menu.AccidentEnum;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.websocket.WebSocketService;
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;

import java.util.Date;

/**
 * @Author ChengWang
 * @Date 2023/5/9
 */
@Service
@Slf4j
public class UpdateAccidentStatusCompletedService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private WebSocketService webSocketService;


    public JsonResult updateStatus(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error(AccidentEnum.ACCIDENT_ID_IS_NULL.getCode(),AccidentEnum.ACCIDENT_ID_IS_NULL.getMsg());
        }
        User user = CurrentUserUtil.getUserInfo();
        String userId = user.getId();
        String tenantId = user.getTenantId();
        //根据任务id查询对应的事故
        Accident accident = new Accident();
        accident.setId(id);
        accident.setStatus(AccidentStatusEnum.COMPLETED.getCode());
        accident.setUpdateTime(new Date());
        accident.setUpdateUser(userId);
        //更新状态
        int result = accidentMapper.updateById(accident);
        if(result == 0){
            return JsonResult.error("状态更新失败");
        }
        //推送通知
        webSocketService.accidentNotice(tenantId);
        return JsonResult.success();
    }
}
