MesClientUpgradeController.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package com.jeesite.modules.mes.web;
  2. import com.jeesite.common.collect.ListUtils;
  3. import com.jeesite.common.config.Global;
  4. import com.jeesite.common.entity.Page;
  5. import com.jeesite.common.lang.ObjectUtils;
  6. import com.jeesite.common.lang.StringUtils;
  7. import com.jeesite.common.mybatis.mapper.query.QueryType;
  8. import com.jeesite.common.web.BaseController;
  9. import com.jeesite.modules.file.entity.FileUpload;
  10. import com.jeesite.modules.file.service.FileUploadService;
  11. import com.jeesite.modules.mes.entity.MesClientUpgrade;
  12. import com.jeesite.modules.mes.entity.MesLine;
  13. import com.jeesite.modules.mes.entity.MesLineProcess;
  14. import com.jeesite.modules.mes.resp.CommonResp;
  15. import com.jeesite.modules.mes.service.MesClientUpgradeService;
  16. import com.jeesite.modules.mes.service.MesLineService;
  17. import com.jeesite.modules.mes.service.MesLineProcessService;
  18. import org.apache.shiro.authz.annotation.RequiresPermissions;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.ui.Model;
  22. import org.springframework.validation.annotation.Validated;
  23. import org.springframework.web.bind.annotation.ModelAttribute;
  24. import org.springframework.web.bind.annotation.PostMapping;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.ResponseBody;
  27. import javax.annotation.Resource;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. @Controller
  33. @RequestMapping(value = "${adminPath}/mes/mesClientUpgrade")
  34. public class MesClientUpgradeController extends BaseController {
  35. @Autowired
  36. private MesClientUpgradeService mesClientUpgradeService;
  37. @Autowired
  38. private MesLineProcessService mesLineProcessService;
  39. @Autowired
  40. private MesLineService mesLineService;
  41. @Resource
  42. private FileUploadService fileUploadService;
  43. @ModelAttribute
  44. public MesClientUpgrade get(String id, boolean isNewRecord) {
  45. return mesClientUpgradeService.get(id, isNewRecord);
  46. }
  47. @RequiresPermissions("mes:mesClient:view")
  48. @RequestMapping(value = {"list", ""})
  49. public String list(MesClientUpgrade mesClientUpgrade, Model model, HttpServletRequest request) {
  50. boolean lineProcessJar = "1".equals(request.getParameter("lineProcessJar"));
  51. String lineProcessId = request.getParameter("lineProcessId");
  52. fillLineProcessJarLineSn(mesClientUpgrade, lineProcessId);
  53. model.addAttribute("mesClientUpgrade", mesClientUpgrade);
  54. model.addAttribute("lineProcessJar", lineProcessJar);
  55. model.addAttribute("lineProcessId", lineProcessId);
  56. return "modules/mes/mesClientUpgradeList";
  57. }
  58. @RequiresPermissions("mes:mesClient:view")
  59. @RequestMapping(value = "listData")
  60. @ResponseBody
  61. public Page<MesClientUpgrade> listData(MesClientUpgrade mesClientUpgrade,
  62. HttpServletRequest request, HttpServletResponse response) {
  63. if ("1".equals(request.getParameter("lineProcessJar"))) {
  64. List<String> oprnos = fillLineProcessJarLineSn(mesClientUpgrade, request.getParameter("lineProcessId"));
  65. if (StringUtils.isNotBlank(mesClientUpgrade.getLineSn())) {
  66. mesClientUpgrade.getSqlMap().getWhere().and("a.line_sn", QueryType.EQ, mesClientUpgrade.getLineSn());
  67. mesClientUpgrade.setLineSn(null);
  68. }
  69. if (!oprnos.isEmpty()) {
  70. mesClientUpgrade.getSqlMap().getWhere().and("a.oprno", QueryType.IN, oprnos.toArray(new String[0]));
  71. mesClientUpgrade.setOprno(null);
  72. } else if (StringUtils.isNotBlank(mesClientUpgrade.getOprno())) {
  73. mesClientUpgrade.getSqlMap().getWhere().and("a.oprno", QueryType.EQ, mesClientUpgrade.getOprno());
  74. mesClientUpgrade.setOprno(null);
  75. }
  76. }
  77. mesClientUpgrade.setPage(new Page<>(request, response));
  78. return mesClientUpgradeService.findPage(mesClientUpgrade);
  79. }
  80. private List<String> fillLineProcessJarLineSn(MesClientUpgrade mesClientUpgrade, String lineProcessId) {
  81. List<String> oprnos = new ArrayList<>();
  82. if (StringUtils.isBlank(lineProcessId)) {
  83. return oprnos;
  84. }
  85. MesLineProcess lineProcess = mesLineProcessService.get(lineProcessId);
  86. if (lineProcess == null) {
  87. return oprnos;
  88. }
  89. if (StringUtils.isBlank(mesClientUpgrade.getLineSn())) {
  90. String lineSn = lineProcess.getLineSn();
  91. if (StringUtils.isBlank(lineSn) && StringUtils.isNotBlank(lineProcess.getLineId())) {
  92. MesLine line = mesLineService.get(lineProcess.getLineId());
  93. if (line != null) {
  94. lineSn = line.getSn();
  95. }
  96. }
  97. mesClientUpgrade.setLineSn(lineSn);
  98. }
  99. for (MesLineProcess child : findChildLineProcesses(lineProcessId)) {
  100. if (StringUtils.isNotBlank(child.getOprno())) {
  101. oprnos.add(child.getOprno());
  102. }
  103. }
  104. if (oprnos.isEmpty() && StringUtils.isNotBlank(lineProcess.getOprno())) {
  105. oprnos.add(lineProcess.getOprno());
  106. }
  107. return oprnos;
  108. }
  109. private List<MesLineProcess> findChildLineProcesses(String lineProcessId) {
  110. if (StringUtils.isBlank(lineProcessId)) {
  111. return ListUtils.newArrayList();
  112. }
  113. MesLineProcess childQuery = new MesLineProcess();
  114. childQuery.setPid(lineProcessId);
  115. return mesLineProcessService.findList(childQuery);
  116. }
  117. @RequiresPermissions("mes:mesClient:view")
  118. @RequestMapping(value = "form")
  119. public String form(MesClientUpgrade mesClientUpgrade, Model model, HttpServletRequest request) {
  120. String lineProcessId = request.getParameter("lineProcessId");
  121. boolean lineProcessJar = StringUtils.isNotBlank(lineProcessId);
  122. if (lineProcessJar) {
  123. fillLineProcessJarLineSn(mesClientUpgrade, lineProcessId);
  124. model.addAttribute("childLineProcessList", findChildLineProcesses(lineProcessId));
  125. }
  126. model.addAttribute("mesClientUpgrade", mesClientUpgrade);
  127. model.addAttribute("lineProcessJar", lineProcessJar);
  128. model.addAttribute("lineProcessId", lineProcessId);
  129. return "modules/mes/mesClientUpgradeForm";
  130. }
  131. @RequiresPermissions("mes:mesClient:edit")
  132. @PostMapping(value = "save")
  133. @ResponseBody
  134. public String save(@Validated MesClientUpgrade mesClientUpgrade) {
  135. mesClientUpgradeService.save(mesClientUpgrade);
  136. return renderResult(Global.TRUE, text("保存客户端升级版本成功!"));
  137. }
  138. @RequiresPermissions("mes:mesClient:edit")
  139. @RequestMapping(value = "delete")
  140. @ResponseBody
  141. public String delete(MesClientUpgrade mesClientUpgrade) {
  142. mesClientUpgradeService.delete(mesClientUpgrade);
  143. return renderResult(Global.TRUE, text("删除客户端升级版本成功!"));
  144. }
  145. @RequestMapping(value = "checkUpdate")
  146. @ResponseBody
  147. public CommonResp<ClientUpdateInfo> checkUpdate(HttpServletRequest request) {
  148. CommonResp<ClientUpdateInfo> resp = new CommonResp<>();
  149. String lineSn = trimToNull(request.getParameter("lineSn"));
  150. String oprno = trimToNull(request.getParameter("oprno"));
  151. //获取最大版本的实体
  152. MesClientUpgrade latest = mesClientUpgradeService.findLatestVersion(lineSn, oprno);
  153. if (ObjectUtils.isEmpty(latest)) {
  154. resp.setResult(Global.FALSE);
  155. resp.setMessage("no version record");
  156. return resp;
  157. }
  158. FileUpload fu = new FileUpload();
  159. fu.setBizKey(latest.getId());
  160. fu.setBizType("mesClientUpgrade_file");
  161. List<FileUpload> files = fileUploadService.findList(fu);
  162. if (ListUtils.isEmpty(files)) {
  163. resp.setResult(Global.FALSE);
  164. resp.setMessage("no downloadable file");
  165. return resp;
  166. }
  167. Long currentVersion = parseLong(request.getParameter("currentVersion"));
  168. FileUpload file = files.get(0);
  169. ClientUpdateInfo updateInfo = new ClientUpdateInfo();
  170. updateInfo.setCurrentVersion(currentVersion);
  171. updateInfo.setLatestVersion(latest.getVersion());
  172. updateInfo.setLineSn(latest.getLineSn());
  173. updateInfo.setOprno(latest.getOprno());
  174. updateInfo.setHasUpdate(currentVersion == null || latest.getVersion() != null && latest.getVersion() > currentVersion);
  175. updateInfo.setContent(latest.getContent());
  176. updateInfo.setDownloadUrl(buildFileUrl(request, file.getFileUrl()));
  177. updateInfo.setFileName(extractFileName(file.getFileUrl()));
  178. resp.setData(updateInfo);
  179. resp.setResult(Global.TRUE);
  180. return resp;
  181. }
  182. private String buildFileUrl(HttpServletRequest request, String fileUrl) {
  183. String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
  184. return host + request.getContextPath() + fileUrl;
  185. }
  186. private String trimToNull(String value) {
  187. if (value == null) {
  188. return null;
  189. }
  190. value = value.trim();
  191. return value.length() == 0 ? null : value;
  192. }
  193. private String extractFileName(String fileUrl) {
  194. if (fileUrl == null) {
  195. return null;
  196. }
  197. int index = fileUrl.lastIndexOf('/');
  198. return index >= 0 ? fileUrl.substring(index + 1) : fileUrl;
  199. }
  200. private Long parseLong(String value) {
  201. try {
  202. return ObjectUtils.isEmpty(value) ? null : Long.valueOf(value);
  203. } catch (NumberFormatException ex) {
  204. return null;
  205. }
  206. }
  207. public static class ClientUpdateInfo {
  208. private Long currentVersion;
  209. private Long latestVersion;
  210. private String lineSn;
  211. private String oprno;
  212. private boolean hasUpdate;
  213. private String content;
  214. private String downloadUrl;
  215. private String fileName;
  216. public Long getCurrentVersion() {
  217. return currentVersion;
  218. }
  219. public void setCurrentVersion(Long currentVersion) {
  220. this.currentVersion = currentVersion;
  221. }
  222. public Long getLatestVersion() {
  223. return latestVersion;
  224. }
  225. public void setLatestVersion(Long latestVersion) {
  226. this.latestVersion = latestVersion;
  227. }
  228. public String getLineSn() {
  229. return lineSn;
  230. }
  231. public void setLineSn(String lineSn) {
  232. this.lineSn = lineSn;
  233. }
  234. public String getOprno() {
  235. return oprno;
  236. }
  237. public void setOprno(String oprno) {
  238. this.oprno = oprno;
  239. }
  240. public boolean isHasUpdate() {
  241. return hasUpdate;
  242. }
  243. public void setHasUpdate(boolean hasUpdate) {
  244. this.hasUpdate = hasUpdate;
  245. }
  246. public String getContent() {
  247. return content;
  248. }
  249. public void setContent(String content) {
  250. this.content = content;
  251. }
  252. public String getDownloadUrl() {
  253. return downloadUrl;
  254. }
  255. public void setDownloadUrl(String downloadUrl) {
  256. this.downloadUrl = downloadUrl;
  257. }
  258. public String getFileName() {
  259. return fileName;
  260. }
  261. public void setFileName(String fileName) {
  262. this.fileName = fileName;
  263. }
  264. }
  265. }