package com.gree.mall.manager.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.gree.mall.manager.enums.material.MaterialSalesFlagEnum;
import com.gree.mall.manager.logic.material.stock.WebitSalesStockLogic;
import com.gree.mall.manager.logic.material.stock.WorkSalesStockLogic;
import com.gree.mall.manager.plus.entity.WebsitSales;
import com.gree.mall.manager.plus.entity.WebsitSalesItem;
import com.gree.mall.manager.plus.service.WebsitSalesItemService;
import com.gree.mall.manager.plus.service.WebsitSalesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Component
public class ImportWebsitSalesStockHandleSchedule {

    @Resource
    WebsitSalesService websitSalesService;
    @Resource
    WebsitSalesItemService websitSalesItemService;
    @Resource
    WebitSalesStockLogic webitSalesStockLogic;
    @Resource
    WorkSalesStockLogic workSalesStockLogic;

    /**
     * 处理导入辅材配件销售单库存执行
     */
    @Scheduled(fixedDelay = 2 * 1000)
    @Transactional
    public void task() {
        List<WebsitSales> salesList = websitSalesService.lambdaQuery()
                .eq(WebsitSales::getFlag, MaterialSalesFlagEnum.IMPORT_WAIT.getKey())
                .list();

        if (CollectionUtil.isNotEmpty(salesList)) {
            // 按批次分组
            Map<String, List<WebsitSales>> salesGroupMap = salesList.stream()
                    .collect(Collectors.groupingBy(WebsitSales::getReason));

            for (Map.Entry<String, List<WebsitSales>> entry : salesGroupMap.entrySet()) {
                // 查询出销售明细
                List<WebsitSalesItem> salesItems = websitSalesItemService.lambdaQuery()
                        .in(WebsitSalesItem::getSalesId, entry.getValue().stream()
                                .map(WebsitSales::getSalesId)
                                .collect(Collectors.toList()))
                        .list();

                Map<String, List<WebsitSalesItem>> itemMap = salesItems.stream()
                        .collect(Collectors.groupingBy(WebsitSalesItem::getSalesId));

                for (WebsitSales sales : entry.getValue()) {
                    List<WebsitSalesItem> salesItemList = itemMap.get(sales.getSalesId());
                    try {
                        // 处理网点库存
                        webitSalesStockLogic.newSalesStock(sales, salesItemList, "销售订单");

                        if (StringUtils.isNotBlank(sales.getWorkerId())) {
                            // 处理师傅库存
                            workSalesStockLogic.salesInStock(sales, salesItemList, "销售订单");
                        }
                        sales.setFlag(MaterialSalesFlagEnum.PAY_TAKE.getKey());
                    } catch (Exception e) {
                        sales.setReason(sales.getReason() + "---单据号: " + sales.getSalesId() + " " + e.getMessage());
                        sales.setFlag(MaterialSalesFlagEnum.IMPORT_ERR.getKey());
                    }
                }
            }

            // 批量更新状态
            websitSalesService.saveOrUpdateBatch(salesList);
        }
    }

}
