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

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.request.accident.ReportAccidentRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
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-03-15
 */
@Slf4j
@Service
public class ReportAccidentService {

    @Autowired
    private AccidentMapper accidentMapper;

    public JsonResult report(ReportAccidentRequest request) {
//        log.info("进入事故上报业务");
        User user = CurrentUserUtil.getUserInfo();
        String userId = user.getId();
        String tenantId = user.getTenantId();
        JsonResult result = this.check(tenantId, request);
        if (0 != result.getCode()) {
            log.info("无事故业务：校验失败：{}", result.getMsg());
            return result;
        }
        Accident accident = (Accident) result.getData();
        this.updateAccident(userId, request, accident);
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult check(String tenantId, ReportAccidentRequest request) {
        if (StringUtils.isEmpty(request.getAccidentId())) {
            throw new ServiceException("事故ID为空");
        }
        Accident accident = accidentMapper.selectOne(new LambdaQueryWrapper<Accident>()
                .eq(Accident::getTenantId, tenantId)
                .eq(Accident::getId, request.getAccidentId())
                .eq(Accident::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(accident)) {
            throw new ServiceException("事故不存在");
        }
        if (AccidentStatusEnum.PROCESSING.getCode() != accident.getStatus()) {
            throw new ServiceException("处理中的事故才能上报");
        }
        return JsonResult.success(accident);
    }

    /**
     * 修改预警记录信息
     *
     * @param userId
     * @param request
     * @param accident
     */
    private void updateAccident(String userId, ReportAccidentRequest request, Accident accident) {
        Accident accidentUpdate = new Accident();
        accidentUpdate.setId(accident.getId());
        accidentUpdate.setIsAccident(1);
        accidentUpdate.setIsCasualties(request.getIsCasualties());
        accidentUpdate.setIsDrivingSafety(request.getIsDrivingSafety());
        accidentUpdate.setIsFire(request.getIsFire());
        accidentUpdate.setStatus(AccidentStatusEnum.PROCESSED.getCode());
        accidentUpdate.setUpdateUser(userId);
        accidentUpdate.setUpdateTime(DateUtils.now());
        accidentUpdate.setCheckUser(userId);
        accidentUpdate.setCheckTime(DateUtils.now());
        accidentUpdate.setRecord(request.getRecord());
        accidentUpdate.setReportTime(DateUtils.now());
        Integer count = accidentMapper.updateById(accidentUpdate);
        if (count <= 0) {
            log.info("事故上报，修改预警信息失败");
        }
    }
}
