controller.ftl 12 KB

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