service.ftl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <#import "code/function.ftl" as func>
  2. <#assign package=table.variable.package>
  3. <#assign class=table.variable.class>
  4. <#assign subtables=table.subTableList>
  5. <#assign classVar=table.variable.classVar>
  6. package com.hotent.${system}.service.${package};
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.HashMap;
  11. import javax.annotation.Resource;
  12. import org.springframework.stereotype.Service;
  13. import com.hotent.core.db.IEntityDao;
  14. import com.hotent.core.service.GenericService;
  15. import com.hotent.core.util.BeanUtils;
  16. import com.hotent.${system}.model.${package}.${class};
  17. import com.hotent.${system}.dao.${package}.${class}Dao;
  18. import com.hotent.core.util.UniqueIdUtil;
  19. import net.sf.json.util.JSONUtils;
  20. import net.sf.ezmorph.object.DateMorpher;
  21. import com.hotent.core.bpm.model.ProcessCmd;
  22. import com.hotent.core.util.StringUtil;
  23. import net.sf.json.JSONObject;
  24. <#if subtables?size != 0>
  25. <#list subtables as subtable>
  26. import com.hotent.${system}.model.${subtable.variable.package}.${subtable.variable.class};
  27. import com.hotent.${system}.dao.${subtable.variable.package}.${subtable.variable.class}Dao;
  28. </#list>
  29. </#if>
  30. <#if flowKey?exists>
  31. import com.hotent.core.web.query.QueryFilter;
  32. import com.hotent.platform.model.bpm.ProcessRun;
  33. import com.hotent.platform.service.bpm.ProcessRunService;
  34. import com.hotent.core.service.WfBaseService;
  35. import com.hotent.core.annotion.WorkFlow;
  36. import com.hotent.platform.service.bpm.util.BpmAspectUtil;
  37. import com.hotent.core.bpm.BpmResult;
  38. import com.hotent.platform.service.bpm.BpmBusLinkService;
  39. <#else>
  40. import com.hotent.core.service.BaseService;
  41. </#if>
  42. @Service
  43. public class ${class}Service extends <#if flowKey?exists>WfBaseService<#else>BaseService</#if><${class}>
  44. {
  45. @Resource
  46. private ${class}Dao dao;
  47. <#if subtables?size != 0>
  48. <#list subtables as subtable>
  49. @Resource
  50. private ${subtable.variable.class}Dao ${subtable.variable.classVar}Dao;
  51. </#list>
  52. </#if>
  53. <#if flowKey?exists>
  54. @Resource
  55. private ProcessRunService processRunService;
  56. @Resource
  57. private BpmBusLinkService bpmBusLinkService;
  58. </#if>
  59. public ${class}Service()
  60. {
  61. }
  62. @Override
  63. protected IEntityDao<${class},Long> getEntityDao()
  64. {
  65. return dao;
  66. }
  67. <#if subtables?size != 0>
  68. /**
  69. * 根据外键删除子表记录
  70. * @param id
  71. */
  72. private void delByPk(Long id){
  73. <#list subtables as subtable>
  74. ${subtable.variable.classVar}Dao.delByMainId(id);
  75. </#list>
  76. }
  77. /**
  78. * 删除数据 包含相应子表记录
  79. * @param lAryId
  80. */
  81. public void delAll(Long[] lAryId) {
  82. for(Long id:lAryId){
  83. delByPk(id);
  84. dao.delById(id);
  85. <#if func.supportFlow(table)>
  86. //删除草稿表。
  87. processRunService.delByBusinessNum(id);
  88. </#if>
  89. }
  90. }
  91. /**
  92. * 添加数据
  93. * @param ${classVar}
  94. * @throws Exception
  95. */
  96. public void addAll(${class} ${classVar}) throws Exception{
  97. super.add(${classVar});
  98. addSubList(${classVar});
  99. }
  100. /**
  101. * 更新数据
  102. * @param ${classVar}
  103. * @throws Exception
  104. */
  105. public void updateAll(${class} ${classVar}) throws Exception{
  106. super.update(${classVar});
  107. delByPk(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>());
  108. addSubList(${classVar});
  109. }
  110. /**
  111. * 添加子表记录
  112. * @param ${classVar}
  113. * @throws Exception
  114. */
  115. public void addSubList(${class} ${classVar}) throws Exception{
  116. <#list subtables as subtable>
  117. <#assign vars=subtable.variable>
  118. <#assign foreignKey=func.convertUnderLine(subtable.relation) >
  119. <#assign subPk=func.getPk(subtable)>
  120. <#assign subPkVar=func.convertUnderLine(subPk)>
  121. List<${vars.class}> ${vars.classVar}List=${classVar}.get${vars.classVar?cap_first}List();
  122. if(BeanUtils.isNotEmpty(${vars.classVar}List)){
  123. for(${vars.class} ${vars.classVar}:${vars.classVar}List){
  124. ${vars.classVar}.set<#if subtable.isExternal==0>RefId<#else>${subtable.relation?cap_first}</#if>(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>());
  125. Long id=UniqueIdUtil.genId();
  126. ${vars.classVar}.set<#if subtable.isExternal==0>Id<#else>${subtable.pkField?cap_first}</#if>(id);
  127. ${vars.classVar}Dao.add(${vars.classVar});
  128. }
  129. }
  130. </#list>
  131. }
  132. <#list subtables as subtable>
  133. <#assign vars=subtable.variable>
  134. /**
  135. * 根据外键获得${subtable.tableDesc}列表
  136. * @param id
  137. * @return
  138. */
  139. public List<${vars.class}> get${vars.classVar?cap_first}List(Long id) {
  140. return ${vars.classVar}Dao.getByMainId(id);
  141. }
  142. </#list>
  143. </#if>
  144. <#if flowKey?exists>
  145. /**
  146. * 重写getAll方法绑定流程runId
  147. * @param queryFilter
  148. */
  149. public List<${class}> getAll(QueryFilter queryFilter){
  150. List<${class}> ${classVar}List=super.getAll(queryFilter);
  151. List<${class}> ${classVar}s=new ArrayList<${class}>();
  152. for(${class} ${classVar}:${classVar}List){
  153. ProcessRun processRun=processRunService.getByBusinessKey(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>().toString());
  154. if(BeanUtils.isNotEmpty(processRun)){
  155. ${classVar}.setRunId(processRun.getRunId());
  156. }
  157. ${classVar}s.add(${classVar});
  158. }
  159. return ${classVar}s;
  160. }
  161. </#if>
  162. /**
  163. * 流程处理器方法 用于处理业务数据
  164. * @param cmd
  165. * @throws Exception
  166. */
  167. public void processHandler(ProcessCmd cmd)throws Exception{
  168. Map data=cmd.getFormDataMap();
  169. if(BeanUtils.isNotEmpty(data)){
  170. String json=data.get("json").toString();
  171. ${class} ${classVar}=get${class}(json);
  172. if(StringUtil.isEmpty(cmd.getBusinessKey())){
  173. Long genId=UniqueIdUtil.genId();
  174. ${classVar}.set<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>(genId);
  175. <#if subtables?exists && subtables?size != 0>
  176. this.addAll(${classVar});
  177. <#else>
  178. this.add(${classVar});
  179. </#if>
  180. }else{
  181. ${classVar}.set<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>(Long.parseLong(cmd.getBusinessKey()));
  182. <#if subtables?exists && subtables?size != 0>
  183. this.updateAll(${classVar});
  184. <#else>
  185. this.update(${classVar});
  186. </#if>
  187. }
  188. cmd.setBusinessKey(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>().toString());
  189. }
  190. }
  191. /**
  192. * 根据json字符串获取${class}对象
  193. * @param json
  194. * @return
  195. */
  196. public ${class} get${class}(String json){
  197. JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher((new String[] { "yyyy-MM-dd" })));
  198. if(StringUtil.isEmpty(json))return null;
  199. JSONObject obj = JSONObject.fromObject(json);
  200. <#if subtables?exists && subtables?size != 0>
  201. Map<String, Class> map=new HashMap<String, Class>();
  202. <#list subtables as subtable>
  203. <#assign vars=subtable.variable>
  204. map.put("${vars.classVar}List", ${vars.class}.class);
  205. </#list>
  206. ${class} ${classVar} = (${class})JSONObject.toBean(obj, ${class}.class,map);
  207. <#else>
  208. ${class} ${classVar} = (${class})JSONObject.toBean(obj, ${class}.class);
  209. </#if>
  210. return ${classVar};
  211. }
  212. /**
  213. * 保存 ${table.tableDesc} 信息
  214. * @param ${classVar}
  215. */
  216. <#if flowKey?exists>
  217. @WorkFlow(flowKey="${flowKey}",tableName="${table.tableName}")
  218. </#if>
  219. public void save(${class} ${classVar}) throws Exception{
  220. Long id=${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>();
  221. if(id==null || id==0){
  222. id=UniqueIdUtil.genId();
  223. ${classVar}.set<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>(id);
  224. <#if subtables?size != 0>
  225. this.addAll(${classVar});
  226. <#else>
  227. this.add(${classVar});
  228. </#if>
  229. <#if flowKey?exists>
  230. //添加到业务关联数据表
  231. bpmBusLinkService.add(${classVar}.get<#if table.isExternal==0>Id<#else>${table.pkField?cap_first}</#if>().toString(), null , true, "${table.tableName}");
  232. </#if>
  233. }
  234. else{
  235. <#if subtables?size != 0>
  236. this.updateAll(${classVar});
  237. <#else>
  238. this.update(${classVar});
  239. }
  240. </#if>
  241. }
  242. <#if flowKey?exists>
  243. BpmResult result=new BpmResult();
  244. //添加流程变量
  245. // result.addVariable("", "");
  246. result.setBusinessKey(id.toString());
  247. BpmAspectUtil.setBpmResult(result);
  248. </#if>
  249. }
  250. }