Commit 39e2f24aa682d466a787322b707be6935824c41a

Authored by 房远帅
1 parent f16b8956

报价:同行报价信息导入

  1 +package com.lframework.xingyun.sc.excel.quotationPeer;
  2 +
  3 +import com.alibaba.excel.context.AnalysisContext;
  4 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5 +import com.lframework.starter.common.exceptions.impl.DefaultClientException;
  6 +import com.lframework.starter.common.utils.StringUtil;
  7 +import com.lframework.starter.web.core.components.excel.ExcelImportListener;
  8 +import com.lframework.starter.web.core.utils.ApplicationUtil;
  9 +import com.lframework.xingyun.basedata.entity.BreedRelationship;
  10 +import com.lframework.xingyun.basedata.service.breed.BreedRelationshipService;
  11 +import com.lframework.xingyun.sc.procurement.service.quotation.SalesmanQuotationPeerService;
  12 +import com.lframework.xingyun.sc.procurement.vo.quotation.CreateSalesmanQuotationPeerVo;
  13 +import lombok.extern.slf4j.Slf4j;
  14 +import java.math.BigDecimal;
  15 +import java.util.List;
  16 +
  17 +/**
  18 + * 同行报价信息导入监听器
  19 + */
  20 +@Slf4j
  21 +public class PeerImportListener extends ExcelImportListener<PeerImportModel> {
  22 +
  23 + @Override
  24 + protected void doInvoke(PeerImportModel data, AnalysisContext context) {
  25 + int rowIndex = context.readRowHolder().getRowIndex();
  26 + BreedRelationshipService breedRelationshipService = ApplicationUtil.getBean(BreedRelationshipService.class);
  27 +
  28 + if (StringUtil.isNotBlank(data.getPeerName())) {
  29 + data.setPeerName(data.getPeerName().trim());
  30 + } else {
  31 + throw new DefaultClientException("第" + rowIndex + "行“同行名称”不能为空");
  32 + }
  33 +
  34 + BreedRelationship breedRelationship = breedRelationshipService.getOne(
  35 + Wrappers.lambdaQuery(BreedRelationship.class)
  36 + .eq(BreedRelationship::getProductName, data.getPeerProductName())
  37 + .last("LIMIT 1"));
  38 + if (breedRelationship == null) {
  39 + throw new DefaultClientException("第" + rowIndex + "行“品名”不存在于品种关系中");
  40 + }
  41 + data.setPeerProductId(breedRelationship.getId());
  42 +
  43 + data.setPeerPriceValue(parsePrice(data.getPeerPrice(), rowIndex));
  44 + }
  45 +
  46 + @Override
  47 + protected void afterAllAnalysed(AnalysisContext context) {
  48 + SalesmanQuotationPeerService salesmanQuotationPeerService = ApplicationUtil.getBean(SalesmanQuotationPeerService.class);
  49 + List<PeerImportModel> datas = this.getDatas();
  50 + if (datas.isEmpty()) {
  51 + return;
  52 + }
  53 + for (int i = 0; i < datas.size(); i++) {
  54 + PeerImportModel peerImportModel = datas.get(i);
  55 + CreateSalesmanQuotationPeerVo vo = new CreateSalesmanQuotationPeerVo();
  56 + vo.setPeerName(peerImportModel.getPeerName());
  57 + vo.setPeerProductId(peerImportModel.getPeerProductId());
  58 + vo.setPeerPrice(peerImportModel.getPeerPriceValue());
  59 + vo.setRemark(peerImportModel.getRemark());
  60 + salesmanQuotationPeerService.create(vo);
  61 + this.setSuccessProcess(i);
  62 + }
  63 + }
  64 +
  65 + @Override
  66 + protected void doComplete() {
  67 +
  68 + }
  69 +
  70 + /**
  71 + * 解析同行报价,并按统一口径返回行级错误
  72 + */
  73 + private BigDecimal parsePrice(String value, int rowIndex) {
  74 + if (StringUtil.isBlank(value)) {
  75 + throw new DefaultClientException("第" + rowIndex + "行“同行报价”不能为空");
  76 + }
  77 +
  78 + BigDecimal decimalValue;
  79 + try {
  80 + decimalValue = new BigDecimal(value.trim());
  81 + } catch (NumberFormatException e) {
  82 + throw new DefaultClientException("第" + rowIndex + "行“同行报价”格式有误");
  83 + }
  84 +
  85 + BigDecimal normalizedValue = decimalValue.stripTrailingZeros();
  86 + int scale = Math.max(normalizedValue.scale(), 0);
  87 + if (scale > 10) {
  88 + throw new DefaultClientException("第" + rowIndex + "行“同行报价”最多允许10位小数");
  89 + }
  90 +
  91 + return decimalValue;
  92 + }
  93 +}
