package com.sqx.modules.member;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sqx.common.utils.Constant;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.member.vo.VipReduceVO;
import com.sqx.modules.order.entity.TbOrder;
import com.sqx.modules.order.service.AppOrderService;
import com.sqx.modules.utils.VipExpirationUtil;
import lombok.RequiredArgsConstructor;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * vip优惠工具类
 * @author codingliang
 * @date 2025-12-22
 */
@RequiredArgsConstructor
public class VipPromotionUtil {

    private final AppOrderService appOrderService;
    private final CommonInfoService commonInfoService;

    /**
     * 查询用户是否可以立减
     * @param order 订单信息，需要设置originGoodsList原始商品信息
     * @param goodsShop 店铺信息
     * @param user 用户信息
     * @return VipReduceVO
     */
    public VipReduceVO getCanReduce(TbOrder order, GoodsShop goodsShop, UserEntity user) {
        // 参与会员活动的商品id
        List<Long> vipGoodsIdList = order.getOriginGoodsList().stream().filter(e -> StrUtil.equals(e.getVipPromotion(), Constant.YES)).map(Goods::getGoodsId)
                .collect(Collectors.toList());

        // 计算参与会员活动的商品总价
        BigDecimal vipGoodsTotalPrice = order.getOrderGoodsList().stream()
                .filter(orderGoods -> vipGoodsIdList.contains(orderGoods.getGoodsId()))
                .map(orderGoods -> orderGoods.getGoodsPrice().multiply(new BigDecimal(orderGoods.getGoodsNum())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // 判断订单是否达到会员立减最低金额（元）
        CommonInfo vipPromotionMinAmount = commonInfoService.findOne(446);
        if (vipGoodsTotalPrice.compareTo(new BigDecimal(vipPromotionMinAmount.getValue())) < 0) {
            return VipReduceVO.builder().reduceAmount(BigDecimal.ZERO)
                    .canReduceFlag(Constant.NO)
                    .vipReduceDesc("订单金额未达到会员立减金额")
                    .build();
        }

        // 查询店铺是否参与会员立减
        if (!StrUtil.equals(goodsShop.getVipPromotion(), Constant.YES)) {
            return VipReduceVO.builder()
                    .reduceAmount(BigDecimal.ZERO)
                    .canReduceFlag(Constant.NO)
                    .vipReduceDesc("店铺未参与会员立减")
                    .build();
        }
        String vipFlag = ObjectUtil.isNotNull(user.getIsVip()) && user.getIsVip() == 1 && VipExpirationUtil.isVipValid(user.getVipExpirationTime())
                ? Constant.YES : Constant.NO;
        if (!StrUtil.equals(vipFlag, Constant.YES)) {
            return VipReduceVO.builder().reduceAmount(BigDecimal.ZERO)
                    .canReduceFlag(Constant.NO)
                    .vipReduceDesc("用户非会员，不能立减")
                    .build();
        }

        // 会员每天限制优惠单数（单）
        int count = appOrderService.getCurDayVipPromotionByUserCount(user.getUserId());
        CommonInfo vipPromotionCount = commonInfoService.findOne(445);
        if (count >= Integer.parseInt(vipPromotionCount.getValue())) {
            return VipReduceVO.builder().reduceAmount(BigDecimal.ZERO)
                    .canReduceFlag(Constant.NO)
                    .vipReduceDesc("会员当日优惠单数已达上限")
                    .build();
        }

        // 会员每单立减金额（元）
        CommonInfo vipPromotionAmount = commonInfoService.findOne(444);
        BigDecimal vipPromotionAmountDecimal = new BigDecimal(vipPromotionAmount.getValue());
        return  VipReduceVO.builder().reduceAmount(vipPromotionAmountDecimal)
                .canReduceFlag(Constant.YES)
                .vipReduceDesc("会员每单立减金额" + vipPromotionAmountDecimal + "元")
                .build();
    }
}
