package com.yunfeiyun.agmp.fms.scheduler;

import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.enums.CommonEnums;
import com.yunfeiyun.agmp.fms.domain.FmsBlock;
import com.yunfeiyun.agmp.fms.domain.FmsPeriod;
import com.yunfeiyun.agmp.fms.domain.FmsPlan;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsPlanReqVo;
import com.yunfeiyun.agmp.fms.service.IFmsBlockService;
import com.yunfeiyun.agmp.fms.service.IFmsCropService;
import com.yunfeiyun.agmp.fms.service.IFmsPeriodService;
import com.yunfeiyun.agmp.fms.service.IFmsPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class FmsPlanScheduler {

    @Autowired
    private IFmsPlanService fmsPlanService;
    @Autowired
    private IFmsCropService fmsCropService;
    @Autowired
    private IFmsPeriodService fmsPeriodService;
    @Autowired
    private IFmsBlockService fmsBlockService;

    /**
     * 种植规划状态的修改
     * @throws Exception
     */
    @Scheduled(cron = "0 1 */1 * * ?")
    public void fmsPlanStatusUpdate() throws Exception{
        log.info("种植规划状态修改定时任务开始");

        FmsPlanReqVo reqVo = new FmsPlanReqVo();
        reqVo.setPlanStatus(CommonEnums.PLAN_STATUS_NOT_START.getCode());
        reqVo.setUpdateType("0");
        reqVo.setNowDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        List<FmsPlan> fmsPlanResVos = fmsPlanService.selectPlanListScheduler(reqVo);
        List<FmsPlan> list = new ArrayList<>();
        for (FmsPlan fmsPlan : fmsPlanResVos) {
            fmsPlan.setPlanStartdate(fmsPlan.getPlanPlanstartdate());
            fmsPlan.setPlanStatus(CommonEnums.PLAN_STATUS_START.getCode());
            list.add(fmsPlan);
        }
        if (list.size() > 0) {
            fmsPlanService.updateFmsPlanBatch(list);
        }
        log.info("种植规划状态修改定时任务结束");
    }

    @Scheduled(cron = "0 1 */1 * * ?")
    public void fmsPlanPeriodNameUpdate() throws Exception{
        log.info("种植规划生成周期阶段修改定时任务开始");
        FmsPlanReqVo reqVo = new FmsPlanReqVo();
        reqVo.setPlanStatus(CommonEnums.PLAN_STATUS_START.getCode());
        List<FmsPlan> fmsPlans = fmsPlanService.selectPlanListScheduler(reqVo);
        List<FmsPlan> list = new ArrayList<>();
        for (FmsPlan fmsPlan : fmsPlans) {
            String cropId = fmsPlan.getCropId();
            //计算开始后的天数
            String planPlanstartdate = fmsPlan.getPlanPlanstartdate();
            LocalDateTime startDate = LocalDateTime.parse(planPlanstartdate, DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode()));
            LocalDateTime nowDate = LocalDateTime.now();
            long days = nowDate.toLocalDate().toEpochDay() - startDate.toLocalDate().toEpochDay();

            List<FmsPeriod> fmsPeriods = fmsPeriodService.selectFmsPeriodByCropId(cropId);
            if (fmsPeriods.size() > 0) {
                int periodDays = 0;
                for (FmsPeriod fmsPeriod : fmsPeriods) {
                    periodDays += fmsPeriod.getPeriodDays();
                    if (periodDays >= days) {
                        fmsPlan.setPlanPeriodname(fmsPeriod.getPeriodName());
                        list.add(fmsPlan);
                        break;
                    }
                }
                if (periodDays < days) {
                    fmsPlan.setPlanPeriodname(fmsPeriods.get(fmsPeriods.size()-1).getPeriodName());
                    list.add(fmsPlan);
                }
            }
        }
        if (list.size() > 0) {
            fmsPlanService.updateFmsPlanBatch(list);
        }
        log.info("种植规划生长周期阶段修改定时任务结束");
    }

    public AjaxResult setBlockAreaplant (FmsBlock fmsBlock, BigDecimal addArea) {
        FmsPlanReqVo fmsPlanReqVo = new FmsPlanReqVo();
        fmsPlanReqVo.setBlockId(fmsBlock.getBlockId());
        fmsPlanReqVo.setPlanStatus(CommonEnums.PLAN_STATUS_START.getCode() + "," + CommonEnums.PLAN_STATUS_NOT_START.getCode());
        BigDecimal sumArea = fmsPlanService.selectSumPlanArea(fmsPlanReqVo);
        if (addArea != null) {
            sumArea = sumArea.add(addArea);
        }
//        if (sumArea > fmsBlock.getBlockArea()) {
//            return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
//        }

        fmsBlock.setBlockAreaplant(sumArea);
        return AjaxResult.success();
    }
}
