package nc.impl.bd.material.baseinfo;

import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.ic.general.businessevent.ICGeneralCommonEvent;
import nc.vo.ic.m45.entity.PurchaseInBodyVO;
import nc.vo.ic.m45.entity.PurchaseInHeadVO;
import nc.vo.ic.m45.entity.PurchaseInVO;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.m4d.entity.MaterialOutVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;

import java.util.Calendar;
import java.util.Date;

public class BillDateCheckListener implements IBusinessListener {

    public void doAction(IBusinessEvent event) throws BusinessException {
        ICGeneralCommonEvent.ICGeneralCommonUserObj obj = (ICGeneralCommonEvent.ICGeneralCommonUserObj)event.getUserObject();
        AggregatedValueObject[] newBillVOs = (AggregatedValueObject[])obj.getNewObjects();
        if (newBillVOs == null) {
            newBillVOs =  (AggregatedValueObject[])obj.getOldObjects();
        }
//        AggregatedValueObject[] oldBillVOs = (AggregatedValueObject[])obj.getOldObjects();
            //检查日期是否符合 每月24日后在系统做单据的时候，对未改系统日期到次月1日制单的情况，增加提示来提醒更改单据日期，否则无法保存，更改的日期不能超过当月24日后10天。
//        "1001".equals(event.getEventType())
        for(int i = 0; i < newBillVOs.length; ++i) {
            Object newObj = newBillVOs[i];
            if (newObj instanceof PurchaseInVO) {
                PurchaseInVO newVO = (PurchaseInVO)newObj;
                UFDate billDate = newVO.getHead().getDbilldate();

                Date currentDate = new Date();
                Date billDateObj = billDate.toDate();
                if (!this.validateBillDate(billDateObj,currentDate)) {
                    throw new BusinessException("当前日期已过当月24号，必须在下月制单且单据日期必须超过当月24号后10天");
                }
            }else if(newObj instanceof SaleOutVO){
                    SaleOutVO newVO = (SaleOutVO)newObj;
                    UFDate billDate = newVO.getHead().getDbilldate();

//                    Date currentDate = new Date(2025,6,25);
                    Date currentDate = new Date();
                    Date billDateObj = billDate.toDate();

                    if (!this.validateBillDate(billDateObj,currentDate)) {
                        throw new BusinessException("当前日期已过当月24号，必须在下月制单且单据日期必须超过当月24号后10天");
                    }

            }else if(newObj instanceof MaterialOutVO){
                MaterialOutVO newVO = (MaterialOutVO)newObj;
                UFDate billDate = newVO.getHead().getDbilldate();

                Date currentDate = new Date();
                Date billDateObj = billDate.toDate();
                if (!this.validateBillDate(billDateObj,currentDate)) {
                    throw new BusinessException("当前日期已过当月24号，必须在下月制单且单据日期必须超过当月24号后10天");
                }
            }
        }

    }


    public boolean validateBillDate(Date billDateObj, Date currentDate) {
        Calendar currentCal = Calendar.getInstance();
        currentCal.setTime(currentDate);

        Calendar billCal = Calendar.getInstance();
        billCal.setTime(billDateObj);

        // 获取当前日期的月份和日
        int currentDay = currentCal.get(Calendar.DAY_OF_MONTH);
        int currentMonth = currentCal.get(Calendar.MONTH);
        int currentYear = currentCal.get(Calendar.YEAR);

        // 只有当当前日期是25号到月底时才需要校验
        if (currentDay >= 15) {
            // 1. 计算本月24号+10天的最大允许日期
            Calendar maxDateCal = Calendar.getInstance();
            maxDateCal.set(currentYear, currentMonth, 24); // 本月24号
            maxDateCal.add(Calendar.DATE, 10); // 24号+10天
            maxDateCal.set(Calendar.HOUR_OF_DAY, 0);    // 清除时间部分
            maxDateCal.set(Calendar.MINUTE, 0);         // 确保时间为00:00:00.000
            maxDateCal.set(Calendar.SECOND, 0);
            maxDateCal.set(Calendar.MILLISECOND, 0);

            // 2. 计算下个月1号的最小允许日期
            Calendar minDateCal = Calendar.getInstance();
            minDateCal.set(currentYear, currentMonth, 1); // 本月1号
            minDateCal.add(Calendar.MONTH, 1); // 下个月1号
            minDateCal.set(Calendar.HOUR_OF_DAY, 0);    // 清除时间部分
            minDateCal.set(Calendar.MINUTE, 0);         // 确保时间为00:00:00.000
            minDateCal.set(Calendar.SECOND, 0);
            minDateCal.set(Calendar.MILLISECOND, 0);

            // 3. 清除billCal的时间部分
            billCal.set(Calendar.HOUR_OF_DAY, 0);
            billCal.set(Calendar.MINUTE, 0);
            billCal.set(Calendar.SECOND, 0);
            billCal.set(Calendar.MILLISECOND, 0);

            // 3. 检查单据日期是否在下个月范围内
            boolean isNextMonth = billCal.get(Calendar.MONTH) == minDateCal.get(Calendar.MONTH) &&
                    billCal.get(Calendar.YEAR) == minDateCal.get(Calendar.YEAR);

            // 4. 检查单据日期是否在允许范围内（下个月1号 至 本月24号+10天）
            if (!isNextMonth || billCal.after(maxDateCal) || billCal.before(minDateCal)) {
                return false;
            }
        }

        return true;
    }
}
