package com.hzs.sale.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.ELevelValue;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaStoreBonus;
import com.hzs.member.store.ICuStoreServiceApi;
import com.hzs.member.store.dto.StoreDto;
import com.hzs.sale.order.mapper.SaStoreBonusMapper;
import com.hzs.sale.order.service.ISaStoreBonusService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzs
 * @since 2024-06-26
 */
@Service
public class SaStoreBonusServiceImpl extends ServiceImpl<SaStoreBonusMapper, SaStoreBonus> implements ISaStoreBonusService {

    @DubboReference
    private ICuStoreServiceApi cuStoreServiceApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSaStoreBonus(SaOrderExt saOrderExt) {
        // 查询店铺等级
        List<SaStoreBonus> storeBonusList = new ArrayList<>();
        List<StoreDto> storeDtoList = cuStoreServiceApi.queryAllMemberStore().getData();
        if (CollectionUtil.isNotEmpty(storeDtoList)) {
            // 服务中心店铺
            Map<Integer, StoreDto> storeMap = new HashMap<>();
            // 区域店铺
            Map<String, StoreDto> areaStoreMap = new HashMap<>();
//                // 国家代理
            Map<Integer, StoreDto> cuntryStoreMap = new HashMap<>();
            for (StoreDto storeDto : storeDtoList) {
                if (ELevelValue.GOLD_DRILL.getValue() == storeDto.getLevelValue()) {
                    cuntryStoreMap.put(storeDto.getBindingCountry(), storeDto);
                } else if (ELevelValue.SILVER_DRILL.getValue() == storeDto.getLevelValue()) {
                    String key = storeDto.getPkCountry() + "_" + storeDto.getBindingProvince();
                    areaStoreMap.put(key, storeDto);
                }
                storeMap.put(storeDto.getPkId(), storeDto);
            }
            // 存在店铺
            if(storeMap.containsKey(saOrderExt.getPkMemberStore())){
                StoreDto storeDto = storeMap.get(saOrderExt.getPkMemberStore());
                BigDecimal ratio = storeDto.getRatio();
                storeBonusList.add(packageStoreBonus(saOrderExt, storeDto, ratio));
                Integer bindingCountry = saOrderExt.getPkCountry();
                // 服务中心 找上一级
                if(ELevelValue.COPPER_DRILL.getValue() == storeDto.getLevelValue()){
                    // 找区域代理
                    String key = storeDto.getPkCountry() + "_" + storeDto.getBindingProvince();
                    if(areaStoreMap.containsKey(key)){
                        StoreDto areaStoreDto = areaStoreMap.get(key);
                        bindingCountry = areaStoreDto.getBindingCountry();
                        ratio = ComputeUtil.computeSubtract(areaStoreDto.getRatio(), ratio);
                        if(ComputeUtil.compareValue(ratio)){
                            storeBonusList.add(packageStoreBonus(saOrderExt, areaStoreDto, ratio));
                        }
                    }
                }
                // 第一级不是国代，查一下有没有下一级
                if(ELevelValue.GOLD_DRILL.getValue() != storeDto.getLevelValue()){
                    if(cuntryStoreMap.containsKey(bindingCountry)){
                        StoreDto countryStoreDto = cuntryStoreMap.get(bindingCountry);
                        ratio = ComputeUtil.computeSubtract(countryStoreDto.getRatio(), ratio);
                        if(ComputeUtil.compareValue(ratio)){
                            storeBonusList.add(packageStoreBonus(saOrderExt, countryStoreDto, ratio));
                        }
                    }
                }
            }
        }
        if(storeBonusList.size() >0){
            saveBatch(storeBonusList);
        }
    }

    private SaStoreBonus packageStoreBonus(SaOrderExt saOrderExt, StoreDto storeDto, BigDecimal ratio) {
        SaStoreBonus saStoreBonus = SaStoreBonus.builder()
                .pkMemberStore(storeDto.getPkId())
                .pkMember(storeDto.getPkMember())
                .pkOrder(saOrderExt.getPkId())
                .bonusRatio(ratio)
                .build();
        saStoreBonus.setPkCreator(saOrderExt.getPkCreator());
        saStoreBonus.setPkCountry(saOrderExt.getPkCountry());
        return saStoreBonus;
    }
}
