package com.jeesharp.business.modules.mall.service.impl;

import com.jeesharp.business.modules.mall.entity.MallRewardLevelConfig;
import com.jeesharp.business.modules.mall.mapper.MallRewardLevelConfigMapper;
import com.jeesharp.business.modules.mall.service.MallRewardLevelConfigService;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 消费赠送配置 Service 实现类
 */
@Service
@Transactional(readOnly = true)
public class MallRewardLevelConfigServiceImpl extends CrudService<MallRewardLevelConfigMapper, MallRewardLevelConfig, MallRewardLevelConfigQueryVO> implements MallRewardLevelConfigService {

    private static final int ENABLED_STATUS = 1;
    private static final int DEFAULT_SORT_ORDER = 0;
    private static final String IDS_SEPARATOR = ",";
    private static final String DEFAULT_LEVEL_CODE = "DEFAULT";
    private static final String NORMAL_LEVEL_CODE = "NORMAL";
    private static final String SILVER_LEVEL_CODE = "SILVER";
    private static final String GOLD_LEVEL_CODE = "GOLD";
    private static final String PLATINUM_LEVEL_CODE = "PLATINUM";
    private static final String DIAMOND_LEVEL_CODE = "DIAMOND";

    @Override
    @Transactional(readOnly = false)
    public void createConfig(MallRewardLevelConfig entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateConfig(MallRewardLevelConfig entity) {
        update(entity);
    }

    @Override
    public List<MallRewardLevelConfig> listEnabledConfigs() {
        MallRewardLevelConfigQueryVO query = new MallRewardLevelConfigQueryVO();
        query.setStatus(ENABLED_STATUS);
        return findList(query);
    }

    @Override
    public MallRewardLevelConfig getConfigByLevel(String memberLevelCode) {
        MallRewardLevelConfigQueryVO query = new MallRewardLevelConfigQueryVO();
        query.setMemberLevelCode(memberLevelCode);
        query.setStatus(ENABLED_STATUS);
        List<MallRewardLevelConfig> configs = findList(query);
        return configs.isEmpty() ? getDefaultConfig() : configs.get(0);
    }

    @Override
    public MallRewardLevelConfig getDefaultConfig() {
        MallRewardLevelConfigQueryVO query = new MallRewardLevelConfigQueryVO();
        query.setMemberLevelCode(DEFAULT_LEVEL_CODE);
        query.setStatus(ENABLED_STATUS);
        List<MallRewardLevelConfig> configs = findList(query);
        return configs.isEmpty() ? null : configs.get(0);
    }

    @Override
    public Integer calculateRewardPoints(String memberLevelCode, BigDecimal orderAmount) {
        MallRewardLevelConfig config = getConfigByLevel(memberLevelCode);
        if (config == null || config.getPointsEnabled() != ENABLED_STATUS) {
            return 0;
        }
        BigDecimal pointsPerYuan = config.getPointsPerYuan() == null ? BigDecimal.ZERO : config.getPointsPerYuan();
        return orderAmount.multiply(pointsPerYuan).intValue();
    }

    @Override
    public Integer calculateRewardCoins(String memberLevelCode, BigDecimal orderAmount) {
        MallRewardLevelConfig config = getConfigByLevel(memberLevelCode);
        if (config == null || config.getCoinsEnabled() != ENABLED_STATUS) {
            return 0;
        }
        BigDecimal coinsPerYuan = config.getCoinsPerYuan() == null ? BigDecimal.ZERO : config.getCoinsPerYuan();
        return orderAmount.multiply(coinsPerYuan).intValue();
    }

    @Override
    @Transactional(readOnly = false)
    public void initDefaultConfigs() {
        createOrUpdateConfig(DEFAULT_LEVEL_CODE, ENABLED_STATUS, ENABLED_STATUS, new BigDecimal("10"), new BigDecimal("1"));
        createOrUpdateConfig(NORMAL_LEVEL_CODE, ENABLED_STATUS, ENABLED_STATUS, new BigDecimal("10"), new BigDecimal("1"));
        createOrUpdateConfig(SILVER_LEVEL_CODE, ENABLED_STATUS, ENABLED_STATUS, new BigDecimal("12"), new BigDecimal("1.2"));
        createOrUpdateConfig(GOLD_LEVEL_CODE, ENABLED_STATUS, ENABLED_STATUS, new BigDecimal("15"), new BigDecimal("1.5"));
        createOrUpdateConfig(PLATINUM_LEVEL_CODE, ENABLED_STATUS, ENABLED_STATUS, new BigDecimal("18"), new BigDecimal("1.8"));
        createOrUpdateConfig(DIAMOND_LEVEL_CODE, ENABLED_STATUS, ENABLED_STATUS, new BigDecimal("20"), new BigDecimal("2"));
    }

    private void createOrUpdateConfig(String levelCode, Integer pointsEnabled, Integer coinsEnabled,
                                      BigDecimal pointsPerYuan, BigDecimal coinsPerYuan) {
        MallRewardLevelConfigQueryVO query = new MallRewardLevelConfigQueryVO();
        query.setMemberLevelCode(levelCode);
        List<MallRewardLevelConfig> existing = findList(query);

        MallRewardLevelConfig config = existing.isEmpty() ? new MallRewardLevelConfig() : existing.get(0);
        if (existing.isEmpty()) {
            config.setMemberLevelCode(levelCode);
        }
        config.setPointsEnabled(pointsEnabled);
        config.setCoinsEnabled(coinsEnabled);
        config.setPointsPerYuan(pointsPerYuan);
        config.setCoinsPerYuan(coinsPerYuan);
        config.setStatus(ENABLED_STATUS);
        config.setSortOrder(DEFAULT_SORT_ORDER);
        save(config);
    }
}
