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

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.enums.InspectionCycleTypeEnum;
import com.tuoheng.admin.request.cycle.EditInspectionCycleRequest;
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.stereotype.Component;

import java.text.ParseException;
import java.util.Date;

@Slf4j
@Component
public class EditInspectionCycleCheckParamService {

    public JsonResult check(EditInspectionCycleRequest request) {
        if(StringUtils.isEmpty(request.getId())){
            return JsonResult.error("周期性任务id为空");
        }

        if(null == request.getRoadId()){
            return JsonResult.error("巡检公路id为空");
        }

        if(StringUtils.isEmpty(request.getName())){
            return JsonResult.error("任务名称为空");
        }

        if(null == request.getSectionId()){
            return JsonResult.error("巡检路段id为空");
        }

        if(null == request.getAirportId()){
            return JsonResult.error("巡检机场id为空");
        }

        if(null == request.getAirportLineId()){
            return JsonResult.error("巡检航线id为空");
        }


        /**
         * 周期性计划需要判断是否在循环开始时间和结束时间内
         */
        if ((ObjectUtil.isNotNull(request.getCycleStartTime()) && ObjectUtil.isNull(request.getCycleEndTime()))
                || (ObjectUtil.isNull(request.getCycleStartTime()) && ObjectUtil.isNotNull(request.getCycleEndTime()))) {
            return JsonResult.error("周期性任务循环开始时间和循环结束时间要么都存在要么都不存在！");
        }

        if(ObjectUtil.isNull(request.getCycleTime())){
            return JsonResult.error("循环执行时间为空");
        }

        if(null == request.getType()){
            return JsonResult.error("执行频次为空");
        }

        if (InspectionCycleTypeEnum.EVERYWEEK.getCode() == request.getType()) {
            if (StringUtils.isEmpty(request.getCycleFrequency())) {
                return JsonResult.error("循环周期类型为周，循环日期不能为空！");
            }
            for (String item : request.getCycleFrequency().split(",")) {
                if ((Integer.valueOf(item) < 0 || Integer.valueOf(item) > 7)) {
                    return JsonResult.error("循环周期类型为周，循环日期只能在1~7范围内！");
                }
            }
        } else if (InspectionCycleTypeEnum.EVERYMONTH.getCode() == request.getType()) {
            if (StringUtils.isEmpty(request.getCycleFrequency())) {
                return JsonResult.error("循环周期类型为月，循环日期不能为空！");
            }
            for (String item : request.getCycleFrequency().split(",")) {
                if ((Integer.valueOf(item) < 0 || Integer.valueOf(item) > 31)) {
                    return JsonResult.error("循环周期类型为月，循环日期只能在1~31范围内！");
                }
            }
        }

        Date dateBegin = null;
        try {
            dateBegin = DateUtils.parseDate(DateUtils.getDate(), DateUtils.YYYY_MM_DD);
        } catch (ParseException e) {
            log.error("获取开始时间异常：{}", e);
            throw new RuntimeException(e);
        }

        return JsonResult.success();
    }

}
