package com.czg.service.market.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.ShopUser;
import com.czg.market.entity.MemberLevelConfig;
import com.czg.market.entity.MkPointsConfig;
import com.czg.market.enums.PointsConstant;
import com.czg.market.service.MemberLevelConfigService;
import com.czg.market.service.MkPointsConfigService;
import com.czg.market.service.MkPointsUserService;
import com.czg.order.entity.OrderInfo;
import com.czg.service.market.mapper.MkPointsConfigMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 积分基本设置 服务层实现。
 *
 * @author ww
 * @since 2025-12-08
 */
@Service
public class MkPointsConfigServiceImpl extends ServiceImpl<MkPointsConfigMapper, MkPointsConfig> implements MkPointsConfigService {

    @Resource
    private MkPointsUserService mkPointsUserService;


    @Resource
    private MemberLevelConfigService levelConfigService;

    @Override
    public void consumeAwardPoints(ShopUser shopUser, OrderInfo orderInfo) {
        BigDecimal payAmount = orderInfo.getPayAmount();
        if (NumberUtil.isLessOrEqual(payAmount, BigDecimal.ZERO)) {
            return;
        }
        BigDecimal consumeAmount = null;
        if (shopUser.getIsVip() == 1 && shopUser.getMemberLevelId() != null) {
            MemberLevelConfig level = levelConfigService.getById(shopUser.getMemberLevelId());
            if (level != null && level.getIsCostRewardPoints() == 1 && level.getCostRewardPoints() != null && level.getCostRewardPoints() > 0) {
                consumeAmount = BigDecimal.valueOf(level.getCostRewardPoints());
            }
        }
        if (consumeAmount == null) {
            MkPointsConfig pointsConfig = getOne(query().eq(MkPointsConfig::getShopId, orderInfo.getShopId()));
            if (pointsConfig == null) {
                return;
            }
            if (pointsConfig.getEnableRewards() == null || pointsConfig.getEnableRewards() == 0) {
                return;
            }
            if (pointsConfig.getConsumeAmount() != null && pointsConfig.getConsumeAmount().compareTo(BigDecimal.ZERO) > 0) {
                consumeAmount = pointsConfig.getConsumeAmount();
            }
        }
        BigDecimal awardPoints = NumberUtil.roundDown(NumberUtil.div(payAmount, consumeAmount), 0);
        if (awardPoints.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        mkPointsUserService.alterPoints(null, shopUser.getId(), orderInfo.getShopId(), PointsConstant.ADD,
                awardPoints.intValue(), orderInfo.getId(), StrUtil.format("消费￥{}送{}积分", payAmount, awardPoints.intValue()));
    }

    @Override
    public void removeConsumeAwardPoints(Long shopId, Long userId, Long orderId, String orderNo) {
        mkPointsUserService.removePointByOrder(shopId, userId, orderId, orderNo, "订单退款扣除赠送积分");
    }
}
