From 7b3ff5b7c7bc669a3da292728c3a647de4fdac4f Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期二, 21 一月 2025 14:46:34 +0800 Subject: [PATCH] 修改流程 增加字典深度翻译 增加List翻译 --- lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/aspect/DncDictAspect.java | 80 +++++ lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/FieldQuery.java | 37 ++ lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/controller/AssignFileStreamController.java | 10 lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java | 27 + lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/ISysDictService.java | 12 lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/impl/AssignFileStreamServiceImpl.java | 11 lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/IAssignFileStreamService.java | 3 lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/DictExt.java | 38 ++ lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java | 39 ++ lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java | 65 --- lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml | 27 + lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/FieldBatchQuery.java | 135 +++++++++ lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/DicAspectService.java | 311 ++++++++++++++++++++++ 13 files changed, 718 insertions(+), 77 deletions(-) diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java index bbf19f1..a4ed593 100644 --- a/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java @@ -94,6 +94,12 @@ private Object parseDictText(Object result) { if (result instanceof Result) { if (((Result) result).getResult() instanceof IPage) { + List<JSONObject> items = new ArrayList<>(); + + //step.1 绛涢�夊嚭鍔犱簡 Dict 娉ㄨВ鐨勫瓧娈靛垪琛� + List<Field> dictFieldList = new ArrayList<>(); + // 瀛楀吀鏁版嵁鍒楄〃锛� key = 瀛楀吀code锛寁alue=鏁版嵁鍒楄〃 + Map<String, List<String>> dataListMap = new HashMap<>(5); //鍙栧嚭缁撴灉闆� List<Object> records=((IPage) ((Result) result).getResult()).getRecords(); //update-begin--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- @@ -101,58 +107,6 @@ if(!hasDict){ return result; } - List<JSONObject> dictText = getDictText(records); - ((IPage) ((Result) result).getResult()).setRecords(dictText); - } - else { - //鍙栧嚭缁撴灉闆� - Object object= (Object) ((Result) result).getResult(); - Class<?> aClass = ((Result) result).getResult().getClass(); - if ("java.util.HashMap".equals(aClass.getName())){ - //灏唌ap杞崲涓洪泦鍚堝湪杞崲涓簃ap - Map<String, Object> mapset = new HashMap<>(); - Map<String,Object> map= (Map<String,Object>) object; - for (String s : map.keySet()) { - Object a1 = map.get(s); - List<Object> records=new ArrayList<>(); - Class<?> aClass1 = a1.getClass(); - if ("java.util.ArrayList".equals(aClass1.getName())){ - records = (ArrayList)map.get(s); - }else{ - records = oConvertUtils.castList(a1, Object.class); - } - Boolean hasDict= checkHasDict(records); - if(!hasDict){ - return result; - } - List<JSONObject> dictText = getDictText(records); - mapset.put(s,dictText); - } - ((Result) result).setResult(mapset); - }else { - List<Object> records = oConvertUtils.castList(object, Object.class); - //update-begin--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- - Boolean hasDict= checkHasDict(records); - if(!hasDict){ - return result; - } - List<JSONObject> dictText = getDictText(records); - ((Result) result).setResult(dictText); - } - } - } - return result; - } - /** - 浼犲叆闆嗗悎 缈昏瘧瀛楀吀鍊� - **/ - private List<JSONObject> getDictText(List<Object> records){ - List<JSONObject> items = new ArrayList<>(); - //step.1 绛涢�夊嚭鍔犱簡 Dict 娉ㄨВ鐨勫瓧娈靛垪琛� - List<Field> dictFieldList = new ArrayList<>(); - // 瀛楀吀鏁版嵁鍒楄〃锛� key = 瀛楀吀code锛寁alue=鏁版嵁鍒楄〃 - Map<String, List<String>> dataListMap = new HashMap<>(5); - log.debug(" __ 杩涘叆瀛楀吀缈昏瘧鍒囬潰 DictAspect 鈥斺�� " ); //update-end--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- @@ -242,7 +196,12 @@ } } } - return items; + + ((IPage) ((Result) result).getResult()).setRecords(items); + } + + } + return result; } /** diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/DictExt.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/DictExt.java new file mode 100644 index 0000000..a8b69d0 --- /dev/null +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/DictExt.java @@ -0,0 +1,38 @@ +package org.jeecg.common.aspect.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 瀛楀吀娉ㄨВ鎵╁睍锛屾敮鎸佸瀛楁鍖归厤 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface DictExt { + + /** + * 鏁版嵁瀛楀吀琛� + * @return + */ + String dicTable() default ""; + + /** + * 鏁版嵁Text + * @return + */ + String dicText() default ""; + + /** + * 鏁版嵁code鏁扮粍 + * @return + */ + String[] dicCode(); + + /** + * 鏁版嵁鍙傛暟鏁扮粍锛屽弬鏁拌窡code鏁扮粍涓�涓�瀵瑰簲 + * @return + */ + String[] dicParams(); +} diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/FieldQuery.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/FieldQuery.java new file mode 100644 index 0000000..c8dc56b --- /dev/null +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/annotation/FieldQuery.java @@ -0,0 +1,37 @@ +package org.jeecg.common.aspect.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 瀛楀吀娉ㄨВ + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface FieldQuery{ + /** + * 鍏宠仈鏁版嵁琛� + * @return + */ + String dicTable() default ""; + + /** + * 鏁版嵁Text + * @return + */ + String dicText() default ""; + + /** + * 鏁版嵁code鏁扮粍 + * @return + */ + String[] dicCode(); + + /** + * 鏁版嵁鍙傛暟鏁扮粍锛屽弬鏁拌窡code鏁扮粍涓�涓�瀵瑰簲 + * @return + */ + String[] dicParams(); +} diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/controller/AssignFileStreamController.java b/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/controller/AssignFileStreamController.java index f42c233..fbec878 100644 --- a/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/controller/AssignFileStreamController.java +++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/controller/AssignFileStreamController.java @@ -1,5 +1,6 @@ package org.jeecg.modules.activiti.controller; +import com.baomidou.mybatisplus.core.metadata.IPage; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @@ -52,11 +53,10 @@ @AutoLog(value = "DNC娴佺▼鎿嶄綔-鑾峰彇褰撳墠鐢ㄦ埛鐨勫緟鍔炲強鍙嬀鍙栫殑浠诲姟") @ApiOperation(value = "DNC娴佺▼鎿嶄綔-鑾峰彇褰撳墠鐢ㄦ埛鐨勫緟鍔炲強鍙嬀鍙栫殑浠诲姟", notes = "DNC娴佺▼鎿嶄綔-鑾峰彇褰撳墠鐢ㄦ埛鐨勫緟鍔炲強鍙嬀鍙栫殑浠诲姟") @GetMapping("/find/task/list") - public QueryListResponseResult<ActTaskExt> getUndoTaskList() { - List<ActTaskExt> list = assignFileStreamService.getUndoTaskList(); - if(list == null) - list = Collections.emptyList(); - return new QueryListResponseResult<>(CommonCode.SUCCESS, list); + public Result<?> getUndoTaskList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) { + IPage<ActTaskExt> actTaskExtIPage = assignFileStreamService.getUndoTaskList(pageNo,pageSize); + return Result.ok(actTaskExtIPage); } @AutoLog(value = "DNC娴佺▼鎿嶄綔-鎵归噺鎸囨淳鍒拌澶�") diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/IAssignFileStreamService.java b/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/IAssignFileStreamService.java index a3689cb..5d755fb 100644 --- a/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/IAssignFileStreamService.java +++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/IAssignFileStreamService.java @@ -1,5 +1,6 @@ package org.jeecg.modules.activiti.service; +import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import org.jeecg.common.api.vo.Result; import org.jeecg.modules.activiti.entity.AssignFileStream; @@ -38,7 +39,7 @@ * 鑾峰彇褰撳墠鐢ㄦ埛鐨勫緟鍔炲強鍙嬀鍙栫殑浠诲姟 * @return */ - List<ActTaskExt> getUndoTaskList(); + IPage<ActTaskExt> getUndoTaskList(Integer pageNo, Integer pageSize); /** * 鍏宠仈鐢ㄦ埛琛� 鑾峰彇鐢ㄦ埛鏄电О diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/impl/AssignFileStreamServiceImpl.java b/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/impl/AssignFileStreamServiceImpl.java index 61d343c..82b3031 100644 --- a/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/impl/AssignFileStreamServiceImpl.java +++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/activiti/service/impl/AssignFileStreamServiceImpl.java @@ -312,7 +312,7 @@ ExceptionCast.cast(CommonCode.INVALID_PARAM); if(!ValidateUtil.validateString(stream.getDeviceId())) ExceptionCast.cast(ActivitiCode.ACT_ASSIGN_DEVICE_NONE); - LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); @@ -512,7 +512,7 @@ } @Override - public List<ActTaskExt> getUndoTaskList() { + public IPage<ActTaskExt> getUndoTaskList(Integer pageNo,Integer pageSize) { LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) @@ -537,7 +537,10 @@ ext.setAssignFileStream(streamDetail); extList.add(ext); }); - return extList; + //灏佽Page + IPage<ActTaskExt> page = new Page<>(pageNo,pageSize); + page.setRecords(extList); + return page; } @Override @@ -552,7 +555,7 @@ public boolean approveAssignFile(String taskId, String streamId, AssignFileStream stream) { if(!ValidateUtil.validateString(taskId) || !ValidateUtil.validateString(streamId) || stream == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); - LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/aspect/DncDictAspect.java b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/aspect/DncDictAspect.java new file mode 100644 index 0000000..6d1aee6 --- /dev/null +++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/aspect/DncDictAspect.java @@ -0,0 +1,80 @@ +package org.jeecg.modules.dnc.aspect; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.metadata.IPage; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.system.aspect.DicAspectService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import java.util.*; + +/** + * @Description: Dnc瀛楀吀aop绫� + * @Version: 1.0 + */ +@Aspect +@Component +@Slf4j +public class DncDictAspect { + @Autowired + private DicAspectService dicAspectService; + + // 瀹氫箟鍒囩偣Pointcut + @Pointcut("execution(public * org.jeecg.modules.dnc..*.*Controller.*(..))") + public void excudeService() { + } + + @Around("excudeService()") + public Object doAround(ProceedingJoinPoint pjp) throws Throwable { + long time1=System.currentTimeMillis(); + Object result = pjp.proceed(); + long time2=System.currentTimeMillis(); + log.debug("鑾峰彇JSON鏁版嵁 鑰楁椂锛�"+(time2-time1)+"ms"); + long start=System.currentTimeMillis(); + this.parseDictText(result); + long end=System.currentTimeMillis(); + log.debug("瑙f瀽娉ㄥ叆JSON鏁版嵁 鑰楁椂"+(end-start)+"ms"); + return result; + } + + /** + * 鏈柟娉曢拡瀵硅繑鍥炲璞′负Result 鐨処Page鐨勫垎椤靛垪琛ㄦ暟鎹繘琛屽姩鎬佸瓧鍏告敞鍏� + * 瀛楀吀娉ㄥ叆瀹炵幇 閫氳繃瀵瑰疄浣撶被娣诲姞娉ㄨВ@dict 鏉ユ爣璇嗛渶瑕佺殑瀛楀吀鍐呭,瀛楀吀鍒嗕负鍗曞瓧鍏竎ode鍗冲彲 锛宼able瀛楀吀 code table text閰嶅悎浣跨敤涓庡師鏉eecg鐨勭敤娉曠浉鍚� + * 绀轰緥涓篠ysUser 瀛楁涓簊ex 娣诲姞浜嗘敞瑙Dict(dicCode = "sex") 浼氬湪瀛楀吀鏈嶅姟绔嬮┈鏌ュ嚭鏉ュ搴旂殑text 鐒跺悗鍦ㄨ姹俵ist鐨勬椂鍊欏皢杩欎釜瀛楀吀text锛屽凡瀛楁鍚嶇О鍔燺dictText褰㈠紡杩斿洖鍒板墠绔� + * 渚嬭緭鍏ュ綋鍓嶈繑鍥炲�肩殑灏变細澶氬嚭涓�涓猻ex_dictText瀛楁 + * { + * sex:1, + * sex_dictText:"鐢�" + * } + * 鍓嶇鐩存帴鍙栧�約ext_dictText鍦╰able閲岄潰鏃犻渶鍐嶈繘琛屽墠绔殑瀛楀吀杞崲浜� + * customRender:function (text) { + * if(text==1){ + * return "鐢�"; + * }else if(text==2){ + * return "濂�"; + * }else{ + * return text; + * } + * } + * 鐩墠vue鏄繖涔堣繘琛屽瓧鍏告覆鏌撳埌table涓婄殑澶氫簡灏卞緢楹荤儲浜� 杩欎釜鐩存帴鍦ㄦ湇鍔$娓叉煋瀹屾垚鍓嶇鍙互鐩存帴鐢� + * @param result + */ + private void parseDictText(Object result) { + if (result instanceof Result) { + if (((Result) result).getResult() instanceof IPage) { + List<JSONObject> items = dicAspectService.detailDict(((IPage) ((Result) result).getResult()).getRecords()); + ((IPage) ((Result) result).getResult()).setRecords(items); + } + else if(((Result) result).getResult() instanceof List){ + List<JSONObject> items = dicAspectService.detailDict(((List) ((Result) result).getResult())); + ((Result) result).setResult(items); + } + } + } + +} diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/DicAspectService.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/DicAspectService.java new file mode 100644 index 0000000..2596820 --- /dev/null +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/DicAspectService.java @@ -0,0 +1,311 @@ +package org.jeecg.modules.system.aspect; + +import cn.hutool.core.util.ArrayUtil; +import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import cn.hutool.core.util.ReflectUtil; +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.aspect.annotation.Dict; +import org.jeecg.common.aspect.annotation.DictExt; +import org.jeecg.common.aspect.annotation.DictList; +import org.jeecg.common.aspect.annotation.FieldQuery; +import org.jeecg.common.constant.CommonConstant; +import org.jeecg.common.util.oConvertUtils; +import org.jeecg.modules.system.service.ISysDictService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.lang.reflect.Field; +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * 瀛楀吀瀛楁缈昏瘧鏈嶅姟 + * 鏀寔宓屽缈昏瘧 + * @since 2020/5/27 + */ +@Service +@Slf4j +public class DicAspectService { + @Autowired + private ISysDictService dictService; + + /** + * 鏈柟娉曢拡瀵瑰垪琛ㄦ暟鎹繘琛屽姩鎬佸瓧鍏告敞鍏� + * 瀛楀吀娉ㄥ叆瀹炵幇 閫氳繃瀵瑰疄浣撶被娣诲姞娉ㄨВ@dict 鏉ユ爣璇嗛渶瑕佺殑瀛楀吀鍐呭,瀛楀吀鍒嗕负鍗曞瓧鍏竎ode鍗冲彲 锛宼able瀛楀吀 code table text閰嶅悎浣跨敤涓庡師鏉eecg鐨勭敤娉曠浉鍚� + * 绀轰緥涓篠ysUser 瀛楁涓簊ex 娣诲姞浜嗘敞瑙Dict(dicCode = "sex") 浼氬湪瀛楀吀鏈嶅姟绔嬮┈鏌ュ嚭鏉ュ搴旂殑text 鐒跺悗鍦ㄨ姹俵ist鐨勬椂鍊欏皢杩欎釜瀛楀吀text锛屽凡瀛楁鍚嶇О鍔燺dictText褰㈠紡杩斿洖鍒板墠绔� + * 渚嬭緭鍏ュ綋鍓嶈繑鍥炲�肩殑灏变細澶氬嚭涓�涓猻ex_dictText瀛楁 + * { + * sex:1, + * sex_dictText:"鐢�" + * } + * 鍓嶇鐩存帴鍙栧�約ext_dictText鍦╰able閲岄潰鏃犻渶鍐嶈繘琛屽墠绔殑瀛楀吀杞崲浜� + * customRender:function (text) { + * if(text==1){ + * return "鐢�"; + * }else if(text==2){ + * return "濂�"; + * }else{ + * return text; + * } + * } + * 鐩墠vue鏄繖涔堣繘琛屽瓧鍏告覆鏌撳埌table涓婄殑澶氫簡灏卞緢楹荤儲浜� 杩欎釜鐩存帴鍦ㄦ湇鍔$娓叉煋瀹屾垚鍓嶇鍙互鐩存帴鐢� + * @param records + * @return + */ + public List<JSONObject> detailDict(List<Object> records){ + List<JSONObject> items = new ArrayList<>(); + FieldBatchQuery batchQuery = new FieldBatchQuery(); + boolean hasDict = false; + + for (Object record : records) { + JSONObject item = parseDictItem(record); + + Field[] fieldsArray = oConvertUtils.getAllFields(record); + for (int i = 0; i < fieldsArray.length; i++) { + Field field = fieldsArray[i]; + if (field.getAnnotation(Dict.class) != null && records.size()<=500) { + hasDict = true; + String code = field.getAnnotation(Dict.class).dicCode(); + String text = field.getAnnotation(Dict.class).dicText(); + String table = field.getAnnotation(Dict.class).dictTable(); + String key = String.valueOf(item.get(field.getName())); + + //缈昏瘧瀛楀吀鍊煎搴旂殑txt + String textValue = translateDictValue(code, text, table, key); + item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + } + // 瀛楀吀缈昏瘧鎵╁睍鍔熻兘锛屾敮鎸佸瓧鍏稿瀛楁鍖归厤 + else if(field.getAnnotation(DictExt.class) != null && records.size() <= 500){ + hasDict = true; + String[] code = field.getAnnotation(DictExt.class).dicCode(); + String text = field.getAnnotation(DictExt.class).dicText(); + String table = field.getAnnotation(DictExt.class).dicTable(); + String[] params = field.getAnnotation(DictExt.class).dicParams(); + String[] paramValues = this.getParamValues(item, params); + + String textValue = this.translateDictValue(code, text, table, paramValues); + item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + item.put(field.getName(), ArrayUtil.join(paramValues, ",")); + } + else if(field.getAnnotation(FieldQuery.class) != null && records.size() <= 500){ + FieldQuery ann = field.getAnnotation(FieldQuery.class); + batchQuery.put(ann.dicTable(), ann.dicText(), ann.dicCode(), + this.getParamValues(item, ann.dicParams())); + } + // 娣卞害缈昏瘧list鍏冪礌 + else if(field.getAnnotation(DictList.class) != null){ + Object fieldValue = ReflectUtil.getFieldValue(record, field); + if(fieldValue == null){ + // no-op + } + else if(null != fieldValue && fieldValue instanceof List){ + try{ + List<JSONObject> listDetail = this.detailDict((List)fieldValue); + item.put(field.getName(), listDetail); + }catch (Exception e){ + log.error("瀛楁缈昏瘧閿欒锛宨ndex=" + i + ",fieldName=" + field.getName() + ",fieldValue=" + fieldValue + ",record=" + record); + } + + }else{ + log.error("娣卞害瀛楀吀缈昏瘧鍙敮鎸丩ist绫诲瀷锛屽綋鍓嶇被鍨嬩负" + fieldValue.getClass()); + } + } + + //date绫诲瀷榛樿杞崲string鏍煎紡鍖栨棩鏈� + if (field.getType().getName().equals("java.util.Date")&&field.getAnnotation(JsonFormat.class)==null&&item.get(field.getName())!=null){ + SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName())))); + } + + } + + if(records.size()>500 && hasDict){ + log.error("浣跨敤 @Dict娉ㄨВ瓒呰繃500鏉¤褰曡浣跨敤鍒嗛〉鏌ヨ"); + } + items.add(item); + } + + // 鎵归噺鏌ヨ鍏宠仈鏁版嵁 + if(batchQuery.size() > 0){ + Map<String, List<Map>> batchResult = new HashMap<>(); + batchQuery.forEach((queryKey, queryParam) -> { + // 鎵归噺鏌ヨ + try { + queryParam.setQueryResult(this.batchQueryFieldValue(queryParam.getQueryTable(), + queryParam.getColumns(), + queryParam.getParamName(), + queryParam.getParamValues())); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + for (int i = 0; i < records.size(); i++) { + Object record = records.get(i); + JSONObject item = items.get(i); + + try { + this.fillItemValue(record, item, batchQuery); + } catch (Exception e) { + e.printStackTrace(); + log.info("鎵归噺鏌ヨ鏁版嵁濉厖鍑洪敊:{}", e.getMessage()); + } + } + } + + return items; + } + + /** + * 鍗曡〃鎵归噺鏌ヨ + * + * @param queryTable + * @param columns + * @param paramName + * @param paramValues + * @return + */ + private List<Map> batchQueryFieldValue(String queryTable, String[] columns, + String paramName, String[] paramValues) throws Exception { + try{ + return this.dictService.queryTableFieldByParams(queryTable, columns, paramName, paramValues); + }catch (Exception e){ + log.error("FieldQuery鏌ヨ閿欒", e); + throw new Exception("FieldQuery鏌ヨ閿欒"); + } + } + + /** + * 濉厖item鎵归噺鏌ヨ鏁版嵁 + * @param record + * @param item + * @param batchQuery + */ + private void fillItemValue(Object record, JSONObject item, FieldBatchQuery batchQuery) throws Exception{ + + for (Field field : oConvertUtils.getAllFields(record)) { + if(field.getAnnotation(FieldQuery.class) != null){ + FieldQuery fieldQuery = field.getAnnotation(FieldQuery.class); + + String[] paramValues = this.getParamValues(item, fieldQuery.dicParams()); + Object textValue = batchQuery.findResult(fieldQuery.dicTable(), + fieldQuery.dicCode(), paramValues, fieldQuery.dicText()); + + item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + item.put(field.getName(), ArrayUtil.join(paramValues, ",")); + } + } + } + + /** + * 瑙f瀽瀛楀吀Item + * @param record + * @return + */ + private JSONObject parseDictItem(Object record) { + ObjectMapper mapper = new ObjectMapper(); + String json="{}"; + try { + //瑙e喅@JsonFormat娉ㄨВ瑙f瀽涓嶄簡鐨勯棶棰樿瑙丼ysAnnouncement绫荤殑@JsonFormat + json = mapper.writeValueAsString(record); + } catch (JsonProcessingException e) { + log.error("json瑙f瀽澶辫触"+e.getMessage(),e); + } + return JSONObject.parseObject(json); + } + + /** + * 鑾峰彇鍙傛暟鏁板�� + * @param item + * @param paramNames + * @return + */ + private String[] getParamValues(JSONObject item, String[] paramNames) { + + // 鑾峰彇瀹為檯鍙傛暟 + String[] values = new String[paramNames.length]; + for (int i = 0; i < paramNames.length; i++) { + String param = paramNames[i]; + + if(param.startsWith("$")){ + values[i] = (String) item.get(param.substring(1)); + }else{ + values[i] = param; + } + + if(values[i] == null) return null; + if(values[i].indexOf("&&") > -1){ + log.error(">>>鍙傛暟["+values[i]+"]鍖呭惈闈炴硶瀛楃','"); + return null; + } + } + + return values; + } + + /** + * 缈昏瘧瀛楀吀鏂囨湰 + * + * @param code + * @param text + * @param table + * @param paramValues + * @return + */ + private String translateDictValue(String[] code, String text, String table, String[] paramValues) { + if(paramValues == null || paramValues.length <=0 || oConvertUtils.isEmpty(table)){ + return null; + } + + try{ + return this.dictService.queryTableDictByParams(table,text, + ArrayUtil.join(code, "&&"), + ArrayUtil.join(paramValues, "&&")); + }catch (Exception e){ + log.error("瀛楀吀缈昏瘧寮傚父table={},text={},code={},paramValues={}", table, text, ArrayUtil.join(code, "&&"),ArrayUtil.join(paramValues, "&&") ); + throw e; + } + + } + + /** + * 缈昏瘧瀛楀吀鏂囨湰 + * @param code + * @param text + * @param table + * @param key + * @return + */ + private String translateDictValue(String code, String text, String table, String key) { + if(oConvertUtils.isEmpty(key)) { + return null; + } + StringBuffer textValue=new StringBuffer(); + String[] keys = key.split(","); + for (String k : keys) { + String tmpValue = null; + if (k.trim().length() == 0) { + continue; //璺宠繃寰幆 + } + if (!StringUtils.isEmpty(table)){ + tmpValue= dictService.queryTableDictTextByKey(table,text,code,k.trim()); + }else { + tmpValue = dictService.queryDictTextByKey(code, k.trim()); + } + + if (tmpValue != null) { + if (!"".equals(textValue.toString())) { + textValue.append(","); + } + textValue.append(tmpValue); + } + + } + return textValue.toString(); + } + +} diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/FieldBatchQuery.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/FieldBatchQuery.java new file mode 100644 index 0000000..246db80 --- /dev/null +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/FieldBatchQuery.java @@ -0,0 +1,135 @@ +package org.jeecg.modules.system.aspect; + +import cn.hutool.core.util.ArrayUtil; + +import java.util.*; +import java.util.function.BiConsumer; + +/** + * 鎵归噺鏌ヨ鍣� + */ +public class FieldBatchQuery { + + // key=鏌ヨ瑕佺礌=table+paramName锛� value=鏌ヨ鏉′欢 + private Map<String, QueryParam> queryBatch = new HashMap<>(); + + /** + * 渚垮埄鏌ヨ瑕佺礌 + * @param action + */ + public void forEach(BiConsumer<? super String, ? super QueryParam> action) { + this.queryBatch.forEach(action); + } + + /** + * 鏌ヨ琛ㄦ暟閲� + * @return + */ + public int size() { + return queryBatch.size(); + } + + /** + * 鏌ユ壘鏌ヨ缁撴灉 + * @param tableName + * @param paramName + * @param paramValue + * @param column + * @return + */ + public Object findResult(String tableName, String[] paramName, String[] paramValue, String column) { + String queryKey = tableName + "()" + ArrayUtil.join(paramName, ","); + if(!this.queryBatch.containsKey(queryKey)) return null; + + return this.queryBatch.get(queryKey).findResult(paramValue, column); + } + + /** + * 淇濆瓨鏌ヨ鏉′欢 + * @param tableName 鏌ヨ琛ㄥ悕 + * @param textName 鏌ヨ瀛楁鍚嶇О + * @param paramName 鏌ヨ鍙傛暟鍚嶇О + * @param paramValue 鏌ヨ鍙傛暟鍊� + */ + public void put(String tableName, String textName, String[] paramName, String[] paramValue) { + String queryKey = tableName + "()" + ArrayUtil.join(paramName, ","); + + QueryParam queryParams; + if(this.queryBatch.containsKey(queryKey)){ + queryParams = this.queryBatch.get(queryKey); + }else{ + queryParams = new QueryParam(tableName, paramName); + this.queryBatch.put(queryKey, queryParams); + } + + // 娣诲姞鏌ヨ缁撴灉column鍚嶇О + queryParams.addTextName(textName); + // 娣诲姞鏌ヨ鍙傛暟 + queryParams.addParamValue(paramValue); + } + + public static class QueryParam { + private String queryTable; + private Set<String> columns = new HashSet<>(); + private Set<String> paramValueSet = new HashSet<>(); + private String[] paramName; + private List<Map> queryResult = new ArrayList<>(); + private Map<String, Integer> resultIndex = new HashMap<>(); + + public QueryParam(String tableName, String[] column) { + this.queryTable = tableName; + this.paramName = column; + } + + public void addTextName(String textName) { + this.columns.add(textName); + } + + public void addParamValue(String[] paramValue) { + paramValueSet.add(ArrayUtil.join(paramValue, ",")); + } + + public String getQueryTable() { + return queryTable; + } + + public String[] getColumns() { + String[] ret = new String[this.columns.size()]; + this.columns.toArray(ret); + return ret; + } + + public String getParamName() { + return "concat(" + ArrayUtil.join(this.paramName, ",',',") + ")"; + } + + public String[] getParamValues() { + String[] ret = new String[this.paramValueSet.size()]; + this.paramValueSet.toArray(ret); + return ret; + } + + public void setQueryResult(List<Map> queryResult) { + this.queryResult = queryResult; + } + + public Object findResult(String[] paramValue, String column) { + if(paramValue == null) return null; + String value = ArrayUtil.join(paramValue, ","); + + if(this.resultIndex.containsKey(value)){ + return this.queryResult.get(this.resultIndex.get(value)).get(column); + } + + for (int i = 0; i < this.queryResult.size(); i++) { + Map<String, Object> map = queryResult.get(i); + if(value.equals(map.get("paramValue"))){ + this.resultIndex.put(value, i); + return map.get(column); + } + } + + return null; + } + } +} diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java index 4a10ad1..2473019 100644 --- a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java @@ -25,7 +25,7 @@ * @since 2018-12-28 */ public interface SysDictMapper extends BaseMapper<SysDict> { - + /** * 閲嶅妫�鏌QL * @param duplicateCheckVo @@ -152,13 +152,13 @@ * @return */ public List<DictModel> queryAllDepartBackDictModel(); - + /** * 鏌ヨ鎵�鏈夌敤鎴� 浣滀负瀛楀吀淇℃伅 username -->value,realname -->text * @return */ public List<DictModel> queryAllUserBackDictModel(); - + // /** // * 閫氳繃鍏抽敭瀛楁煡璇㈠嚭瀛楀吀琛� // * @param table @@ -263,4 +263,25 @@ */ @Deprecated List<DictModel> queryTableDictByKeysAndFilterSql(@Param("table") String table, @Param("text") String text, @Param("code") String code, @Param("filterSql") String filterSql, @Param("codeValues") List<String> codeValues); + + /** + * 浣跨敤瀛怱ql鏌ヨ瀛楀吀鏁版嵁 + * @param table + * @param text + * @param subSql + * @return + */ + String queryTableDictTextBySubSql(@Param("table") String table, @Param("text") String text, @Param("subSql") String subSql); + + /** + * 鍗曡〃鎵归噺鏌ヨ + * @param queryTable + * @param columns + * @param paramName + * @param paramValues + * @return + */ + List<Map> queryTableFieldByParams(@Param("table") String queryTable, @Param("columns") String columns, @Param("paramName") String paramName, @Param("paramValues") String[] paramValues); + + } diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml index ef1ec1d..89bacbf 100644 --- a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml @@ -36,7 +36,7 @@ <!-- 閫氳繃瀛楀吀code鑾峰彇瀛楀吀鏁版嵁 --> <select id="queryDictTextByKey" parameterType="String" resultType="String"> - select s.item_text from sys_dict_item s + select s.item_text from sys_dict_item s where s.dict_id = (select id from sys_dict where dict_code = #{code}) and s.item_value = #{key} </select> @@ -66,7 +66,7 @@ <select id="queryTableDictItemsByCode" parameterType="String" resultType="org.jeecg.common.system.vo.DictModel"> select ${text} as "text",${code} as "value" from ${table} </select> - + <!--閫氳繃鏌ヨ鎸囧畾table鐨� text code 鑾峰彇瀛楀吀锛堟寚瀹氭煡璇㈡潯浠讹級--> <select id="queryTableDictItemsByCodeAndFilter" parameterType="String" resultType="org.jeecg.common.system.vo.DictModel"> select ${text} as "text",${code} as "value" from ${table} @@ -74,7 +74,7 @@ where ${filterSql} </if> </select> - + <!--閫氳繃鏌ヨ鎸囧畾table鐨� text code key 鑾峰彇瀛楀吀鍊�--> <select id="queryTableDictTextByKey" parameterType="String" resultType="String"> select ${text} as "text" from ${table} where ${code}= #{key} @@ -101,27 +101,27 @@ <select id="duplicateCheckCountSql" resultType="Long" parameterType="org.jeecg.modules.system.model.DuplicateCheckVo"> SELECT COUNT(*) FROM ${tableName} WHERE ${fieldName} = #{fieldVal} and id <> #{dataId} </select> - + <!-- 閲嶅鏍¢獙 sql璇彞 --> <select id="duplicateCheckCountSqlNoDataId" resultType="Long" parameterType="org.jeecg.modules.system.model.DuplicateCheckVo"> SELECT COUNT(*) FROM ${tableName} WHERE ${fieldName} = #{fieldVal} </select> - + <!-- 鏌ヨ閮ㄩ棬淇℃伅 浣滀负瀛楀吀鏁版嵁 --> <select id="queryAllDepartBackDictModel" resultType="org.jeecg.common.system.vo.DictModel"> select id as "value",depart_name as "text" from sys_depart where del_flag = '0' </select> - + <!-- 鏌ヨ鐢ㄦ埛淇℃伅 浣滀负瀛楀吀鏁版嵁 --> <select id="queryAllUserBackDictModel" resultType="org.jeecg.common.system.vo.DictModel"> select username as "value",realname as "text" from sys_user where del_flag = '0' </select> - + <!--閫氳繃鏌ヨ鎸囧畾table鐨� text code 鑾峰彇瀛楀吀鏁版嵁锛屼笖鏀寔鍏抽敭瀛楁煡璇� <select id="queryTableDictItems" parameterType="String" resultType="org.jeecg.common.system.vo.DictModel"> select ${text} as "text",${code} as "value" from ${table} where ${text} like #{keyword} </select> --> - + <!-- 鏍规嵁琛ㄥ悕銆佹樉绀哄瓧娈靛悕銆佸瓨鍌ㄥ瓧娈靛悕銆佺埗ID鏌ヨ鏍� --> <select id="queryTreeList" parameterType="Object" resultType="org.jeecg.modules.system.model.TreeSelectModel"> select ${text} as "title", @@ -213,7 +213,16 @@ and ${filterSql} </if> </select> - + <select id="queryTableDictTextBySubSql" resultType="java.lang.String"> + select ${text} as "text" from ${table} where ${subSql} + </select> + <select id="queryTableFieldByParams" resultType="java.util.Map"> + select ${columns},${paramName} as paramValue + from ${table} where ${paramName} in + <foreach item="key" collection="paramValues" open="(" separator="," close=")"> + #{key} + </foreach> + </select> </mapper> diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/ISysDictService.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/ISysDictService.java index 1680a88..1373ddb 100644 --- a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/ISysDictService.java +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/ISysDictService.java @@ -255,4 +255,16 @@ */ List<DictModel> loadDict(String dictCode, String keyword, Integer pageSize); + String queryTableDictByParams(String table, String text, String codes, String params); + + /** + * 鍗曡〃鎵归噺鏌ヨ + * @param queryTable + * @param columns + * @param paramName + * @param paramValues + * @return + */ + List<Map> queryTableFieldByParams(String queryTable, String[] columns, String paramName, String[] paramValues); + } diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java index 3776248..c9f52a2 100644 --- a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java @@ -1,5 +1,7 @@ package org.jeecg.modules.system.service.impl; +import cn.hutool.core.lang.Validator; +import cn.hutool.core.util.ArrayUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -153,7 +155,7 @@ log.debug("鏃犵紦瀛榙ictTableList鐨勬椂鍊欒皟鐢ㄨ繖閲岋紒"); return sysDictMapper.queryTableDictItemsByCodeAndFilter(table,text,code,filterSql); } - + /** * 閫氳繃鏌ヨ鎸囧畾table鐨� text code 鑾峰彇瀛楀吀鍊紅ext * dictTableCache閲囩敤redis缂撳瓨鏈夋晥鏈�10鍒嗛挓 @@ -282,7 +284,7 @@ public List<DictModel> queryAllUserBackDictModel() { return baseMapper.queryAllUserBackDictModel(); } - + // @Override // public List<DictModel> queryTableDictItems(String table, String text, String code, String keyword) { // return baseMapper.queryTableDictItems(table, text, code, "%"+keyword+"%"); @@ -475,4 +477,37 @@ } } + @Override + @Cacheable(value = CacheConstant.SYS_DICT_TABLE_CACHE) + public String queryTableDictByParams(String table, String text, String codes, String params) { + log.info("鏃犵紦瀛榙ictTable queryTableDictByKeys鐨勬椂鍊欒皟鐢ㄨ繖閲岋紒"); + String[] codeArr = codes.split("&&"); + String[] paramArr = params.split("&&"); + + String subSql = ""; + for (int i = 0; i < codeArr.length; i++) { + String column = codeArr[i]; + // 榛樿鍙傛暟涓虹┖瀛楃涓� + String param = paramArr.length > i ? paramArr[i] : ""; + if(column == null || ! Validator.isGeneral(column)){ + log.error("<<<闈炴硶瀛楁:" + column); + return null; + } + if(null == param || (!"-1".equals(param) && ! Validator.isGeneralWithChinese(param.replaceAll(" ", "")))){ + log.error("<<<闈炴硶鍙傛暟:" + param); + return null; + } + + if(i != 0) subSql += " and "; + subSql += column+" = '"+param+"'"; + } + return sysDictMapper.queryTableDictTextBySubSql(table,text,subSql); + } + + @Override + public List<Map> queryTableFieldByParams(String queryTable, String[] columns, String paramName, String[] paramValues) { + String column = ArrayUtil.join(columns, ","); + return this.baseMapper.queryTableFieldByParams(queryTable, column, paramName, paramValues); + } + } -- Gitblit v1.9.3