|
@@ -1,10 +1,14 @@
|
|
|
package com.kingtom.shengtai.api.controller;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.MessageFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
|
import cn.dev33.satoken.annotation.SaCheckRole;
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
import com.kingtom.kirin.api.security.SaSecurityUtils;
|
|
|
import com.kingtom.kirin.api.system.mapper.SystemMapper;
|
|
|
import com.kingtom.kirin.api.system.model.SystemUserDTO;
|
|
@@ -14,23 +18,30 @@ import com.kingtom.kirin.app.system.model.SystemUser;
|
|
|
import com.kingtom.kirin.core.common.CommonConst;
|
|
|
import com.kingtom.kirin.core.common.base.PageInfo;
|
|
|
import com.kingtom.kirin.core.common.base.Result;
|
|
|
+import com.kingtom.kirin.core.common.exception.AppException;
|
|
|
import com.kingtom.kirin.core.common.utils.CollectionUtils;
|
|
|
+import com.kingtom.kirin.core.common.utils.IDUtils;
|
|
|
import com.kingtom.kirin.core.common.utils.JsonUtils;
|
|
|
import com.kingtom.shengtai.api.mapper.SrmMapper;
|
|
|
import com.kingtom.shengtai.api.model.SrmSupplierDTO;
|
|
|
+import com.kingtom.shengtai.api.model.SrmSupplierImportDTO;
|
|
|
+import com.kingtom.shengtai.app.srm.excel.SrmSupplierImportTask;
|
|
|
import com.kingtom.shengtai.app.srm.model.ProductDir;
|
|
|
-import com.kingtom.shengtai.app.srm.service.IProductDirService;
|
|
|
import com.kingtom.shengtai.app.srm.model.SrmSupplier;
|
|
|
import com.kingtom.shengtai.app.srm.model.SrmSupplierDO;
|
|
|
+import com.kingtom.shengtai.app.srm.service.IProductDirService;
|
|
|
import com.kingtom.shengtai.app.srm.service.ISrmSupplierService;
|
|
|
import com.mybatisflex.core.paginate.Page;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
/**
|
|
|
* 供应商信息表 控制层。
|
|
@@ -41,6 +52,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
@Tag(name = "SrmSupplierController", description = "供应商信息表 控制层。")
|
|
|
@RestController
|
|
|
@RequestMapping("/srmSuppliers")
|
|
|
+@Slf4j
|
|
|
public class SrmSupplierController{
|
|
|
|
|
|
@Autowired
|
|
@@ -52,8 +64,7 @@ public class SrmSupplierController{
|
|
|
/**
|
|
|
* 添加供应商信息表。
|
|
|
*
|
|
|
- * @param dto
|
|
|
- * 供应商信息表
|
|
|
+ * @param dto 供应商信息表
|
|
|
* @return {@code true} 添加成功,{@code false} 添加失败
|
|
|
*/
|
|
|
@Operation(summary = "添加供应商信息表。", description = "添加供应商信息表。")
|
|
@@ -68,8 +79,7 @@ public class SrmSupplierController{
|
|
|
/**
|
|
|
* 根据主键删除供应商信息表。
|
|
|
*
|
|
|
- * @param id
|
|
|
- * 主键
|
|
|
+ * @param id 主键
|
|
|
* @return {@code true} 删除成功,{@code false} 删除失败
|
|
|
*/
|
|
|
@Parameter(name = "id", description = "主键", in = ParameterIn.PATH, required = true)
|
|
@@ -84,8 +94,7 @@ public class SrmSupplierController{
|
|
|
/**
|
|
|
* 根据主键删除供应商信息表。
|
|
|
*
|
|
|
- * @param ids
|
|
|
- * 主键
|
|
|
+ * @param ids 主键
|
|
|
* @return {@code true} 删除成功,{@code false} 删除失败
|
|
|
*/
|
|
|
@Operation(summary = "根据主键批量删除供应商信息表。", description = "根据主键批量删除供应商信息表。")
|
|
@@ -99,8 +108,7 @@ public class SrmSupplierController{
|
|
|
/**
|
|
|
* 根据主键更新供应商信息表。
|
|
|
*
|
|
|
- * @param dto
|
|
|
- * 供应商信息表
|
|
|
+ * @param dto 供应商信息表
|
|
|
* @return {@code true} 更新成功,{@code false} 更新失败
|
|
|
*/
|
|
|
@Parameter(name = "id", description = "", in = ParameterIn.PATH, required = true)
|
|
@@ -125,8 +133,7 @@ public class SrmSupplierController{
|
|
|
/**
|
|
|
* 根据供应商信息表主键获取详细信息。
|
|
|
*
|
|
|
- * @param id
|
|
|
- * 供应商信息表主键
|
|
|
+ * @param id 供应商信息表主键
|
|
|
* @return 供应商信息表详情
|
|
|
*/
|
|
|
@Parameter(name = "id", description = "供应商信息表主键", in = ParameterIn.PATH, required = true)
|
|
@@ -141,12 +148,9 @@ public class SrmSupplierController{
|
|
|
/**
|
|
|
* 分页查询供应商信息表。
|
|
|
*
|
|
|
- * @param query
|
|
|
- * 查询条件
|
|
|
- * @param pageNum
|
|
|
- * 当前页码
|
|
|
- * @param pageSize
|
|
|
- * 每页数量
|
|
|
+ * @param query 查询条件
|
|
|
+ * @param pageNum 当前页码
|
|
|
+ * @param pageSize 每页数量
|
|
|
* @return 分页对象
|
|
|
*/
|
|
|
@Operation(summary = "分页查询供应商信息表。", description = "分页查询供应商信息表。")
|
|
@@ -159,11 +163,110 @@ public class SrmSupplierController{
|
|
|
String state = object.get("state") instanceof String ? object.get("state").toString() : null;
|
|
|
List<String> products = object.get("products") instanceof Collection<?> ?
|
|
|
((Collection<?>)object.get("products")).stream().map(e -> e instanceof String ? ((String)e) : null)
|
|
|
- .collect(Collectors.toList()) : null;
|
|
|
+ .collect(Collectors.toList()) : null;
|
|
|
Page<SrmSupplierDO> page = iSrmSupplierService.page(name, type, state, products, pageNum, pageSize);
|
|
|
return Result.ok(CollectionUtils.convertPage(page, SrmSupplierDTO::convert));
|
|
|
}
|
|
|
|
|
|
+ @Operation(description = "获取导入供应商的excel模板")
|
|
|
+ @GetMapping("/importTemplate")
|
|
|
+ @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
|
|
|
+ public void getSuppliersImportTemplate(HttpServletResponse response) throws IOException{
|
|
|
+ Page<SrmSupplierDO> page = iSrmSupplierService.page(null, null, null, null, 1, 1);
|
|
|
+ SrmSupplierDO supplierDO = CollectionUtils.isEmpty(page.getRecords()) ? null : page.getRecords().get(0);
|
|
|
+ SrmSupplierDTO srmSupplierDTO = SrmSupplierDTO.convert(supplierDO);
|
|
|
+ SrmSupplierImportDTO dto = new SrmSupplierImportDTO();
|
|
|
+ dto.setName(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getName()) ? "供应商" :
|
|
|
+ srmSupplierDTO.getName());
|
|
|
+ dto.setMark(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getMark()) ? "1" :
|
|
|
+ srmSupplierDTO.getMark());
|
|
|
+ dto.setPerson(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getPersonName()) ? "张三" :
|
|
|
+ srmSupplierDTO.getPersonName());
|
|
|
+ dto.setPhone(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getPhone()) ? "13188888888" :
|
|
|
+ srmSupplierDTO.getPhone());
|
|
|
+ dto.setType(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getType()) ? "类型" :
|
|
|
+ srmSupplierDTO.getType());
|
|
|
+ dto.setAddress(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getAddress()) ? "公司地址" :
|
|
|
+ srmSupplierDTO.getAddress());
|
|
|
+ dto.setIntro(srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getIntro()) ? "公司简介" :
|
|
|
+ srmSupplierDTO.getIntro());
|
|
|
+ if(srmSupplierDTO != null && CollectionUtils.isNotEmpty(srmSupplierDTO.getProduct())){
|
|
|
+ List<ProductDir> dirs = productDirService.findByIds(srmSupplierDTO.getProduct());
|
|
|
+ String collect = dirs.stream().map(ProductDir::getName).collect(Collectors.joining(","));
|
|
|
+ dto.setProduct(StringUtils.isBlank(collect) ? "A产品,B产品" : collect);
|
|
|
+ } else{
|
|
|
+ dto.setProduct("A产品,B产品");
|
|
|
+ }
|
|
|
+ dto.setSuperiorProduct(
|
|
|
+ srmSupplierDTO == null || StringUtils.isBlank(srmSupplierDTO.getSuperiorProduct()) ? "优势产品描述" :
|
|
|
+ srmSupplierDTO.getSuperiorProduct());
|
|
|
+
|
|
|
+ //这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
|
|
+ try{
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + IDUtils.newUUID() + ".xlsx");
|
|
|
+ // 这里需要设置不关闭流
|
|
|
+ EasyExcel.write(response.getOutputStream(), SrmSupplierImportDTO.class).autoCloseStream(Boolean.FALSE)
|
|
|
+ .sheet("模板").doWrite(List.of(dto));
|
|
|
+ } catch(Exception e){
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ // 重置response
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/json");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ throw new AppException("下载文件失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(description = "批量导入供应商")
|
|
|
+ @PostMapping("/import")
|
|
|
+ @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
|
|
|
+ public Result<Map<String, String>> importSuppliers(@RequestPart("file") MultipartFile file){
|
|
|
+ if(file == null || file.isEmpty()){
|
|
|
+ throw new AppException("导入文件不存在!");
|
|
|
+ }
|
|
|
+ List<SrmSupplierImportDTO> list = null;
|
|
|
+ try{
|
|
|
+ list = EasyExcel.read(file.getInputStream()).head(SrmSupplierImportDTO.class).sheet().doReadSync();
|
|
|
+ } catch(IOException e){
|
|
|
+ throw new AppException("导入文件不存在!");
|
|
|
+ }
|
|
|
+ if(list.isEmpty()){
|
|
|
+ throw new AppException("导入数据为空!");
|
|
|
+ }
|
|
|
+ //用于存储每个任务的Future对象
|
|
|
+ List<Future<String[]>> furtures = new ArrayList<>();
|
|
|
+ // 创建线程池
|
|
|
+ ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
|
+ // 创建CountDownLatch
|
|
|
+ CountDownLatch latch = new CountDownLatch(list.size());
|
|
|
+ for(int i = 0; i < list.size(); i++){
|
|
|
+ SrmSupplierImportTask task = new SrmSupplierImportTask(i, latch, list.get(i));
|
|
|
+ Future<String[]> submit = executor.submit(task);
|
|
|
+ furtures.add(submit);
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ // 等待所有导入任务完成
|
|
|
+ latch.await();
|
|
|
+ } catch(InterruptedException e){
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ log.error("Thread interrupted: {}", e.getMessage());
|
|
|
+ } finally{
|
|
|
+ // 关闭线程池
|
|
|
+ executor.shutdown();
|
|
|
+ }
|
|
|
+ Map<String, String> errorInfo = furtures.stream().map(e -> {
|
|
|
+ try{
|
|
|
+ return e.get();
|
|
|
+ } catch(InterruptedException | ExecutionException ex){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }).filter(Objects::nonNull).collect(Collectors.toMap(e -> e[0], e -> e[1]));
|
|
|
+ String msg = MessageFormat.format("导入完成,失败{0}条!", errorInfo.size());
|
|
|
+ return Result.ok(msg, errorInfo);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 我的供应商。
|
|
|
*
|
|
@@ -232,8 +335,8 @@ public class SrmSupplierController{
|
|
|
}
|
|
|
List<String> productIds = JsonUtils.toObjectList(supplier.getProduct(), String.class);
|
|
|
List<ProductDir> dirs = productDirService.findByIds(productIds);
|
|
|
- LinkedHashMap<String,String> map = new LinkedHashMap<>(dirs.size());
|
|
|
- dirs.forEach(e-> map.put(e.getCode(),e.getName()));
|
|
|
+ LinkedHashMap<String, String> map = new LinkedHashMap<>(dirs.size());
|
|
|
+ dirs.forEach(e -> map.put(e.getCode(), e.getName()));
|
|
|
return Result.ok(map);
|
|
|
}
|
|
|
|