package com.neusoft.smart.pos.trade.utils;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.trade.persistent.domain.BillSettlementCycle;
import com.neusoft.smart.pos.trade.persistent.ext.domain.BillCycleExt;

import java.util.Date;

public class BillTaskUtils {
    
    /**
     * 根据账单编号获取账单周期详细信息（目前只支持月结）
     * @param orgId
     * @param billNumber:20180101
     * @return
     */
    public static BillCycleExt getBillCycleByBillMonth(Integer orgId, String billNumber, BillSettlementCycle billSettlementCycle){
        
        if(null == billSettlementCycle){
            return null;
        }
        
        //2.整理账单周期信息
        BillCycleExt billCycleExt = new BillCycleExt();
        billCycleExt.setOrgId(billSettlementCycle.getOrgId());
        billCycleExt.setShopId(billSettlementCycle.getShopId());
        billCycleExt.setCycleType(billSettlementCycle.getCycleType());  //周期类型@1:月结@2:年结@3:季结@4:周结
        DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.BILL_CYCLE_TYPE.getCode(),billSettlementCycle.getCycleType());
        switch (dict){
            case BILL_CYCLE_TYPE_MONTH:
                //月结,计算本次账单开始时间及结束时间，精确到日
                //结算账单日类型@1:自然日@2:结算周期最后一日
                if(billSettlementCycle.getBillDayType().equals("1")){
                    if(billSettlementCycle.getBillDay()<10){
                        billNumber = billNumber+"0"+billSettlementCycle.getBillDay();
                    }else{
                        billNumber = billNumber+billSettlementCycle.getBillDay();
                    }
                    Date billTimeDate = DateUtils.parse(billNumber,DateUtils.FORMAT_SHORT_EXT);
                    Date billStartDate = DateUtils.getDayStartTime(billTimeDate);
                    Date billEndDate = DateUtils.subDay(DateUtils.getDayEndTime(DateUtils.addMonth(billTimeDate,1)),1);
                    billCycleExt.setBillName(DateUtils.format(billStartDate,DateUtils.FORMAT_SHORT_EXT)+"-"+DateUtils.format(billEndDate,DateUtils.FORMAT_SHORT_EXT));
                    billCycleExt.setBillStartTime(billStartDate);
                    billCycleExt.setBillEndTime(billEndDate);
                    billCycleExt.setBillNumber(DateUtils.format(billEndDate,DateUtils.FORMAT_SHORT_EXT));
                }else if(billSettlementCycle.getBillDayType().equals("2")){
                    Date billTimeDate = DateUtils.parse(billNumber,DateUtils.FORMAT_SHORTER_EXT);
                    Date billStartDate = DateUtils.getDayStartTime(DateUtils.getMonthFirstDay(billTimeDate));
                    Date billEndDate = DateUtils.getDayEndTime(DateUtils.getMonthLastDay(billTimeDate));
                    billNumber = DateUtils.format(billEndDate,DateUtils.FORMAT_SHORT_EXT);
                    billCycleExt.setBillName(DateUtils.format(billStartDate,DateUtils.FORMAT_SHORT_EXT)+"-"+DateUtils.format(billEndDate,DateUtils.FORMAT_SHORT_EXT));
                    billCycleExt.setBillStartTime(billStartDate);
                    billCycleExt.setBillEndTime(billEndDate);
                    billCycleExt.setBillNumber(DateUtils.format(billEndDate,DateUtils.FORMAT_SHORT_EXT));
                }
                break;
            case BILL_CYCLE_TYPE_QUARTER:
                //季结
                break;
            case BILL_CYCLE_TYPE_WEEK:
                //周结
                break;
            case BILL_CYCLE_TYPE_YEAR:
                //年结
                break;
        }
        //整理返回数据
        return billCycleExt;
    }
}
