package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.entity.TbPointsBasicSetting;
import cn.ysk.cashier.mybatis.mapper.TbPointsBasicSettingMapper;
import cn.ysk.cashier.mybatis.service.TbPointsBasicSettingService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 积分基本设置
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 2.0 2024-10-25
 */
@Service
public class TbPointsBasicSettingServiceImpl extends ServiceImpl<TbPointsBasicSettingMapper, TbPointsBasicSetting> implements TbPointsBasicSettingService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(TbPointsBasicSetting entity) {
        try {
            Assert.notNull(entity.getShopId(), "{}({})不能为空", "店铺id", "shopId");
            Assert.notNull(entity.getEnableRewards(), "{}({})不能为空", "开启消费赠送积分", "enableRewards");
            Assert.notEmpty(entity.getRewardsGroup(), "{}({})不能为空", "赠积分适用群体", "rewardsGroup");
            Assert.notNull(entity.getConsumeAmount(), "{}({})不能为空", "每消费xx元赠送1积分", "consumeAmount");
            Assert.notNull(entity.getEnableDeduction(), "{}({})不能为空", "开启下单积分抵扣", "enableDeduction");
            Assert.notEmpty(entity.getDeductionGroup(), "{}({})不能为空", "抵扣适用群体", "deductionGroup");
            Assert.notNull(entity.getMinPaymentAmount(), "{}({})不能为空", "下单实付抵扣门槛", "minPaymentAmount");
            Assert.notNull(entity.getMaxDeductionRatio(), "{}({})不能为空", "下单最高抵扣比例", "maxDeductionRatio");
            Assert.notNull(entity.getEquivalentPoints(), "{}({})不能为空", "下单抵扣积分比例", "equivalentPoints");
            Assert.notNull(entity.getEnablePointsMall(), "{}({})不能为空", "开启积分商城", "enablePointsMall");
            Assert.notEmpty(entity.getBrowseMode(), "{}({})不能为空", "浏览模式", "browseMode");
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e.getMessage());
        }
        if (NumberUtil.isLessOrEqual(entity.getMinPaymentAmount(),BigDecimal.ZERO)) {
            throw new BadRequestException("下单实付抵扣门槛不能小于等于0");
        }
        if (NumberUtil.isLessOrEqual(entity.getMaxDeductionRatio(), BigDecimal.ZERO)) {
            throw new BadRequestException("下单最高抵扣比例不能小于等于0");
        }
        if (NumberUtil.isGreater(entity.getMaxDeductionRatio(), new BigDecimal("100"))) {
            throw new BadRequestException("下单最高抵扣比例不能大于100");
        }
        entity.setCreateTime(new Date());
        super.remove(Wrappers.<TbPointsBasicSetting>lambdaQuery().eq(TbPointsBasicSetting::getShopId, entity.getShopId()));
        super.save(entity);
        return true;
    }

    @Override
    public TbPointsBasicSetting getByShopId(Long shopId) {
        return super.getOne(Wrappers.<TbPointsBasicSetting>lambdaQuery().eq(TbPointsBasicSetting::getShopId, shopId));
    }
}