service.impl.java.ftl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package ${package};
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.github.yulichang.base.MPJBaseServiceImpl;
  4. <#if isMulti>
  5. <#--子表遍历-->
  6. <#list childTables as childTable>
  7. import com.xjrsoft.module.${outputArea}.entity.${childTable.tableName?cap_first};
  8. import com.xjrsoft.module.${outputArea}.mapper.${childTable.tableName?cap_first}Mapper;
  9. </#list>
  10. </#if>
  11. import com.xjrsoft.module.${outputArea}.entity.${entityClass};
  12. import com.xjrsoft.module.${outputArea}.mapper.${entityClass}Mapper;
  13. import com.xjrsoft.module.${outputArea}.service.I${className}Service;
  14. import lombok.AllArgsConstructor;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. <#if databaseId?? && databaseId != "master">
  18. import com.baomidou.dynamic.datasource.annotation.DS;
  19. </#if>
  20. import java.util.List;
  21. import java.util.Objects;
  22. import java.util.stream.Collectors;
  23. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  24. /**
  25. * @title: ${tableComment}
  26. * @Author ${author}
  27. * @Date: ${date}
  28. * @Version 1.0
  29. */
  30. @Service
  31. @AllArgsConstructor
  32. <#if databaseId?? && databaseId != "master">
  33. @DS("${databaseId}")
  34. </#if>
  35. public class ${className}ServiceImpl extends ${isMulti?string("MPJBaseService","Service")}Impl<${entityClass}Mapper, ${entityClass}> implements I${className}Service {
  36. <#if isMulti>
  37. private final ${entityClass}Mapper ${className?uncap_first}${entityClass}Mapper;
  38. <#--子表遍历-->
  39. <#list childTables as childTable>
  40. private final ${childTable.tableName?cap_first}Mapper ${className?uncap_first}${childTable.tableName?cap_first}Mapper;
  41. </#list>
  42. @Override
  43. @Transactional(rollbackFor = Exception.class)
  44. public Boolean add(${entityClass} ${entityClass?uncap_first}) {
  45. ${className?uncap_first}${entityClass}Mapper.insert(${entityClass?uncap_first});
  46. <#--子表遍历-->
  47. <#list childTables as childTable>
  48. for (${childTable.tableName?cap_first} ${childTable.tableName} : ${entityClass?uncap_first}.get${childTable.tableName?cap_first}List()) {
  49. ${childTable.tableName}.set${childTable.relationField?cap_first}(${entityClass?uncap_first}.get${childTable.relationTableField?cap_first}());
  50. ${className?uncap_first}${childTable.tableName?cap_first}Mapper.insert(${childTable.tableName});
  51. }
  52. </#list>
  53. return true;
  54. }
  55. @Override
  56. @Transactional(rollbackFor = Exception.class)
  57. public Boolean update(${entityClass} ${entityClass?uncap_first}) {
  58. ${className?uncap_first}${entityClass}Mapper.updateById(${entityClass?uncap_first});
  59. <#--子表遍历-->
  60. <#list childTables as childTable>
  61. //********************************* ${childTable.tableName?cap_first} 增删改 开始 *******************************************/
  62. {
  63. // 查出所有子级的id
  64. List<${childTable.tableName?cap_first}> ${childTable.tableName}List = ${className?uncap_first}${childTable.tableName?cap_first}Mapper.selectList(Wrappers.lambdaQuery(${childTable.tableName?cap_first}.class).eq(${childTable.tableName?cap_first}::get${childTable.relationField?cap_first}, ${entityClass?uncap_first}.get${childTable.relationTableField?cap_first}()).select(${childTable.tableName?cap_first}::get${childTable.pkField?cap_first}));
  65. List<${childTable.pkType}> ${childTable.tableName}Ids = ${childTable.tableName}List.stream().map(${childTable.tableName?cap_first}::get${childTable.pkField?cap_first}).collect(Collectors.toList());
  66. //原有子表单 没有被删除的主键
  67. List<${childTable.pkType}> ${childTable.tableName}OldIds = ${entityClass?uncap_first}.get${childTable.tableName?cap_first}List().stream().map(${childTable.tableName?cap_first}::get${childTable.pkField?cap_first}).filter(Objects::nonNull).collect(Collectors.toList());
  68. //找到需要删除的id
  69. List<${childTable.pkType}> ${childTable.tableName}RemoveIds = ${childTable.tableName}Ids.stream().filter(item -> !${childTable.tableName}OldIds.contains(item)).collect(Collectors.toList());
  70. for (${childTable.tableName?cap_first} ${childTable.tableName} : ${entityClass?uncap_first}.get${childTable.tableName?cap_first}List()) {
  71. //如果不等于空则修改
  72. if (${childTable.tableName}.get${childTable.pkField?cap_first}() != null) {
  73. ${className?uncap_first}${childTable.tableName?cap_first}Mapper.updateById(${childTable.tableName});
  74. }
  75. //如果等于空 则新增
  76. else {
  77. //已经不存在的id 删除
  78. ${childTable.tableName}.set${childTable.relationField?cap_first}(${entityClass?uncap_first}.get${childTable.relationTableField?cap_first}());
  79. ${className?uncap_first}${childTable.tableName?cap_first}Mapper.insert(${childTable.tableName});
  80. }
  81. }
  82. //已经不存在的id 删除
  83. if(${childTable.tableName}RemoveIds.size() > 0){
  84. ${className?uncap_first}${childTable.tableName?cap_first}Mapper.deleteBatchIds(${childTable.tableName}RemoveIds);
  85. }
  86. }
  87. //********************************* ${childTable.tableName?cap_first} 增删改 结束 *******************************************/
  88. </#list>
  89. return true;
  90. }
  91. @Override
  92. @Transactional(rollbackFor = Exception.class)
  93. public Boolean delete(List<${pkType}> ids) {
  94. ${className?uncap_first}${entityClass}Mapper.deleteBatchIds(ids);
  95. <#--子表遍历-->
  96. <#list childTables as childTable>
  97. ${className?uncap_first}${childTable.tableName?cap_first}Mapper.delete(Wrappers.lambdaQuery(${childTable.tableName?cap_first}.class).in(${childTable.tableName?cap_first}::get${childTable.relationField?cap_first}, ids));
  98. </#list>
  99. return true;
  100. }
  101. </#if>
  102. }