package com.jeesharp.business.modules.mall.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallRewardLevelConfigConvert;
import com.jeesharp.business.modules.mall.dto.MallRewardLevelConfigSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallRewardLevelConfig;
import com.jeesharp.business.modules.mall.service.MallRewardLevelConfigService;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigInfoVO;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigListVO;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 消费赠送配置 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallRewardLevelConfigApplicationService {

    private final MallRewardLevelConfigService mallRewardLevelConfigService;

    public MallRewardLevelConfigInfoVO get(String id) {
        return MallRewardLevelConfigConvert.INSTANCE.toInfoVO(mallRewardLevelConfigService.get(id));
    }

    public PageRespVO<MallRewardLevelConfigListVO> findPage(Page<MallRewardLevelConfig> page, MallRewardLevelConfigQueryVO query) {
        Page<MallRewardLevelConfig> result = mallRewardLevelConfigService.findPage(page, query);
        return MallRewardLevelConfigConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallRewardLevelConfigSaveDTO dto) {
        MallRewardLevelConfig entity = MallRewardLevelConfigConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallRewardLevelConfigService.createConfig(entity);
            return;
        }
        mallRewardLevelConfigService.updateConfig(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        mallRewardLevelConfigService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 获取所有启用的赠送配置
     */
    public List<MallRewardLevelConfig> listEnabledConfigs() {
        return mallRewardLevelConfigService.listEnabledConfigs();
    }

    /**
     * 根据会员等级获取赠送配置
     */
    public MallRewardLevelConfig getConfigByLevel(String memberLevelCode) {
        return mallRewardLevelConfigService.getConfigByLevel(memberLevelCode);
    }

    /**
     * 获取默认赠送配置
     */
    public MallRewardLevelConfig getDefaultConfig() {
        return mallRewardLevelConfigService.getDefaultConfig();
    }

    /**
     * 计算赠送积分
     */
    public Integer calculateRewardPoints(String memberLevelCode, BigDecimal orderAmount) {
        return mallRewardLevelConfigService.calculateRewardPoints(memberLevelCode, orderAmount);
    }

    /**
     * 计算赠送金币
     */
    public Integer calculateRewardCoins(String memberLevelCode, BigDecimal orderAmount) {
        return mallRewardLevelConfigService.calculateRewardCoins(memberLevelCode, orderAmount);
    }

    /**
     * 初始化默认配置
     */
    @Transactional(readOnly = false)
    public void initDefaultConfigs() {
        mallRewardLevelConfigService.initDefaultConfigs();
    }
}
