package com.yiqi.task;

import cn.hutool.core.date.DateUtil;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.core.domain.dto.SettlementBillAddDTO;
import com.yiqi.core.service.ISettlementManageBillService;
import com.yiqi.core.service.ISettlementStoreStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 门店衣服每日结算
 *
 * @author ruoyi
 */
@Component
@Slf4j
public class SettlementStoreOrderTask {

    @Autowired
    private ISettlementStoreStatisticsService settlementStoreStatisticsService;

    public void run(String dateStr) {
        // 获取上个月的时间范围
        Date date = DateUtils.addDays(new Date(), -1);
        if (StringUtils.isNotBlank(dateStr)) {
            date = DateUtils.parseDate(dateStr);
        }
        //昨天结算
        if(dateStr.contains(";")){
            Date startDate = DateUtils.parseDate(dateStr.split(";")[0]);
            Date endDate = DateUtils.parseDate(dateStr.split(";")[1]);
            //循环统计，从startDate到endDate
            while (startDate.getTime() <= endDate.getTime()) {
                log.info("*****开始执行门店衣服订单"+ DateUtil.formatDate(date) +"日结算任务*****");
                settlementStoreStatisticsService.generateDayStatistics(startDate);
                log.info("*****结束执行门店衣服订单"+ DateUtil.formatDate(date) +"日结算任务*****");
                startDate = DateUtils.addDays(startDate, 1);
            }
        }else{
            log.info("*****开始执行门店衣服订单"+ DateUtil.formatDate(date) +"日结算任务*****");
            settlementStoreStatisticsService.generateDayStatistics(date);
            log.info("*****结束执行门店衣服订单"+ DateUtil.formatDate(date) +"日结算任务*****");
        }

    }
}
