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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.InspectionCycle;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.InspectionCycleMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
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 org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UpdateInspectionCycleEnableService {

    private final InspectionCycleMapper inspectionCycleMapper;
    private final InspectionMapper inspectionMapper;

    public UpdateInspectionCycleEnableService(InspectionCycleMapper inspectionCycleMapper,
                                              InspectionMapper inspectionMapper) {
        this.inspectionCycleMapper = inspectionCycleMapper;
        this.inspectionMapper = inspectionMapper;
    }


    public JsonResult updateEnable(String id, Integer isEnable) {
        JsonResult result = this.check(id, isEnable);
        if (0 != result.getCode()) {
            return result;
        }
        InspectionCycle inspectionCycle = (InspectionCycle) result.getData();
        inspectionCycle.setIsEnable(isEnable);
        inspectionCycle.setUpdateUser(CurrentUserUtil.getUserId());
        inspectionCycle.setUpdateTime(DateUtils.now());
        inspectionCycleMapper.updateEnable(inspectionCycle);

        // 禁用
        if (0 == isEnable) {
            // 将其下子任务都删除
            List<Inspection> inspectionList = inspectionMapper.selectList(Wrappers.<Inspection>lambdaQuery()
                    .eq(Inspection::getTenantId, inspectionCycle.getTenantId())
                    .eq(Inspection::getInspectionCycleId, inspectionCycle.getId())
                    .eq(Inspection::getStatus, InspectionStatusEnum.WAIT_FLIGHT.getCode())
                    .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                    .orderByDesc(Inspection::getCreateTime));
            if (CollectionUtil.isNotEmpty(inspectionList)) {
                for (Inspection inspection : inspectionList) {
                    inspection.setMark(MarkEnum.NOTVALID.getCode());
                    inspection.setUpdateUser(CurrentUserUtil.getUserId());
                    inspection.setUpdateTime(DateUtils.now());
                    inspectionMapper.update(inspection);
                }
            }
        }
        return JsonResult.success();
    }

    private JsonResult check(String id, Integer isEnable) {
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("周期任务ID为空");
        }
        InspectionCycle inspectionCycle = inspectionCycleMapper.selectById(id);
        if (ObjectUtil.isEmpty(inspectionCycle)) {
            throw new ServiceException("周期任务不存在");
        }
        return JsonResult.success(inspectionCycle);
    }

}
