DictionariesController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.zhcs.dt.controller.system.dictionaries;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import javax.annotation.Resource;
  10. import net.sf.json.JSONArray;
  11. import org.springframework.beans.propertyeditors.CustomDateEditor;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.web.bind.WebDataBinder;
  15. import org.springframework.web.bind.annotation.InitBinder;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import org.springframework.web.servlet.ModelAndView;
  20. import com.zhcs.dt.controller.base.BaseController;
  21. import com.zhcs.dt.entity.Page;
  22. import com.zhcs.dt.entity.system.Dictionaries;
  23. import com.zhcs.dt.service.system.dictionaries.DictionariesManager;
  24. import com.zhcs.dt.util.AppUtil;
  25. import com.zhcs.dt.util.Jurisdiction;
  26. import com.zhcs.dt.util.PageData;
  27. import com.zhcs.dt.util.Tools;
  28. /**
  29. * 说明:数据字典
  30. * 创建人:FH Q313596790
  31. * 创建时间:2015-12-16
  32. */
  33. @Controller
  34. @RequestMapping(value="/dictionaries")
  35. public class DictionariesController extends BaseController {
  36. String menuUrl = "dictionaries/list.do"; //菜单地址(权限用)
  37. @Resource(name="dictionariesService")
  38. private DictionariesManager dictionariesService;
  39. /**获取连级数据
  40. * @return
  41. */
  42. @RequestMapping(value="/getLevels")
  43. @ResponseBody
  44. public Object getLevels(){
  45. Map<String,Object> map = new HashMap<String,Object>();
  46. String errInfo = "success";
  47. PageData pd = new PageData();
  48. try{
  49. pd = this.getPageData();
  50. String DICTIONARIES_ID = pd.getString("DICTIONARIES_ID");
  51. DICTIONARIES_ID = Tools.isEmpty(DICTIONARIES_ID)?"0":DICTIONARIES_ID;
  52. List<Dictionaries> varList = dictionariesService.listSubDictByParentId(DICTIONARIES_ID); //用传过来的ID获取此ID下的子列表数据
  53. List<PageData> pdList = new ArrayList<PageData>();
  54. for(Dictionaries d :varList){
  55. PageData pdf = new PageData();
  56. pdf.put("DICTIONARIES_ID", d.getDICTIONARIES_ID());
  57. pdf.put("BIANMA", d.getBIANMA());
  58. pdf.put("NAME", d.getNAME());
  59. pdList.add(pdf);
  60. }
  61. map.put("list", pdList);
  62. } catch(Exception e){
  63. errInfo = "error";
  64. logger.error(e.toString(), e);
  65. }
  66. map.put("result", errInfo); //返回结果
  67. return AppUtil.returnObject(new PageData(), map);
  68. }
  69. /**保存
  70. * @param
  71. * @throws Exception
  72. */
  73. @RequestMapping(value="/save")
  74. public ModelAndView save() throws Exception{
  75. logBefore(logger, Jurisdiction.getUsername()+"新增Dictionaries");
  76. if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
  77. ModelAndView mv = this.getModelAndView();
  78. PageData pd = new PageData();
  79. pd = this.getPageData();
  80. pd.put("DICTIONARIES_ID", this.get32UUID()); //主键
  81. dictionariesService.save(pd);
  82. mv.addObject("msg","success");
  83. mv.setViewName("save_result");
  84. return mv;
  85. }
  86. /**
  87. * 删除
  88. * @param DICTIONARIES_ID
  89. * @param
  90. * @throws Exception
  91. */
  92. @RequestMapping(value="/delete")
  93. @ResponseBody
  94. public Object delete(@RequestParam String DICTIONARIES_ID) throws Exception{
  95. if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return null;} //校验权限
  96. logBefore(logger, Jurisdiction.getUsername()+"删除Dictionaries");
  97. Map<String,String> map = new HashMap<String,String>();
  98. PageData pd = new PageData();
  99. pd.put("DICTIONARIES_ID", DICTIONARIES_ID);
  100. String errInfo = "success";
  101. if(dictionariesService.listSubDictByParentId(DICTIONARIES_ID).size() > 0){//判断是否有子级,是:不允许删除
  102. errInfo = "false";
  103. }else{
  104. pd = dictionariesService.findById(pd); //根据ID读取
  105. if("yes".equals(pd.getString("YNDEL")))return null; //当禁止删除字段值为yes, 则禁止删除,只能从手动从数据库删除
  106. if(null != pd.get("TBSNAME") && !"".equals(pd.getString("TBSNAME"))){
  107. String TBFIELD = pd.getString("TBFIELD");
  108. if(Tools.isEmpty(TBFIELD))TBFIELD = "BIANMA"; //如果关联字段没有设置,就默认字段为 BIANMA
  109. pd.put("TBFIELD", TBFIELD);
  110. String[] table = pd.getString("TBSNAME").split(",");
  111. for(int i=0;i<table.length;i++){
  112. pd.put("thisTable", table[i]);
  113. try {
  114. if(Integer.parseInt(dictionariesService.findFromTbs(pd).get("zs").toString())>0){//判断是否被占用,是:不允许删除(去排查表检查字典表中的编码字段)
  115. errInfo = "false";
  116. break;
  117. }
  118. } catch (Exception e) {
  119. errInfo = "false2";
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. if("success".equals(errInfo)){
  126. dictionariesService.delete(pd); //执行删除
  127. }
  128. map.put("result", errInfo);
  129. return AppUtil.returnObject(new PageData(), map);
  130. }
  131. /**修改
  132. * @param
  133. * @throws Exception
  134. */
  135. @RequestMapping(value="/edit")
  136. public ModelAndView edit() throws Exception{
  137. logBefore(logger, Jurisdiction.getUsername()+"修改Dictionaries");
  138. if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
  139. ModelAndView mv = this.getModelAndView();
  140. PageData pd = new PageData();
  141. pd = this.getPageData();
  142. dictionariesService.edit(pd);
  143. mv.addObject("msg","success");
  144. mv.setViewName("save_result");
  145. return mv;
  146. }
  147. /**列表
  148. * @param page
  149. * @throws Exception
  150. */
  151. @RequestMapping(value="/list")
  152. public ModelAndView list(Page page) throws Exception{
  153. logBefore(logger, Jurisdiction.getUsername()+"列表Dictionaries");
  154. ModelAndView mv = this.getModelAndView();
  155. PageData pd = new PageData();
  156. pd = this.getPageData();
  157. String keywords = pd.getString("keywords"); //检索条件
  158. if(null != keywords && !"".equals(keywords)){
  159. pd.put("keywords", keywords.trim());
  160. }
  161. String DICTIONARIES_ID = null == pd.get("DICTIONARIES_ID")?"":pd.get("DICTIONARIES_ID").toString();
  162. if(null != pd.get("id") && !"".equals(pd.get("id").toString())){
  163. DICTIONARIES_ID = pd.get("id").toString();
  164. }
  165. pd.put("DICTIONARIES_ID", DICTIONARIES_ID); //上级ID
  166. page.setPd(pd);
  167. List<PageData> varList = dictionariesService.list(page); //列出Dictionaries列表
  168. mv.addObject("pd", dictionariesService.findById(pd)); //传入上级所有信息
  169. mv.addObject("DICTIONARIES_ID", DICTIONARIES_ID); //上级ID
  170. mv.setViewName("system/dictionaries/dictionaries_list");
  171. mv.addObject("varList", varList);
  172. mv.addObject("QX",Jurisdiction.getHC()); //按钮权限
  173. return mv;
  174. }
  175. /**
  176. * 显示列表ztree
  177. * @param model
  178. * @return
  179. */
  180. @RequestMapping(value="/listAllDict")
  181. public ModelAndView listAllDict(Model model,String DICTIONARIES_ID)throws Exception{
  182. ModelAndView mv = this.getModelAndView();
  183. PageData pd = new PageData();
  184. pd = this.getPageData();
  185. try{
  186. JSONArray arr = JSONArray.fromObject(dictionariesService.listAllDict("0"));
  187. String json = arr.toString();
  188. json = json.replaceAll("DICTIONARIES_ID", "id").replaceAll("PARENT_ID", "pId").replaceAll("NAME", "name").replaceAll("subDict", "nodes").replaceAll("hasDict", "checked").replaceAll("treeurl", "url");
  189. model.addAttribute("zTreeNodes", json);
  190. mv.addObject("DICTIONARIES_ID",DICTIONARIES_ID);
  191. mv.addObject("pd", pd);
  192. mv.setViewName("system/dictionaries/dictionaries_ztree");
  193. } catch(Exception e){
  194. logger.error(e.toString(), e);
  195. }
  196. return mv;
  197. }
  198. /**
  199. * 显示列表ztree (用于代码生成器引用数据字典)
  200. * @param model
  201. * @return
  202. */
  203. @RequestMapping(value="/listAllDictToCreateCode")
  204. public ModelAndView listAllDictToCreateCode(Model model)throws Exception{
  205. ModelAndView mv = this.getModelAndView();
  206. PageData pd = new PageData();
  207. pd = this.getPageData();
  208. try{
  209. JSONArray arr = JSONArray.fromObject(dictionariesService.listAllDictToCreateCode("0"));
  210. String json = arr.toString();
  211. json = json.replaceAll("DICTIONARIES_ID", "id").replaceAll("PARENT_ID", "pId").replaceAll("NAME", "name").replaceAll("subDict", "nodes").replaceAll("hasDict", "checked").replaceAll("treeurl", "url");
  212. model.addAttribute("zTreeNodes", json);
  213. mv.addObject("pd", pd);
  214. mv.setViewName("system/dictionaries/dictionaries_ztree_windows");
  215. } catch(Exception e){
  216. logger.error(e.toString(), e);
  217. }
  218. return mv;
  219. }
  220. /**去新增页面
  221. * @param
  222. * @throws Exception
  223. */
  224. @RequestMapping(value="/goAdd")
  225. public ModelAndView goAdd()throws Exception{
  226. ModelAndView mv = this.getModelAndView();
  227. PageData pd = new PageData();
  228. pd = this.getPageData();
  229. String DICTIONARIES_ID = null == pd.get("DICTIONARIES_ID")?"":pd.get("DICTIONARIES_ID").toString();
  230. pd.put("DICTIONARIES_ID", DICTIONARIES_ID); //上级ID
  231. mv.addObject("pds",dictionariesService.findById(pd)); //传入上级所有信息
  232. mv.addObject("DICTIONARIES_ID", DICTIONARIES_ID); //传入ID,作为子级ID用
  233. mv.setViewName("system/dictionaries/dictionaries_edit");
  234. mv.addObject("msg", "save");
  235. return mv;
  236. }
  237. /**去修改页面
  238. * @param
  239. * @throws Exception
  240. */
  241. @RequestMapping(value="/goEdit")
  242. public ModelAndView goEdit()throws Exception{
  243. ModelAndView mv = this.getModelAndView();
  244. PageData pd = new PageData();
  245. pd = this.getPageData();
  246. String DICTIONARIES_ID = pd.getString("DICTIONARIES_ID");
  247. pd = dictionariesService.findById(pd); //根据ID读取
  248. mv.addObject("pd", pd); //放入视图容器
  249. pd.put("DICTIONARIES_ID",pd.get("PARENT_ID").toString()); //用作上级信息
  250. mv.addObject("pds",dictionariesService.findById(pd)); //传入上级所有信息
  251. mv.addObject("DICTIONARIES_ID", pd.get("PARENT_ID").toString()); //传入上级ID,作为子ID用
  252. pd.put("DICTIONARIES_ID",DICTIONARIES_ID); //复原本ID
  253. mv.setViewName("system/dictionaries/dictionaries_edit");
  254. mv.addObject("msg", "edit");
  255. return mv;
  256. }
  257. /**判断编码是否存在
  258. * @return
  259. */
  260. @RequestMapping(value="/hasBianma")
  261. @ResponseBody
  262. public Object hasBianma(){
  263. Map<String,String> map = new HashMap<String,String>();
  264. String errInfo = "success";
  265. PageData pd = new PageData();
  266. try{
  267. pd = this.getPageData();
  268. if(dictionariesService.findByBianma(pd) != null){
  269. errInfo = "error";
  270. }
  271. } catch(Exception e){
  272. logger.error(e.toString(), e);
  273. }
  274. map.put("result", errInfo); //返回结果
  275. return AppUtil.returnObject(new PageData(), map);
  276. }
  277. @InitBinder
  278. public void initBinder(WebDataBinder binder){
  279. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  280. binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
  281. }
  282. }