package com.ruoyi.system.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.page.BeanUtils;
import com.ruoyi.system.api.constants.ConfigEnum;
import com.ruoyi.system.api.domain.CustomConfig;
import com.ruoyi.system.api.domain.dto.MgtAfterSaleSettingDTO;
import com.ruoyi.system.domain.dto.PointsConfigDTO;
import com.ruoyi.system.domain.vo.CustomConfigVO;
import com.ruoyi.system.domain.vo.WishSettingVO;
import com.ruoyi.system.mapper.CustomConfigMapper;
import com.ruoyi.system.service.ICustomConfigService;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
*
* 系统配置 服务实现类
*
*
* @author mitao
* @since 2024-05-21
*/
@Service
public class CustomConfigServiceImpl extends ServiceImpl implements ICustomConfigService {
/**
* 保存积分设置
*
* @param dto 积分配置数据传输对象
*/
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void savePointsSettings(PointsConfigDTO dto) {
Optional customConfigOptional = getCustomConfigByKey(
ConfigEnum.MEMBER_POINTS_MONEY.getKey());
CustomConfig moneyConfig = customConfigOptional.orElseGet(() -> {
CustomConfig config = new CustomConfig();
config.setConfigKey(ConfigEnum.MEMBER_POINTS_MONEY.getKey());
config.setConfigType(ConfigEnum.MEMBER_POINTS_MONEY.getKeyType());
config.setConfigName(ConfigEnum.MEMBER_POINTS_MONEY.getKeyName());
return config;
});
moneyConfig.setConfigValue(dto.getConsumeAmount().toString());
Optional customConfigByKey = getCustomConfigByKey(
ConfigEnum.MEMBER_POINTS_POINTS.getKey());
CustomConfig pointsConfig = customConfigByKey.orElseGet(() -> {
CustomConfig config = new CustomConfig();
config.setConfigKey(ConfigEnum.MEMBER_POINTS_POINTS.getKey());
config.setConfigType(ConfigEnum.MEMBER_POINTS_POINTS.getKeyType());
config.setConfigName(ConfigEnum.MEMBER_POINTS_POINTS.getKeyName());
return config;
});
pointsConfig.setConfigValue(dto.getPoints().toString());
this.saveOrUpdateBatch(Lists.newArrayList(moneyConfig, pointsConfig));
}
private Optional getCustomConfigByKey(String key) {
return this.lambdaQuery()
.eq(CustomConfig::getConfigKey, key)
.eq(CustomConfig::getDelFlag, 0).oneOpt();
}
/**
* 获取积分设置
*
* @return List
*/
@Override
public PointsConfigDTO getPointsConfig() {
PointsConfigDTO dto = new PointsConfigDTO();
List list = this.lambdaQuery()
.in(CustomConfig::getConfigKey, ConfigEnum.MEMBER_POINTS_MONEY.getKey(),
ConfigEnum.MEMBER_POINTS_POINTS.getKey())
.eq(CustomConfig::getDelFlag, 0).list();
for (CustomConfig customConfig : list) {
if (ConfigEnum.MEMBER_POINTS_MONEY.getKey().equals(customConfig.getConfigKey())) {
dto.setConsumeAmount(
BigDecimal.valueOf(Double.parseDouble(customConfig.getConfigValue())));
}
if (ConfigEnum.MEMBER_POINTS_POINTS.getKey().equals(customConfig.getConfigKey())) {
dto.setPoints(Integer.parseInt(customConfig.getConfigValue()));
}
}
return dto;
}
/**
* 获取订单说明设置
*
* @return CustomConfigVO
*/
@Override
public CustomConfigVO getOrderDesc() {
CustomConfigVO vo;
CustomConfig customConfig = getCustomConfigByKey(
ConfigEnum.MALL_ORDER_DESCRIPTION.getKey()).orElse(new CustomConfig());
vo = BeanUtils.copyBean(customConfig, CustomConfigVO.class);
return vo;
}
/**
* 订单说明设置
*
* @param description 订单说明
*/
@Override
public void saveOrderDescription(String description) {
CustomConfig orderDescription = getCustomConfigByKey(
ConfigEnum.MALL_ORDER_DESCRIPTION.getKey()).orElseGet(() -> {
CustomConfig customConfig = new CustomConfig();
customConfig.setConfigKey(ConfigEnum.MALL_ORDER_DESCRIPTION.getKey());
customConfig.setConfigName(ConfigEnum.MALL_ORDER_DESCRIPTION.getKeyName());
customConfig.setConfigType(ConfigEnum.MALL_ORDER_DESCRIPTION.getKeyType());
return customConfig;
});
orderDescription.setConfigValue(description);
this.saveOrUpdate(orderDescription);
}
/**
* 售后设置
*
* @param dto 售后设置对象
*/
@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void saveAfterSaleSetting(MgtAfterSaleSettingDTO dto) {
handleConfigSetting(dto.getReceiverName(), ConfigEnum.RETURN_ADDRESS_USER_NAME);
handleConfigSetting(dto.getReceiverPhone(), ConfigEnum.RETURN_ADDRESS_USER_PHONE);
handleConfigSetting(dto.getReceiverAddress(), ConfigEnum.RETURN_ADDRESS_USER_ADDRESS);
handleConfigSetting(dto.getReturnCycle().toString(), ConfigEnum.RETURN_CYCLE);
}
private void handleConfigSetting(String value, ConfigEnum configEnum) {
Optional configSettingOptional = getCustomConfigByKey(configEnum.getKey());
CustomConfig configSetting = configSettingOptional.orElseGet(() -> {
CustomConfig config = new CustomConfig();
config.setConfigKey(configEnum.getKey());
config.setConfigType(configEnum.getKeyType());
config.setConfigName(configEnum.getKeyName());
return config;
});
configSetting.setConfigValue(value);
this.saveOrUpdate(configSetting);
}
/**
* 获取售后设置
*
* @return List
*/
@Override
public MgtAfterSaleSettingDTO getAfterSaleSetting() {
MgtAfterSaleSettingDTO dto = new MgtAfterSaleSettingDTO();
List list = this.lambdaQuery()
.in(CustomConfig::getConfigKey, ConfigEnum.RETURN_ADDRESS_USER_NAME,
ConfigEnum.RETURN_ADDRESS_USER_PHONE,
ConfigEnum.RETURN_ADDRESS_USER_ADDRESS, ConfigEnum.RETURN_CYCLE)
.eq(CustomConfig::getDelFlag, 0).list();
for (CustomConfig customConfig : list) {
if (ConfigEnum.RETURN_ADDRESS_USER_NAME.getKey().equals(customConfig.getConfigKey())) {
dto.setReceiverName(customConfig.getConfigValue());
}
if (ConfigEnum.RETURN_ADDRESS_USER_PHONE.getKey().equals(customConfig.getConfigKey())) {
dto.setReceiverPhone(customConfig.getConfigValue());
}
if (ConfigEnum.RETURN_ADDRESS_USER_ADDRESS.getKey()
.equals(customConfig.getConfigKey())) {
dto.setReceiverAddress(customConfig.getConfigValue());
}
if (ConfigEnum.RETURN_CYCLE.getKey().equals(customConfig.getConfigKey())) {
dto.setReturnCycle(Integer.parseInt(customConfig.getConfigValue()));
}
}
return dto;
}
/**
* 保存客服电话
*
* @param phone 客服电话
*/
@Override
public void updateServicePhone(String phone) {
Optional customConfigByKey = getCustomConfigByKey(
ConfigEnum.PLATFORM_SERVICE_PHONE.getKey());
if (customConfigByKey.isPresent()) {
CustomConfig customConfig = customConfigByKey.get();
String[] split = customConfig.getConfigValue().split(",");
if (split.length > 1) {
throw new ServiceException("客户电话最多配置两个");
}
boolean flag = Arrays.asList(split).contains(phone);
if (flag) {
throw new ServiceException("客户电话已存在");
}
if (StringUtils.isBlank(customConfig.getConfigValue())) {
customConfig.setConfigValue(phone);
} else {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(customConfig.getConfigValue());
stringBuilder.append(",");
stringBuilder.append(phone);
customConfig.setConfigValue(stringBuilder.toString());
}
customConfig.setUpdateTime(LocalDateTime.now());
this.updateById(customConfig);
} else {
CustomConfig config = new CustomConfig();
config.setConfigKey(ConfigEnum.PLATFORM_SERVICE_PHONE.getKey());
config.setConfigType(ConfigEnum.PLATFORM_SERVICE_PHONE.getKeyType());
config.setConfigName(ConfigEnum.PLATFORM_SERVICE_PHONE.getKeyName());
config.setConfigValue(phone);
config.setCreateTime(LocalDateTime.now());
this.save(config);
}
}
/**
* 获取客服电话
*
* @return 客服电话
*/
@Override
public String getServicePhone() {
Optional customConfigByKey = getCustomConfigByKey(
ConfigEnum.PLATFORM_SERVICE_PHONE.getKey());
return customConfigByKey.map(CustomConfig::getConfigValue).orElse(null);
}
/**
* 删除客户电话
*
* @param phone 客服电话
*/
@Override
public void deleteServicePhone(String phone) {
Optional customConfigByKey = getCustomConfigByKey(
ConfigEnum.PLATFORM_SERVICE_PHONE.getKey());
if (customConfigByKey.isPresent()) {
CustomConfig customConfig = customConfigByKey.get();
String configValue = customConfig.getConfigValue();
if (StringUtils.isNotBlank(configValue)) {
String[] split = configValue.split(",");
// 判断当前phone是否存在
boolean flag = Arrays.asList(split).contains(phone);
if (flag) {
// 删除 split 中与phone相同的元素
configValue = Arrays.stream(split).filter(s -> !s.equals(phone))
.collect(Collectors.joining(","));
customConfig.setConfigValue(configValue);
this.updateById(customConfig);
}
}
}
}
/**
* 获取心愿求购设置
*
* @return
*/
@Override
public WishSettingVO getWishSetting() {
WishSettingVO vo = new WishSettingVO();
CustomConfig customConfig = getCustomConfigByKey(
ConfigEnum.WISH_DESCRIPTION.getKey()).orElse(new CustomConfig());
List list = this.lambdaQuery()
.in(CustomConfig::getConfigKey, ConfigEnum.WISH_DESCRIPTION.getKey(),
ConfigEnum.WISH_SHARE_TITLE.getKey(),
ConfigEnum.WISH_SHARE_IMG_URL.getKey())
.eq(CustomConfig::getDelFlag, 0).list();
for (CustomConfig config : list) {
if (ConfigEnum.WISH_DESCRIPTION.getKey().equals(config.getConfigKey())) {
vo.setDescription(customConfig.getConfigValue());
} else if (ConfigEnum.WISH_SHARE_TITLE.getKey().equals(config.getConfigKey())) {
vo.setShareTitle(config.getConfigValue());
} else if (ConfigEnum.WISH_SHARE_IMG_URL.getKey().equals(config.getConfigKey())) {
vo.setShareImgUrl(config.getConfigValue());
}
}
return vo;
}
/**
* 保存心愿求购说明
*
* @param vo 保存心愿求购说明
*/
@Override
public void saveWishDescription(WishSettingVO vo) {
String description = vo.getDescription();
String shareTitle = vo.getShareTitle();
String shareImgUrl = vo.getShareImgUrl();
CustomConfig wishDescription = getCustomConfigByKey(
ConfigEnum.WISH_DESCRIPTION.getKey()).orElseGet(() -> {
CustomConfig customConfig = new CustomConfig();
customConfig.setConfigKey(ConfigEnum.WISH_DESCRIPTION.getKey());
customConfig.setConfigName(ConfigEnum.WISH_DESCRIPTION.getKeyName());
customConfig.setConfigType(ConfigEnum.WISH_DESCRIPTION.getKeyType());
return customConfig;
});
wishDescription.setConfigValue(description);
CustomConfig wishShareTitle = getCustomConfigByKey(
ConfigEnum.WISH_SHARE_TITLE.getKey()).orElseGet(() -> {
CustomConfig customConfig = new CustomConfig();
customConfig.setConfigKey(ConfigEnum.WISH_SHARE_TITLE.getKey());
customConfig.setConfigName(ConfigEnum.WISH_SHARE_TITLE.getKeyName());
customConfig.setConfigType(ConfigEnum.WISH_SHARE_TITLE.getKeyType());
return customConfig;
});
wishShareTitle.setConfigValue(shareTitle);
CustomConfig wishShareImgUrl = getCustomConfigByKey(
ConfigEnum.WISH_SHARE_IMG_URL.getKey()).orElseGet(() -> {
CustomConfig customConfig = new CustomConfig();
customConfig.setConfigKey(ConfigEnum.WISH_SHARE_IMG_URL.getKey());
customConfig.setConfigName(ConfigEnum.WISH_SHARE_IMG_URL.getKeyName());
customConfig.setConfigType(ConfigEnum.WISH_SHARE_IMG_URL.getKeyType());
return customConfig;
});
wishShareImgUrl.setConfigValue(shareImgUrl);
saveOrUpdateBatch(
Lists.newArrayList(wishDescription, wishShareTitle, wishShareImgUrl));
}
/**
* 保存保证金退还
*/
@Override
@Transactional
public void saveRefundOfDeposit(String refundOfDeposit) {
CustomConfig wishDescription = getCustomConfigByKey(
ConfigEnum.REFUND_OF_DEPOSIT.getKey()).orElseGet(() -> {
CustomConfig customConfig = new CustomConfig();
customConfig.setConfigKey(ConfigEnum.REFUND_OF_DEPOSIT.getKey());
customConfig.setConfigName(ConfigEnum.REFUND_OF_DEPOSIT.getKeyName());
customConfig.setConfigType(ConfigEnum.REFUND_OF_DEPOSIT.getKeyType());
return customConfig;
});
wishDescription.setConfigValue(refundOfDeposit);
saveOrUpdate(wishDescription);
}
@Override
public String getRefundOfDeposit() {
AtomicReference refundOfDeposit = new AtomicReference<>("");
getCustomConfigByKey(
ConfigEnum.REFUND_OF_DEPOSIT.getKey()).ifPresent(
customConfig ->
refundOfDeposit.set(customConfig.getConfigValue())
);
return refundOfDeposit.get();
}
}