Commit 712ba94c681bc876882f54df76a6c498ab9088a1

Authored by yeqianyong
1 parent ad8d78c2

楚江erp:客户简称导入相关接口开发

... ... @@ -2,8 +2,11 @@ package com.lframework.xingyun.basedata.controller;
2 2
3 3 import com.lframework.starter.web.core.annotations.security.HasPermission;
4 4 import com.lframework.starter.web.core.controller.DefaultBaseController;
  5 +import com.lframework.starter.web.core.utils.ExcelUtil;
5 6 import com.lframework.xingyun.basedata.bo.customer.GetCustomerShortBo;
6 7 import com.lframework.xingyun.basedata.bo.customer.QueryCustomerShortBo;
  8 +import com.lframework.xingyun.basedata.excel.customer.CustomerShortImportListener;
  9 +import com.lframework.xingyun.basedata.excel.customer.CustomerShortModel;
7 10 import com.lframework.xingyun.basedata.vo.customer.QueryCustomerShortVo;
8 11 import com.lframework.xingyun.basedata.service.customer.CustomerShortService;
9 12 import com.lframework.xingyun.basedata.vo.customer.CreateCustomerShortVo;
... ... @@ -24,8 +27,10 @@ import io.swagger.annotations.Api;
24 27 import org.springframework.web.bind.annotation.DeleteMapping;
25 28 import org.springframework.validation.annotation.Validated;
26 29 import org.springframework.web.bind.annotation.*;
  30 +import org.springframework.web.multipart.MultipartFile;
27 31
28 32 import javax.validation.Valid;
  33 +import javax.validation.constraints.NotNull;
