feat:十二分制-奖惩规则
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package com.cool.store.service.dict.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollStreamUtil;
|
||||
import com.cool.store.annotation.DictField;
|
||||
import com.cool.store.dao.dict.SysDictColumnDAO;
|
||||
import javafx.util.Pair;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 字典服务类
|
||||
* </p>
|
||||
*
|
||||
* @author wangff
|
||||
* @since 2025/11/4
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class DictService {
|
||||
private final SysDictColumnDAO columnDAO;
|
||||
private final Map<Class<?>, List<Pair<Field, Field>>> dictFieldCache = new ConcurrentHashMap<>();
|
||||
|
||||
public <T> void fillDictField(T obj) {
|
||||
fillDictField(Collections.singletonList(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充字典字段
|
||||
*/
|
||||
public <T> void fillDictField(List<T> objs) {
|
||||
if (CollectionUtils.isEmpty(objs)) {
|
||||
return;
|
||||
}
|
||||
Class<?> clazz = objs.get(0).getClass();
|
||||
List<Pair<Field, Field>> dictFields = getDictField(clazz);
|
||||
if (CollectionUtils.isEmpty(dictFields)) {
|
||||
return;
|
||||
}
|
||||
Set<String> columnCodes = new HashSet<>();
|
||||
for (T obj : objs) {
|
||||
for (Pair<Field, Field> dictField : dictFields) {
|
||||
try {
|
||||
Object value = dictField.getValue().get(obj);
|
||||
if (value != null) {
|
||||
columnCodes.add((String) value);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
log.info("字典字段值获取失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (columnCodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<String, String> nameMap = columnDAO.getNameMapByCodes(new ArrayList<>(columnCodes));
|
||||
for (T obj : objs) {
|
||||
for (Pair<Field, Field> dictField : dictFields) {
|
||||
try {
|
||||
dictField.getKey().set(obj, nameMap.get((String) dictField.getValue().get(obj)));
|
||||
} catch (IllegalAccessException e) {
|
||||
log.info("字典字段值填充失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private <T> List<Pair<Field, Field>> getDictField(Class<T> clazz) {
|
||||
return dictFieldCache.computeIfAbsent(clazz, this::computeDictFields);
|
||||
}
|
||||
|
||||
private <T> List<Pair<Field, Field>> computeDictFields(Class<T> clazz) {
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
Map<String, Field> fieldMap = CollStreamUtil.toMap(Arrays.asList(fields), Field::getName, v -> v);
|
||||
List<Pair<Field, Field>> dictFields = new ArrayList<>();
|
||||
for (Field field : fields) {
|
||||
DictField dictField = field.getAnnotation(DictField.class);
|
||||
if (Objects.nonNull(dictField)) {
|
||||
field.setAccessible(true);
|
||||
String fieldName = field.getName();
|
||||
String sourceFieldName = StringUtils.isBlank(dictField.sourceField()) && fieldName.endsWith("Name")
|
||||
? fieldName.substring(0, fieldName.length() - 4)
|
||||
: dictField.sourceField();
|
||||
if (StringUtils.isNotBlank(sourceFieldName) && fieldMap.containsKey(sourceFieldName)) {
|
||||
Field sourceField = fieldMap.get(sourceFieldName);
|
||||
sourceField.setAccessible(true);
|
||||
dictFields.add(new Pair<>(field, sourceField));
|
||||
}
|
||||
}
|
||||
}
|
||||
return dictFields;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.cool.store.service.tp;
|
||||
|
||||
import com.cool.store.request.tp.TpPenaltyRuleUpdateRequest;
|
||||
import com.cool.store.request.tp.TpRewardRuleUpdateRequest;
|
||||
import com.cool.store.request.tp.TpRuleQueryRequest;
|
||||
import com.cool.store.vo.tp.TpPenaltyRuleDetailVO;
|
||||
import com.cool.store.vo.tp.TpPenaltyRuleListVO;
|
||||
import com.cool.store.vo.tp.TpRewardRuleDetailVO;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 十二分制-奖惩规则
|
||||
* </p>
|
||||
*
|
||||
* @author wangff
|
||||
* @since 2025/11/4
|
||||
*/
|
||||
public interface TpRuleService {
|
||||
|
||||
/**
|
||||
* 新增惩处规则
|
||||
* @param request 惩处规则更新Request
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean insertPenaltyRule(TpPenaltyRuleUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 修改惩处规则
|
||||
* @param request 惩处规则更新Request
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean updatePenaltyRule(TpPenaltyRuleUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 惩处规则详情
|
||||
* @param ruleId 规则id
|
||||
* @return 惩处规则详情VO
|
||||
*/
|
||||
TpPenaltyRuleDetailVO penaltyRuleDetail(Long ruleId);
|
||||
|
||||
/**
|
||||
* 惩处规则分页查询
|
||||
* @param request 规则查询Request
|
||||
* @return 惩处规则列表
|
||||
*/
|
||||
PageInfo<TpPenaltyRuleListVO> penaltyRulePage(TpRuleQueryRequest request);
|
||||
|
||||
/**
|
||||
* 新增加分规则
|
||||
* @param request 加分规则更新Request
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean insertRewardRule(TpRewardRuleUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 修改加分规则
|
||||
* @param request 加分规则更新Request
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean updateRewardRule(TpRewardRuleUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 加分规则详情
|
||||
* @param ruleId 规则id
|
||||
* @return 加分规则详情VO
|
||||
*/
|
||||
TpRewardRuleDetailVO rewardRuleDetail(Long ruleId);
|
||||
|
||||
/**
|
||||
* 加分规则分页查询
|
||||
* @param request 规则查询Request
|
||||
* @return 加分规则详情列表
|
||||
*/
|
||||
PageInfo<TpRewardRuleDetailVO> rewardRulePage(TpRuleQueryRequest request);
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
* @param ruleIds 规则id列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean deleteRuleByIds(List<Long> ruleIds);
|
||||
|
||||
/**
|
||||
* 批量启用
|
||||
* @param ruleIds 规则id列表
|
||||
* @param enable 启用状态,0未启用 1启用
|
||||
* @return 是否成功
|
||||
*/
|
||||
Boolean enableRuleByIds(List<Long> ruleIds, Integer enable);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.cool.store.service.tp.impl;
|
||||
|
||||
import com.cool.store.dao.tp.TpRuleDAO;
|
||||
import com.cool.store.entity.tp.TpRuleDO;
|
||||
import com.cool.store.request.tp.TpPenaltyRuleUpdateRequest;
|
||||
import com.cool.store.request.tp.TpRewardRuleUpdateRequest;
|
||||
import com.cool.store.request.tp.TpRuleQueryRequest;
|
||||
import com.cool.store.service.dict.impl.DictService;
|
||||
import com.cool.store.service.tp.TpRuleService;
|
||||
import com.cool.store.utils.BeanUtil;
|
||||
import com.cool.store.utils.TpHelper;
|
||||
import com.cool.store.vo.tp.TpPenaltyRuleDetailVO;
|
||||
import com.cool.store.vo.tp.TpPenaltyRuleListVO;
|
||||
import com.cool.store.vo.tp.TpRewardRuleDetailVO;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 十二分制-奖惩规则 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author wangff
|
||||
* @since 2025/11/4
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TpRuleServiceImpl implements TpRuleService {
|
||||
private final TpRuleDAO tpRuleDAO;
|
||||
private final DictService dictService;
|
||||
|
||||
@Override
|
||||
public Boolean insertPenaltyRule(TpPenaltyRuleUpdateRequest request) {
|
||||
TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
|
||||
tpRuleDO.setRuleCode(TpHelper.generatePenaltyRuleCode());
|
||||
tpRuleDO.setType(0);
|
||||
return tpRuleDAO.insertSelective(tpRuleDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updatePenaltyRule(TpPenaltyRuleUpdateRequest request) {
|
||||
TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
|
||||
return tpRuleDAO.updateSelective(tpRuleDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TpPenaltyRuleDetailVO penaltyRuleDetail(Long ruleId) {
|
||||
TpRuleDO tpRuleDO = tpRuleDAO.getById(ruleId);
|
||||
if (Objects.nonNull(tpRuleDO)) {
|
||||
TpPenaltyRuleDetailVO vo = BeanUtil.toBean(tpRuleDO, TpPenaltyRuleDetailVO.class);
|
||||
dictService.fillDictField(vo);
|
||||
return vo;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<TpPenaltyRuleListVO> penaltyRulePage(TpRuleQueryRequest request) {
|
||||
PageHelper.startPage(request.getPageNum(), request.getPageSize());
|
||||
List<TpRuleDO> list = tpRuleDAO.getList(request);
|
||||
PageInfo<TpRuleDO> page = new PageInfo<>(list);
|
||||
PageInfo<TpPenaltyRuleListVO> newPage = BeanUtil.toPage(page, TpPenaltyRuleListVO.class);
|
||||
dictService.fillDictField(newPage.getList());
|
||||
return newPage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean insertRewardRule(TpRewardRuleUpdateRequest request) {
|
||||
TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
|
||||
tpRuleDO.setRuleCode(TpHelper.generateRewardRuleCode());
|
||||
tpRuleDO.setType(1);
|
||||
return tpRuleDAO.insertSelective(tpRuleDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateRewardRule(TpRewardRuleUpdateRequest request) {
|
||||
TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
|
||||
return tpRuleDAO.updateSelective(tpRuleDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TpRewardRuleDetailVO rewardRuleDetail(Long ruleId) {
|
||||
TpRuleDO tpRuleDO = tpRuleDAO.getById(ruleId);
|
||||
if (Objects.nonNull(tpRuleDO)) {
|
||||
TpRewardRuleDetailVO vo = BeanUtil.toBean(tpRuleDO, TpRewardRuleDetailVO.class);
|
||||
dictService.fillDictField(vo);
|
||||
return vo;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<TpRewardRuleDetailVO> rewardRulePage(TpRuleQueryRequest request) {
|
||||
PageHelper.startPage(request.getPageNum(), request.getPageSize());
|
||||
List<TpRuleDO> list = tpRuleDAO.getList(request);
|
||||
PageInfo<TpRuleDO> page = new PageInfo<>(list);
|
||||
PageInfo<TpRewardRuleDetailVO> newPage = BeanUtil.toPage(page, TpRewardRuleDetailVO.class);
|
||||
dictService.fillDictField(newPage.getList());
|
||||
return newPage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteRuleByIds(List<Long> ruleIds) {
|
||||
return tpRuleDAO.deleteByIds(ruleIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean enableRuleByIds(List<Long> ruleIds, Integer enable) {
|
||||
return tpRuleDAO.updateStatus(ruleIds, enable);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user