From 1b5a723592ea63e5eec0bdeef7855b9c40e9df71 Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期四, 03 七月 2025 17:46:35 +0800 Subject: [PATCH] art:三保-设计修改 --- lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/aspect/DicAspectService.java | 311 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 311 insertions(+), 0 deletions(-) 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..a00c0a5 --- /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 cn.hutool.core.util.ReflectUtil; +import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +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(); + } + +} -- Gitblit v1.9.3