1919import lombok .extern .slf4j .Slf4j ;
2020import org .apache .commons .collections4 .CollectionUtils ;
2121import org .apache .commons .lang3 .StringUtils ;
22- import org .apache .commons .lang3 .Strings ;
2322import org .apache .commons .lang3 .tuple .Pair ;
2423import org .springframework .stereotype .Service ;
2524import org .springframework .transaction .annotation .Transactional ;
2625
2726import java .util .*;
28- import java .util .concurrent .ExecutorService ;
29- import java .util .concurrent .Executors ;
30- import java .util .concurrent .Future ;
31- import java .util .concurrent .Semaphore ;
27+ import java .util .concurrent .*;
28+ import java .util .function .Function ;
29+ import java .util .stream .Collectors ;
3230
3331@ Service
3432@ Transactional (rollbackFor = Exception .class )
@@ -82,7 +80,7 @@ private MergeResult parallelBuildMergeResult(String taskId, ExportDTO exportPara
8280 List <ContractListResponse > dataList ,
8381 ExportFieldParam exportFieldParam ) {
8482 int size = dataList .size ();
85- var cacheMap = new HashMap <>();
83+ var cacheMap = new ConcurrentHashMap <>();
8684 List <List <Object >> mergeRowData = new ArrayList <>(size );
8785 List <int []> mergeRegions = new ArrayList <>();
8886
@@ -99,8 +97,7 @@ private MergeResult parallelBuildMergeResult(String taskId, ExportDTO exportPara
9997 // 获取数据库访问许可
10098 dbSemaphore .acquire ();
10199 try {
102- List <List <Object >> buildData = buildData (detail , exportFieldParam ,
103- exportParam .getExportMetas (), cacheMap );
100+ List <List <Object >> buildData = buildData (detail , exportFieldParam , exportParam .getExportMetas (), cacheMap );
104101 return Pair .of (idx , buildData );
105102 } finally {
106103 dbSemaphore .release (); // 确保释放
@@ -139,7 +136,7 @@ private MergeResult parallelBuildMergeResult(String taskId, ExportDTO exportPara
139136 .build ();
140137 }
141138
142- private List <List <Object >> buildData (ContractListResponse detail , ExportFieldParam exportFieldParam , List <FieldExportMeta > exportMetas , HashMap <Object , Object > cacheMap ) {
139+ private List <List <Object >> buildData (ContractListResponse detail , ExportFieldParam exportFieldParam , List <FieldExportMeta > exportMetas , Map <Object , Object > cacheMap ) {
143140 return buildDataWithSub (detail .getModuleFields (), exportFieldParam , exportMetas , getSystemFieldMap (detail , exportMetas ), cacheMap );
144141 }
145142
@@ -152,28 +149,39 @@ public LinkedHashMap<String, Object> getSystemFieldMap(ContractListResponse data
152149 systemFieldMap .put ("amount" , data .getAmount ());
153150 systemFieldMap .put ("alreadyPayAmount" , data .getAlreadyPayAmount ());
154151 systemFieldMap .put ("number" , data .getNumber ());
152+
155153 if (StringUtils .isNotBlank (data .getApprovalStatus ())) {
156154 systemFieldMap .put ("approvalStatus" , Translator .get ("contract.approval_status." + data .getApprovalStatus ().toLowerCase (), Locale .SIMPLIFIED_CHINESE ));
157155 }
158156 if (StringUtils .isNotBlank (data .getStage ())) {
159157 systemFieldMap .put ("stage" , Translator .get ("contract.stage." + data .getStage ().toLowerCase (), Locale .SIMPLIFIED_CHINESE ));
160158 }
159+
161160 systemFieldMap .put ("createUser" , data .getCreateUserName ());
162161 systemFieldMap .put ("createTime" , TimeUtils .getDateTimeStr (data .getCreateTime ()));
163162 systemFieldMap .put ("updateUser" , data .getUpdateUserName ());
164163 systemFieldMap .put ("updateTime" , TimeUtils .getDateTimeStr (data .getUpdateTime ()));
165164 systemFieldMap .put ("voidReason" , data .getVoidReason ());
166165
167- FieldExportMeta startTime = exportMetas .stream ().filter (field -> Strings .CI .equals (field .getBusinessKey (), "startTime" )).findFirst ().orElse (null );
168- if (startTime != null ) {
169- AbstractModuleFieldResolver customFieldResolver = ModuleFieldResolverFactory .getResolver (startTime .getField ().getType ());
170- systemFieldMap .put ("startTime" , customFieldResolver .transformToValue (startTime .getField (), String .valueOf (data .getStartTime ())));
171- }
172- FieldExportMeta endTime = exportMetas .stream ().filter (field -> Strings .CI .equals (field .getBusinessKey (), "endTime" )).findFirst ().orElse (null );
173- if (endTime != null ) {
174- AbstractModuleFieldResolver customFieldResolver = ModuleFieldResolverFactory .getResolver (endTime .getField ().getType ());
175- systemFieldMap .put ("endTime" , customFieldResolver .transformToValue (endTime .getField (), String .valueOf (data .getEndTime ())));
176- }
166+ // 将 exportMetas 转为 Map,避免重复遍历
167+ Map <String , FieldExportMeta > metaMap = exportMetas .stream ()
168+ .collect (Collectors .toMap (FieldExportMeta ::getBusinessKey , Function .identity (), (a , b ) -> a ));
169+
170+ resolveAndPutTimeField (systemFieldMap , metaMap , "startTime" , String .valueOf (data .getStartTime ()));
171+ resolveAndPutTimeField (systemFieldMap , metaMap , "endTime" , String .valueOf (data .getEndTime ()));
172+
177173 return systemFieldMap ;
178174 }
175+
176+ private void resolveAndPutTimeField (LinkedHashMap <String , Object > map ,
177+ Map <String , FieldExportMeta > metaMap ,
178+ String businessKey ,
179+ String rawValue ) {
180+ FieldExportMeta meta = metaMap .get (businessKey );
181+ if (meta != null ) {
182+ AbstractModuleFieldResolver resolver = ModuleFieldResolverFactory .getResolver (meta .getField ().getType ());
183+ map .put (businessKey , resolver .transformToValue (meta .getField (), rawValue ));
184+ }
185+ }
186+
179187}
0 commit comments