优化商品导入功能

This commit is contained in:
季圣华
2021-08-20 00:33:07 +08:00
parent d7ade048be
commit 81465e5dd6
3 changed files with 89 additions and 78 deletions

View File

@@ -287,6 +287,9 @@ public class ExceptionConstants {
//商品条码重复 //商品条码重复
public static final int MATERIAL_BARCODE_EXISTS_CODE = 8000005; public static final int MATERIAL_BARCODE_EXISTS_CODE = 8000005;
public static final String MATERIAL_BARCODE_EXISTS_MSG = "商品条码:%s重复"; public static final String MATERIAL_BARCODE_EXISTS_MSG = "商品条码:%s重复";
//商品-单位匹配不上
public static final int MATERIAL_UNIT_MATE_CODE = 8000006;
public static final String MATERIAL_UNIT_MATE_MSG = "抱歉,单位匹配不上,请完善计量单位信息!";
/** /**
* 单据信息 * 单据信息
* type = 85 * type = 85

View File

@@ -441,88 +441,93 @@ public class MaterialService {
@Transactional(value = "transactionManager", rollbackFor = Exception.class) @Transactional(value = "transactionManager", rollbackFor = Exception.class)
public BaseResponseInfo importExcel(Sheet src, HttpServletRequest request) throws Exception { public BaseResponseInfo importExcel(Sheet src, HttpServletRequest request) throws Exception {
List<Depot> depotList= depotService.getDepot(); BaseResponseInfo info = new BaseResponseInfo();
int depotCount = depotList.size(); try {
List<MaterialWithInitStock> mList = new ArrayList<>(); List<Depot> depotList= depotService.getDepot();
for (int i = 2; i < src.getRows(); i++) { int depotCount = depotList.size();
String name = ExcelUtils.getContent(src, i, 0); //名称 List<MaterialWithInitStock> mList = new ArrayList<>();
String standard = ExcelUtils.getContent(src, i, 1); //规格 for (int i = 2; i < src.getRows(); i++) {
String model = ExcelUtils.getContent(src, i, 2); //型号 String name = ExcelUtils.getContent(src, i, 0); //名称
String color = ExcelUtils.getContent(src, i, 3); //颜色 String standard = ExcelUtils.getContent(src, i, 1); //规格
String categoryName = ExcelUtils.getContent(src, i, 4); //类别 String model = ExcelUtils.getContent(src, i, 2); //型号
String safetyStock = ExcelUtils.getContent(src, i, 5); //安全存量 String color = ExcelUtils.getContent(src, i, 3); //颜色
String unit = ExcelUtils.getContent(src, i, 6); //基础单位 String categoryName = ExcelUtils.getContent(src, i, 4); //类别
//校验名称、单位是否为空 String safetyStock = ExcelUtils.getContent(src, i, 5); //安全存量
if(StringUtil.isNotEmpty(name) && StringUtil.isNotEmpty(unit)) { String unit = ExcelUtils.getContent(src, i, 6); //基础单位
MaterialWithInitStock m = new MaterialWithInitStock(); //校验名称、单位是否为空
m.setName(name); if(StringUtil.isNotEmpty(name) && StringUtil.isNotEmpty(unit)) {
m.setStandard(standard); MaterialWithInitStock m = new MaterialWithInitStock();
m.setModel(model); m.setName(name);
m.setColor(color); m.setStandard(standard);
Long categoryId = materialCategoryService.getCategoryIdByName(categoryName); m.setModel(model);
if(null!=categoryId){ m.setColor(color);
m.setCategoryId(categoryId); Long categoryId = materialCategoryService.getCategoryIdByName(categoryName);
} if(null!=categoryId){
m.setSafetyStock(parseBigDecimalEx(safetyStock)); m.setCategoryId(categoryId);
String manyUnit = ExcelUtils.getContent(src, i, 7); //副单位 }
String barCode = ExcelUtils.getContent(src, i, 8); //基础条码 m.setSafetyStock(parseBigDecimalEx(safetyStock));
String manyBarCode = ExcelUtils.getContent(src, i, 9); //副条码 String manyUnit = ExcelUtils.getContent(src, i, 7); //副单位
String ratio = ExcelUtils.getContent(src, i, 10); //比例 String barCode = ExcelUtils.getContent(src, i, 8); //基础条码
String purchaseDecimal = ExcelUtils.getContent(src, i, 11); //采购价 String manyBarCode = ExcelUtils.getContent(src, i, 9); //副条码
String commodityDecimal = ExcelUtils.getContent(src, i, 12); //零售价 String ratio = ExcelUtils.getContent(src, i, 10); //比例
String wholesaleDecimal = ExcelUtils.getContent(src, i, 13); //销售 String purchaseDecimal = ExcelUtils.getContent(src, i, 11); //采购
String lowDecimal = ExcelUtils.getContent(src, i, 14); //最低售价 String commodityDecimal = ExcelUtils.getContent(src, i, 12); //售价
JSONObject materialExObj = new JSONObject(); String wholesaleDecimal = ExcelUtils.getContent(src, i, 13); //销售价
JSONObject basicObj = new JSONObject(); String lowDecimal = ExcelUtils.getContent(src, i, 14); //最低售价
basicObj.put("barCode", barCode); JSONObject materialExObj = new JSONObject();
basicObj.put("commodityUnit", unit); JSONObject basicObj = new JSONObject();
basicObj.put("purchaseDecimal", purchaseDecimal); basicObj.put("barCode", barCode);
basicObj.put("commodityDecimal", commodityDecimal); basicObj.put("commodityUnit", unit);
basicObj.put("wholesaleDecimal", wholesaleDecimal); basicObj.put("purchaseDecimal", purchaseDecimal);
basicObj.put("lowDecimal", lowDecimal); basicObj.put("commodityDecimal", commodityDecimal);
materialExObj.put("basic", basicObj); basicObj.put("wholesaleDecimal", wholesaleDecimal);
if(StringUtil.isNotEmpty(manyUnit.trim())){ //多单位 basicObj.put("lowDecimal", lowDecimal);
String manyUnitAll = unit + "," + manyUnit + "(1:" + ratio + ")"; materialExObj.put("basic", basicObj);
Long unitId = unitService.getUnitIdByName(manyUnitAll); if(StringUtil.isNotEmpty(manyUnit.trim())){ //多单位
m.setUnitId(unitId); String manyUnitAll = unit + "," + manyUnit + "(1:" + ratio + ")";
JSONObject otherObj = new JSONObject(); Long unitId = unitService.getUnitIdByName(manyUnitAll);
otherObj.put("barCode", manyBarCode); if(unitId != null) {
otherObj.put("commodityUnit", manyUnit); m.setUnitId(unitId);
otherObj.put("purchaseDecimal", parsePrice(purchaseDecimal,ratio)); } else {
otherObj.put("commodityDecimal", parsePrice(commodityDecimal,ratio)); throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_UNIT_MATE_CODE,
otherObj.put("wholesaleDecimal", parsePrice(wholesaleDecimal,ratio)); String.format(ExceptionConstants.MATERIAL_UNIT_MATE_MSG));
otherObj.put("lowDecimal", parsePrice(lowDecimal,ratio)); }
materialExObj.put("other", otherObj); JSONObject otherObj = new JSONObject();
} else { otherObj.put("barCode", manyBarCode);
m.setUnit(unit); otherObj.put("commodityUnit", manyUnit);
} otherObj.put("purchaseDecimal", parsePrice(purchaseDecimal,ratio));
m.setMaterialExObj(materialExObj); otherObj.put("commodityDecimal", parsePrice(commodityDecimal,ratio));
String enabled = ExcelUtils.getContent(src, i, 15); //状态 otherObj.put("wholesaleDecimal", parsePrice(wholesaleDecimal,ratio));
m.setEnabled(enabled.equals("1")? true: false); otherObj.put("lowDecimal", parsePrice(lowDecimal,ratio));
//缓存各个仓库的库存信息 materialExObj.put("other", otherObj);
Map<Long, BigDecimal> stockMap = new HashMap<Long, BigDecimal>(); } else {
for(int j=1; j<=depotCount;j++) { m.setUnit(unit);
int col = 15+j; }
if(col < src.getColumns()){ m.setMaterialExObj(materialExObj);
String depotName = ExcelUtils.getContent(src, 1, col); //获取仓库名称 String enabled = ExcelUtils.getContent(src, i, 15); //状态
Long depotId = depotService.getIdByName(depotName); m.setEnabled(enabled.equals("1")? true: false);
if(depotId!=0L){ //缓存各个仓库的库存信息
String stockStr = ExcelUtils.getContent(src, i, col); Map<Long, BigDecimal> stockMap = new HashMap<Long, BigDecimal>();
if(StringUtil.isNotEmpty(stockStr)) { for(int j=1; j<=depotCount;j++) {
stockMap.put(depotId, parseBigDecimalEx(stockStr)); int col = 15+j;
if(col < src.getColumns()){
String depotName = ExcelUtils.getContent(src, 1, col); //获取仓库名称
Long depotId = depotService.getIdByName(depotName);
if(depotId!=0L){
String stockStr = ExcelUtils.getContent(src, i, col);
if(StringUtil.isNotEmpty(stockStr)) {
stockMap.put(depotId, parseBigDecimalEx(stockStr));
}
} }
} }
} }
m.setStockMap(stockMap);
mList.add(m);
} }
m.setStockMap(stockMap);
mList.add(m);
} }
} logService.insertLog("商品",
logService.insertLog("商品", new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_IMPORT).append(mList.size()).append(BusinessConstants.LOG_DATA_UNIT).toString(),
new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_IMPORT).append(mList.size()).append(BusinessConstants.LOG_DATA_UNIT).toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
BaseResponseInfo info = new BaseResponseInfo();
try {
Long mId = 0L; Long mId = 0L;
for(MaterialWithInitStock m: mList) { for(MaterialWithInitStock m: mList) {
//判断该商品是否存在,如果不存在就新增,如果存在就更新 //判断该商品是否存在,如果不存在就新增,如果存在就更新
@@ -598,6 +603,9 @@ public class MaterialService {
} }
info.code = 200; info.code = 200;
info.data = "导入成功"; info.data = "导入成功";
} catch (BusinessRunTimeException e) {
info.code = e.getCode();
info.data = e.getData().get("message");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
info.code = 500; info.code = 500;

View File

@@ -190,7 +190,7 @@ public class UnitService {
* @param name * @param name
*/ */
public Long getUnitIdByName(String name){ public Long getUnitIdByName(String name){
Long unitId = 0L; Long unitId = null;
UnitExample example = new UnitExample(); UnitExample example = new UnitExample();
example.createCriteria().andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED); example.createCriteria().andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
List<Unit> list = unitMapper.selectByExample(example); List<Unit> list = unitMapper.selectByExample(example);