Commit 9f17d57ccda1a6302a4095c0ebd0b544b4c11198

Authored by 房远帅
1 parent 1dcb2990

报价:品种关系导入

@@ -3,13 +3,17 @@ package com.lframework.xingyun.basedata.controller; @@ -3,13 +3,17 @@ package com.lframework.xingyun.basedata.controller;
3 import com.baomidou.mybatisplus.core.toolkit.Wrappers; 3 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
4 import com.lframework.starter.web.core.annotations.security.HasPermission; 4 import com.lframework.starter.web.core.annotations.security.HasPermission;
5 import com.lframework.starter.web.core.controller.DefaultBaseController; 5 import com.lframework.starter.web.core.controller.DefaultBaseController;
  6 +import com.lframework.starter.web.core.utils.ExcelUtil;
6 import com.lframework.starter.web.core.utils.PageResultUtil; 7 import com.lframework.starter.web.core.utils.PageResultUtil;
7 import com.lframework.starter.web.core.components.resp.PageResult; 8 import com.lframework.starter.web.core.components.resp.PageResult;
8 import com.lframework.starter.web.core.components.resp.InvokeResult; 9 import com.lframework.starter.web.core.components.resp.InvokeResult;
  10 +import javax.servlet.http.HttpServletResponse;
9 import javax.validation.constraints.NotBlank; 11 import javax.validation.constraints.NotBlank;
10 import com.lframework.xingyun.basedata.bo.breed.QueryBreedRelationshipBo; 12 import com.lframework.xingyun.basedata.bo.breed.QueryBreedRelationshipBo;
11 import com.lframework.xingyun.basedata.entity.BreedRelationship; 13 import com.lframework.xingyun.basedata.entity.BreedRelationship;
12 import com.lframework.xingyun.basedata.entity.PriceSubTable; 14 import com.lframework.xingyun.basedata.entity.PriceSubTable;
  15 +import com.lframework.xingyun.basedata.excel.breed.BreedRelationshipImportListener;
  16 +import com.lframework.xingyun.basedata.excel.breed.BreedRelationshipImportModel;
13 import com.lframework.xingyun.basedata.service.breed.BreedRelationshipService; 17 import com.lframework.xingyun.basedata.service.breed.BreedRelationshipService;
14 import com.lframework.xingyun.basedata.service.price.PriceSubTableService; 18 import com.lframework.xingyun.basedata.service.price.PriceSubTableService;
15 import com.lframework.xingyun.basedata.vo.breed.CreateBreedRelationshipVo; 19 import com.lframework.xingyun.basedata.vo.breed.CreateBreedRelationshipVo;
@@ -25,7 +29,10 @@ import org.springframework.web.bind.annotation.DeleteMapping; @@ -25,7 +29,10 @@ import org.springframework.web.bind.annotation.DeleteMapping;
25 import org.springframework.beans.factory.annotation.Autowired; 29 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.validation.annotation.Validated; 30 import org.springframework.validation.annotation.Validated;
27 import org.springframework.web.bind.annotation.*; 31 import org.springframework.web.bind.annotation.*;
  32 +import org.springframework.web.multipart.MultipartFile;
28 import javax.validation.Valid; 33 import javax.validation.Valid;
  34 +import javax.validation.constraints.NotNull;
  35 +import java.io.IOException;
29 import java.util.Collections; 36 import java.util.Collections;
30 import java.util.HashSet; 37 import java.util.HashSet;
31 import java.util.List; 38 import java.util.List;
@@ -129,6 +136,26 @@ public class BreedRelationshipController extends DefaultBaseController { @@ -129,6 +136,26 @@ public class BreedRelationshipController extends DefaultBaseController {
129 return InvokeResultBuilder.success(); 136 return InvokeResultBuilder.success();
130 } 137 }
131 138
  139 + @ApiOperation("下载导入模板")
  140 + @HasPermission({"base-data:variety:import"})
  141 + @GetMapping("/import/template")
  142 + public void downloadImportTemplate(HttpServletResponse response) throws IOException {
  143 + ExcelUtil.exportXls("品种关系导入模板", BreedRelationshipImportModel.class);
  144 + }
  145 +
  146 + @ApiOperation("导入")
  147 + @HasPermission({"base-data:variety:import"})
  148 + @PostMapping("/import")
  149 + public InvokeResult<Void> importExcel(@NotBlank(message = "ID不能为空") String id,
  150 + @NotNull(message = "请上传文件") MultipartFile file) {
  151 +
  152 + BreedRelationshipImportListener listener = new BreedRelationshipImportListener();
  153 + listener.setTaskId(id);
  154 + ExcelUtil.read(file, BreedRelationshipImportModel.class, listener).sheet().doRead();
  155 +
  156 + return InvokeResultBuilder.success();
  157 + }
  158 +
