SupplierController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.kmall.admin.controller;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import com.kmall.admin.entity.SupplierEntity;
  7. import com.kmall.admin.service.SupplierService;
  8. import com.kmall.admin.utils.ParamUtils;
  9. import com.kmall.common.constant.JxlsXmlTemplateName;
  10. import com.kmall.common.utils.PageUtils;
  11. import com.kmall.common.utils.Query;
  12. import com.kmall.common.utils.R;
  13. import com.kmall.common.utils.RRException;
  14. import com.kmall.common.utils.excel.ExcelUtil;
  15. import com.kmall.manager.manager.express.sf.ServiceException;
  16. import org.apache.commons.logging.Log;
  17. import org.apache.commons.logging.LogFactory;
  18. import org.apache.shiro.authz.annotation.RequiresPermissions;
  19. import org.jxls.reader.XLSDataReadException;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Controller;
  22. import org.springframework.web.bind.annotation.*;
  23. import org.springframework.web.multipart.MultipartFile;
  24. /**
  25. * Controller
  26. *
  27. * @author huangyq
  28. * @email admin@qhdswl.com
  29. * @date 2018-11-26 15:14:24
  30. */
  31. @RestController
  32. @RequestMapping("supplier")
  33. public class SupplierController {
  34. private Log logger = LogFactory.getLog(SupplierController.class);
  35. @Autowired
  36. private SupplierService supplierService;
  37. @Autowired
  38. private ExcelUtil excelUtil;
  39. /**
  40. * 查看列表
  41. */
  42. @RequestMapping("/list")
  43. @RequiresPermissions("supplier:list")
  44. @ResponseBody
  45. public R list(@RequestParam Map<String, Object> params) {
  46. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
  47. // ParamUtils.setName(params, "name");
  48. //查询列表数据
  49. Query query = new Query(params);
  50. List<SupplierEntity> supplierList = supplierService.queryList(query);
  51. int total = supplierService.queryTotal(query);
  52. PageUtils pageUtil = new PageUtils(supplierList, total, query.getLimit(), query.getPage());
  53. return R.ok().put("page", pageUtil);
  54. }
  55. /**
  56. * 查看信息
  57. */
  58. @RequestMapping("/info/{id}")
  59. @RequiresPermissions("supplier:info")
  60. @ResponseBody
  61. public R info(@PathVariable("id") Integer id) {
  62. SupplierEntity supplier = supplierService.queryObject(id);
  63. return R.ok().put("supplier", supplier);
  64. }
  65. /**
  66. * 保存
  67. */
  68. @RequestMapping("/save")
  69. @RequiresPermissions("supplier:save")
  70. @ResponseBody
  71. public R save(@RequestBody SupplierEntity supplier) {
  72. supplierService.save(supplier);
  73. return R.ok();
  74. }
  75. /**
  76. * 修改
  77. */
  78. @RequestMapping("/update")
  79. @RequiresPermissions("supplier:update")
  80. @ResponseBody
  81. public R update(@RequestBody SupplierEntity supplier) {
  82. supplierService.update(supplier);
  83. return R.ok();
  84. }
  85. /**
  86. * 删除
  87. */
  88. @RequestMapping("/delete")
  89. @RequiresPermissions("supplier:delete")
  90. @ResponseBody
  91. public R delete(@RequestBody Integer[]ids) {
  92. supplierService.deleteBatch(ids);
  93. return R.ok();
  94. }
  95. /**
  96. * 查看所有列表
  97. */
  98. @RequestMapping("/queryAll")
  99. @ResponseBody
  100. public R queryAll(@RequestParam Map<String, Object> params) {
  101. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
  102. List<SupplierEntity> list = supplierService.queryList(params);
  103. return R.ok().put("list", list);
  104. }
  105. /**
  106. * 导入 excel 数据
  107. * @author zhuhh
  108. * @param file
  109. * @return
  110. */
  111. @RequestMapping("/upload")
  112. public R upload(@RequestParam("file") MultipartFile file) {
  113. List<SupplierEntity> supplierEntityList = new ArrayList<>();
  114. // 读取 excel 数据
  115. try {
  116. SupplierEntity supplierEntity = new SupplierEntity();
  117. Map<String, Object> beans = new HashMap<>();
  118. beans.put("SupplierEntity", supplierEntity);
  119. beans.put("SupplierEntityList", supplierEntityList);
  120. if (file.isEmpty()) {
  121. return R.error("文件不能为空!");
  122. }
  123. excelUtil.readExcel(JxlsXmlTemplateName.SUPPLIER_DTO_List, beans, file.getInputStream());
  124. } catch (XLSDataReadException e) {
  125. e.printStackTrace();
  126. logger.error("商品供货商读取excel失败:" + e.getMessage());
  127. return R.error("导入失败!请在单元格输入正确的数据格式");
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. logger.error("商品供货商读取excel失败:" + e.getMessage());
  131. return R.error("读取excel数据失败!");
  132. }
  133. // 新增数据
  134. try {
  135. if (supplierEntityList == null || supplierEntityList.size() == 0) {
  136. return R.error("读取excel失败!单元格内容不能为空!");
  137. }
  138. supplierService.uploadExcel(supplierEntityList);
  139. } catch (ServiceException e) {
  140. e.printStackTrace();
  141. logger.error("商品供货商数据格式校验失败:" + e.getMessage());
  142. return R.error(e.getMessage());
  143. } catch (RRException e) {
  144. e.printStackTrace();
  145. logger.error("商品供货商添加数据失败:" + e.getMsg());
  146. return R.error(e.getMsg());
  147. } catch (Exception e) {
  148. e.printStackTrace();
  149. logger.error("商品供货商添加数据失败:" + e.getMessage());
  150. return R.error("数据保存数据库失败!");
  151. }
  152. return R.ok("导入成功!");
  153. }
  154. }