|
|
@@ -0,0 +1,168 @@
|
|
|
+package com.jeesite.modules.mes.web;
|
|
|
+
|
|
|
+import com.jeesite.common.collect.ListUtils;
|
|
|
+import com.jeesite.common.config.Global;
|
|
|
+import com.jeesite.common.lang.DateUtils;
|
|
|
+import com.jeesite.common.lang.StringUtils;
|
|
|
+import com.jeesite.common.service.ServiceException;
|
|
|
+import com.jeesite.common.utils.excel.ExcelExport;
|
|
|
+import com.jeesite.common.utils.excel.ExcelImport;
|
|
|
+import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
|
|
|
+import com.jeesite.common.web.BaseController;
|
|
|
+import com.jeesite.modules.mes.entity.excel.ExcelPost;
|
|
|
+import com.jeesite.modules.sys.entity.Post;
|
|
|
+import com.jeesite.modules.sys.service.PostService;
|
|
|
+import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping(value = "${adminPath}/sys/post")
|
|
|
+public class PostImportExportController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PostService postService;
|
|
|
+
|
|
|
+ @RequiresPermissions("sys:post:view")
|
|
|
+ @RequestMapping(value = "exportData")
|
|
|
+ public void exportData(Post post, HttpServletResponse response) {
|
|
|
+ List<ExcelPost> list = ListUtils.newArrayList();
|
|
|
+ for (Post item : postService.findList(post)) {
|
|
|
+ list.add(toExcelPost(item));
|
|
|
+ }
|
|
|
+ String fileName = "\u5c97\u4f4d\u6570\u636e" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
|
|
|
+ try (ExcelExport ee = new ExcelExport("\u5c97\u4f4d\u6570\u636e", ExcelPost.class)) {
|
|
|
+ ee.setDataList(list).write(response, fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequiresPermissions("sys:post:view")
|
|
|
+ @RequestMapping(value = "importTemplate")
|
|
|
+ public void importTemplate(HttpServletResponse response) {
|
|
|
+ List<ExcelPost> list = ListUtils.newArrayList(new ExcelPost());
|
|
|
+ String fileName = "\u5c97\u4f4d\u6570\u636e\u6a21\u677f.xlsx";
|
|
|
+ try (ExcelExport ee = new ExcelExport("\u5c97\u4f4d\u6570\u636e", ExcelPost.class, Type.IMPORT)) {
|
|
|
+ ee.setDataList(list).write(response, fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @RequiresPermissions("sys:post:edit")
|
|
|
+ @PostMapping(value = "importData")
|
|
|
+ public String importData(MultipartFile file, String updateSupport) {
|
|
|
+ try {
|
|
|
+ String message = importPostData(file, "1".equals(updateSupport));
|
|
|
+ return renderResult(Global.TRUE, "posfull:" + message);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ return renderResult(Global.FALSE, "posfull:" + ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String importPostData(MultipartFile file, boolean updateSupport) {
|
|
|
+ if (file == null) {
|
|
|
+ throw new ServiceException("\u8bf7\u9009\u62e9\u5bfc\u5165\u7684\u5c97\u4f4d\u6570\u636e\u6587\u4ef6\uff01");
|
|
|
+ }
|
|
|
+ int successNum = 0;
|
|
|
+ int failureNum = 0;
|
|
|
+ StringBuilder successMsg = new StringBuilder();
|
|
|
+ StringBuilder failureMsg = new StringBuilder();
|
|
|
+ try (ExcelImport ei = new ExcelImport(file, 2, 0)) {
|
|
|
+ List<ExcelPost> list = ei.getDataList(ExcelPost.class);
|
|
|
+ for (ExcelPost item : list) {
|
|
|
+ try {
|
|
|
+ Post post = toPost(item);
|
|
|
+ saveImportPost(post, updateSupport);
|
|
|
+ successNum++;
|
|
|
+ successMsg.append("<br/>").append(successNum).append("\u3001\u5c97\u4f4d ")
|
|
|
+ .append(StringUtils.defaultString(post.getPostName(), post.getPostCode()))
|
|
|
+ .append(" \u5bfc\u5165\u6210\u529f");
|
|
|
+ } catch (Exception e) {
|
|
|
+ failureNum++;
|
|
|
+ String msg = "<br/>" + failureNum + "\u3001\u5c97\u4f4d "
|
|
|
+ + StringUtils.defaultString(item.getPostName(), item.getPostCode())
|
|
|
+ + " \u5bfc\u5165\u5931\u8d25\uff1a" + e.getMessage();
|
|
|
+ failureMsg.append(msg);
|
|
|
+ logger.error(msg, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
+ }
|
|
|
+ if (failureNum > 0) {
|
|
|
+ failureMsg.insert(0, "\u5f88\u62b1\u6b49\uff0c\u5bfc\u5165\u5931\u8d25\uff01\u5171 " + failureNum + " \u6761\u5c97\u4f4d\u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u9519\u8bef\u5982\u4e0b\uff1a");
|
|
|
+ throw new ServiceException(failureMsg.toString());
|
|
|
+ }
|
|
|
+ successMsg.insert(0, "\u606d\u559c\u60a8\uff0c\u5c97\u4f4d\u6570\u636e\u5df2\u5168\u90e8\u5bfc\u5165\u6210\u529f\uff01\u5171 " + successNum + " \u6761\uff0c\u6570\u636e\u5982\u4e0b\uff1a");
|
|
|
+ return successMsg.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveImportPost(Post post, boolean updateSupport) {
|
|
|
+ post.setPostCode(StringUtils.trim(post.getPostCode()));
|
|
|
+ post.setViewCode(StringUtils.trim(post.getViewCode()));
|
|
|
+ post.setPostName(StringUtils.trim(post.getPostName()));
|
|
|
+ if (StringUtils.isBlank(post.getPostCode()) && StringUtils.isBlank(post.getViewCode())) {
|
|
|
+ throw new ServiceException("\u5c97\u4f4d\u7f16\u7801\u4e0d\u80fd\u4e3a\u7a7a");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(post.getPostCode())) {
|
|
|
+ post.setPostCode(post.getViewCode());
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(post.getViewCode())) {
|
|
|
+ post.setViewCode(post.getPostCode());
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(post.getPostName())) {
|
|
|
+ throw new ServiceException("\u5c97\u4f4d\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a");
|
|
|
+ }
|
|
|
+ Post exists = postService.get(new Post(post.getPostCode()));
|
|
|
+ if (exists != null && !updateSupport) {
|
|
|
+ throw new ServiceException("\u5c97\u4f4d\u7f16\u7801\u5df2\u5b58\u5728");
|
|
|
+ }
|
|
|
+ if (exists != null) {
|
|
|
+ post.setId(exists.getId());
|
|
|
+ post.setPostCode(exists.getPostCode());
|
|
|
+ post.setIsNewRecord(false);
|
|
|
+ } else {
|
|
|
+ post.setId(post.getPostCode());
|
|
|
+ post.setIsNewRecord(true);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(post.getStatus())) {
|
|
|
+ post.setStatus(Post.STATUS_NORMAL);
|
|
|
+ }
|
|
|
+ postService.save(post);
|
|
|
+ Post saved = postService.get(new Post(post.getPostCode()));
|
|
|
+ if (saved == null) {
|
|
|
+ throw new ServiceException("\u5c97\u4f4d\u6570\u636e\u672a\u5199\u5165\u6570\u636e\u5e93");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ExcelPost toExcelPost(Post post) {
|
|
|
+ ExcelPost excelPost = new ExcelPost();
|
|
|
+ excelPost.setPostCode(post.getPostCode());
|
|
|
+ excelPost.setViewCode(post.getViewCode());
|
|
|
+ excelPost.setPostName(post.getPostName());
|
|
|
+ excelPost.setPostType(post.getPostType());
|
|
|
+ excelPost.setPostSort(post.getPostSort());
|
|
|
+ excelPost.setStatus(post.getStatus());
|
|
|
+ excelPost.setRemarks(post.getRemarks());
|
|
|
+ return excelPost;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Post toPost(ExcelPost excelPost) {
|
|
|
+ Post post = new Post();
|
|
|
+ post.setPostCode(excelPost.getPostCode());
|
|
|
+ post.setViewCode(excelPost.getViewCode());
|
|
|
+ post.setPostName(excelPost.getPostName());
|
|
|
+ post.setPostType(excelPost.getPostType());
|
|
|
+ post.setPostSort(excelPost.getPostSort());
|
|
|
+ post.setStatus(excelPost.getStatus());
|
|
|
+ post.setRemarks(excelPost.getRemarks());
|
|
|
+ return post;
|
|
|
+ }
|
|
|
+}
|