物料导入
parent
6c483a7118
commit
2441611940
|
|
@ -22,6 +22,7 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @website https://eladmin.vip
|
* @website https://eladmin.vip
|
||||||
|
|
@ -37,4 +38,7 @@ public interface ItemRepository extends JpaRepository<Item, Long>, JpaSpecificat
|
||||||
|
|
||||||
@Query(value = "SELECT DISTINCT b.* FROM `data_item_key` a INNER JOIN `base_item` b on a.item_id = b.id", nativeQuery = true)
|
@Query(value = "SELECT DISTINCT b.* FROM `data_item_key` a INNER JOIN `base_item` b on a.item_id = b.id", nativeQuery = true)
|
||||||
List<Item> quryOneItemAll();
|
List<Item> quryOneItemAll();
|
||||||
|
|
||||||
|
@Query(value = "from Item i where i.code in :itemCodes ")
|
||||||
|
List<Item> findByCodes(Set itemCodes);
|
||||||
}
|
}
|
||||||
|
|
@ -79,8 +79,6 @@ import java.util.Map;
|
||||||
@RequestMapping("/api/importData")
|
@RequestMapping("/api/importData")
|
||||||
public class ImportDataController {
|
public class ImportDataController {
|
||||||
private final FileProperties properties;
|
private final FileProperties properties;
|
||||||
private final BigItemRepository bigItemRepository;
|
|
||||||
private final ItemRepository itemRepository;
|
|
||||||
private final ImportDataService importDataService;
|
private final ImportDataService importDataService;
|
||||||
private final AsnService asnService;
|
private final AsnService asnService;
|
||||||
private final PickTicketService pickTicketService;
|
private final PickTicketService pickTicketService;
|
||||||
|
|
@ -112,23 +110,14 @@ public class ImportDataController {
|
||||||
@Transactional
|
@Transactional
|
||||||
@AnonymousAccess
|
@AnonymousAccess
|
||||||
public ResponseEntity<Object> importItem( @RequestParam("file") MultipartFile multipartFile) {
|
public ResponseEntity<Object> importItem( @RequestParam("file") MultipartFile multipartFile) {
|
||||||
List<Map<String, Object>> readAll = getMaps(multipartFile);
|
|
||||||
int a=0;//新增
|
//编码、名称、物料类型
|
||||||
int b=0;//修改
|
try {
|
||||||
for (int i = 0; i < readAll.size(); i++) {
|
String result = importDataService.importItem(multipartFile);
|
||||||
Map<String, Object> map=readAll.get(i);
|
return new ResponseEntity(result, HttpStatus.OK);
|
||||||
String code =(String) map.get("品番");
|
} catch (Exception e) {
|
||||||
if(code==null||code.equals("")){
|
return new ResponseEntity("导入失败:" + e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
String re = importDataService.importItem(map);
|
|
||||||
if (re.equals("a")){
|
|
||||||
a=a+1;
|
|
||||||
}else if (re.equals("b")){
|
|
||||||
b=b+1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new ResponseEntity("导入成功:执行"+(a+b)+"行 新增"+a+"行,修改"+b+"行",HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
@Log("导入其他入库")
|
@Log("导入其他入库")
|
||||||
@ApiOperation("导入其他入库")
|
@ApiOperation("导入其他入库")
|
||||||
|
|
|
||||||
|
|
@ -42,10 +42,10 @@ public interface ImportDataService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param stringObjectMap
|
* @param multipartFile web上传的文件
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
String importItem(Map<String, Object> stringObjectMap);
|
String importItem(MultipartFile multipartFile);
|
||||||
|
|
||||||
String importBigItem(Map<String, Object> stringObjectMap);
|
String importBigItem(Map<String, Object> stringObjectMap);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Set;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -112,4 +113,12 @@ public interface ItemService {
|
||||||
*/
|
*/
|
||||||
Item existItem(String itemCode);
|
Item existItem(String itemCode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据物料编码查询物料信息
|
||||||
|
*
|
||||||
|
* @param itemCodes
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Map<String, Item> findByCodes(Set itemCodes);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
package com.youchain.basicdata.service.impl;
|
package com.youchain.basicdata.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.poi.excel.ExcelReader;
|
||||||
|
import cn.hutool.poi.excel.ExcelUtil;
|
||||||
import com.youchain.basicdata.domain.*;
|
import com.youchain.basicdata.domain.*;
|
||||||
import com.youchain.basicdata.repository.*;
|
import com.youchain.basicdata.repository.*;
|
||||||
import com.youchain.basicdata.service.*;
|
import com.youchain.basicdata.service.*;
|
||||||
|
|
@ -30,6 +32,7 @@ import com.youchain.businessdata.domain.PickTicket;
|
||||||
import com.youchain.businessdata.service.*;
|
import com.youchain.businessdata.service.*;
|
||||||
import com.youchain.config.FileProperties;
|
import com.youchain.config.FileProperties;
|
||||||
import com.youchain.exception.BadRequestException;
|
import com.youchain.exception.BadRequestException;
|
||||||
|
import com.youchain.modules.system.domain.Dept;
|
||||||
import com.youchain.modules.system.domain.DictDetail;
|
import com.youchain.modules.system.domain.DictDetail;
|
||||||
import com.youchain.modules.system.repository.DictDetailRepository;
|
import com.youchain.modules.system.repository.DictDetailRepository;
|
||||||
import com.youchain.modules.system.repository.DictRepository;
|
import com.youchain.modules.system.repository.DictRepository;
|
||||||
|
|
@ -62,13 +65,9 @@ public class ImportDataServiceImpl implements ImportDataService {
|
||||||
private final BigItemRepository bigItemRepository;
|
private final BigItemRepository bigItemRepository;
|
||||||
private final AreaRepository areaRepository;
|
private final AreaRepository areaRepository;
|
||||||
private final ItemRepository itemRepository;
|
private final ItemRepository itemRepository;
|
||||||
private final DictDetailRepository dictDetailRepository;
|
|
||||||
private final PointRepository pointRepository;
|
private final PointRepository pointRepository;
|
||||||
private final PointService pointService;
|
private final PointService pointService;
|
||||||
private final AreaService areaService;
|
|
||||||
private final BomAccountRepository bomAccountRepository;
|
private final BomAccountRepository bomAccountRepository;
|
||||||
private final AreaMapper areaMapper;
|
|
||||||
private final PointMapper pointMapper;
|
|
||||||
private final ItemService itemService;
|
private final ItemService itemService;
|
||||||
private final AsnDetailService asnDetailService;
|
private final AsnDetailService asnDetailService;
|
||||||
private final AsnService asnService;
|
private final AsnService asnService;
|
||||||
|
|
@ -78,6 +77,7 @@ public class ImportDataServiceImpl implements ImportDataService {
|
||||||
private final BigItemService bigItemService;
|
private final BigItemService bigItemService;
|
||||||
private final BomAccountService bomAccountService;
|
private final BomAccountService bomAccountService;
|
||||||
private final BomAccountLogService bomAccountLogService;
|
private final BomAccountLogService bomAccountLogService;
|
||||||
|
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importBomAccount(int row, Map<String, Object> readAll) {
|
public void importBomAccount(int row, Map<String, Object> readAll) {
|
||||||
|
|
@ -179,39 +179,113 @@ public class ImportDataServiceImpl implements ImportDataService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String importItem(Map<String, Object> readAll) {
|
public String importItem(MultipartFile multipartFile) {
|
||||||
String re = "";
|
//编码、名称、物料类型
|
||||||
String code = readAll.get("品番")==null?null:readAll.get("品番").toString();
|
FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
|
||||||
String name = readAll.get("品名")==null?null:readAll.get("品名").toString();
|
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
||||||
String specs = readAll.get("规格")==null?null:readAll.get("规格").toString();
|
String type = FileUtil.getFileType(suffix);
|
||||||
String unit = readAll.get("单位")==null?null:readAll.get("单位").toString();
|
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
|
||||||
String hz = readAll.get("荷资")==null?null:readAll.get("荷资").toString();
|
Dept dept = UserUtils.getDept();
|
||||||
String srs =readAll.get("收容数")==null?null:readAll.get("收容数").toString();
|
ExcelReader reader = ExcelUtil.getReader(file);
|
||||||
if (code.length() > 0) {
|
List<Map<String, Object>> readAll = reader.readAll();
|
||||||
Item item = itemRepository.findByAllCode(code);
|
Set<String> itemCodes = new HashSet<>();//物料集合
|
||||||
if (item == null) {
|
for (Map<String, Object> record : readAll) {
|
||||||
re = "a";
|
String itemCode = record.get("料号").toString().trim();
|
||||||
item = new Item();
|
if (!StringUtils.isEmpty(itemCode)) {
|
||||||
item.setDept(UserUtils.getDept());
|
itemCodes.add(itemCode);
|
||||||
item.setCode(code);
|
}
|
||||||
} else {
|
|
||||||
re = "b";
|
|
||||||
}
|
}
|
||||||
item.setName(name);
|
|
||||||
|
|
||||||
if (unit!=null&&!unit.equals("")) {
|
|
||||||
DictDetail dictDetail = dictDetailRepository.findByDictAndLabel("item_unit", unit);
|
//获取已存在的物料
|
||||||
item.setUnit(dictDetail.getValue());
|
Map<String, Item> existingPoint = itemService.findByCodes(itemCodes);
|
||||||
}
|
|
||||||
if (re.equals("a")){
|
|
||||||
itemRepository.save(item);
|
List<Item> itemsToInsert= new ArrayList<>();//新增物料集合
|
||||||
}else if (re.equals("b")){
|
List<Item> itemsToUpdate = new ArrayList<>();//修改物料集合
|
||||||
itemService.update(item);
|
for (Map<String, Object> record : readAll) {
|
||||||
|
String itemCode = record.get("料号").toString().trim();
|
||||||
|
//判断是否已存在物料
|
||||||
|
if (existingPoint.containsKey(itemCode)) {
|
||||||
|
Item item = existingPoint.get(itemCode);
|
||||||
|
//更新物料
|
||||||
|
itemsToUpdate.add(updateItems(item, dept, record));
|
||||||
|
} else {
|
||||||
|
//新增点位
|
||||||
|
itemsToInsert.add(createItem(dept, record));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return re;
|
|
||||||
|
//批量新增物料
|
||||||
|
if (!itemsToInsert.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchInsert(itemsToInsert);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//批量更新物料
|
||||||
|
if (!itemsToUpdate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchUpdate(itemsToUpdate);
|
||||||
|
}
|
||||||
|
return ("导入成功:" + " 新增(" + itemsToInsert.size() + ")修改(" + itemsToUpdate.size() + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Item updateItems(Item item, Dept dept, Map<String, Object> record) {
|
||||||
|
item.setDept(dept);
|
||||||
|
item.setName(record.get("物料描述").toString().trim());
|
||||||
|
item.setPackageType(record.get("包装类型").toString().trim());
|
||||||
|
item.setSpecQuantity(record.get("规格数量") == null ? 0 : Double.parseDouble(record.get("规格数量").toString()));
|
||||||
|
item.setSpecWeight(record.get("规格重量") == null ? 0 : Double.parseDouble(record.get("规格重量").toString()));
|
||||||
|
item.setGrossWeight(record.get("毛重") == null ? 0 : Double.parseDouble(record.get("毛重").toString()));
|
||||||
|
item.setLength(record.get("长") == null ? 0 : Double.parseDouble(record.get("长").toString()));
|
||||||
|
item.setWidth(record.get("宽") == null ? 0 : Double.parseDouble(record.get("宽").toString()));
|
||||||
|
item.setHeight(record.get("高") == null ? 0 : Double.parseDouble(record.get("高").toString()));
|
||||||
|
item.setVolume(record.get("体积") == null ? 0 : Double.parseDouble(record.get("体积").toString()));
|
||||||
|
item.setUnit(record.get("单位").toString().trim());
|
||||||
|
item.setType(record.get("类型").toString().trim());
|
||||||
|
item.setValueGrade(record.get("物料价值等级").toString().trim());
|
||||||
|
item.setValidPeriod(record.get("保质期") == null ? 0 : Integer.parseInt(record.get("保质期").toString()));
|
||||||
|
item.setAlertDays(record.get("预警天数") == null ? 0 : Integer.parseInt(record.get("预警天数").toString()));
|
||||||
|
item.setIsBatch("是".equals(record.get("是否批次").toString().trim()) ? true : false);
|
||||||
|
item.setIsSerial("是".equals(record.get("是否序列号").toString().trim()) ? true : false);
|
||||||
|
item.setIsValidPeriod("是".equals(record.get("是否有效期").toString().trim()) ? true : false);
|
||||||
|
item.setIsHazardous("是".equals(record.get("是否危化品").toString().trim()) ? true : false);
|
||||||
|
item.setIsInvAvailable("是".equals(record.get("是否有库存").toString().trim()) ? true : false);
|
||||||
|
item.setIsSapMaterial("是".equals(record.get("是否SAP料号").toString().trim()) ? true : false);
|
||||||
|
item.setHeatValue(record.get("热度值") == null ? 0 : Integer.parseInt(record.get("热度值").toString()));
|
||||||
|
item.setDescription(record.get("备注").toString().trim());
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Item createItem(Dept dept, Map<String, Object> record) {
|
||||||
|
Item item = new Item();
|
||||||
|
item.setCode(record.get("料号").toString().trim());
|
||||||
|
item.setName(record.get("物料描述").toString().trim());
|
||||||
|
item.setPackageType(record.get("包装类型").toString().trim());
|
||||||
|
item.setSpecQuantity(record.get("规格数量") == null ? 0 : Double.parseDouble(record.get("规格数量").toString()));
|
||||||
|
item.setSpecWeight(record.get("规格重量") == null ? 0 : Double.parseDouble(record.get("规格重量").toString()));
|
||||||
|
item.setGrossWeight(record.get("毛重") == null ? 0 : Double.parseDouble(record.get("毛重").toString()));
|
||||||
|
item.setLength(record.get("长") == null ? 0 : Double.parseDouble(record.get("长").toString()));
|
||||||
|
item.setWidth(record.get("宽") == null ? 0 : Double.parseDouble(record.get("宽").toString()));
|
||||||
|
item.setHeight(record.get("高") == null ? 0 : Double.parseDouble(record.get("高").toString()));
|
||||||
|
item.setVolume(record.get("体积") == null ? 0 : Double.parseDouble(record.get("体积").toString()));
|
||||||
|
item.setUnit(record.get("单位").toString().trim());
|
||||||
|
item.setType(record.get("类型").toString().trim());
|
||||||
|
item.setValueGrade(record.get("物料价值等级").toString().trim());
|
||||||
|
item.setValidPeriod(record.get("保质期") == null ? 0 : Integer.parseInt(record.get("保质期").toString()));
|
||||||
|
item.setAlertDays(record.get("预警天数") == null ? 0 : Integer.parseInt(record.get("预警天数").toString()));
|
||||||
|
item.setIsBatch("是".equals(record.get("是否批次").toString().trim()) ? true : false);
|
||||||
|
item.setIsSerial("是".equals(record.get("是否序列号").toString().trim()) ? true : false);
|
||||||
|
item.setIsValidPeriod("是".equals(record.get("是否有效期").toString().trim()) ? true : false);
|
||||||
|
item.setIsHazardous("是".equals(record.get("是否危化品").toString().trim()) ? true : false);
|
||||||
|
item.setIsInvAvailable("是".equals(record.get("是否有库存").toString().trim()) ? true : false);
|
||||||
|
item.setIsSapMaterial("是".equals(record.get("是否SAP料号").toString().trim()) ? true : false);
|
||||||
|
item.setHeatValue(record.get("热度值") == null ? 0 : Integer.parseInt(record.get("热度值").toString()));
|
||||||
|
item.setDescription(record.get("备注").toString().trim());
|
||||||
|
item.setDept(dept);
|
||||||
|
item.setEnabled(true);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String importBigItem(Map<String, Object> readAll) {
|
public String importBigItem(Map<String, Object> readAll) {
|
||||||
String re = "";
|
String re = "";
|
||||||
|
|
@ -228,7 +302,8 @@ public class ImportDataServiceImpl implements ImportDataService {
|
||||||
bigItem.setCode(code);
|
bigItem.setCode(code);
|
||||||
bigItem.setDeptId(UserUtils.getDept().getId());
|
bigItem.setDeptId(UserUtils.getDept().getId());
|
||||||
} else {
|
} else {
|
||||||
re = "b";
|
//新增物料
|
||||||
|
//itemsToCreate.add(createItem(dept, record));
|
||||||
}
|
}
|
||||||
bigItem.setModels(models);
|
bigItem.setModels(models);
|
||||||
bigItem.setName(name);
|
bigItem.setName(name);
|
||||||
|
|
|
||||||
|
|
@ -29,16 +29,14 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.Query;
|
import javax.persistence.Query;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import org.springframework.data.domain.Example;
|
import org.springframework.data.domain.Example;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @website https://eladmin.vip
|
* @website https://eladmin.vip
|
||||||
|
|
@ -138,4 +136,14 @@ public class ItemServiceImpl implements ItemService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Item> findByCodes(Set itemCodes) {
|
||||||
|
List<Item> items = itemRepository.findByCodes(itemCodes);
|
||||||
|
Map<String, Item> itemMap = new HashMap<>();
|
||||||
|
for (Item item : items) {
|
||||||
|
itemMap.put(item.getCode(), item);
|
||||||
|
}
|
||||||
|
return itemMap;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.youchain.utils;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BatchCreateOrUpdate {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
private int batchSize=500;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量插入
|
||||||
|
*
|
||||||
|
* @param list 实体类集合
|
||||||
|
* @param <T> 表对应的实体类
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public <T> void batchInsert(List<T> list) {
|
||||||
|
if (list == null || list.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
entityManager.persist(list.get(i));
|
||||||
|
if (i % batchSize == 0) {
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新
|
||||||
|
*
|
||||||
|
* @param list 实体类集合
|
||||||
|
* @param <T> 表对应的实体类
|
||||||
|
*/
|
||||||
|
@Transactional
|
||||||
|
public <T> void batchUpdate(List<T> list) {
|
||||||
|
if (list == null || list.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
entityManager.merge(list.get(i));
|
||||||
|
if (i % batchSize == 0) {
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entityManager.flush();
|
||||||
|
entityManager.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue