给单据增加接口:Excel导入明细

This commit is contained in:
季圣华
2023-05-12 00:47:49 +08:00
parent 8b3c4215f7
commit a3f1040a20
4 changed files with 178 additions and 8 deletions

View File

@@ -10,31 +10,31 @@ import com.jsh.erp.datasource.vo.DepotItemVoBatchNumberList;
import com.jsh.erp.exception.BusinessRunTimeException; import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.service.depot.DepotService; import com.jsh.erp.service.depot.DepotService;
import com.jsh.erp.service.depotHead.DepotHeadService; import com.jsh.erp.service.depotHead.DepotHeadService;
import com.jsh.erp.service.materialExtend.MaterialExtendService;
import com.jsh.erp.service.depotItem.DepotItemService; import com.jsh.erp.service.depotItem.DepotItemService;
import com.jsh.erp.service.material.MaterialService; import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.service.role.RoleService; import com.jsh.erp.service.role.RoleService;
import com.jsh.erp.service.systemConfig.SystemConfigService; import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.unit.UnitService; import com.jsh.erp.service.unit.UnitService;
import com.jsh.erp.utils.*; import com.jsh.erp.utils.*;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import jxl.Sheet;
import jxl.Workbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat; import java.util.ArrayList;
import java.text.SimpleDateFormat; import java.util.HashMap;
import java.util.*; import java.util.List;
import java.util.Map;
import static com.jsh.erp.utils.ResponseJsonUtil.returnJson; import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
import static com.jsh.erp.utils.Tools.getCenternTime;
/** /**
* @author ji-sheng-hua 华夏erp * @author ji-sheng-hua 华夏erp
@@ -860,4 +860,74 @@ public class DepotItemController {
} }
return res; return res;
} }
/**
* Excel导入明细
* @param file
* @param request
* @param response
* @return
*/
@PostMapping(value = "/importItemExcel")
public BaseResponseInfo importItemExcel(MultipartFile file,
@RequestParam(required = false, value = "prefixNo") String prefixNo,
HttpServletRequest request, HttpServletResponse response) throws Exception{
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> data = new HashMap<>();
String message = "";
try {
String barCodes = "";
Map<String, Map<String, String>> barCodeNumMap = new HashMap<>();
//文件合法性校验
Sheet src = null;
try {
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
src = workbook.getSheet(0);
} catch (Exception e) {
message = "导入文件不合法,请检查";
data.put("message", message);
res.code = 400;
res.data = data;
}
int length = src.getRows();
if(length>1000) {
message = "导入失败明细不能超出1000条";
res.code = 500;
data.put("message", message);
res.data = data;
} else {
for (int i = 2; i < length; i++) {
String barCode = ExcelUtils.getContent(src, i, 0);
String num = ExcelUtils.getContent(src, i, 2);
String unitPrice = ExcelUtils.getContent(src, i, 3);
String taxRate = ExcelUtils.getContent(src, i, 4);
String remark = ExcelUtils.getContent(src, i, 5);
Map<String, String> materialMap = new HashMap<>();
materialMap.put("num", num);
materialMap.put("unitPrice", unitPrice);
materialMap.put("taxRate", taxRate);
materialMap.put("remark", remark);
barCodeNumMap.put(barCode, materialMap);
barCodes += barCode + ",";
}
if (StringUtil.isNotEmpty(barCodes)) {
barCodes = barCodes.substring(0, barCodes.length() - 1);
}
JSONObject map = depotItemService.parseMapByExcelData(barCodes, barCodeNumMap, prefixNo);
if (map != null) {
res.code = 200;
} else {
res.code = 500;
}
res.data = map;
}
} catch (Exception e) {
e.printStackTrace();
message = "导入失败,请检查表格内容";
res.code = 500;
data.put("message", message);
res.data = data;
}
return res;
}
} }

View File

@@ -217,4 +217,11 @@ public interface DepotItemMapperEx {
List<DepotItem> getDepotItemByBatchNumber( List<DepotItem> getDepotItemByBatchNumber(
@Param("batchNumber") String batchNumber); @Param("batchNumber") String batchNumber);
List<MaterialVo4Unit> getBillItemByParam(
@Param("barCodes") String barCodes);
BigDecimal getCurrentStockByParam(
@Param("depotId") Long depotId,
@Param("mId") Long mId);
} }

View File