29 34 import java.util.List;
30 35 import java.util.stream.Collectors;
31 36
... ... @@ -50,7 +55,7 @@ public class CustomerShortController extends DefaultBaseController {
50 55 * 查询列表
51 56 */
52 57 @ApiOperation("查询列表")
53   - @HasPermission({"customer:customer:query"})
  58 + @HasPermission({"base-data:custome-abbreviation:query"})
54 59 @GetMapping("/query")
55 60 public InvokeResult<PageResult<QueryCustomerShortBo>> query(@Valid QueryCustomerShortVo vo) {
56 61 PageResult<CustomerShort> pageResult = customerShortService.query(getPageIndex(vo), getPageSize(vo), vo);
... ... @@ -67,7 +72,7 @@ public class CustomerShortController extends DefaultBaseController {
67 72 */
68 73 @ApiOperation("根据ID查询")
69 74 @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
70   - @HasPermission({"customer:customer:query"})
  75 + @HasPermission({"base-data:custome-abbreviation:query"})
71 76 @GetMapping
72 77 public InvokeResult<GetCustomerShortBo> get(@NotBlank(message = "id不能为空!") String id) {
73 78 CustomerShort data = customerShortService.findById(id);
... ... @@ -83,7 +88,7 @@ public class CustomerShortController extends DefaultBaseController {
83 88 * 新增
84 89 */
85 90 @ApiOperation("新增")
86   - @HasPermission({"customer:customer:add"})
  91 + @HasPermission({"base-data:custome-abbreviation:add"})
87 92 @PostMapping
88 93 public InvokeResult<Void> create(@Valid CreateCustomerShortVo vo) {
89 94 customerShortService.create(vo);
... ... @@ -94,7 +99,7 @@ public class CustomerShortController extends DefaultBaseController {
94 99 * 修改
95 100 */
96 101 @ApiOperation("修改")
97   - @HasPermission({"customer:customer:modify"})
  102 + @HasPermission({"base-data:custome-abbreviation:modify"})
98 103 @PutMapping
99 104 public InvokeResult<Void> update(@Valid UpdateCustomerShortVo vo) {
100 105 customerShortService.update(vo);
... ... @@ -106,10 +111,38 @@ public class CustomerShortController extends DefaultBaseController {
106 111 */
107 112 @ApiOperation("根据ID删除")
108 113 @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
109   - @HasPermission({"customer:customer:delete"})
  114 + @HasPermission({"base-data:custome-abbreviation:delete"})
110 115 @DeleteMapping
111 116 public InvokeResult<Void> deleteById(@NotBlank(message = "id不能为空!") String id) {
112 117 customerShortService.deleteById(id);
113 118 return InvokeResultBuilder.success();
114 119 }
  120 +
  121 + /**
  122 + * 下载导入模板
  123 + */
  124 + @ApiOperation("下载导入模板")
  125 + @HasPermission({"base-data:custome-abbreviation:import"})
  126 + @GetMapping("/import/template")
  127 + public void downloadImportTemplate() {
  128 + ExcelUtil.exportXls("客户简称导入模板", CustomerShortModel.class);
  129 + }
  130 +
  131 + /**
  132 + * 导入
  133 + *
  134 + * @param id 任务ID
  135 + * @param file 文件
  136 + */
  137 + @ApiOperation("导入")
  138 + @HasPermission({"base-data:custome-abbreviation:import"})
  139 + @PostMapping("/import")
  140 + public InvokeResult<Void> importExcel(@NotBlank(message = "ID不能为空") String id,
  141 + @NotNull(message = "请上传文件") MultipartFile file) {
  142 + CustomerShortImportListener listener = new CustomerShortImportListener();
  143 + listener.setTaskId(id);
  144 + ExcelUtil.read(file, CustomerShortModel.class, listener).sheet().doRead();
  145 +
  146 + return InvokeResultBuilder.success();
  147 + }
115 148 }
... ...
  1 +package com.lframework.xingyun.basedata.excel.customer;
  2 +
  3 +import com.alibaba.excel.context.AnalysisContext;
  4 +import com.baomidou.mybatisplus.core.conditions.Wrapper;
  5 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7 +import com.lframework.starter.common.exceptions.impl.DefaultClientException;
  8 +import com.lframework.starter.web.core.components.excel.ExcelImportListener;
  9 +import com.lframework.starter.web.core.utils.ApplicationUtil;
  10 +import com.lframework.starter.web.core.utils.JsonUtil;
  11 +import com.lframework.starter.web.inner.entity.SysDept;
  12 +import com.lframework.starter.web.inner.service.system.SysDeptService;
  13 +import com.lframework.xingyun.basedata.entity.Customer;
  14 +import com.lframework.xingyun.basedata.entity.CustomerShort;
  15 +import com.lframework.xingyun.basedata.service.customer.CustomerService;
  16 +import com.lframework.xingyun.basedata.service.customer.CustomerShortService;
  17 +import lombok.extern.slf4j.Slf4j;
  18 +import org.apache.commons.collections4.CollectionUtils;
  19 +
  20 +import java.util.List;
  21 +import java.util.stream.Collectors;
  22 +
  23 +@Slf4j
  24 +public class CustomerShortImportListener extends ExcelImportListener<CustomerShortModel> {
  25 +
  26 + @Override
  27 + protected void doInvoke(CustomerShortModel data, AnalysisContext context) {
  28 + // 客户名称
  29 + CustomerService customerService = ApplicationUtil.getBean(CustomerService.class);
  30 + Wrapper<Customer> customerWrapper = Wrappers.lambdaQuery(Customer.class)
  31 + .eq(Customer::getName, data.getCustomerName());
  32 + Customer customer = customerService.getOne(customerWrapper);
  33 + if (customer == null) {
  34 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,客户[" + data.getCustomerName()
  35 + + "]不存在,请检查后再次导入!");
  36 + }
  37 + data.setCustomerId(customer.getId());
  38 + // 简称
  39 + CustomerShortService customerShortService = ApplicationUtil.getBean(CustomerShortService.class);
  40 + CustomerShort customerShort = customerShortService.getByCustomerId(customer.getId());
  41 + if (customerShort != null) {
  42 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,客户[" + data.getCustomerName()
  43 + + "]简称已存在,请检查后再次导入!");
  44 + }
  45 + // 客户类型
  46 + String type = data.getType();
  47 + if ("终端".equals(type)) {
  48 + type = "TERMINAL";
  49 + } else if ("经销".equals(type)) {
  50 + type = "DEALER";
  51 + } else {
  52 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,客户类型只能填写“终端”或“经销”,请检查后再次导入!");
  53 + }
  54 + data.setType(type);
  55 + // 办事处
  56 + SysDeptService sysDeptService = ApplicationUtil.getBean(SysDeptService.class);
  57 + LambdaQueryWrapper<SysDept> deptWrapper = Wrappers.lambdaQuery(SysDept.class);
  58 + deptWrapper.eq(SysDept::getName, data.getDeptName());
  59 + SysDept office = sysDeptService.getOne(deptWrapper);
  60 + if (office == null) {
  61 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,[" + data.getDeptName()
  62 + + "]不存在,请检查后再次导入!");
  63 + }
  64 + data.setDeptId(office.getId());
  65 + // 区域
  66 + deptWrapper.eq(SysDept::getName, data.getRegionName());
  67 + SysDept region = sysDeptService.getOne(deptWrapper);
  68 + if (region == null) {
  69 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,[" + data.getRegionName()
  70 + + "]不存在,请检查后再次导入!");
  71 + }
  72 + List<SysDept> regionList = sysDeptService.queryArea(null, office.getId());
  73 + if (CollectionUtils.isEmpty(regionList)) {
  74 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,[" + data.getRegionName()
  75 + + "]不属于[" + data.getDeptName() + "],请检查后再次导入!");
  76 + }
  77 + List<String> regionNameList = regionList.stream().map(SysDept::getName).collect(Collectors.toList());
  78 + if (regionNameList.contains(region.getName())) {
  79 + throw new DefaultClientException("第" + context.readRowHolder().getRowIndex() + "行,[" + data.getRegionName()
  80 + + "]不属于[" + data.getDeptName() + "],请检查后再次导入!");
  81 + }
  82 + data.setRegion(region.getId());
  83 + }
  84 +
  85 + @Override
  86 + protected void afterAllAnalysed(AnalysisContext context) {
  87 + CustomerShortService customerShortService = ApplicationUtil.getBean(CustomerShortService.class);
  88 + for (int i = 0; i < datas.size(); i++) {
  89 + CustomerShortModel data = datas.get(i);
  90 + CustomerShort customerShort = JsonUtil.parseObject(JsonUtil.toJsonString(data), CustomerShort.class);
  91 + try {
  92 + customerShortService.save(customerShort);
  93 + } catch (Exception e) {
  94 + throw new DefaultClientException(
  95 + "第" + (i + 1) + "行新增失败,失败原因:" + e.getMessage());
  96 + }
  97 + this.setSuccessProcess(i);
  98 + }
  99 + }
  100 +
  101 + @Override
  102 + protected void doComplete() {
  103 +
  104 + }
  105 +}
... ...
  1 +package com.lframework.xingyun.basedata.excel.customer;
  2 +
  3 +import com.alibaba.excel.annotation.ExcelIgnore;
  4 +import com.alibaba.excel.annotation.ExcelProperty;
  5 +import com.lframework.starter.web.core.annotations.excel.ExcelRequired;
  6 +import com.lframework.starter.web.core.components.excel.ExcelModel;
  7 +import lombok.Data;
  8 +
  9 +import javax.validation.constraints.Pattern;
  10 +
  11 +/**
  12 + * <p>
  13 + * 客户简称
  14 + * </p>
  15 + *
  16 + */
  17 +@Data
  18 +public class CustomerShortModel implements ExcelModel {
  19 +
  20 + /**
  21 + * 客户简称
  22 + */
  23 + @ExcelRequired
  24 + @ExcelProperty("客户简称")
  25 + private String shortName;
  26 +
  27 + /**
  28 + * 客户ID
  29 + */
  30 + @ExcelIgnore
  31 + private String customerId;
  32 +
  33 + /**
  34 + * 客户名称
  35 + */
  36 + @ExcelRequired
  37 + @ExcelProperty("客户名称")
  38 + private String customerName;
  39 +
  40 + /**
  41 + * 客户类型
  42 + */
  43 + @ExcelRequired
  44 + @ExcelProperty("客户类型")
  45 + @Pattern(regexp = "终端|经销", message = "客户类型只能填写“终端”或“经销”")
  46 + private String type;
  47 +
  48 + /**
  49 + * 办事处ID
  50 + */
  51 + @ExcelIgnore
  52 + private String deptId;
  53 +
  54 + /**
  55 + * 办事处名称
  56 + */
  57 + @ExcelRequired
  58 + @ExcelProperty("办事处名称")
  59 + private String deptName;
  60 +
  61 + /**
  62 + * 区域ID
  63 + */
  64 + @ExcelIgnore
  65 + private String region;
  66 +
  67 + /**
  68 + * 区域名称
  69 + */
  70 + @ExcelRequired
  71 + @ExcelProperty("区域名称")
  72 + private String regionName;
  73 +}
... ...