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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.conver.InspectionConverMapper;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.entity.domain.User;
import com.tuoheng.admin.enums.InspectionExecutionStatusEnum;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.EditInspectionCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.entity.request.inspection.EditInspectionRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
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 2022-11-24
 */
@Slf4j
@Service
public class UpdateInspectionService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    /**
     * 修改任务
     *  1、机场任务：
     *      1）、只有本部门管理员（上上级部门管理员不可编辑）和创建用户才能编辑；
     *      2）、待执行任务，执行后不可编辑；
     *      3）、编辑信息包括，任务地点，航段，巡检机场，巡检路线，巡检时间，执行时间
     *  2、飞手任务不能编辑。
     *
     * @return
     */
    public JsonResult update(EditInspectionRequest editInspectionRequest) {
        log.info("进入修改任务业务接口");
        User user = CurrentUserUtil.getUserInfo();
        String tenantId = user.getTenantId();
        JsonResult result = this.check(tenantId, editInspectionRequest);
        if (0 != result.getCode()) {
            log.info("修改任务业务接口：校验失败：{}", result.getMsg());
            return result;
        }
        Inspection inspection = InspectionConverMapper.INSTANCE.fromEditInspectionRequestToInspection(editInspectionRequest);
        inspection.setUpdateUser(user.getId());
        inspection.setUpdateTime(DateUtils.now());

        Integer rowId = inspectionMapper.update(inspection);
        if (rowId <= 0) {
            log.info("修改任务业务接口：修改任务失败：{}", result.getMsg());
            return JsonResult.error(EditInspectionCodeEnum.EDIT_IS_FAILED.getCode(), EditInspectionCodeEnum.EDIT_IS_FAILED.getMsg());
        }

        log.info("修改任务业务接口：修改任务成功:{}", inspection);
        return JsonResult.success();
    }

    /**
     * 检查参数
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult check(String tenantId, EditInspectionRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return JsonResult.error(EditInspectionCodeEnum.ID_IS_NULL.getCode(), EditInspectionCodeEnum.ID_IS_NULL.getMsg());
        }
        if(StringUtils.isEmpty(request.getName())){
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_NAME_IS_NULL.getCode(),EditInspectionCodeEnum.INSPECTION_NAME_IS_NULL.getMsg());
        }

        if (ObjectUtil.isEmpty(request.getAirportId()) || 0 == request.getAirportId()) {
            return JsonResult.error(EditInspectionCodeEnum.AIRPORT_IS_NULL.getCode(), EditInspectionCodeEnum.AIRPORT_IS_NULL.getMsg());
        }

        if (ObjectUtil.isEmpty(request.getInspectionLine()) || 0 == request.getInspectionLine()) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_LINE_IS_NULL.getCode(), EditInspectionCodeEnum.INSPECTION_LINE_IS_NULL.getMsg());
        }
        if (ObjectUtil.isEmpty(request.getInspectionTime())) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_TIME_IS_NULL.getCode(), EditInspectionCodeEnum.INSPECTION_TIME_IS_NULL.getMsg());
        }
        int timaDiff = DateUtils.compareToDate(request.getInspectionTime(), DateUtils.now());
        if (2 != timaDiff) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_TIME_NOT_AFTER_THAN_CURRENT_TIME.getCode(), EditInspectionCodeEnum.INSPECTION_TIME_NOT_AFTER_THAN_CURRENT_TIME.getMsg());
        }
        //飞行高度
        if(StringUtils.isEmpty(request.getFlyHeight())){
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_FLY_HEIGHT_IS_NULL.getCode(),EditInspectionCodeEnum.INSPECTION_FLY_HEIGHT_IS_NULL.getMsg());
        }

        //查询当前对应的任务
        Inspection inspection = inspectionMapper.selectOne(Wrappers.<Inspection>lambdaQuery()
                                    .eq(Inspection::getId, request.getId())
                                    .eq(Inspection::getTenantId,tenantId)
                                    .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(inspection)) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_IS_NOT_EXIST.getCode(), EditInspectionCodeEnum.INSPECTION_IS_NOT_EXIST.getMsg());
        }
        if (InspectionStatusEnum.WAIT_FLIGHT.getCode() != inspection.getStatus()) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_IS_EXECUTED.getCode(), EditInspectionCodeEnum.INSPECTION_IS_EXECUTED.getMsg());
        }
        if (InspectionExecutionStatusEnum.EXECUTED.getCode() == inspection.getExecutionStatus()) {
            return JsonResult.error(EditInspectionCodeEnum.INSPECTION_IS_EXECUTED.getCode(), EditInspectionCodeEnum.INSPECTION_IS_EXECUTED.getMsg());
        }
        return JsonResult.success(inspection);
    }

}
