package com.chinaweal.aiccs.aiccs.punishment.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.punishment.service.TridmakerService;
import com.chinaweal.aiccs.common.entity.Tridmaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author Lee
 * @since 2021-06-28 1:56
 */
@Component
public class BusTridMakeUtil {

    @Autowired
    private TridmakerService tridmakerService;

    private String getTridmakerWithDay(String makerID) {
        String num = null;
        Integer curnum = -1;
        Integer nextNum = -1;
        //获取当前的时间
        LocalDateTime curDate = LocalDateTime.now();

        Tridmaker tridmaker = tridmakerService.getById(makerID);
        //比较当前时间和数据库存储的时间
        if(tridmaker == null){
            tridmaker = new Tridmaker();
            tridmaker.setCurdate(curDate);
            tridmaker.setMakerid(makerID);
            tridmaker.setCurnum(0);
            //tridmaker.setAreaCode(areaCode);
            tridmakerService.save(tridmaker);
        }
        LocalDateTime dbDate = tridmaker.getCurdate();
        if(dbDate != null) {
            if (curDate.toLocalDate().toEpochDay() - dbDate.toLocalDate().toEpochDay() == 0) {// 同一天
                curnum = tridmaker.getCurnum();
            } else {//不是同一天
                curnum = 0;// 重置
                tridmaker.setCurdate(curDate);
            }
        }

        nextNum = curnum + 1;
        tridmaker.setCurnum(nextNum);
        tridmakerService.updateById(tridmaker);

        //根据规则生成受理号
        int year = curDate.getYear();
        int month = curDate.getMonthValue();
        int day = curDate.getDayOfMonth();
        num = String.valueOf(year).substring(2, 4)+beforeZeroFill(month, 2) +beforeZeroFill(day, 2) + beforeZeroFill(nextNum, 4);
        return num;
    }

    private String getTridmakerWithDayAndAreaCode(String makerID,String areaCode) {
        String num = null;
        Integer curnum = -1;
        Integer nextNum = -1;
        //获取当前的时间
        LocalDateTime curDate = LocalDateTime.now();
        LambdaQueryWrapper<Tridmaker> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tridmaker::getMakerid,areaCode + makerID)
                .eq(Tridmaker::getAreaCode,areaCode);
        Tridmaker tridmakerDB = tridmakerService.getOne(wrapper);
        if(tridmakerDB == null){
            Tridmaker tridmaker = new Tridmaker();
            tridmaker.setCurdate(curDate);
            tridmaker.setMakerid(areaCode + makerID);
            tridmaker.setCurnum(0);
            tridmaker.setAreaCode(areaCode);
            tridmakerService.save(tridmaker);
        }

        Tridmaker tridmaker = tridmakerService.getById(areaCode + makerID);
        //比较当前时间和数据库存储的时间
        LocalDateTime dbDate = tridmaker.getCurdate();
        if(dbDate != null) {
            if (curDate.toLocalDate().toEpochDay() - dbDate.toLocalDate().toEpochDay() == 0) {// 同一天
                curnum = tridmaker.getCurnum();
            } else {//不是同一天
                curnum = 0;// 重置
                tridmaker.setCurdate(curDate);
            }
        }

        nextNum = curnum + 1;
        tridmaker.setCurnum(nextNum);
        tridmakerService.updateById(tridmaker);

        //根据规则生成受理号
        int year = curDate.getYear();
        int month = curDate.getMonthValue();
        int day = curDate.getDayOfMonth();
        num = String.valueOf(year).substring(2, 4)+beforeZeroFill(month, 2) +beforeZeroFill(day, 2) + beforeZeroFill(nextNum, 4);
        return num;
    }

    /**
     * 联合惩戒锁定：规则年月日+4位顺序号。例如：2106260001
     *
     * @return 新产生的流水号
     */
    public synchronized String getLockCurAcceptNo() {
        return getTridmakerWithDay("acceptNoForPunishLock");
    }
    /**
     * 联合惩戒解锁：规则年月日+4位顺序号。例如：2106260001
     *
     * @return 新产生的流水号
     */
    public synchronized String getUnlockCurAcceptNo() {
        return getTridmakerWithDay("acceptNoForPunishUnlock");
    }
    /**
     * 批量吊销案件编号：规则年月日+4位顺序号。例如：2106260001
     *
     * @return 新产生的流水号
     */
    public synchronized String getBatchRevokeCaseNo(String areaCode) {
        return getTridmakerWithDayAndAreaCode("caseNoForBatchRevoke",areaCode);
    }
    /**
     * @param number 需要填充0的数字
     * @param length 需要多少位
     * @return
     */
    private String beforeZeroFill(int number, int length) {
        return beforeZeroFill(String.valueOf(number), length);
    }
    private String beforeZeroFill(String number, int length) {
        String tmp = number;
        for (int i = 0; i < length - number.length(); i++) {
            tmp = "0" + tmp;
        }
        return tmp;
    }
}