... ...
  1 +package com.lframework.xingyun.sc.excel.quotationPeer;
  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 +/**
  10 + * 同行报价信息导入模型
  11 + */
  12 +@Data
  13 +public class PeerImportModel implements ExcelModel {
  14 +
  15 + /**
  16 + * 同行名称
  17 + */
  18 + @ExcelRequired
  19 + @ExcelProperty("同行名称")
  20 + private String peerName;
  21 +
  22 + /**
  23 + * 同行品种名称
  24 + */
  25 + @ExcelRequired
  26 + @ExcelProperty("同行品种名称")
  27 + private String peerProductName;
  28 +
  29 + /**
  30 + * 同行报价
  31 + */
  32 + @ExcelRequired
  33 + @ExcelProperty("同行报价")
  34 + private String peerPrice;
  35 +
  36 + @ExcelProperty("备注")
  37 + private String remark;
  38 +
  39 + /**
  40 + * 导入时解析出的同行品种ID
  41 + */
  42 + @ExcelIgnore
  43 + private String peerProductId;
  44 +
  45 + /**
  46 + * 导入时解析出的采同行报价
  47 + */
  48 + @ExcelIgnore
  49 + private java.math.BigDecimal peerPriceValue;
  50 +}
... ...
... ... @@ -6,6 +6,9 @@ import com.lframework.starter.web.core.components.resp.InvokeResult;
6 6 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
7 7 import com.lframework.starter.web.core.components.resp.PageResult;
8 8 import com.lframework.starter.web.core.controller.DefaultBaseController;
  9 +import com.lframework.starter.web.core.utils.ExcelUtil;
  10 +import com.lframework.xingyun.sc.excel.quotationPeer.PeerImportListener;
  11 +import com.lframework.xingyun.sc.excel.quotationPeer.PeerImportModel;
9 12 import com.lframework.xingyun.sc.procurement.entity.SalesmanQuotationPeer;
10 13 import com.lframework.xingyun.sc.procurement.service.quotation.SalesmanQuotationPeerService;
11 14 import com.lframework.xingyun.sc.procurement.vo.quotation.CreateSalesmanQuotationPeerVo;
... ... @@ -15,8 +18,10 @@ import io.swagger.annotations.Api;
15 18 import io.swagger.annotations.ApiImplicitParam;
16 19 import io.swagger.annotations.ApiOperation;
17 20 import javax.annotation.Resource;
  21 +import javax.servlet.http.HttpServletResponse;
18 22 import javax.validation.Valid;
19 23 import javax.validation.constraints.NotBlank;
  24 +import javax.validation.constraints.NotNull;
20 25 import org.springframework.validation.annotation.Validated;
21 26 import org.springframework.web.bind.annotation.GetMapping;
22 27 import org.springframework.web.bind.annotation.PostMapping;
... ... @@ -24,6 +29,8 @@ import org.springframework.web.bind.annotation.PutMapping;
24 29 import org.springframework.web.bind.annotation.RequestBody;
25 30 import org.springframework.web.bind.annotation.RequestMapping;
26 31 import org.springframework.web.bind.annotation.RestController;
  32 +import org.springframework.web.multipart.MultipartFile;
  33 +import java.io.IOException;
27 34
28 35 /**
29 36 * 同行报价信息 Controller
... ... @@ -99,4 +106,30 @@ public class SalesmanQuotationPeerController extends DefaultBaseController {
99 106 salesmanQuotationPeerService.update(vo);
100 107 return InvokeResultBuilder.success();
101 108 }
  109 +
  110 + /**
  111 + * 下载导入模板
  112 + */
  113 + @ApiOperation("下载导入模板")
  114 + @HasPermission({"procure-manage:sales-price-peer:import"})
  115 + @GetMapping("/import/template")
  116 + public void downloadImportTemplate(HttpServletResponse response) throws IOException {
  117 + ExcelUtil.exportXls("同行报价信息导入模板", PeerImportModel.class);
  118 + }
  119 +
  120 + /**
  121 + * 导入
  122 + */
  123 + @ApiOperation("导入")
  124 + @HasPermission({"procure-manage:sales-price-peer:import"})
  125 + @PostMapping("/import")
  126 + public InvokeResult<Void> importExcel(@NotBlank(message = "ID不能为空") String id,
  127 + @NotNull(message = "请上传文件") MultipartFile file) {
  128 +
  129 + PeerImportListener listener = new PeerImportListener();
  130 + listener.setTaskId(id);
  131 + ExcelUtil.read(file, PeerImportModel.class, listener).sheet().doRead();
  132 +
  133 + return InvokeResultBuilder.success();
  134 + }
102 135 }
... ...