@@ -8,6 +8,7 @@ import com.ai.da.common.constant.CommonConstant;
import com.ai.da.common.context.UserContext ;
import com.ai.da.common.enums.* ;
import com.ai.da.common.utils.* ;
import com.ai.da.mapper.primary.DesignBatchMapper ;
import com.ai.da.mapper.primary.DesignMapper ;
import com.ai.da.mapper.primary.GenerateDetailMapper ;
import com.ai.da.mapper.primary.TDesignPythonOutfitMapper ;
@@ -110,6 +111,9 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
@Resource
private RedisUtil redisUtil ;
@Resource
private DesignBatchMapper designBatchMapper ;
private final ConcurrentHashMap < String , Map < String , Object > > designContext = new ConcurrentHashMap < > ( ) ;
@@ -1623,14 +1627,6 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
collectionId = collectionService . saveCollection ( userInfo . getId ( ) , designDTO . getTimeZone ( ) , designDTO . getMoodTemplateId ( ) , designDTO . getMoodboardPostion ( ) ) ;
} else {
collectionId = collectionIdParam ;
// Collection byId = collectionService.getById(collectionIdParam);
// if (!designDTO.getMoodboardPostion().equals(byId.getMoodboardPosition())) {
// byId.setMoodboardPosition(designDTO.getMoodboardPostion());
// }
// if (!designDTO.getMoodTemplateId().equals(byId.getMoodTemplateId())) {
// byId.setMoodTemplateId(designDTO.getMoodTemplateId());
// }
// collectionService.updateById(byId);
}
List < Long > elementIds = getElementId ( elementVO ) ;
//批量关联element 到 collection
@@ -1662,7 +1658,20 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
startTime = System . currentTimeMillis ( ) ;
String requestId = UUID . randomUUID ( ) . toString ( ) ;
pythonObjects . setRequestId ( requestId ) ;
JSONObject responseJSONObject = pythonService . designBatch ( pythonObjects ) ;
AuthPrincipalVo userHolder = UserContext . getUserHolder ( ) ;
String taskId = pythonService . designBatch ( pythonObjects , userHolder . getId ( ) , elementVO . getDesignNum ( ) , requestId ) ;
DesignBatch designBatch = new DesignBatch ( ) ;
designBatch . setAccountId ( userInfo . getId ( ) ) ;
designBatch . setDesignId ( designId ) ;
designBatch . setCollectionId ( collectionId ) ;
designBatch . setTaskId ( taskId ) ;
designBatch . setCreateTime ( LocalDateTime . now ( ) ) ;
designBatch . setStatus ( 0 ) ;
designBatch . setTotalNum ( elementVO . getDesignNum ( ) ) ;
designBatchMapper . insert ( designBatch ) ;
endTime = System . currentTimeMillis ( ) ;
totalTimeInSeconds = ( endTime - startTime ) / 1000 ;
log . info ( " design python端运行时间: " + totalTimeInSeconds + " 秒 " ) ;
@@ -1688,11 +1697,181 @@ public class DesignServiceImpl extends ServiceImpl<DesignMapper, Design> impleme
context . put ( " requestIdList " , elementVO . getRequestIdList ( ) ) ;
// 将上下文存入全局设计上下文中
designContext . put ( request Id, context ) ;
designContext . put ( task Id, context ) ;
return taskId ;
}
@Override
public void processDesignBatch ( Map < String , Object > designBatchResult ) {
Object progress = designBatchResult . get ( " progress " ) ;
if ( progress instanceof String ) {
if ( progress . equals ( " 0/100 " ) ) {
return ;
}
if ( progress . equals ( " ok " ) ) {
String taskId = ( String ) designBatchResult . get ( " task_id " ) ;
QueryWrapper < DesignBatch > qw = new QueryWrapper < > ( ) ;
qw . lambda ( ) . eq ( DesignBatch : : getTaskId , taskId ) ;
List < DesignBatch > designBatches = designBatchMapper . selectList ( qw ) ;
if ( CollectionUtil . isNotEmpty ( designBatches ) ) {
DesignBatch designBatch = designBatches . get ( 0 ) ;
designBatch . setStatus ( 1 ) ;
designBatchMapper . updateById ( designBatch ) ;
}
}
} else {
String taskId = ( String ) designBatchResult . get ( " task_id " ) ;
QueryWrapper < DesignBatch > qw = new QueryWrapper < > ( ) ;
qw . lambda ( ) . eq ( DesignBatch : : getTaskId , taskId ) ;
List < DesignBatch > designBatches = designBatchMapper . selectList ( qw ) ;
if ( CollectionUtil . isNotEmpty ( designBatches ) ) {
DesignBatch designBatch = designBatches . get ( 0 ) ;
if ( designBatch . getCompletedNum ( ) = = null ) {
designBatch . setCompletedNum ( 1 ) ;
} else {
designBatch . setCompletedNum ( designBatch . getCompletedNum ( ) + 1 ) ;
}
designBatchMapper . updateById ( designBatch ) ;
}
Integer i = ( Integer ) progress ;
Map < String , Object > context ;
synchronized ( designContext ) {
context = designContext . get ( taskId ) ;
if ( context = = null ) {
log . error ( " 上下文数据缺失,无法完成操作 " ) ;
return ;
}
DesignPythonObjects pythonObjects = ( DesignPythonObjects ) context . get ( " pythonObjects " ) ;
Long designId = ( Long ) context . get ( " designId " ) ;
Long collectionId = ( Long ) context . get ( " collectionId " ) ;
AuthPrincipalVo userInfo = ( AuthPrincipalVo ) context . get ( " userInfo " ) ;
String timeZone = ( String ) context . get ( " timeZone " ) ;
String singleOverall = ( String ) context . get ( " singleOverall " ) ;
DesignPythonObject item = pythonObjects . getObjects ( ) . get ( i ) ;
DesignItem designItem = new DesignItem ( ) ;
designItem . setAccountId ( userInfo . getId ( ) ) ;
designItem . setCollectionId ( collectionId ) ;
designItem . setDesignId ( designId ) ;
designItem . setCreateDate ( DateUtil . getByTimeZone ( timeZone ) ) ;
//生成的八张图片
designItem . setDesignUrl ( item . getBasic ( ) . getSave_name ( ) ) ;
designItem . setHasLike ( ( byte ) 0 ) ;
//生成designItem
Long designItemId = designItemService . saveOne ( designItem ) ;
// python design返回入库及封装
JSONObject outfit = ( JSONObject ) designBatchResult . get ( " result " ) ;
if ( null = = outfit ) {
return ;
}
TDesignPythonOutfit designPythonOutfit = new TDesignPythonOutfit ( ) ;
designPythonOutfit . setDesignItemId ( designItemId ) ;
designPythonOutfit . setUserId ( userInfo . getId ( ) ) ;
designPythonOutfit . setDesignId ( designId ) ;
designPythonOutfit . setCollectionId ( collectionId ) ;
String synthesisUrl = outfit . getString ( " synthesis_url " ) ;
if ( ! StringUtils . isEmpty ( synthesisUrl ) ) {
designPythonOutfit . setDesignUrl ( synthesisUrl ) ;
} else {
throw new BusinessException ( " design.interface.exception " ) ;
}
designPythonOutfitService . save ( designPythonOutfit ) ;
JSONArray layers = outfit . getJSONArray ( " layers " ) ;
List < TDesignPythonOutfitDetail > list = new ArrayList < > ( ) ;
DesignCollectionItemVO designCollectionItemVO = new DesignCollectionItemVO ( ) ;
for ( int i1 = 0 ; i1 < layers . size ( ) ; i1 + + ) {
JSONObject jsonObject = layers . getJSONObject ( i1 ) ;
TDesignPythonOutfitDetail designPythonOutfitDetail = new TDesignPythonOutfitDetail ( ) ;
designPythonOutfitDetail . setDesignId ( designId ) ;
designPythonOutfitDetail . setDesignPythonOutfitId ( designPythonOutfit . getId ( ) ) ;
designPythonOutfitDetail . setPosition ( jsonObject . getString ( " position " ) ) ;
designPythonOutfitDetail . setImageSize ( jsonObject . getString ( " image_size " ) ) ;
designPythonOutfitDetail . setImageUrl ( jsonObject . getString ( " image_url " ) ) ;
if ( singleOverall . equals ( SingleOverallEnum . SINGLE . getRealName ( ) ) ) {
designCollectionItemVO . setDesignItemUrl ( designItem . getDesignUrl ( ) ) ;
}
designPythonOutfitDetail . setImageCategory ( jsonObject . getString ( " image_category " ) ) ;
designPythonOutfitDetail . setMaskUrl ( jsonObject . getString ( " mask_url " ) ) ;
designPythonOutfitDetail . setUserId ( userInfo . getId ( ) ) ;
designPythonOutfitDetail . setPriority ( Integer . parseInt ( jsonObject . getString ( " priority " ) ) ) ;
designPythonOutfitDetail . setCreateDate ( LocalDateTime . now ( ) ) ;
list . add ( designPythonOutfitDetail ) ;
}
designPythonOutfitDetailService . saveBatch ( list ) ;
designCollectionItemVO . setDesignItemId ( designItemId ) ;
designCollectionItemVO . setDesignItemUrl ( designItem . getDesignUrl ( ) ) ;
designCollectionItemVO . setDesignOutfitId ( designPythonOutfit . getId ( ) ) ;
String designUrl = designPythonOutfit . getDesignUrl ( ) ;
if ( ! StringUtils . isEmpty ( designUrl ) & & designUrl . contains ( " / " ) ) {
int firstIndex = designUrl . indexOf ( " / " ) ;
designCollectionItemVO . setDesignOutfitUrl ( minioUtil . getPreSignedUrl ( designUrl . substring ( 0 , firstIndex ) + " / " + designUrl . substring ( firstIndex + 1 ) , 24 * 60 ) ) ;
}
//response
// designCollectionItems.add(designCollectionItemVO);
List < DesignItemDetail > designItemDetails = Lists . newArrayList ( ) ;
Map < String , Integer > typePriority = list . stream ( ) . collect ( Collectors . toMap ( d - > d . getImageCategory ( ) . split ( " _ " ) [ 0 ] ,
d - > Math . abs ( d . getPriority ( ) ) ,
( existing , replacement ) - > replacement ) ) ;
Map < String , String > typeAndUndividedLayer = designItemService . setTypeAndUndividedLayer ( layers ) ;
for ( DesignPythonItem detail : item . getItems ( ) ) {
if ( null = = detail ) {
continue ;
}
DesignItemDetail designItemDetail = CopyUtil . copyObject ( detail , DesignItemDetail . class ) ;
designItemDetail . setAccountId ( userInfo . getId ( ) ) ;
designItemDetail . setDesignId ( designId ) ;
designItemDetail . setDesignItemId ( designItemId ) ;
designItemDetail . setCollectionElementId ( detail . getElementId ( ) ) ;
designItemDetail . setCreateDate ( DateUtil . getByTimeZone ( timeZone ) ) ;
designItemDetail . setUndividedLayer ( typeAndUndividedLayer . get ( designItemDetail . getType ( ) . toLowerCase ( ) ) ) ;
if ( SysFileLevel2TypeEnum . BODY . getRealName ( ) . equals ( detail . getType ( ) ) ) {
designItemDetail . setPath ( detail . getBody_path ( ) ) ;
//BODY不关联businessId
designItemDetail . setBusinessId ( 0L ) ;
}
designItemDetail . setIconPath ( detail . getIcon ( ) ) ;
designItemDetail . setPriority ( typePriority . get ( detail . getType ( ) . toLowerCase ( ) ) ) ;
if ( ! detail . getType ( ) . equals ( " Body " ) ) {
DesignPythonItemPrint printObject = detail . getPrint ( ) . getOverall ( ) ;
// designItemDetail.setPrintPath(Objects.isNull(printObject) ? "" : printObject.getPath());
designItemDetail . setPrintPath ( CollectionUtils . isEmpty ( printObject . getPrint_path_list ( ) ) ? " " : printObject . getPrint_path_list ( ) . get ( 0 ) ) ;
}
designItemDetailService . save ( designItemDetail ) ;
if ( ! SysFileLevel2TypeEnum . BODY . getRealName ( ) . equals ( detail . getType ( ) ) & & ! StringUtil . isNullOrEmpty ( designItemDetail . getPrintPath ( ) ) ) {
DesignItemDetailPrint print = new DesignItemDetailPrint ( ) ;
print . setDesignItemDetailId ( designItemDetail . getId ( ) ) ;
print . setPrintType ( " print " ) ;
print . setPath ( designItemDetail . getPrintPath ( ) ) ;
print . setSingleOrOverall ( " overall " ) ;
print . setPosition ( " [0.0,0.0] " ) ;
// print.setScale(1d);
// todo mark 将print默认scale置为0.3
print . setScale ( 0 . 3d ) ;
print . setAngle ( 0 . 0 ) ;
print . setPriority ( 1 ) ;
QueryWrapper < CollectionElement > getPrintboardLevel2TypeQw = new QueryWrapper < > ( ) ;
getPrintboardLevel2TypeQw . lambda ( ) . eq ( CollectionElement : : getUrl , print . getPath ( ) ) ;
getPrintboardLevel2TypeQw . lambda ( ) . orderByDesc ( CollectionElement : : getCreateDate ) ;
getPrintboardLevel2TypeQw . last ( " limit 1 " ) ;
CollectionElement one = collectionElementService . getOne ( getPrintboardLevel2TypeQw ) ;
print . setLevel2Type ( one . getLevel2Type ( ) ) ;
print . setCreateDate ( LocalDateTime . now ( ) ) ;
designItemDetailPrintService . save ( print ) ;
}
}
}
// for (int i = 0; i < pythonObjects.getObjects().size(); i++) {
//
// }
// response.setProcessId(pythonObjects.getProcess_id());
return ;
}
//保存python返回信息;保存designItem和detail
// return savePythonDesignItemAndDetail(pythonObjects, designId, collectionId, userInfo, designDTO.getTimeZone(), responseJSONObject, designDTO.getSingleOverall());
return requestId ;
}
}