5757import java .time .LocalDateTime ;
5858import java .time .format .DateTimeFormatter ;
5959import java .util .ArrayList ;
60- import java .util .Arrays ;
6160import java .util .Collections ;
6261import java .util .HashMap ;
6362import java .util .HashSet ;
@@ -94,6 +93,8 @@ public class BlockServiceImpl implements BlockService {
9493 @ Autowired
9594 private BlockGroupBlockMapper blockGroupBlockMapper ;
9695
96+ private static final int DEFAULT_PLATFORM_ID = 1 ;
97+ private static final String DEFAULT_USER_ID = "1" ;
9798 /**
9899 * 查询表t_block所有数据
99100 *
@@ -120,8 +121,7 @@ public BlockDto queryBlockById(@Param("id") Integer id) {
120121 && blockDto .getLastBuildInfo ().get ("result" ) instanceof Boolean
121122 ? (Boolean ) blockDto .getLastBuildInfo ().get ("result" ) : Boolean .FALSE ;
122123 blockDto .setIsPublished (isPublished );
123- String createdBy = "1" ;
124- List <BlockGroup > groups = blockGroupMapper .findBlockGroupByBlockId (blockDto .getId (), createdBy );
124+ List <BlockGroup > groups = blockGroupMapper .findBlockGroupByBlockId (blockDto .getId (), DEFAULT_USER_ID );
125125 blockDto .setGroups (groups );
126126 return blockDto ;
127127 }
@@ -156,15 +156,20 @@ public Integer deleteBlockById(@Param("id") Integer id) {
156156 */
157157 @ Override
158158 public Result <BlockDto > updateBlockById (BlockParam blockParam , Integer appId ) {
159+ if (blockParam == null || blockParam .getId () == null ) {
160+ return Result .failed (ExceptionEnum .CM002 );
161+ }
159162 Block blockResult = blockMapper .queryBlockById (blockParam .getId ());
160-
163+ if (blockResult == null ) {
164+ return Result .failed (ExceptionEnum .CM001 );
165+ }
161166 if (!Objects .equals (blockResult .getAppId (), appId )) {
162167 return Result .failed (ExceptionEnum .CM007 );
163168 }
164169 // 把前端传参赋值给实体
165170 Block blocks = new Block ();
166171 BeanUtils .copyProperties (blockParam , blocks );
167- blocks .setOccupierBy (String . valueOf ( 1 ) );
172+ blocks .setOccupierBy (DEFAULT_USER_ID );
168173 if (blockParam .getLatestHistoryId () != null ) {
169174 blocks .setLatestHistoryId (blockParam .getLatestHistoryId ().getId ());
170175 }
@@ -180,10 +185,8 @@ public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) {
180185 return Result .success (blockDtoResult );
181186 }
182187
183- // 对接登录后获取用户id
184- String createdBy = "1" ;
185188 // 根据区块id获取区块所在分组
186- List <BlockGroup > blockGroups = blockGroupMapper .findBlockGroupByBlockId (blocks .getId (), createdBy );
189+ List <BlockGroup > blockGroups = blockGroupMapper .findBlockGroupByBlockId (blocks .getId (), DEFAULT_USER_ID );
187190 if (!blockGroups .isEmpty ()) {
188191 List <Integer > blockGroupIds = blockGroups .stream ().map (BlockGroup ::getId ).collect (Collectors .toList ());
189192 for (Integer id : blockGroupIds ) {
@@ -212,6 +215,9 @@ public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) {
212215 */
213216 @ Override
214217 public Result <BlockDto > createBlock (BlockParam blockParam ) {
218+ if (blockParam == null || blockParam .getLabel () == null ) {
219+ return Result .failed (ExceptionEnum .CM002 );
220+ }
215221 // 对接收到的参数occupier为对应的一个对象,进行特殊处理并重新赋值
216222 Block blocks = new Block ();
217223 if (blockParam .getOccupier () != null ) {
@@ -220,7 +226,7 @@ public Result<BlockDto> createBlock(BlockParam blockParam) {
220226 BeanUtils .copyProperties (blockParam , blocks );
221227 blocks .setIsDefault (false );
222228 blocks .setIsOfficial (false );
223- blocks .setPlatformId (1 ); // 新建区块给默认值
229+ blocks .setPlatformId (DEFAULT_PLATFORM_ID ); // 新建区块给默认值
224230
225231 int result = blockMapper .createBlock (blocks );
226232 if (result < 1 ) {
@@ -229,12 +235,17 @@ public Result<BlockDto> createBlock(BlockParam blockParam) {
229235 int id = blocks .getId ();
230236 BlockDto blocksResult = queryBlockById (id );
231237 List <Integer > groups = blockParam .getGroups ();
232- if (groups != null && !groups .isEmpty ()) {
233- Integer groupId = groups .get (0 ); // 强制类型转换
234- BlockGroupBlock blockGroupBlock = new BlockGroupBlock ();
235- blockGroupBlock .setBlockGroupId (groupId );
236- blockGroupBlock .setBlockId (id );
237- blockGroupBlockMapper .createBlockGroupBlock (blockGroupBlock );
238+ if (groups == null || groups .isEmpty ()) {
239+ return Result .success (blocksResult );
240+ }
241+
242+ Integer groupId = groups .get (0 ); // 强制类型转换
243+ BlockGroupBlock blockGroupBlock = new BlockGroupBlock ();
244+ blockGroupBlock .setBlockGroupId (groupId );
245+ blockGroupBlock .setBlockId (id );
246+ int groupResult = blockGroupBlockMapper .createBlockGroupBlock (blockGroupBlock );
247+ if (groupResult < 1 ) {
248+ return Result .failed (ExceptionEnum .CM001 );
238249 }
239250 return Result .success (blocksResult );
240251 }
@@ -428,16 +439,13 @@ public List<String> allTags() {
428439 @ SystemServiceLog (description = "getNotInGroupBlocks 获取不在分组内的区块 实现类" )
429440 @ Override
430441 public List <BlockDto > getNotInGroupBlocks (NotGroupDto notGroupDto ) {
431- // 获取缓存中的登录用户
432- int userId = 1 ;
433- User user = userMapper .queryUserById (userId );
434442 List <BlockDto > blocksList = blockMapper .findBlocksReturn (notGroupDto );
435443 if (blocksList == null || blocksList .isEmpty ()) {
436444 return blocksList ;
437445 }
438446
439447 for (BlockDto blockDto : blocksList ) {
440- List <BlockGroup > blockGroups = blockGroupMapper .findBlockGroupByBlockId (blockDto .getId (), String . valueOf ( userId ) );
448+ List <BlockGroup > blockGroups = blockGroupMapper .findBlockGroupByBlockId (blockDto .getId (), DEFAULT_USER_ID );
441449 blockDto .setGroups (blockGroups );
442450 }
443451 return blocksList .stream ()
@@ -449,15 +457,15 @@ public List<BlockDto> getNotInGroupBlocks(NotGroupDto notGroupDto) {
449457 }
450458 // 组过滤
451459 if (item .getGroups () != null && item .getGroups ().stream ()
452- .anyMatch (group -> group instanceof BlockGroup
453- && (( BlockGroup ) group ) .getId ().equals (notGroupDto .getGroupId ()))) {
460+ .anyMatch (group -> group != null
461+ && group .getId ().equals (notGroupDto .getGroupId ()))) {
454462 return false ;
455463 }
456464 // 公开范围过滤
457465 if (item .getPublicStatus () == Enums .Scope .FULL_PUBLIC .getValue ()) {
458466 return true ;
459467 }
460- return user != null && item .getPublicStatus () == Enums .Scope .PUBLIC_IN_TENANTS .getValue ();
468+ return item .getPublicStatus () == Enums .Scope .PUBLIC_IN_TENANTS .getValue ();
461469 })
462470 .collect (Collectors .toList ());
463471 }
@@ -491,6 +499,9 @@ public Result<BlockDto> getBlockByLabel(String label, Integer appId) {
491499 */
492500 @ Override
493501 public Result <BlockDto > deploy (BlockBuildDto blockBuildDto ) {
502+ if (blockBuildDto == null || blockBuildDto .getBlock () == null ) {
503+ return Result .failed (ExceptionEnum .CM002 );
504+ }
494505 Map <String , Object > content = blockBuildDto .getBlock ().getContent ();
495506 if (content .isEmpty ()) {
496507 return Result .failed (ExceptionEnum .CM204 );
@@ -502,45 +513,43 @@ public Result<BlockDto> deploy(BlockBuildDto blockBuildDto) {
502513 if (isHistory ) {
503514 return Result .failed (ExceptionEnum .CM205 );
504515 }
505- BlockParam blockParam = blockBuildDto .getBlock ();
506- List <I18nEntryDto > i18nList = i18nEntryMapper .findI18nEntriesByHostandHostType (id , "block" );
507- // 序列化国际化词条
508- SchemaI18n appEntries = i18nEntryService .formatEntriesList (i18nList );
509- BlockHistory blockHistory = new BlockHistory ();
510- blockParam .setCreatedTime (null );
511- blockParam .setLastUpdatedTime (null );
512- blockParam .setTenantId (null );
513- Map <String , Map <String , String >> i18n = new HashMap <>();
514- i18n .put ("zh_CN" , appEntries .getZhCn ());
515- i18n .put ("en_US" , appEntries .getEnUs ());
516-
517- blockParam .setI18n (i18n );
518- blockHistory .setIsPublic (blockParam .getPublic ());
519- BeanUtil .copyProperties (blockParam , blockHistory );
520- blockHistory .setRefId (id );
521- blockHistory .setVersion (blockBuildDto .getVersion ());
522- blockHistory .setMessage (blockBuildDto .getDeployInfo ());
523- Map <String , Object > buildInfo = new HashMap <>();
524- buildInfo .put ("result" , true );
525- buildInfo .put ("versions" , Arrays .asList (blockBuildDto .getVersion ()));
526- // 获取当前时间
527- LocalDateTime now = LocalDateTime .now ();
516+ try {
517+ BlockParam blockParam = blockBuildDto .getBlock ();
518+ List <I18nEntryDto > i18nList = i18nEntryMapper .findI18nEntriesByHostandHostType (id , "block" );
519+ // 序列化国际化词条
520+ SchemaI18n appEntries = i18nEntryService .formatEntriesList (i18nList );
521+ BlockHistory blockHistory = new BlockHistory ();
522+ blockParam .setCreatedTime (null );
523+ blockParam .setLastUpdatedTime (null );
524+ blockParam .setTenantId (null );
525+ Map <String , Map <String , String >> i18n = new HashMap <>();
526+ i18n .put ("zh_CN" , appEntries .getZhCn ());
527+ i18n .put ("en_US" , appEntries .getEnUs ());
528+
529+ blockParam .setI18n (i18n );
530+ blockHistory .setIsPublic (blockParam .getPublic ());
531+ BeanUtil .copyProperties (blockParam , blockHistory );
532+ blockHistory .setRefId (id );
533+ blockHistory .setVersion (blockBuildDto .getVersion ());
534+ blockHistory .setMessage (blockBuildDto .getDeployInfo ());
535+
536+ // 获取当前时间
537+ LocalDateTime now = LocalDateTime .now ();
538+ Map <String , Object > buildInfo = createBuildInfo (blockBuildDto .getVersion (), now );
539+ blockHistory .setBuildInfo (buildInfo );
540+ blockHistory .setId (null );
541+ int blockHistoryResult = blockHistoryMapper .createBlockHistory (blockHistory );
542+ if (blockHistoryResult < 1 ) {
543+ return Result .failed (ExceptionEnum .CM008 );
544+ }
545+ blockParam .setLastBuildInfo (buildInfo );
546+ blockParam .setLatestHistoryId (blockHistory );
547+ blockParam .setLatestVersion (blockHistory .getVersion ());
528548
529- // 使用自定义格式化输出
530- DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" );
531- String formattedDate = now .format (formatter );
532- buildInfo .put ("endTime" , formattedDate );
533- blockHistory .setBuildInfo (buildInfo );
534- blockHistory .setId (null );
535- int blockHistoryResult = blockHistoryMapper .createBlockHistory (blockHistory );
536- if (blockHistoryResult < 1 ) {
537- return Result .failed (ExceptionEnum .CM008 );
538- }
539- blockParam .setLastBuildInfo (buildInfo );
540- blockParam .setLatestHistoryId (blockHistory );
541- blockParam .setLatestVersion (blockHistory .getVersion ());
542-
543- return updateBlockById (blockParam , blockParam .getAppId ());
549+ return updateBlockById (blockParam , blockParam .getAppId ());
550+ } catch (Exception e ) {
551+ return Result .failed (ExceptionEnum .CM001 );
552+ }
544553 }
545554
546555 /**
@@ -619,10 +628,9 @@ public Result<List<Block>> listNew(String appId, String groupId) {
619628 }
620629 }
621630 List <Block > blocksList = new ArrayList <>();
622- String createdBy = "1" ; // 获取用户登录id
623631 // 如果有 groupId, 只查group下的block,以及自己创建的区块
624632 if (groupIdTemp != 0 ) {
625- blocksList = blockMapper .findBlockByBlockGroupId (groupIdTemp , createdBy );
633+ blocksList = blockMapper .findBlockByBlockGroupId (groupIdTemp , DEFAULT_USER_ID );
626634 return Result .success (blocksList );
627635 }
628636 // 如果没有 groupId
@@ -634,7 +642,7 @@ public Result<List<Block>> listNew(String appId, String groupId) {
634642 List <Block > appBlocks = blocksList ;
635643 // 通过createBy查询区块表数据
636644 Block blocks = new Block ();
637- blocks .setCreatedBy (createdBy );
645+ blocks .setCreatedBy (DEFAULT_USER_ID );
638646 List <Block > personalBlocks = queryBlockByCondition (blocks );
639647 List <Block > retBlocks = new ArrayList <>();
640648 // 合并 personalBlocks 和 appBlocks 数组
@@ -670,15 +678,16 @@ public Result<List<Block>> listNew(String appId, String groupId) {
670678 * @return the id
671679 */
672680 public int ensureBlockId (BlockParam blockParam ) {
681+ log .debug ("Ensuring block ID for label: {}" , blockParam .getLabel ());
673682 if (blockParam .getId () != null ) {
683+ log .debug ("Block ID already exists: {}" , blockParam .getId ());
674684 return blockParam .getId ();
675685 }
676686 // 查询当前用户信息
677- int userId = 86 ;
678687 Block queryBlock = new Block ();
679688 queryBlock .setLabel (blockParam .getLabel ());
680689 queryBlock .setFramework (blockParam .getFramework ());
681- queryBlock .setCreatedBy (String . valueOf ( userId ) );
690+ queryBlock .setCreatedBy (DEFAULT_USER_ID );
682691 List <Block > blockList = blockMapper .queryBlockByCondition (queryBlock );
683692 if (blockList .isEmpty ()) {
684693 createBlock (blockParam );
@@ -705,4 +714,20 @@ public boolean isHistoryExisted(Integer id, String version) {
705714 }
706715 return true ;
707716 }
717+
718+ /**
719+ * 创建构建信息
720+ *
721+ * @param version the id
722+ * @param buildTime the buildTime
723+ * @return buildInfo the buildInfo
724+ */
725+ private Map <String , Object > createBuildInfo (String version , LocalDateTime buildTime ) {
726+ Map <String , Object > buildInfo = new HashMap <>();
727+ buildInfo .put ("result" , true );
728+ buildInfo .put ("versions" , Collections .singletonList (version ));
729+ DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" );
730+ buildInfo .put ("endTime" , buildTime .format (formatter ));
731+ return buildInfo ;
732+ }
708733}
0 commit comments