JghdCtl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.xt.jygl.gkjcxx.jghd.ctl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.persistence.criteria.CriteriaBuilder;
  6. import javax.persistence.criteria.CriteriaQuery;
  7. import javax.persistence.criteria.Predicate;
  8. import javax.persistence.criteria.Root;
  9. import javax.persistence.criteria.CriteriaBuilder.In;
  10. import javax.servlet.http.HttpServletRequest;
  11. import org.springframework.beans.BeanUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.data.jpa.domain.Specification;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.ui.Model;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import cache.CacheSearchCondition;
  20. import com.fasterxml.jackson.core.JsonProcessingException;
  21. import com.xt.jygl.common.BaseWebCtl;
  22. import com.xt.jygl.common.Constants;
  23. import com.xt.jygl.common.GlobalData;
  24. import com.xt.jygl.common.SearchCondition;
  25. import com.xt.jygl.common.Utils;
  26. import com.xt.jygl.gkjcxx.gk.entity.GkEntity;
  27. import com.xt.jygl.gkjcxx.gk.service.GkService;
  28. import com.xt.jygl.gkjcxx.gq.service.GqService;
  29. import com.xt.jygl.gkjcxx.jghd.entity.JghdEntity;
  30. import com.xt.jygl.gkjcxx.jghd.service.JghdService;
  31. import com.xt.jygl.gkjcxx.jghd.vo.JghdVo;
  32. import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.service.GkjyrService;
  33. import com.xtframe.core.anon.auth.RequiresPermissions;
  34. import com.xtframe.core.base.ctl.WebJsonResult;
  35. import com.xtframe.core.support.SecurityMgr;
  36. import com.xtframe.sec.code.entity.CodeEntity;
  37. import com.xtframe.sec.common.BaseEntity;
  38. import com.xtframe.sec.common.QueryService;
  39. import com.xtframe.sec.common.SimplePageRequest;
  40. import com.xtframe.sec.utils.SecUtils;
  41. import com.xtframe.util.StringUtils;
  42. @Controller
  43. @RequestMapping("/jghd")
  44. public class JghdCtl extends BaseWebCtl {
  45. @Autowired
  46. private SecurityMgr securityMgr;
  47. @Autowired
  48. private QueryService query;
  49. @Autowired
  50. private JghdService jghdService;
  51. @Autowired
  52. private GkService gkService;
  53. @Autowired
  54. private GqService gqService;
  55. @Autowired
  56. private GkjyrService gkjyrService;
  57. /**
  58. * 一览页面
  59. * @param page 分页
  60. * @param model
  61. * @param request
  62. * @param szdid 所在地ID
  63. * @param szgkid 所在港口ID
  64. * @param hdmc 航道名称
  65. * @param hdlx
  66. * @param menuid
  67. * @return
  68. * @throws JsonProcessingException
  69. */
  70. @RequestMapping(value = "/main")
  71. @RequiresPermissions("jghd:main")
  72. public String main(SimplePageRequest page, Model model,final HttpServletRequest request, final String szdid, final String szgkid, final String hdmc, final String hdlx, String menuid) throws JsonProcessingException {
  73. final Map<String, Object> cacheSCmap = CacheSearchCondition.cacheSCmap(request,"jghd");
  74. Specification<JghdEntity> spec = new Specification<JghdEntity>() {
  75. // 查询条件
  76. @Override
  77. public Predicate toPredicate(Root<JghdEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
  78. List<Predicate> list = new ArrayList<Predicate>();
  79. if (StringUtils.isNotEmpty(szdid)) {
  80. list.add(cb.like(root.get("szd").get("id").as(String.class), "%" + szdid + "%"));
  81. }else if(isGET(request)){
  82. //list.add(cb.like(root.get("szd").get("id").as(String.class), "%" + SearchCondition.hdszdid + "%"));
  83. CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "id","szd","szdid");
  84. }
  85. if (StringUtils.isNotEmpty(szgkid)) {
  86. list.add(cb.like(root.get("szgk").get("id").as(String.class), "%" + szgkid + "%"));
  87. }else if(isGET(request)){
  88. //list.add(cb.like(root.get("szgk").get("id").as(String.class), "%" + SearchCondition.hdszgkid + "%"));
  89. CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "id","szgk","szgkid");
  90. }
  91. if (StringUtils.isNotEmpty(hdmc)) {
  92. list.add(cb.like(root.get("hdmc").as(String.class), "%" + hdmc + "%"));
  93. }else if(isGET(request)){
  94. CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, null,"hdmc");
  95. }
  96. if (StringUtils.isNotEmpty(hdlx)) {
  97. list.add(cb.like(root.get("hdlx").as(String.class), "%" + hdlx + "%"));
  98. }
  99. if (isJyr()) {
  100. //list.add(cb.like(root.get("ssjyr").get("id").as(String.class), "%" + getSsjyr().getId() + "%"));
  101. } else {
  102. In<String> in = cb.in(root.get("szgq").as(String.class));
  103. @SuppressWarnings("unchecked")
  104. List<String> gqlist = (List<String>) Utils.getSession().getAttribute("gqList");
  105. if (gqlist != null && gqlist.size() > 0) {
  106. for (int i = 0; i < gqlist.size(); i++) {
  107. in.value(gqlist.get(i));
  108. }
  109. list.add(in);
  110. }
  111. }
  112. list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORD_STATE_DELETE));
  113. return cb.and(list.toArray(new Predicate[] {}));
  114. }
  115. };
  116. if(page.getPage()<1){
  117. page.setPage(SearchCondition.hdpage);
  118. }
  119. page.setSort("id");
  120. page.setOrder("desc");
  121. Page<JghdEntity> list = query.findAll(spec, toPageRequest(page), JghdEntity.class);
  122. List<JghdVo> voList = new ArrayList<JghdVo>();
  123. for (JghdEntity entity : list) {
  124. JghdVo vo = new JghdVo();
  125. if (null != entity) {
  126. BeanUtils.copyProperties(entity, vo);
  127. vo.setSzdzw(entity.getSzd() != null ? entity.getSzd().getText() : "");
  128. vo.setSzgkzw(entity.getSzgk() != null ? entity.getSzgk().getGkmc() : "");
  129. voList.add(vo);
  130. }
  131. }
  132. if (isPOST(request)) {
  133. SearchCondition.hdpage=page.getPage();
  134. //cacheSCmap.put("hdpage", page.getPage());
  135. CacheSearchCondition.putCacheSCmap(request.getParameterMap(), cacheSCmap);
  136. }
  137. model.addAttribute("list", voList);
  138. model.addAttribute("szd", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "szdid"));
  139. model.addAttribute("szgk",CacheSearchCondition.cacheSCmapVal(cacheSCmap, "szgkid"));
  140. model.addAttribute("hdmc", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "hdmc"));
  141. model.addAttribute("hdlx", hdlx);
  142. model.addAttribute("menuid", menuid);
  143. // 翻页共通
  144. putPageInfo(model, page, list);
  145. return "gkjcxx/jghd/jghdmain";
  146. }
  147. @RequestMapping(value = "/delete")
  148. @RequiresPermissions("jghd:delete")
  149. @ResponseBody
  150. public WebJsonResult logicdelete(Model model, String id) {
  151. WebJsonResult wr = success();
  152. try {
  153. // 逻辑删除
  154. jghdService.logicDelete(id);
  155. } catch (Exception e) {
  156. wr.setSuccess(false);
  157. wr.setMessage("操作失败!");
  158. e.printStackTrace();
  159. }
  160. return wr;
  161. }
  162. /**
  163. * 保存信息
  164. * @param model
  165. * @param code
  166. * @param szdid
  167. * @param szgkid
  168. * @return
  169. */
  170. @RequestMapping(value = "/save")
  171. @RequiresPermissions("jghd:add")
  172. @ResponseBody
  173. public WebJsonResult save(Model model, JghdEntity code, String szdid, String szgkid) {
  174. // how:如果是新增直接保存,如果更新,先取出数据库中记录,然后更新页面传过来的字段
  175. // why:这样的话一些在前台无需显示的字段保证不会丢失,同时一个个字段设置有助于bug排查
  176. WebJsonResult wr = success();
  177. if (StringUtils.isEmpty(szdid)) {
  178. wr.setSuccess(false);
  179. wr.setMessage("请选择所在地");
  180. }
  181. if (StringUtils.isEmpty(szgkid)) {
  182. wr.setSuccess(false);
  183. wr.setMessage("请选择所在港口");
  184. }
  185. JghdEntity jghdEntity = new JghdEntity();
  186. try {
  187. if (!StringUtils.isEmpty(code.getId())) {
  188. // 修改
  189. jghdEntity = jghdService.findOne(code.getId());
  190. }
  191. GkEntity szgk = new GkEntity();
  192. szgk.setId(szgkid);
  193. jghdEntity.setSzgk(szgk);
  194. GkEntity gk = gkService.findOne(szgkid);
  195. if (gk != null) {
  196. jghdEntity.setSsgkglbm(gk.getSsgkglbm());
  197. }
  198. CodeEntity szd = new CodeEntity(szdid);
  199. jghdEntity.setSzd(szd);
  200. jghdEntity.setBz(code.getBz());
  201. jghdEntity.setHdcd(code.getHdcd());
  202. jghdEntity.setHddm(code.getHddm());
  203. jghdEntity.setHdkd(code.getHdkd());
  204. jghdEntity.setHdmc(code.getHdmc());
  205. jghdEntity.setHdqdmc(code.getHdqdmc());
  206. jghdEntity.setHdqdmm(code.getHdqdmm());
  207. jghdEntity.setHdss(code.getHdss());
  208. jghdEntity.setHdyxkd(code.getHdyxkd());
  209. jghdEntity.setHdzdss(code.getHdzdss());
  210. jghdEntity.setHdtgnl(code.getHdtgnl());
  211. jghdEntity.setSzgq(code.getSzgq());
  212. jghdService.save(jghdEntity);
  213. } catch (Exception e) {
  214. wr.setSuccess(false);
  215. wr.setMessage("操作失败!");
  216. e.printStackTrace();
  217. }
  218. return wr;
  219. }
  220. /**
  221. * 编辑
  222. * @param model
  223. * @param id
  224. * @return
  225. */
  226. @RequestMapping(value = "/edit")
  227. @RequiresPermissions("jghd:edit")
  228. public String edit(Model model, String id) {
  229. JghdEntity jghd = jghdService.findOne(id);
  230. JghdVo vo = new JghdVo();
  231. BeanUtils.copyProperties(jghd, vo);
  232. vo.setSzd(jghd.getSzd() != null ? jghd.getSzd().getId() : "");
  233. vo.setSzgk(jghd.getSzgk() != null ? jghd.getSzgk().getId() : "");
  234. vo.setSzdzw(jghd.getSzd() != null ? jghd.getSzd().getText() : "");
  235. vo.setSzgkzw(jghd.getSzgk() != null ? jghd.getSzgk().getGkmc() : "");
  236. model.addAttribute("record", vo);
  237. return "gkjcxx/jghd/jghdedit";
  238. }
  239. /**
  240. * 查看
  241. * @param model
  242. * @param id
  243. * @return
  244. */
  245. @RequestMapping(value = "/view")
  246. @RequiresPermissions("jghd:main")
  247. public String view(Model model, String id) {
  248. JghdVo vo = new JghdVo();
  249. JghdEntity jghd = jghdService.findOne(id);
  250. BeanUtils.copyProperties(jghd, vo);
  251. vo.setSzd(jghd.getSzd() != null ? jghd.getSzd().getId() : "");
  252. vo.setSzdzw(jghd.getSzd() != null ? jghd.getSzd().getText() : "");
  253. vo.setSzgk(jghd.getSzgk() != null ? jghd.getSzgk().getId() : "");
  254. vo.setSzgkzw(jghd.getSzgk() != null ? jghd.getSzgk().getGkmc() : "");
  255. String gqName = "";
  256. if (jghd.getSzgq() != null) {
  257. String[] szgqNameArray = jghd.getSzgq().split(",");
  258. for (String s : szgqNameArray) {
  259. gqName += gqService.findOne(s).getGqmc() + " ";
  260. }
  261. }
  262. vo.setSzgqzw(gqName);
  263. model.addAttribute("record", vo);
  264. return "gkjcxx/jghd/jghdview";
  265. }
  266. /**
  267. * 新增
  268. * @param model
  269. * @return
  270. */
  271. @RequestMapping(value = "/add")
  272. @RequiresPermissions("jghd:add")
  273. public String add(Model model) {
  274. if(SecUtils.getCurrUser() != null){
  275. List<CodeEntity> list = securityMgr.codeService().findValidCodesByGroupCodeAndCity(Constants.GROUP_CODE_SZD, GlobalData.CITY_CODE);
  276. model.addAttribute("sz",list.size()==1?list.get(0).getId():"");
  277. if(list.size()==1){
  278. List<GkEntity> gklist = gkService.findBySzd(list.get(0).getId());
  279. model.addAttribute("gk",gklist.size()==1?gklist.get(0).getId():"");
  280. }
  281. }
  282. return "gkjcxx/jghd/jghdadd";
  283. }
  284. }