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

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.Inspection;
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.mapper.InspectionMapper;
import com.tuoheng.admin.request.accident.ContinueToInspectRequest;
import com.tuoheng.admin.service.third.airport.drone.DroneService;
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-04-14
 */
@Slf4j
@Service
public class ContinueToInspectService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private DroneService droneService;

    /**
     * 继续巡检
     *
     * @return
     */
    public JsonResult continueToInspect(ContinueToInspectRequest request) {
        log.info("进入继续巡检业务, accidentId={}", request.getAccidentId());
        User user = CurrentUserUtil.getUserInfo();
        String tenantId = user.getTenantId();
        JsonResult result = this.checkParam(user.getId(), tenantId, request);
        if (0 != result.getCode()) {
            log.info("继续巡检：校验参数失败：{}", result.getMsg());
            return result;
        }

        Accident accident = (Accident) result.getData();

        Inspection inspection = inspectionMapper.selectById(accident.getInspectionId());

        droneService.continuee(inspection.getAirportId(), inspection.getId());

        this.updateAccident(user, accident);

        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult checkParam(String userId, String tenantId, ContinueToInspectRequest request) {
        if (StringUtils.isNull(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("应急事故不存在空");
        }
        return JsonResult.success(accident);
    }

    /**
     * 修改应急事故状态
     *
     * @param user
     * @param accident
     */
    private void updateAccident(User user, Accident accident) {
        accident.setStatus(AccidentStatusEnum.COMPLETED.getCode());
        accident.setUpdateUser(user.getId());
        accident.setUpdateTime(DateUtils.now());
        accident.setEndTime(DateUtils.now());
        int count = accidentMapper.updateById(accident);
        if (count <= 0) {
            log.info("应急事故处理完成修改状态失败, accidentId={}", accident.getId());
            throw new ServiceException("应急事故处理完成修改状态失败");
        }
    }
}
