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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.mall.constant.MallConfigConstants;
import com.ruoyi.web.modules.mall.domain.BizMallConfig;
import com.ruoyi.web.modules.mall.dto.MallSettingSaveDTO;
import com.ruoyi.web.modules.mall.facade.IMallConfigFacade;
import com.ruoyi.web.modules.mall.mapper.BizMallConfigMapper;
import com.ruoyi.web.modules.mall.service.IMallConfigService;
import com.ruoyi.web.modules.mall.support.MallConfigSupport;
import com.ruoyi.web.modules.mall.vo.MallConfigAppVO;
import com.ruoyi.web.modules.mall.vo.MallSettingEntryHintsVO;
import com.ruoyi.web.modules.mall.vo.MallSettingVO;

/**
 * 商城设置 Service 实现
 */
@Service
public class MallConfigServiceImpl implements IMallConfigService, IMallConfigFacade
{
    @Autowired
    private BizMallConfigMapper mallConfigMapper;

    @Override
    public MallSettingVO getSettings()
    {
        return toSettingVo(requireConfig());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSettings(MallSettingSaveDTO dto, String operator)
    {
        MallConfigSupport.validateSave(dto);
        requireConfig();
        BizMallConfig update = new BizMallConfig();
        update.setConfigId(MallConfigConstants.CONFIG_ID);
        update.setIcpNo(trimToNull(dto.getIcpNo()));
        update.setIcpLink(trimToNull(dto.getIcpLink()));
        update.setPsbNo(trimToNull(dto.getPsbNo()));
        update.setPsbLink(trimToNull(dto.getPsbLink()));
        update.setShowSales(dto.getShowSales());
        update.setAutoConfirmDays(dto.getAutoConfirmDays());
        update.setAftersaleLimitDays(dto.getAftersaleLimitDays());
        update.setDefaultAvatar(dto.getDefaultAvatar().trim());
        update.setUpdateBy(operator);
        mallConfigMapper.update(update);
    }

    @Override
    public MallSettingEntryHintsVO getEntryHints()
    {
        BizMallConfig config = requireConfig();
        MallSettingEntryHintsVO vo = new MallSettingEntryHintsVO();
        vo.setCurrencySymbol(config.getCurrencySymbol());
        vo.setCurrencyNameCn(config.getCurrencyNameCn());
        vo.setGoodsSnPrefix(config.getGoodsSnPrefix());
        return vo;
    }

    @Override
    public MallConfigAppVO getForApp()
    {
        BizMallConfig config = requireConfig();
        MallConfigAppVO vo = new MallConfigAppVO();
        if (MallConfigSupport.hasFilingPair(config.getIcpNo(), config.getIcpLink()))
        {
            vo.setIcpNo(config.getIcpNo());
            vo.setIcpLink(config.getIcpLink());
        }
        if (MallConfigSupport.hasFilingPair(config.getPsbNo(), config.getPsbLink()))
        {
            vo.setPsbNo(config.getPsbNo());
            vo.setPsbLink(config.getPsbLink());
        }
        vo.setShowSales(MallConfigSupport.isYes(config.getShowSales()));
        vo.setDefaultAvatar(StringUtils.isNotEmpty(config.getDefaultAvatar()) ? config.getDefaultAvatar() : null);
        vo.setCurrencySymbol(config.getCurrencySymbol());
        return vo;
    }

    @Override
    public String getGoodsSnPrefix()
    {
        String prefix = requireConfig().getGoodsSnPrefix();
        return StringUtils.isNotEmpty(prefix) ? prefix : MallConfigConstants.GOODS_SN_PREFIX;
    }

    @Override
    public boolean isShowSales()
    {
        return MallConfigSupport.isYes(requireConfig().getShowSales());
    }

    @Override
    public int getAutoConfirmDays()
    {
        Integer days = requireConfig().getAutoConfirmDays();
        return days != null ? days.intValue() : MallConfigConstants.DEFAULT_AUTO_CONFIRM_DAYS;
    }

    @Override
    public int getAftersaleLimitDays()
    {
        Integer days = requireConfig().getAftersaleLimitDays();
        return days != null ? days.intValue() : MallConfigConstants.DEFAULT_AFTERSALE_LIMIT_DAYS;
    }

    @Override
    public String getDefaultAvatarUrl()
    {
        String avatar = requireConfig().getDefaultAvatar();
        return StringUtils.isNotEmpty(avatar) ? avatar : null;
    }

    @Override
    public String getCurrencySymbol()
    {
        String symbol = requireConfig().getCurrencySymbol();
        return StringUtils.isNotEmpty(symbol) ? symbol : MallConfigConstants.CURRENCY_SYMBOL;
    }

    private BizMallConfig requireConfig()
    {
        BizMallConfig config = mallConfigMapper.selectById(MallConfigConstants.CONFIG_ID);
        if (config != null)
        {
            return config;
        }
        BizMallConfig init = new BizMallConfig();
        init.setConfigId(MallConfigConstants.CONFIG_ID);
        init.setCurrencySymbol(MallConfigConstants.CURRENCY_SYMBOL);
        init.setCurrencyNameCn(MallConfigConstants.CURRENCY_NAME_CN);
        init.setGoodsSnPrefix(MallConfigConstants.GOODS_SN_PREFIX);
        init.setShowSales(MallConfigConstants.FLAG_YES);
        init.setAutoConfirmDays(MallConfigConstants.DEFAULT_AUTO_CONFIRM_DAYS);
        init.setAftersaleLimitDays(MallConfigConstants.DEFAULT_AFTERSALE_LIMIT_DAYS);
        init.setDefaultAvatar("");
        init.setCreateBy("system");
        mallConfigMapper.insert(init);
        return mallConfigMapper.selectById(MallConfigConstants.CONFIG_ID);
    }

    private MallSettingVO toSettingVo(BizMallConfig config)
    {
        MallSettingVO vo = new MallSettingVO();
        vo.setIcpNo(config.getIcpNo());
        vo.setIcpLink(config.getIcpLink());
        vo.setPsbNo(config.getPsbNo());
        vo.setPsbLink(config.getPsbLink());
        vo.setCurrencySymbol(config.getCurrencySymbol());
        vo.setCurrencyNameCn(config.getCurrencyNameCn());
        vo.setGoodsSnPrefix(config.getGoodsSnPrefix());
        vo.setShowSales(config.getShowSales());
        vo.setAutoConfirmDays(config.getAutoConfirmDays());
        vo.setAftersaleLimitDays(config.getAftersaleLimitDays());
        vo.setPaidOrderCancelable(Boolean.FALSE);
        vo.setShippedOrderCancelable(Boolean.FALSE);
        vo.setDefaultAvatar(config.getDefaultAvatar());
        return vo;
    }

    private String trimToNull(String value)
    {
        if (StringUtils.isEmpty(value))
        {
            return null;
        }
        return value.trim();
    }
}
