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

import com.jeesharp.business.modules.mall.entity.MallSetting;
import com.jeesharp.business.modules.mall.mapper.MallSettingMapper;
import com.jeesharp.business.modules.mall.service.MallSettingService;
import com.jeesharp.business.modules.mall.vo.MallSettingQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商城设置 Service 实现类
 */
@Service
@Transactional(readOnly = true)
public class MallSettingServiceImpl extends CrudService<MallSettingMapper, MallSetting, MallSettingQueryVO> implements MallSettingService {

    private static final int SYSTEM_SETTING = 1;
    private static final int NON_SYSTEM_SETTING = 0;
    private static final int DEFAULT_SORT_ORDER = 0;
    private static final String IDS_SEPARATOR = ",";

    @Override
    @Transactional(readOnly = false)
    public void createSetting(MallSetting entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateSetting(MallSetting entity) {
        save(entity);
    }

    @Override
    public String getConfigValue(String configKey) {
        MallSettingQueryVO query = new MallSettingQueryVO();
        query.setConfigKey(configKey);
        List<MallSetting> settings = findList(query);
        return settings.isEmpty() ? null : settings.get(0).getConfigValue();
    }

    @Override
    @Transactional(readOnly = false)
    public void setConfigValue(String configKey, String configValue) {
        MallSettingQueryVO query = new MallSettingQueryVO();
        query.setConfigKey(configKey);
        List<MallSetting> settings = findList(query);

        MallSetting setting = settings.isEmpty() ? new MallSetting() : settings.get(0);
        if (settings.isEmpty()) {
            setting.setConfigKey(configKey);
            setting.setIsSystem(NON_SYSTEM_SETTING);
            createSetting(setting);
            setting.setConfigValue(configValue);
            update(setting);
            return;
        }
        setting.setConfigValue(configValue);
        update(setting);
    }

    @Override
    public Map<String, String> getConfigValues(List<String> configKeys) {
        return findList(new MallSettingQueryVO()).stream()
                .filter(setting -> configKeys.contains(setting.getConfigKey()))
                .collect(Collectors.toMap(MallSetting::getConfigKey, MallSetting::getConfigValue));
    }

    @Override
    public List<MallSetting> getSettingsByGroup(String configGroup) {
        MallSettingQueryVO query = new MallSettingQueryVO();
        query.setConfigGroup(configGroup);
        return findList(query);
    }

    @Override
    @Transactional(readOnly = false)
    public void initSystemSettings() {
        setSystemSetting("order_timeout_minutes", "30", "订单支付超时时间（分钟）", "ORDER", "NUMBER");
        setSystemSetting("auto_confirm_days", "7", "自动确认收货天数", "ORDER", "NUMBER");
        setSystemSetting("product_audit_enabled", "1", "是否开启商品审核：0-关闭 1-开启", "PRODUCT", "BOOLEAN");
        setSystemSetting("review_enabled", "1", "是否开启评价功能", "FEATURE", "BOOLEAN");
        setSystemSetting("invoice_enabled", "1", "是否开启发票功能", "FEATURE", "BOOLEAN");
        setSystemSetting("pickup_enabled", "1", "是否开启到店自提", "FEATURE", "BOOLEAN");
        setSystemSetting("points_consume_rate", "100", "积分抵扣比例（100积分=1元）", "POINTS", "NUMBER");
        setSystemSetting("coins_consume_rate", "100", "金币抵扣比例（100金币=1元）", "POINTS", "NUMBER");
        setSystemSetting("after_sale_refund_trigger_mode", "MANUAL",
                "售后退款触发：AUTO-审核通过自动退款 MANUAL-运营手动发起", "ORDER", "SELECT");
    }

    private void setSystemSetting(String configKey, String configValue, String configDesc,
                                  String configGroup, String configType) {
        MallSettingQueryVO query = new MallSettingQueryVO();
        query.setConfigKey(configKey);
        if (!findList(query).isEmpty()) {
            return;
        }
        MallSetting setting = new MallSetting();
        setting.setConfigKey(configKey);
        setting.setConfigValue(configValue);
        setting.setConfigDesc(configDesc);
        setting.setConfigGroup(configGroup);
        setting.setConfigType(configType);
        setting.setIsSystem(SYSTEM_SETTING);
        setting.setSortOrder(DEFAULT_SORT_ORDER);
        createSetting(setting);
    }
}
