package com.awspaas.user.apps.bnbm.datalinkup.job;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.bnbm.datalinkup.entity.DateRange;
import com.awspaas.user.apps.bnbm.datalinkup.service.impl.PurchaseDataSummaryServiceImpl;
import com.awspaas.user.apps.bnbm.datalinkup.service.impl.PurchaseDataSyncServiceImpl;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName: PurchaseDataLinkUpJob
 * @Description: 采购数据同步定时任务
 */
public class PurchaseDataLinkUpJob implements IJob {
    private static final Logger LOGGER = LoggerFactory.getLogger(PurchaseDataLinkUpJob.class);

    @Override
    public void execute(JobExecutionContext job) throws JobExecutionException {
        //时间范围数值
        ArrayList<DateRange> list = new ArrayList<>();
        PurchaseDataSyncServiceImpl syncService = new PurchaseDataSyncServiceImpl();
        PurchaseDataSummaryServiceImpl purchaseDataSummaryService = new PurchaseDataSummaryServiceImpl();
        LOGGER.info("======== 开始执行销售数据同步任务 ========");
        // 查询销售业务的主配置
        List<BO> mainConfigs = SDK.getBOAPI().query("BO_EU_BNBM_DATALINKUP_SJGTPZ")
                .addQuery("SSYW =", "采购")
                .list();
        // 使用服务层处理同步逻辑
        ArrayList<DateRange> dateRanges = syncService.syncDataByConfigs(mainConfigs);
        list.addAll(dateRanges);
        LOGGER.info("======== 完成销售数据同步任务 ========");

        // 汇总各板块数据
        LOGGER.info("======== 开始执行销售汇总各板块数据 ========");
        syncService.sumBkTable(mainConfigs);
        int update = DBSql.update("UPDATE BO_EU_DWD_ORDER_RKD_HZ rkd " +
                " JOIN BO_EU_ZZDYQY zzd ON rkd.BKGS = zzd.BKGS AND rkd.KCZZ = zzd.XSZZ " +
                " SET rkd.QYGS = zzd.QYGS " +
                " WHERE rkd.QYGS IS NULL");
        LOGGER.info("更新入库单区域公司条数：{}",update);
        LOGGER.info("======== 销售汇总各板块数据执行完成 ========");

        //计算汇总维度
        Set<DateRange> collect = list.stream().filter(o -> o.getStartDate() != null || o.getEndDate() != null)
                .collect(Collectors.toSet());
        DateRange dateRange = new DateRange();
        for (DateRange range : collect) {
            if (range!=null || range.getStartDate()==null || range.getEndDate()==null) {
                dateRange.setStartDate(range.getStartDate());
                dateRange.setEndDate(range.getEndDate());
            }
        }
        LOGGER.info("采购数据汇总计算开始时间为：{}，结束时间为：{}",dateRange.getStartDate(),dateRange.getEndDate());
        List<RowMap> bkgsMaps = DBSql.getMaps("SELECT BKGS FROM BO_EU_DWD_ORDER_RKD_HZ GROUP BY BKGS");
        if (bkgsMaps!=null) {
            for (RowMap map : bkgsMaps) {
                BO bo = new BO();
                bo.set("BKGS", map.getString("BKGS"));
                purchaseDataSummaryService.calculateSummary(dateRange, bo);
            }
        }
        LOGGER.info("======== 销售数据同步任务执行完成 ========");
    }
}
