package com.hzs.sale.component.load;

import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.Constants;
import com.hzs.common.core.enums.ETransportType;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.sale.ext.BdWaresExt;
import com.hzs.common.domain.sale.postage.BdPostageDetail;
import com.hzs.sale.postage.service.IBdPostageDetailService;
import com.hzs.sale.wares.service.IBdWaresService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: sui q
 * @Time: 2022/9/16 17:10
 * @Classname: RedisComponent
 * @PackageName: com.hzs.sale.component.load
 */
@Component
@Slf4j
class RedisComponent {

    private RedisService redisService;

    private IBdWaresService bdWaresService;

    private IBdPostageDetailService bdPostageDetailService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Autowired
    public void setBdPostageDetailService(IBdPostageDetailService bdPostageDetailService) {
        this.bdPostageDetailService = bdPostageDetailService;
    }

    @Autowired
    public void setBdWaresService(IBdWaresService bdWaresService) {
        this.bdWaresService = bdWaresService;
    }

    public void initConfig() {
        // 商品信息
        initWaresConfig();
        // 邮费
        initPostageTemp();
    }

    /**
     * 初始化商品信息 bd_wares
     */
    private void initWaresConfig() {
        List<BdWaresExt> bdWaresExtList = bdWaresService.queryWaresByCondition();
        bdWaresExtList.forEach(waresExt -> redisService.setCacheObject(CacheConstants.BD_WARES + waresExt.getPkId(), waresExt, Constants.TIMEOUT_DAY, TimeUnit.DAYS));
    }

    /**
     * 初始化邮费模板
     */
    private void initPostageTemp() {
        for (ETransportType transportType : ETransportType.values()) {
            List<BdPostageDetail> bdPostageDetailList = bdPostageDetailService.queryPostageDetail(transportType.getValue());
            Map<String, List<BdPostageDetail>> bdPostageDetailMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdPostageDetailList.size()));
            bdPostageDetailList.forEach(bdPostageDetail -> {
                String key = CacheConstants.BD_POSTAGE + transportType.getValue() + bdPostageDetail.getPkPostage();
                if (bdPostageDetailMap.containsKey(key)) {
                    List<BdPostageDetail> bdPostageDetailList1 = bdPostageDetailMap.get(key);
                    bdPostageDetailList1.add(bdPostageDetail);
                } else {
                    List<BdPostageDetail> bdPostageDetailList1 = new ArrayList<>();
                    bdPostageDetailList1.add(bdPostageDetail);
                    bdPostageDetailMap.put(key, bdPostageDetailList1);
                }
            });
            bdPostageDetailMap.forEach((key, value) -> {
                redisService.setCacheList(key, value);
                redisService.expire(key, Constants.TIMEOUT_DAY, TimeUnit.DAYS);
            });
        }
    }
}
