package com.gxwebsoft.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import com.gxwebsoft.common.core.utils.RequestUtil;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.*;
import com.gxwebsoft.shop.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;

/**
 *
 * @author 科技小王子
 * @since 2025-01-11 10:45:12
 */
@Service
public class ShopOrderUpdate10550ServiceImpl implements ShopOrderUpdate10550Service {
    @Resource
    private RequestUtil requestUtil;
    @Resource
    private ShopDealerOrderService shopDealerOrderService;
    @Resource
    private ShopDealerCapitalService shopDealerCapitalService;
    @Resource
    private ShopOrderGoodsService shopOrderGoodsService;
    @Resource
    private ShopGoodsService shopGoodsService;

    @Override
    public void update(ShopOrder order){
        requestUtil.setTenantId(order.getTenantId().toString());
        ApiResult<?> partnerConditionReq = requestUtil.pageDictData(1460);
        if (partnerConditionReq.getCode().equals(0) && partnerConditionReq.getData() != null) {
            LinkedHashMap<String, Object> dictDataMap = (LinkedHashMap<String, Object>) partnerConditionReq.getData();
            List<LinkedHashMap> dictDataList = (List<LinkedHashMap>) dictDataMap.get("list");
            String dictDataCode = (String) dictDataList.get(0).get("dictDataCode");
            BigDecimal partnerCondition = new BigDecimal(dictDataCode);

            User user = requestUtil.getByUserIdWithoutLogin(order.getUserId());
            if (user != null) {
                user.setExpendMoney(user.getExpendMoney().add(order.getPayPrice()));
                if (user.getExpendMoney().compareTo(partnerCondition) >= 0) {
                    user.setGradeId(3);
                }
                requestUtil.updateWithoutLogin(user);

                // 上级
                User parent = requestUtil.getParent(order.getUserId());
                if (parent != null) {

                    List<ShopOrderGoods> shopOrderGoodsList = shopOrderGoodsService.getListByOrderId(order.getOrderId());
                    List<Integer> goodsIds = shopOrderGoodsList.stream().map(ShopOrderGoods::getGoodsId).toList();
                    List<ShopGoods> shopGoodsList = shopGoodsService.listByIds(goodsIds);
                    BigDecimal commission = BigDecimal.ZERO;
                    for (ShopOrderGoods shopOrderGoods : shopOrderGoodsList) {
                        ShopGoods shopGoods = shopGoodsList.stream().filter(sG -> sG.getGoodsId().equals(shopOrderGoods.getGoodsId())).findFirst().orElse(null);
                        if (shopGoods != null) {
                            commission = commission.add(shopGoods.getCommission().multiply(BigDecimal.valueOf(shopOrderGoods.getTotalNum())));
                        }
                    }
                    parent.setBalance(parent.getBalance().add(commission));
                    requestUtil.updateWithoutLogin(user);

                    // 分销订单
                    ShopDealerOrder shopDealerOrder = new ShopDealerOrder();
                    shopDealerOrder.setUserId(parent.getUserId());
                    shopDealerOrder.setOrderId(order.getOrderId());
                    shopDealerOrder.setOrderPrice(order.getTotalPrice());
                    shopDealerOrder.setFirstUserId(order.getUserId());
                    shopDealerOrder.setFirstMoney(commission);
                    shopDealerOrder.setIsSettled(1);
                    shopDealerOrder.setSettleTime(DateUtil.currentSeconds());
                    shopDealerOrderService.save(shopDealerOrder);

                    // 分销资明细
                    ShopDealerCapital shopDealerCapital = new ShopDealerCapital();
                    shopDealerCapital.setUserId(parent.getUserId());
                    shopDealerCapital.setOrderId(order.getOrderId());
                    shopDealerCapital.setFlowType(10);
                    shopDealerCapitalService.save(shopDealerCapital);
                }
            }
        }
    }
}
