parent
9fddf9c06b
commit
7f53bce8df
@ -0,0 +1,107 @@
|
||||
package com.service.impl; // 定义服务实现类包路径
|
||||
|
||||
import com.utils.StringUtil; // 导入字符串工具类
|
||||
import com.service.DictionaryService; // 导入字典服务接口
|
||||
import com.utils.ClazzDiff; // 导入类差异工具类
|
||||
import org.springframework.beans.BeanUtils; // 导入Spring Bean工具类
|
||||
import org.springframework.beans.factory.annotation.Autowired; // 导入自动装配注解
|
||||
import org.springframework.stereotype.Service; // 导入服务注解
|
||||
import java.lang.reflect.Field; // 导入反射Field类
|
||||
import java.util.*; // 导入常用集合类
|
||||
import com.baomidou.mybatisplus.plugins.Page; // 导入MyBatis-Plus分页组件
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl; // 导入MyBatis-Plus服务实现基类
|
||||
import org.springframework.transaction.annotation.Transactional; // 导入事务注解
|
||||
import com.utils.PageUtils; // 导入分页工具类
|
||||
import com.utils.Query; // 导入查询工具类
|
||||
import org.springframework.web.context.ContextLoader; // 导入上下文加载器
|
||||
import javax.servlet.ServletContext; // 导入Servlet上下文接口
|
||||
import javax.servlet.http.HttpServletRequest; // 导入HTTP请求接口
|
||||
import org.springframework.lang.Nullable; // 导入可空注解
|
||||
import org.springframework.util.Assert; // 导入断言工具
|
||||
import com.dao.DictionaryDao; // 导入字典数据访问接口
|
||||
import com.entity.DictionaryEntity; // 导入字典实体类
|
||||
import com.service.DictionaryService; // 导入字典服务接口
|
||||
import com.entity.view.DictionaryView; // 导入字典视图类
|
||||
|
||||
@Service("dictionaryService") // 声明为Spring服务组件
|
||||
@Transactional // 启用事务管理
|
||||
public class DictionaryServiceImpl extends ServiceImpl<DictionaryDao, DictionaryEntity> implements DictionaryService {
|
||||
|
||||
@Override // 重写分页查询方法
|
||||
public PageUtils queryPage(Map<String,Object> params) {
|
||||
Page<DictionaryView> page =new Query<DictionaryView>(params).getPage(); // 创建分页对象
|
||||
page.setRecords(baseMapper.selectListView(page,params)); // 设置分页记录
|
||||
return new PageUtils(page); // 返回分页结果
|
||||
}
|
||||
|
||||
public void dictionaryConvert(Object obj, HttpServletRequest request) {
|
||||
try {
|
||||
if (obj == null) return; // 空对象直接返回
|
||||
|
||||
List<String> fieldNameList = new ArrayList<>(); // 创建字段名列表
|
||||
Class tempClass = obj.getClass(); // 获取对象类
|
||||
while (tempClass !=null) {
|
||||
Field[] declaredFields = tempClass.getDeclaredFields(); // 获取所有声明字段
|
||||
for (Field f : declaredFields) {
|
||||
f.setAccessible(true); // 设置字段可访问
|
||||
if (f.getType().getName().equals("java.lang.Integer") && f.getName().contains("Types")) {
|
||||
fieldNameList.add(f.getName()); // 添加符合条件的字段名
|
||||
}
|
||||
}
|
||||
tempClass = tempClass.getSuperclass(); // 获取父类继续查找
|
||||
}
|
||||
|
||||
ServletContext servletContext = request.getServletContext(); // 获取Servlet上下文
|
||||
Map<String, Map<Integer, String>> dictionaryMap= (Map<String, Map<Integer, String>>) servletContext.getAttribute("dictionaryMap"); // 获取字典映射
|
||||
|
||||
for (String s : fieldNameList) {
|
||||
Field types = null; // 声明Types字段
|
||||
if(hasField(obj.getClass(),s)){
|
||||
types= obj.getClass().getDeclaredField(s); // 获取当前类字段
|
||||
}else{
|
||||
types=obj.getClass().getSuperclass().getDeclaredField(s); // 获取父类字段
|
||||
}
|
||||
Field value = obj.getClass().getDeclaredField(s.replace("Types", "Value")); // 获取Value字段
|
||||
types.setAccessible(true); // 设置可访问
|
||||
value.setAccessible(true); // 设置可访问
|
||||
|
||||
if (StringUtil.isNotEmpty(String.valueOf(types.get(obj)))) { // 值不为空
|
||||
int i = Integer.parseInt(String.valueOf(types.get(obj))); // 获取整数值
|
||||
char[] chars = s.toCharArray(); // 转换为字符数组
|
||||
StringBuffer sbf = new StringBuffer(); // 创建字符串缓冲区
|
||||
for(int b=0; b< chars.length; b++){
|
||||
char ch = chars[b]; // 获取当前字符
|
||||
if(ch <= 90 && ch >= 65){ // 大写字母处理
|
||||
sbf.append("_"); // 添加下划线
|
||||
ch += 32; // 转小写
|
||||
}
|
||||
sbf.append(ch); // 添加字符
|
||||
}
|
||||
String s2 = dictionaryMap.get(sbf.toString()).get(i); // 获取字典值
|
||||
value.set(obj, s2); // 设置字段值
|
||||
} else {
|
||||
new Exception("字典表赋值出现问题::::"+value.getName()); // 抛出异常
|
||||
value.set(obj, ""); // 设置空值
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace(); // 打印异常
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace(); // 打印异常
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); // 打印异常
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasField(Class c, String fieldName){
|
||||
Field[] fields = c.getDeclaredFields(); // 获取所有声明字段
|
||||
|
||||
for (Field f : fields) {
|
||||
if (fieldName.equals(f.getName())) { // 匹配字段名
|
||||
return true; // 返回存在
|
||||
}
|
||||
}
|
||||
|
||||
return false; // 返回不存在
|
||||
}
|
||||
}
|
Loading…
Reference in new issue