132 private void fillCanOperate(List<QueryBreedRelationshipBo> results) { 159 private void fillCanOperate(List<QueryBreedRelationshipBo> results) {
133 if (CollectionUtil.isEmpty(results)) { 160 if (CollectionUtil.isEmpty(results)) {
134 return; 161 return;
  1 +package com.lframework.xingyun.basedata.excel.breed;
  2 +
  3 +import com.alibaba.excel.context.AnalysisContext;
  4 +import com.lframework.starter.common.exceptions.impl.DefaultClientException;
  5 +import com.lframework.starter.common.utils.StringUtil;
  6 +import com.lframework.starter.web.core.components.excel.ExcelImportListener;
  7 +import com.lframework.starter.web.core.utils.ApplicationUtil;
  8 +import com.lframework.xingyun.basedata.service.breed.BreedRelationshipService;
  9 +import com.lframework.xingyun.basedata.vo.breed.CreateBreedRelationshipVo;
  10 +import lombok.extern.slf4j.Slf4j;
  11 +import java.math.BigDecimal;
  12 +import java.util.ArrayList;
  13 +import java.util.List;
  14 +
  15 +@Slf4j
  16 +public class BreedRelationshipImportListener extends ExcelImportListener<BreedRelationshipImportModel> {
  17 +
  18 + private List<String> checkList = new ArrayList<>();
  19 +
  20 + @Override
  21 + protected void doInvoke(BreedRelationshipImportModel data, AnalysisContext context) {
  22 + BreedRelationshipService breedRelationshipService = ApplicationUtil.getBean(BreedRelationshipService.class);
  23 + if (StringUtil.isBlank(data.getProductName())) {
  24 + throw new DefaultClientException(
  25 + "第" + context.readRowHolder().getRowIndex() + "行“品种”不能为空");
  26 + } else {
  27 + Integer integer = breedRelationshipService.checkProductName(data.getProductName());
  28 + if (integer > 0) {
  29 + throw new DefaultClientException(
  30 + "第" + context.readRowHolder().getRowIndex() + "行“品种”已存在");
  31 + }
  32 + }
  33 + data.setAttritionRateValue(parseRate(data.getAttritionRate(), "损耗率", context));
  34 + data.setMiscellaneousTaxRateValue(parseRate(data.getMiscellaneousTaxRate(), "杂率", context));
  35 + if (checkList.contains(data.getProductName())) {
  36 + throw new DefaultClientException(
  37 + "第" + context.readRowHolder().getRowIndex() + "行“品种”与第" + (checkList.indexOf(data.getProductName()) + 1) + "行重复");
  38 + }
  39 + checkList.add(data.getProductName());
  40 + }
  41 +
  42 + @Override
  43 + protected void afterAllAnalysed(AnalysisContext context) {
  44 + BreedRelationshipService breedRelationshipService = ApplicationUtil.getBean(BreedRelationshipService.class);
  45 + List<BreedRelationshipImportModel> datas = this.getDatas();
  46 + for (int i = 0; i < datas.size(); i++) {
  47 + BreedRelationshipImportModel data = datas.get(i);
  48 + CreateBreedRelationshipVo vo = new CreateBreedRelationshipVo();
  49 + vo.setProductName(data.getProductName());
  50 + vo.setCode(data.getCode());
  51 + vo.setType(data.getType());
  52 + vo.setAttritionRate(data.getAttritionRateValue());
  53 + vo.setMiscellaneousTaxRate(data.getMiscellaneousTaxRateValue());
  54 + breedRelationshipService.create(vo);
  55 + this.setSuccessProcess(i);
  56 + }
  57 + }
  58 +
  59 + @Override
  60 + protected void doComplete() {
  61 + }
  62 +
  63 + /**
  64 + * 解析导入的比例字段,并按统一口径返回行级错误
  65 + */
  66 + private BigDecimal parseRate(String value, String fieldName, AnalysisContext context) {
  67 + if (StringUtil.isBlank(value)) {
  68 + return null;
  69 + }
  70 +
  71 + BigDecimal decimalValue;
  72 + try {
  73 + decimalValue = new BigDecimal(value.trim());
  74 + } catch (NumberFormatException e) {
  75 + throw new DefaultClientException(
  76 + "第" + context.readRowHolder().getRowIndex() + "行“" + fieldName + "”格式有误");
  77 + }
  78 +
  79 + BigDecimal normalizedValue = decimalValue.stripTrailingZeros();
  80 + int scale = Math.max(normalizedValue.scale(), 0);
  81 + if (scale > 10) {
  82 + throw new DefaultClientException(
  83 + "第" + context.readRowHolder().getRowIndex() + "行“" + fieldName + "”最多允许10位小数");
  84 + }
  85 + return decimalValue;
  86 + }
  87 +}
  1 +package com.lframework.xingyun.basedata.excel.breed;
  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 +@Data
  10 +public class BreedRelationshipImportModel implements ExcelModel {
  11 +
  12 + @ExcelIgnore
  13 + private String id;
  14 +
  15 + /**
  16 + * 品种
  17 + */
  18 + @ExcelRequired
  19 + @ExcelProperty("品种")
  20 + private String productName;
  21 +
  22 + /**
  23 + * 代号
  24 + */
  25 + @ExcelProperty("代号")
  26 + private String code;
  27 +
  28 + /**
  29 + * 类型
  30 + */
  31 + @ExcelProperty("类型")
  32 + private String type;
  33 +
  34 + /**
  35 + * 损耗率
  36 + */
  37 + @ExcelProperty("损耗率")
  38 + private String attritionRate;
  39 +
  40 + /**
  41 + * 杂率
  42 + */
  43 + @ExcelProperty("杂率")
  44 + private String miscellaneousTaxRate;
  45 +
  46 + /**
  47 + * 损耗率转换后的值
  48 + */
  49 + @ExcelIgnore
  50 + private java.math.BigDecimal attritionRateValue;
  51 +
  52 + /**
  53 + * 杂率转换后的值
  54 + */
  55 + @ExcelIgnore
  56 + private java.math.BigDecimal miscellaneousTaxRateValue;
  57 +
  58 +}
@@ -129,6 +129,17 @@ public class BreedRelationshipServiceImpl extends BaseMpServiceImpl<BreedRelatio @@ -129,6 +129,17 @@ public class BreedRelationshipServiceImpl extends BaseMpServiceImpl<BreedRelatio
129 } 129 }
130 } 130 }
131 131
  132 + /**
  133 + * 校验品名是否已存在
  134 + *
  135 + * @param productName 品名
  136 + */
  137 + public Integer checkProductName(String productName) {
  138 + LambdaQueryWrapper<BreedRelationship> queryWrapper = Wrappers.lambdaQuery(BreedRelationship.class)
  139 + .eq(BreedRelationship::getProductName, productName);
  140 + return getBaseMapper().selectCount(queryWrapper);
  141 + }
  142 +
132 @Override 143 @Override
133 public void cleanCacheByKey(Serializable key) { 144 public void cleanCacheByKey(Serializable key) {
134 145
@@ -52,4 +52,6 @@ public interface BreedRelationshipService extends BaseMpService<BreedRelationshi @@ -52,4 +52,6 @@ public interface BreedRelationshipService extends BaseMpService<BreedRelationshi
52 * @return 52 * @return
53 */ 53 */
54 void deleteById(String id); 54 void deleteById(String id);
  55 +
  56 + Integer checkProductName(String productName);
55 } 57 }