@@ -16,7 +16,6 @@ import com.jsh.erp.service.depotHead.DepotHeadService;
import com.jsh.erp.service.log.LogService; import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.material.MaterialService; import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.service.materialExtend.MaterialExtendService; import com.jsh.erp.service.materialExtend.MaterialExtendService;
import com.jsh.erp.service.role.RoleService;
import com.jsh.erp.service.serialNumber.SerialNumberService; import com.jsh.erp.service.serialNumber.SerialNumberService;
import com.jsh.erp.service.systemConfig.SystemConfigService; import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.unit.UnitService; import com.jsh.erp.service.unit.UnitService;
@@ -1114,4 +1113,71 @@ public class DepotItemService {
public Long getCountByMaterialAndDepot(Long mId, Long depotId) { public Long getCountByMaterialAndDepot(Long mId, Long depotId) {
return depotItemMapperEx.getCountByMaterialAndDepot(mId, depotId); return depotItemMapperEx.getCountByMaterialAndDepot(mId, depotId);
} }
public JSONObject parseMapByExcelData(String barCodes, Map<String, Map<String, String>> barCodeNumMap, String prefixNo) {
JSONObject map = new JSONObject();
try {
JSONArray arr = new JSONArray();
List<MaterialVo4Unit> list = depotItemMapperEx.getBillItemByParam(barCodes);
for (MaterialVo4Unit m: list) {
JSONObject item = new JSONObject();
item.put("barCode", m.getmBarCode());
item.put("name", m.getName());
item.put("standard", m.getStandard());
if(StringUtil.isNotEmpty(m.getModel())) {
item.put("model", m.getModel());
}
if(StringUtil.isNotEmpty(m.getColor())) {
item.put("color", m.getColor());
}
if(StringUtil.isNotEmpty(m.getSku())) {
item.put("sku", m.getSku());
}
BigDecimal stock = depotItemMapperEx.getCurrentStockByParam(null, m.getId());
item.put("stock", stock);
item.put("unit", m.getCommodityUnit());
Map<String, String> materialMap = barCodeNumMap.get(m.getmBarCode());
BigDecimal operNumber = BigDecimal.ZERO;
BigDecimal unitPrice = BigDecimal.ZERO;
BigDecimal taxRate = BigDecimal.ZERO;
if(materialMap.get("num")!=null) {
operNumber = new BigDecimal(materialMap.get("num"));
}
if(StringUtil.isNotEmpty(materialMap.get("unitPrice"))) {
unitPrice = new BigDecimal(materialMap.get("unitPrice"));
} else {
if("CGDD".equals(prefixNo)) {
unitPrice = m.getPurchaseDecimal();
} else if("XSDD".equals(prefixNo)) {
unitPrice = m.getWholesaleDecimal();
}
}
if(materialMap.get("taxRate")!=null) {
taxRate = new BigDecimal(materialMap.get("taxRate"));
}
String remark = materialMap.get("remark");
item.put("operNumber", operNumber);
item.put("unitPrice", unitPrice);
BigDecimal allPrice = BigDecimal.ZERO;
if(unitPrice!=null && unitPrice.compareTo(BigDecimal.ZERO)!=0) {
allPrice = unitPrice.multiply(operNumber);
}
BigDecimal taxMoney = BigDecimal.ZERO;
if(taxRate.compareTo(BigDecimal.ZERO) != 0) {
taxMoney = taxRate.multiply(allPrice).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
}
BigDecimal taxLastMoney = allPrice.add(taxMoney);
item.put("allPrice", allPrice);
item.put("taxRate", taxRate);
item.put("taxMoney", taxMoney);
item.put("taxLastMoney", taxLastMoney);
item.put("remark", remark);
arr.add(item);
}
map.put("rows", arr);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
} }

View File

@@ -924,4 +924,31 @@
and di.batch_number = #{batchNumber} and di.batch_number = #{batchNumber}
and dh.type = '入库' and dh.type = '入库'
</select> </select>
<select id="getBillItemByParam" resultType="com.jsh.erp.datasource.entities.MaterialVo4Unit">
select m.id, m.name, m.standard, m.model, me.id meId,me.commodity_unit commodityUnit,
me.purchase_decimal purchaseDecimal, me.wholesale_decimal wholesaleDecimal, me.bar_code mBarCode
from jsh_material m
left join jsh_material_extend me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
where 1=1
and me.default_flag='1'
<if test="barCodes != null">
and me.bar_code in (${barCodes})
</if>
and ifnull(m.delete_flag,'0') !='1'
order by m.id desc
</select>
<select id="getCurrentStockByParam" resultType="java.math.BigDecimal">
select sum(ifnull(current_number,0))
from jsh_material_current_stock
where 1=1
<if test="depotId != null">
and depot_id = ${depotId}
</if>
<if test="mId != null">
and material_id = ${mId}
</if>
and ifnull(delete_flag,'0') !='1'
</select>
</mapper> </mapper>