package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.czg.account.dto.points.PointsBasicSettingDTO;
import com.czg.account.entity.PointsBasicSetting;
import com.czg.account.service.PointsBasicSettingService;
import com.czg.exception.CzgException;
import com.czg.log.annotation.OperationLog;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import com.czg.validator.group.InsertGroup;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


/**
 * 积分基本设置
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/points/basicSetting")
public class PointsBasicSettingController {

    private final PointsBasicSettingService pointsBasicSettingService;

    /**
     * 详情
     */
    @GetMapping()
    @OperationLog("积分基本设置-详情")
    //@SaAdminCheckPermission("basicSetting:info")
    public CzgResult<PointsBasicSettingDTO> getPointsBasicSetting() {
        Long shopId = StpKit.USER.getShopId(0L);
        PointsBasicSetting entity = pointsBasicSettingService.getById(shopId);
        PointsBasicSettingDTO data = BeanUtil.copyProperties(entity, PointsBasicSettingDTO.class);
        return CzgResult.success(data);
    }

    /**
     * 保存
     */
    @PostMapping
    @OperationLog("积分基本设置-保存")
    //@SaAdminCheckPermission("basicSetting:save")
    public CzgResult<Void> savePointsBasicSetting(@RequestBody PointsBasicSettingDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        ValidatorUtil.validateEntity(dto, InsertGroup.class, DefaultGroup.class);
        validateDTO(dto);
        PointsBasicSetting entity = BeanUtil.copyProperties(dto, PointsBasicSetting.class);
        PointsBasicSetting record = pointsBasicSettingService.getById(shopId);
        if (record == null) {
            pointsBasicSettingService.save(entity);
        } else {
            pointsBasicSettingService.updateById(entity);
        }
        return CzgResult.success();
    }

    private void validateDTO(PointsBasicSettingDTO dto) {
        try {
            Assert.notNull(dto.getShopId(), "{}({})不能为空", "店铺id", "shopId");
            Assert.notNull(dto.getEnableRewards(), "{}({})不能为空", "开启消费赠送积分", "enableRewards");
            Assert.notEmpty(dto.getRewardsGroup(), "{}({})不能为空", "赠积分适用群体", "rewardsGroup");
            Assert.notNull(dto.getConsumeAmount(), "{}({})不能为空", "每消费xx元赠送1积分", "consumeAmount");
            Assert.notNull(dto.getEnableDeduction(), "{}({})不能为空", "开启下单积分抵扣", "enableDeduction");
            Assert.notEmpty(dto.getDeductionGroup(), "{}({})不能为空", "抵扣适用群体", "deductionGroup");
            Assert.notNull(dto.getMinPaymentAmount(), "{}({})不能为空", "下单实付抵扣门槛", "minPaymentAmount");
            Assert.notNull(dto.getMaxDeductionRatio(), "{}({})不能为空", "下单最高抵扣比例", "maxDeductionRatio");
            Assert.notNull(dto.getEquivalentPoints(), "{}({})不能为空", "下单抵扣积分比例", "equivalentPoints");
            Assert.notNull(dto.getEnablePointsMall(), "{}({})不能为空", "开启积分商城", "enablePointsMall");
            Assert.notEmpty(dto.getBrowseMode(), "{}({})不能为空", "浏览模式", "browseMode");
        } catch (IllegalArgumentException e) {
            throw new CzgException(e.getMessage());
        }
        if (NumberUtil.isLessOrEqual(dto.getMinPaymentAmount(), BigDecimal.ZERO)) {
            throw new CzgException("下单实付抵扣门槛不能小于等于0");
        }
        if (NumberUtil.isLessOrEqual(dto.getMaxDeductionRatio(), BigDecimal.ZERO)) {
            throw new CzgException("下单最高抵扣比例不能小于等于0");
        }
        if (NumberUtil.isGreater(dto.getMaxDeductionRatio(), new BigDecimal("100"))) {
            throw new CzgException("下单最高抵扣比例不能大于100");
        }
    }

}