controller.ftl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <#import "function.ftl" as func>
  2. <#assign package=model.variables.package>
  3. <#assign class=model.variables.class>
  4. <#assign system=vars.system>
  5. <#assign comment=model.tabComment>
  6. <#assign subtables=model.subTableList>
  7. <#assign classVar=model.variables.classVar>
  8. <#assign pk=func.getPk(model) >
  9. <#assign pkVar=func.convertUnderLine(pk) >
  10. package com.hotent.${system}.controller.${package};
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.List;
  14. import javax.annotation.Resource;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import com.hotent.core.annotion.Action;
  20. import org.springframework.web.servlet.ModelAndView;
  21. import com.hotent.core.util.BeanUtils;
  22. import com.hotent.core.util.UniqueIdUtil;
  23. import com.hotent.core.util.ContextUtil;
  24. import com.hotent.core.web.util.RequestUtil;
  25. import com.hotent.core.web.controller.BaseController;
  26. import com.hotent.core.web.query.QueryFilter;
  27. import com.hotent.core.util.StringUtil;
  28. import com.hotent.core.web.query.util.QueryUtil;
  29. import net.sf.ezmorph.object.DateMorpher;
  30. import net.sf.json.JSONObject;
  31. import net.sf.json.util.JSONUtils;
  32. import com.hotent.core.util.StringUtil;
  33. import com.hotent.platform.model.bus.BusQueryRule;
  34. import com.hotent.${system}.model.${package}.${class};
  35. import com.hotent.${system}.service.${package}.${class}Service;
  36. <#if subtables?exists && subtables?size != 0>
  37. <#list subtables as table>
  38. import com.hotent.${system}.model.${table.variables.package}.${table.variables.class};
  39. </#list>
  40. </#if>
  41. import com.hotent.core.web.ResultMessage;
  42. <#if model.variables.flowKey?exists>
  43. import com.hotent.platform.model.bpm.ProcessRun;
  44. import com.hotent.platform.service.bpm.ProcessRunService;
  45. import com.hotent.core.bpm.model.ProcessCmd;
  46. </#if>
  47. /**
  48. *<pre>
  49. * 对象功能:${comment} 控制器类
  50. <#if vars.company?exists>
  51. * 开发公司:${vars.company}
  52. </#if>
  53. <#if vars.developer?exists>
  54. * 开发人员:${vars.developer}
  55. </#if>
  56. * 创建时间:${date?string("yyyy-MM-dd HH:mm:ss")}
  57. *</pre>
  58. */
  59. @Controller
  60. @RequestMapping("/${system}/${package}/${classVar}/")
  61. public class ${class}Controller extends BaseController
  62. {
  63. @Resource
  64. private ${class}Service ${classVar}Service;
  65. <#if model.variables.flowKey?exists>
  66. @Resource
  67. private ProcessRunService processRunService;
  68. </#if>
  69. <#--直接绑定工作流生成-->
  70. <#if model.variables.flowKey?exists>
  71. private final String flowKey = "${model.variables.flowKey}"; //绑定流程定义
  72. </#if>
  73. /**
  74. * 添加或更新${comment}。
  75. * @param request
  76. * @param response
  77. * @param ${classVar} 添加或更新的实体
  78. * @param bindResult
  79. * @param viewName
  80. * @return
  81. * @throws Exception
  82. */
  83. @RequestMapping("save")
  84. @Action(description="添加或更新${comment}")
  85. public void save(HttpServletRequest request, HttpServletResponse response,${class} ${classVar}) throws Exception
  86. {
  87. String resultMsg=null;
  88. try{
  89. if(${classVar}.get${pkVar?cap_first}()==null||${classVar}.get${pkVar?cap_first}()==0){
  90. ${classVar}.set${pkVar?cap_first}(UniqueIdUtil.genId());
  91. <#if subtables?exists && subtables?size != 0>
  92. ${classVar}Service.addAll(${classVar});
  93. <#else>
  94. ${classVar}Service.add(${classVar});
  95. </#if>
  96. resultMsg=getText("添加","${comment}");
  97. }else{
  98. <#if subtables?exists && subtables?size != 0>
  99. ${classVar}Service.updateAll(${classVar});
  100. <#else>
  101. ${classVar}Service.update(${classVar});
  102. </#if>
  103. resultMsg=getText("更新","${comment}");
  104. }
  105. writeResultMessage(response.getWriter(),resultMsg,ResultMessage.Success);
  106. }catch(Exception e){
  107. writeResultMessage(response.getWriter(),resultMsg+","+e.getMessage(),ResultMessage.Fail);
  108. }
  109. }
  110. /**
  111. * 取得${comment}分页列表
  112. * @param request
  113. * @param response
  114. * @param page
  115. * @return
  116. * @throws Exception
  117. */
  118. @RequestMapping("list")
  119. @Action(description="查看${comment}分页列表")
  120. public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception
  121. {
  122. String displayTagId = "${classVar}Item";
  123. BusQueryRule busQueryRule = QueryUtil.getBusQueryRule(displayTagId,
  124. request);
  125. List<${class}> list=${classVar}Service.getAll(new QueryFilter(request,displayTagId),busQueryRule);
  126. return this.getAutoView()
  127. .addObject("${classVar}List",list)
  128. .addObject("busQueryRule",busQueryRule);
  129. }
  130. /**
  131. * 删除${comment}
  132. * @param request
  133. * @param response
  134. * @throws Exception
  135. */
  136. @RequestMapping("del")
  137. @Action(description="删除${comment}")
  138. public void del(HttpServletRequest request, HttpServletResponse response) throws Exception
  139. {
  140. String preUrl= RequestUtil.getPrePage(request);
  141. ResultMessage message=null;
  142. try{
  143. Long[] lAryId =RequestUtil.getLongAryByStr(request, "${pkVar}");
  144. <#if subtables?exists && subtables?size != 0>
  145. ${classVar}Service.delAll(lAryId);
  146. message=new ResultMessage(ResultMessage.Success,"删除${comment}及其从表成功!");
  147. <#else>
  148. ${classVar}Service.delByIds(lAryId);
  149. message=new ResultMessage(ResultMessage.Success, "删除${comment}成功!");
  150. </#if>
  151. }catch(Exception ex){
  152. message=new ResultMessage(ResultMessage.Fail, "删除失败" + ex.getMessage());
  153. }
  154. addMessage(message, request);
  155. response.sendRedirect(preUrl);
  156. }
  157. /**
  158. * 编辑${comment}
  159. * @param request
  160. * @param response
  161. * @throws Exception
  162. */
  163. @RequestMapping("edit")
  164. @Action(description="编辑${comment}")
  165. public ModelAndView edit(HttpServletRequest request) throws Exception
  166. {
  167. Long ${pkVar}=RequestUtil.getLong(request,"${pkVar}",<#if model.variables.flowDefKey?exists>RequestUtil.getLong(request,"id")<#else>0L</#if>);
  168. String returnUrl=RequestUtil.getPrePage(request);
  169. <#if model.variables.flowKey?exists>
  170. Long runId=0L;
  171. ProcessRun processRun=processRunService.getByBusinessKey(${pkVar}.toString());
  172. if(BeanUtils.isNotEmpty(processRun)){
  173. runId=processRun.getRunId();
  174. }
  175. </#if>
  176. ${class} ${classVar}=${classVar}Service.getById(${pkVar});
  177. <#if subtables?exists && subtables?size != 0>
  178. <#list subtables as table>
  179. <#assign vars=table.variables>
  180. List<${table.variables.class}> ${table.variables.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(${pkVar});
  181. </#list>
  182. </#if>
  183. return getAutoView().addObject("${classVar}",${classVar})
  184. <#if model.variables.flowKey?exists>
  185. .addObject("runId",runId)
  186. </#if>
  187. <#if subtables?exists && subtables?size != 0>
  188. <#list subtables as table>
  189. .addObject("${table.variables.classVar}List",${table.variables.classVar}List)
  190. </#list>
  191. </#if>
  192. .addObject("returnUrl",returnUrl);
  193. }
  194. /**
  195. * 取得${comment}明细
  196. * @param request
  197. * @param response
  198. * @return
  199. * @throws Exception
  200. */
  201. @RequestMapping("get")
  202. @Action(description="查看${comment}明细")
  203. public ModelAndView get(HttpServletRequest request, HttpServletResponse response) throws Exception
  204. {
  205. Long ${pkVar}=RequestUtil.getLong(request,"${pkVar}");
  206. ${class} ${classVar} = ${classVar}Service.getById(${pkVar});
  207. <#if model.variables.flowKey?exists>
  208. Long runId=0L;
  209. ProcessRun processRun=processRunService.getByBusinessKey(${pkVar}.toString());
  210. if(BeanUtils.isNotEmpty(processRun)){
  211. runId=processRun.getRunId();
  212. }
  213. </#if>
  214. <#if subtables?exists && subtables?size != 0>
  215. <#list subtables as table>
  216. <#assign vars=table.variables>
  217. List<${table.variables.class}> ${table.variables.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(${pkVar});
  218. </#list>
  219. return getAutoView().addObject("${classVar}",${classVar})
  220. <#if model.variables.flowKey?exists>
  221. .addObject("runId",runId)
  222. </#if>
  223. <#list subtables as table>
  224. .addObject("${table.variables.classVar}List",${table.variables.classVar}List)<#if !table_has_next>;</#if>
  225. </#list>
  226. <#else>
  227. return getAutoView().addObject("${classVar}", ${classVar})<#if model.variables.flowKey?exists>.addObject("runId",runId)</#if>;
  228. </#if>
  229. }
  230. <#if model.variables.flowKey?exists>
  231. /**
  232. * 流程url表单 绑定的表单明细
  233. * @param request
  234. * @param response
  235. * @return
  236. * @throws Exception
  237. */
  238. @RequestMapping("detail")
  239. @Action(description="表单明细")
  240. public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception
  241. {
  242. long ${pkVar}=RequestUtil.getLong(request,"${pkVar}");
  243. ${class} ${classVar} = ${classVar}Service.getById(${pkVar});
  244. <#if subtables?exists && subtables?size != 0>
  245. <#list subtables as table>
  246. <#assign vars=table.variables>
  247. List<${table.variables.class}> ${table.variables.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(${pkVar});
  248. </#list>
  249. return getAutoView().addObject("${classVar}",${classVar})
  250. <#list subtables as table>
  251. .addObject("${table.variables.classVar}List",${table.variables.classVar}List)<#if !table_has_next>;</#if>
  252. </#list>
  253. <#else>
  254. return getAutoView().addObject("${classVar}", ${classVar});
  255. </#if>
  256. }
  257. /**
  258. * 流程url表单 绑定的表单编辑页面
  259. * @param request
  260. * @param response
  261. * @return
  262. * @throws Exception
  263. */
  264. @RequestMapping("modify")
  265. public ModelAndView modify(HttpServletRequest request, HttpServletResponse response) throws Exception
  266. {
  267. long ${pkVar}=RequestUtil.getLong(request,"${pkVar}");
  268. ${class} ${classVar} = ${classVar}Service.getById(${pkVar});
  269. <#if subtables?exists && subtables?size != 0>
  270. <#list subtables as table>
  271. <#assign vars=table.variables>
  272. List<${table.variables.class}> ${table.variables.classVar}List=${classVar}Service.get${vars.classVar?cap_first}List(${pkVar});
  273. </#list>
  274. return getAutoView().addObject("${classVar}",${classVar})
  275. <#list subtables as table>
  276. .addObject("${table.variables.classVar}List",${table.variables.classVar}List)<#if !table_has_next>;</#if>
  277. </#list>
  278. <#else>
  279. return getAutoView().addObject("${classVar}", ${classVar});
  280. </#if>
  281. }
  282. /**
  283. * 启动流程
  284. * @param request
  285. * @param response
  286. * @return
  287. * @throws Exception
  288. */
  289. @RequestMapping("run")
  290. @Action(description="启动流程")
  291. public void run(HttpServletRequest request, HttpServletResponse response,${class} ${classVar}) throws Exception
  292. {
  293. long ${pkVar}=RequestUtil.getLong(request,"${pkVar}");
  294. Integer isList=RequestUtil.getInt(request, "isList",0);
  295. ProcessCmd processCmd=new ProcessCmd();
  296. processCmd.setFlowKey(flowKey);
  297. processCmd.setUserAccount(ContextUtil.getCurrentUser().getAccount());
  298. try {
  299. if(${pkVar}!=0L){
  300. processCmd.setBusinessKey(Long.toString(${pkVar}));
  301. processRunService.startProcess(processCmd);
  302. if(isList==0){
  303. <#if subtables?size==0>
  304. ${classVar}Service.update(${classVar});
  305. <#else>
  306. ${classVar}Service.updateAll(${classVar});
  307. </#if>
  308. }
  309. }else{
  310. Long genId=UniqueIdUtil.genId();
  311. processCmd.setBusinessKey(genId.toString());
  312. ${classVar}.set${pkVar?cap_first}(genId);
  313. processRunService.startProcess(processCmd);
  314. <#if subtables?size==0>
  315. ${classVar}Service.add(${classVar});
  316. <#else>
  317. ${classVar}Service.addAll(${classVar});
  318. </#if>
  319. }
  320. writeResultMessage(response.getWriter(), new ResultMessage(ResultMessage.Success, "启动流程成功"));
  321. } catch (Exception e) {
  322. writeResultMessage(response.getWriter(), new ResultMessage(ResultMessage.Fail, "启动流程失败"));
  323. }
  324. }
  325. </#if>
  326. }