package com.webmanage.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.webmanage.common.BusinessException;
|
import com.webmanage.common.PageResult;
|
import com.webmanage.dto.PointsRuleDTO;
|
import com.webmanage.entity.Points;
|
import com.webmanage.entity.PointsRule;
|
import com.webmanage.entity.PointsRuleDetail;
|
import com.webmanage.mapper.PointsRuleMapper;
|
import com.webmanage.service.PointsRuleService;
|
import com.webmanage.service.PointsService;
|
import com.webmanage.service.PointsRuleDetailService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.StringUtils;
|
|
import java.time.LocalDateTime;
|
import java.util.List;
|
|
/**
|
* 积分规则Service实现类
|
*/
|
@Slf4j
|
@Service
|
public class PointsRuleServiceImpl extends ServiceImpl<PointsRuleMapper, PointsRule> implements PointsRuleService {
|
|
@Autowired
|
private PointsService pointsService;
|
|
@Autowired
|
private PointsRuleDetailService pointsRuleDetailService;
|
|
@Override
|
public PageResult<PointsRule> getPointsRulePage(Integer pageNum, Integer pageSize, String ruleName, String ruleType) {
|
Page<PointsRule> page = new Page<>(pageNum, pageSize);
|
|
QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
|
wrapper.eq("deleted", 0);
|
|
if (StringUtils.hasText(ruleName)) {
|
wrapper.like("rule_name", ruleName);
|
}
|
if (StringUtils.hasText(ruleType)) {
|
wrapper.eq("rule_type", ruleType);
|
}
|
|
wrapper.orderByDesc("priority", "created_at");
|
|
IPage<PointsRule> result = page(page, wrapper);
|
|
return new PageResult<PointsRule>(
|
result.getRecords(),
|
result.getTotal(),
|
pageNum.longValue(),
|
pageSize.longValue(),
|
result.getPages()
|
);
|
}
|
|
@Override
|
public boolean addPointsRule(PointsRuleDTO pointsRuleDTO) {
|
// 验证规则名称是否重复
|
QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
|
wrapper.eq("rule_name", pointsRuleDTO.getRuleName())
|
.eq("deleted", 0);
|
|
if (count(wrapper) > 0) {
|
throw new BusinessException("规则名称已存在");
|
}
|
|
PointsRule pointsRule = new PointsRule();
|
BeanUtils.copyProperties(pointsRuleDTO, pointsRule);
|
|
return save(pointsRule);
|
}
|
|
@Override
|
public boolean updatePointsRule(PointsRuleDTO pointsRuleDTO) {
|
if (pointsRuleDTO.getId() == null) {
|
throw new BusinessException("规则ID不能为空");
|
}
|
|
PointsRule existingRule = getById(pointsRuleDTO.getId());
|
if (existingRule == null) {
|
throw new BusinessException("积分规则不存在");
|
}
|
|
// 验证规则名称是否重复(排除自身)
|
QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
|
wrapper.eq("rule_name", pointsRuleDTO.getRuleName())
|
.eq("deleted", 0)
|
.ne("id", pointsRuleDTO.getId());
|
|
if (count(wrapper) > 0) {
|
throw new BusinessException("规则名称已存在");
|
}
|
|
// 创建新的积分主表记录
|
Points points = new Points();
|
points.setPointsName(pointsRuleDTO.getRuleName() + "_" + System.currentTimeMillis());
|
points.setEffectiveStart(LocalDateTime.now());
|
points.setModifierId(1L); // 这里应该从上下文中获取当前用户ID
|
points.setModifierName("admin"); // 这里应该从上下文中获取当前用户名
|
points.setVersion(1.0f);
|
points.setStatus(0);
|
pointsService.save(points);
|
|
// 创建积分规则详情记录
|
PointsRuleDetail pointsRuleDetail = new PointsRuleDetail();
|
pointsRuleDetail.setRuleId(pointsRuleDTO.getId());
|
pointsRuleDetail.setPointsId(points.getId());
|
pointsRuleDetail.setPointsValue(pointsRuleDTO.getPointsValue());
|
pointsRuleDetail.setEffectiveStart(LocalDateTime.now());
|
pointsRuleDetailService.save(pointsRuleDetail);
|
|
BeanUtils.copyProperties(pointsRuleDTO, existingRule);
|
existingRule.setUpdatedAt(LocalDateTime.now());
|
|
return updateById(existingRule);
|
}
|
|
@Override
|
public boolean deletePointsRule(Long id) {
|
if (id == null) {
|
throw new BusinessException("规则ID不能为空");
|
}
|
|
PointsRule pointsRule = getById(id);
|
if (pointsRule == null) {
|
throw new BusinessException("积分规则不存在");
|
}
|
|
return removeById(id);
|
}
|
|
@Override
|
public boolean togglePointsRuleStatus(Long id, Boolean isEnabled) {
|
if (id == null) {
|
throw new BusinessException("规则ID不能为空");
|
}
|
|
PointsRule pointsRule = getById(id);
|
if (pointsRule == null) {
|
throw new BusinessException("积分规则不存在");
|
}
|
|
pointsRule.setIsEnabled(isEnabled);
|
pointsRule.setUpdatedAt(LocalDateTime.now());
|
|
return updateById(pointsRule);
|
}
|
|
@Override
|
public List<PointsRule> getEnabledRulesByType(String ruleType) {
|
QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
|
wrapper.eq("deleted", 0)
|
.eq("is_enabled", true)
|
.eq("rule_type", ruleType)
|
.orderByAsc("priority", "created_at");
|
|
return list(wrapper);
|
}
|
|
@Override
|
public PointsRule getRuleByTriggerCondition(String triggerCondition) {
|
if (!StringUtils.hasText(triggerCondition)) {
|
return null;
|
}
|
|
QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
|
wrapper.eq("deleted", 0)
|
.eq("is_enabled", true)
|
.eq("trigger_condition", triggerCondition)
|
.orderByAsc("priority")
|
.last("LIMIT 1");
|
|
return getOne(wrapper);
|
}
|
}
|