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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallSettingConvert;
import com.jeesharp.business.modules.mall.dto.MallSettingSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSetting;
import com.jeesharp.business.modules.mall.service.MallSettingService;
import com.jeesharp.business.modules.mall.vo.MallSettingInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSettingListVO;
import com.jeesharp.business.modules.mall.vo.MallSettingQueryVO;
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.util.List;
import java.util.Map;

/**
 * 商城设置 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallSettingApplicationService {

    private final MallSettingService mallSettingService;

    public MallSettingInfoVO get(String id) {
        return MallSettingConvert.INSTANCE.toInfoVO(mallSettingService.get(id));
    }

    public PageRespVO<MallSettingListVO> findPage(Page<MallSetting> page, MallSettingQueryVO query) {
        Page<MallSetting> result = mallSettingService.findPage(page, query);
        return MallSettingConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallSettingSaveDTO dto) {
        MallSetting entity = MallSettingConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallSettingService.createSetting(entity);
            return;
        }
        mallSettingService.updateSetting(entity);
    }

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

    /**
     * 获取配置值
     */
    public String getConfigValue(String configKey) {
        return mallSettingService.getConfigValue(configKey);
    }

    /**
     * 设置配置值
     */
    @Transactional(readOnly = false)
    public void setConfigValue(String configKey, String configValue) {
        mallSettingService.setConfigValue(configKey, configValue);
    }

    /**
     * 批量获取配置值
     */
    public Map<String, String> getConfigValues(List<String> configKeys) {
        return mallSettingService.getConfigValues(configKeys);
    }

    /**
     * 获取分组配置
     */
    public List<MallSetting> getSettingsByGroup(String configGroup) {
        return mallSettingService.getSettingsByGroup(configGroup);
    }

    /**
     * 初始化系统默认配置
     */
    @Transactional(readOnly = false)
    public void initSystemSettings() {
        mallSettingService.initSystemSettings();
    }
}
