package com.ruoyi.web.modules.store.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.store.constant.ShopConstants;
import com.ruoyi.web.modules.store.constant.ShopGlobalConfigConstants;
import com.ruoyi.web.modules.store.domain.BizShopGlobalConfig;
import com.ruoyi.web.modules.store.dto.ShopSettingsDTO;
import com.ruoyi.web.modules.store.facade.IShopGlobalConfigFacade;
import com.ruoyi.web.modules.store.mapper.BizShopGlobalConfigMapper;
import com.ruoyi.web.modules.store.service.IShopGlobalConfigService;
import com.ruoyi.web.modules.store.vo.ShopSettingsVO;

@Service
public class ShopGlobalConfigServiceImpl implements IShopGlobalConfigService, IShopGlobalConfigFacade
{
    @Autowired
    private BizShopGlobalConfigMapper globalConfigMapper;

    @Override
    public ShopSettingsVO getGlobalSettings()
    {
        return toVo(requireConfig());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGlobalSettings(ShopSettingsDTO dto, String operator)
    {
        validateSettings(dto);
        BizShopGlobalConfig update = new BizShopGlobalConfig();
        update.setConfigId(ShopGlobalConfigConstants.CONFIG_ID);
        update.setDefaultAuditPass(dto.getDefaultAuditPass());
        update.setMaxSubAdmin(dto.getMaxSubAdmin());
        update.setUpdateBy(operator);
        globalConfigMapper.update(update);
    }

    @Override
    public String getDefaultAuditPass()
    {
        return requireConfig().getDefaultAuditPass();
    }

    @Override
    public Integer getMaxSubAdmin()
    {
        return requireConfig().getMaxSubAdmin();
    }

    private BizShopGlobalConfig requireConfig()
    {
        BizShopGlobalConfig config = globalConfigMapper.selectById(ShopGlobalConfigConstants.CONFIG_ID);
        if (config != null)
        {
            return config;
        }
        BizShopGlobalConfig init = new BizShopGlobalConfig();
        init.setConfigId(ShopGlobalConfigConstants.CONFIG_ID);
        init.setDefaultAuditPass(ShopConstants.AUDIT_PASS_NO);
        init.setMaxSubAdmin(ShopConstants.DEFAULT_MAX_SUB_ADMIN);
        init.setCreateBy("system");
        globalConfigMapper.insert(init);
        return globalConfigMapper.selectById(ShopGlobalConfigConstants.CONFIG_ID);
    }

    private ShopSettingsVO toVo(BizShopGlobalConfig config)
    {
        ShopSettingsVO vo = new ShopSettingsVO();
        vo.setDefaultAuditPass(config.getDefaultAuditPass());
        vo.setMaxSubAdmin(config.getMaxSubAdmin());
        return vo;
    }

    private void validateSettings(ShopSettingsDTO dto)
    {
        if (dto == null)
        {
            throw new ServiceException("参数不能为空");
        }
        if (!ShopConstants.AUDIT_PASS_NO.equals(dto.getDefaultAuditPass())
                && !ShopConstants.AUDIT_PASS_YES.equals(dto.getDefaultAuditPass()))
        {
            throw new ServiceException("默认审核通过参数无效");
        }
        if (dto.getMaxSubAdmin() == null
                || dto.getMaxSubAdmin() < ShopConstants.MIN_SUB_ADMIN
                || dto.getMaxSubAdmin() > ShopConstants.MAX_SUB_ADMIN)
        {
            throw new ServiceException("请输入 1～99 之间的整数");
        }
    }
}
