package com.yunfeiyun.agmp.fms.service.impl;

import com.yunfeiyun.agmp.common.constant.Constants;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.core.domain.model.Policy;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.framework.web.service.PolicyService;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.fms.mapper.FmsPlanMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsTaskMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsTaskpatrolMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsTaskpickMapper;
import com.yunfeiyun.agmp.fms.constant.Fms;
import com.yunfeiyun.agmp.fms.service.IFmsWaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 农事模块流水号业务层处理
 *
 * @author 杨晓辉
 * @date 2023-05-24
 */
@Service
public class FmsWaterServiceImpl implements IFmsWaterService {

    private static ReentrantLock waterLock = new ReentrantLock();

    public static final Integer waterLeftV1 = 1;
    public static final Integer waterLeftV6 = 6;
    public static final Integer waterLeftV7 = 7;

    @Autowired
    private FmsPlanMapper fmsPlanMapper;

    @Autowired
    private FmsTaskMapper fmsTaskMapper;

    @Autowired
    private FmsTaskpickMapper fmsTaskpickMapper;

    @Autowired
    private FmsTaskpatrolMapper fmsTaskpatrolMapper;

    @Autowired
    private PolicyService policyService;

    @Override
    public  String genWater(Fms.WaterType waterType) {
        String waterNo = "-1";
        waterNo = genWater(waterType,"");
        return waterNo;
    }


    private  Integer getWater(Fms.WaterType waterType) {
        if(Fms.WaterType.PLAN.equals(waterType)){  //种植计划编号
            return getFmsPlanWater();
        }else if(Fms.WaterType.PICKS.equals(waterType)){ //采收编号
            return getTaskpickWater();
        }else if(Fms.WaterType.TASK.equals(waterType) ||
                Fms.WaterType.PICK.equals(waterType) ||
                Fms.WaterType.PATROL.equals(waterType)  ){    //农事任务
            return getFmsTaskWater();
        }
        return 0;
    }

    /**
    采收编号
     */
    private  Integer getTaskpickWater() {
        Integer reqNo = 0;
        String maxNo = fmsTaskpickMapper.selectFmsTaskpickMaxWaterKey();
        if (StringUtils.isNull(maxNo)) { reqNo = 1; }
        else{
            if(!eqDay(maxNo.substring(waterLeftV1,waterLeftV6))) reqNo = 0;
            else reqNo = Integer.parseInt(maxNo.substring(waterLeftV7));
        }
        return  reqNo;
    }
    /**
     种植计划编号
     */
    private  Integer getFmsPlanWater() {
        Integer reqNo = 0;
        String maxNo = fmsPlanMapper.selectFmsPlanMaxWaterKey();
        if (StringUtils.isNull(maxNo)) { reqNo = 0; }
        else{
            if(!eqDay(maxNo.substring(waterLeftV1,waterLeftV6))) reqNo = 0;
            else reqNo = Integer.parseInt(maxNo.substring(waterLeftV7));
        }
        return  reqNo;
    }


    /**
     *
     * 农事，采收，巡田任务编号
     * @return
     */
    private  Integer getFmsTaskWater() {
        Integer reqNo = 0;
        String maxNo = fmsTaskMapper.selectFmsTaskMaxWaterKey();
        if (StringUtils.isNull(maxNo)) { reqNo = 0; }
        else{
            if(!eqDay(maxNo.substring(waterLeftV1,waterLeftV6))) reqNo = 0;
            else reqNo = Integer.parseInt(maxNo.substring(waterLeftV7));
        }
        return  reqNo;
    }



    private  boolean eqDay(String waterDay) {
       return  waterDay.equals(new SimpleDateFormat("yyMMdd").format(new Date()));
    }

    private  String genWater(Integer waterNo) {
        String water = "";
        waterLock.lock();
        try {
            String nextNo = String.format("%03d", waterNo + 1);
            water = new SimpleDateFormat("yyMMdd").format(new Date()) + nextNo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            waterLock.unlock();
        }
        return water;
    }



    private  String genWater(Fms.WaterType waterType,String waterPolicy) {
        Policy policy = policyService.getPolicy();
        if(Constants.POLICY_WATER_TABLE.equals(policy.getWater())) {
            Integer waterMaxSeq = this.getWater(waterType);
            return waterType.getKey() + this.genWater(waterMaxSeq);
        }else {
            throw new BizException(ErrorCode.REQUEST_TYPE_UNSUPPORT.getCode(),ErrorCode.REQUEST_TYPE_UNSUPPORT.getMessage());
        }
        //return "-1";
    }

}
