diff --git a/hzims-service-api/assets-api/src/main/java/com/hnac/hzims/spare/entity/WtSpBasicEntity.java b/hzims-service-api/assets-api/src/main/java/com/hnac/hzims/spare/entity/WtSpBasicEntity.java
index 82c550c..c77f508 100644
--- a/hzims-service-api/assets-api/src/main/java/com/hnac/hzims/spare/entity/WtSpBasicEntity.java
+++ b/hzims-service-api/assets-api/src/main/java/com/hnac/hzims/spare/entity/WtSpBasicEntity.java
@@ -1,5 +1,7 @@
package com.hnac.hzims.spare.entity;
+import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
+import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
@@ -20,6 +22,7 @@ import java.math.BigDecimal;
@Data
@TableName("WT_SP_BASIC")
@EqualsAndHashCode(callSuper = true)
+@ExcelIgnoreUnannotated
@ApiModel(value = "WtSpBasic对象", description = "")
public class WtSpBasicEntity extends TenantEntity {
@@ -33,52 +36,63 @@ public class WtSpBasicEntity extends TenantEntity {
/**
* 名称
*/
+ @ExcelProperty(value = "物品名称(必填)",index = 1 )
@ApiModelProperty(value = "名称")
private String name;
/**
* 编码
*/
+ @ExcelProperty(value = "物品编码(必填)",index = 0 )
@ApiModelProperty(value = "编码")
private String code;
/**
* 规格
*/
+ @ExcelProperty(value = "规格型号",index = 2 )
@ApiModelProperty(value = "规格")
private String specifications;
/**
* 品牌
*/
+ @ExcelProperty(value = "物品品牌(必填)",index = 3 )
@ApiModelProperty(value = "品牌")
private String brand;
/**
* 单位
*/
+ @ExcelProperty(value = "物品单位(必填)",index = 5 )
@ApiModelProperty(value = "单位")
private String unit;
/**
* 单价
*/
+ @ExcelProperty(value = "单价(元)(必填)",index = 10)
@ApiModelProperty(value = "单价")
@JsonSerialize(nullsUsing = NullSerializer.class)
private BigDecimal price;
+ @ExcelProperty(value = "重量(kg)",index = 6 )
@ApiModelProperty(value = "重量")
@JsonSerialize(nullsUsing = NullSerializer.class)
private BigDecimal weight;
+ @ExcelProperty(value = "库存上限(必填)",index = 7 )
@ApiModelProperty(value = "库存上限")
@JsonSerialize(nullsUsing = NullSerializer.class)
private Integer upperLimit;
+ @ExcelProperty(value = "库存下限(必填)",index = 8 )
@ApiModelProperty(value = "库存下限")
@JsonSerialize(nullsUsing = NullSerializer.class)
private Integer lowerLimit;
+ @ExcelProperty(value = "是否回收废品(必填)",index = 9 )
@ApiModelProperty(value = "是否回收废品")
private String isWaste;
/**
* 主要参数
*/
+ @ExcelProperty(value = "物品类别代码(必填)",index = 4 )
@ApiModelProperty(value = "主要参数")
private String parameter;
/**
diff --git a/hzims-service/assets/pom.xml b/hzims-service/assets/pom.xml
index 7a4a75d..80a5994 100644
--- a/hzims-service/assets/pom.xml
+++ b/hzims-service/assets/pom.xml
@@ -64,6 +64,10 @@
message-api
compile
+
+ cn.afterturn
+ easypoi-base
+
diff --git a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/controller/WtSpBasicController.java b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/controller/WtSpBasicController.java
index 02bf825..bcb74c0 100644
--- a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/controller/WtSpBasicController.java
+++ b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/controller/WtSpBasicController.java
@@ -12,8 +12,6 @@ import com.hnac.hzims.spare.entity.WtSpBasicEntity;
import com.hnac.hzims.spare.service.IWtSpBasicService;
import com.hnac.hzims.spare.service.IWtSpManagementService;
import com.hnac.hzims.spare.vo.WtSpBasicVO;
-import com.hnac.hzims.ticket.allTicket.fegin.ITicketInfoAllClient;
-import com.hnac.hzims.ticket.allTicket.vo.TicketInfoStatisticVO;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
@@ -28,8 +26,10 @@ import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
+import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
@@ -128,13 +128,12 @@ public class WtSpBasicController extends BladeController {
@ApiOperation(value = "新增", notes = "传入SP_BASIC")
public R save(@Valid @RequestBody WtSpBasicEntity SP_BASIC) {
List wtSpBasicEntityList = SP_BASICService.list(Wrappers.lambdaQuery()
- .eq(WtSpBasicEntity::getCode, SP_BASIC.getCode()));
+ .eq(WtSpBasicEntity::getCode, SP_BASIC.getCode()));
Assert.isTrue(CollectionUtil.isEmpty(wtSpBasicEntityList),()->{
throw new ServiceException("物资台账物品编码不能重复,保存失败!");
});
return R.status(SP_BASICService.save(SP_BASIC));
}
-
/**
* 修改 代码自定义代号
*/
@@ -174,4 +173,26 @@ public class WtSpBasicController extends BladeController {
return R.status(SP_BASICService.deleteLogic(Func.toLongList(ids)));
}
+ /**
+ * 库存填写模板导出
+ * @param response
+ */
+ @ApiOperationSupport(order = 8)
+ @ApiOperation(value = "库存填写模板导出", notes = "库存填写模板导出")
+ @RequestMapping(value = "/writeTemplateDownload", method = {RequestMethod.GET, RequestMethod.POST})
+ public void writeTemplateDownload(HttpServletResponse response) {
+ SP_BASICService.writeTemplateDownload(response);
+ }
+
+ /**
+ * 导入数据
+ */
+ @ApiOperationSupport(order = 9)
+ @ApiOperation(value = "导入台账填写Excel")
+ @RequestMapping(value = "/importExcel", method = {RequestMethod.POST})
+ public R importExcel( @RequestParam MultipartFile file) throws Exception {
+ System.out.println(11);
+ return SP_BASICService.importExcel(file);
+ }
+
}
diff --git a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/IWtSpBasicService.java b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/IWtSpBasicService.java
index ab6d8a1..64fcef5 100644
--- a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/IWtSpBasicService.java
+++ b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/IWtSpBasicService.java
@@ -7,7 +7,10 @@ import com.hnac.hzims.spare.entity.WtSpBasicEntity;
import com.hnac.hzims.spare.vo.WtSpBasicVO;
import org.springblade.core.mp.base.BaseService;
import org.springblade.core.mp.support.Query;
+import org.springblade.core.tool.api.R;
+import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@@ -63,6 +66,14 @@ public interface IWtSpBasicService extends BaseService {
List stockDetail(Long spBasicId);
/**
+ * 库存填写模板导出
+ * @param response
+ */
+ void writeTemplateDownload(HttpServletResponse response);
+
+ R importExcel(MultipartFile file) throws Exception;
+
+ /**
* 获取流程中的用户
* @param deptId
* @param role
diff --git a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/impl/WtSpBasicServiceImpl.java b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/impl/WtSpBasicServiceImpl.java
index 634f885..53cc782 100644
--- a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/impl/WtSpBasicServiceImpl.java
+++ b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/service/impl/WtSpBasicServiceImpl.java
@@ -1,6 +1,15 @@
package com.hnac.hzims.spare.service.impl;
-import com.alibaba.excel.util.DateUtils;
+import cn.afterturn.easypoi.excel.entity.ImportParams;
+import com.alibaba.druid.support.json.JSONUtils;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.converters.longconverter.LongStringConverter;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -16,29 +25,43 @@ import com.hnac.hzims.spare.service.IWtSpBasicService;
import com.hnac.hzims.spare.service.IWtSpManagementService;
import com.hnac.hzims.spare.service.IWtSpTotalService;
import com.hnac.hzims.spare.service.IWtSpWarehouseService;
+import com.hnac.hzims.spare.utils.ExcelToolListener;
+import com.hnac.hzims.spare.utils.SheetWriteStyleHandler;
import com.hnac.hzims.spare.vo.WtSpBasicVO;
import com.hnac.hzims.ticket.allTicket.fegin.ITicketInfoAllClient;
import com.hnac.hzims.ticket.allTicket.vo.TicketInfoStatisticVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springblade.core.launch.constant.FlowConstant;
+import org.springblade.core.excel.support.ExcelException;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
+import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.system.entity.Dept;
import org.springblade.system.feign.ISysClient;
-import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
+import org.springframework.util.CollectionUtils;
+import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URLEncoder;
import java.util.*;
+import java.util.stream.Collectors;
+
+import static com.hnac.hzims.spare.utils.ExcelUtil.asyncReadModel;
/**
* 服务实现类
@@ -66,10 +89,11 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl voList = new ArrayList<>();
IPage voPages = new Page<>();
- if(Func.isNotEmpty(spBasicEntity.getName()) && "工具".equals(spBasicEntity.getName())){
+ if (Func.isNotEmpty(spBasicEntity.getName()) && "工具".equals(spBasicEntity.getName())) {
// if(Func.isNotEmpty(spBasicEntity.getName())){
- List list = managementService.list(Wrappers.query().lambda().
- eq(WtSpManagementEntity::getName,spBasicEntity.getName()));
- if(list.size()==0){
+ List list = managementService.list(Wrappers.query().lambda().
+ eq(WtSpManagementEntity::getName, spBasicEntity.getName()));
+ if (list.size() == 0) {
return null;
}
List result = managementService.list(Wrappers.query().lambda().like(WtSpManagementEntity::getAncestors, list.get(0).getId()));
List longList = new ArrayList<>();
- if(result.size()>0){
+ if (result.size() > 0) {
for (int i = 0; i < result.size(); i++) {
longList.add(result.get(i).getId());
}
}
- if(longList.size() >0){
+ if (longList.size() > 0) {
queryWrapper.lambda().in(WtSpBasicEntity::getManagementId, longList);
- }else{
+ } else {
return voPages;
}
spBasicEntity.setName(null);
@@ -173,63 +197,63 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl pages = super.page(Condition.getPage(query), queryWrapper);
for (int i = 0; i < pages.getRecords().size(); i++) {
- if(null == warehouseId ){
+ if (null == warehouseId) {
WtSpBasicVO basicVO = new WtSpBasicVO();
WtSpBasicEntity basicEntity = pages.getRecords().get(i);
WtSpManagementEntity managementEntity = managementMapper.selectById(basicEntity.getManagementId());
- if(null!=managementEntity){
+ if (null != managementEntity) {
basicVO.setSparePartsName(managementEntity.getName());
}
List totalList = totalService.list(Wrappers.query().lambda().eq(WtSpTotalEntity::getSpBasicId, basicEntity.getId()));
- if(totalList.size() >0){
+ if (totalList.size() > 0) {
basicVO.setStock(totalList.get(0).getStock());
- }else{
+ } else {
basicVO.setStock(0L);
}
- BeanUtils.copyProperties(basicEntity,basicVO);
+ BeanUtils.copyProperties(basicEntity, basicVO);
- List ticketVOS = ticketInfoAllClient.getTicketStatistic("2022-02");
+ List ticketVOS = ticketInfoAllClient.getTicketStatistic("2022-02");
// R deptR = sysClient.getDept(basicEntity.getWaterPlantId());
// if(Func.isNotEmpty(deptR.getData())){
// basicVO.setWaterPlantName(deptR.getData().getDeptName());
// }
voList.add(basicVO);
- }else{
+ } else {
WtSpBasicVO basicVO = new WtSpBasicVO();
WtSpBasicEntity basicEntity = pages.getRecords().get(i);
- Map params = new HashMap<>();
- params.put("spBasicId",basicEntity.getId());
- params.put("warehouseId",warehouseId);
+ Map params = new HashMap<>();
+ params.put("spBasicId", basicEntity.getId());
+ params.put("warehouseId", warehouseId);
WtSpTotalEntity totalEntity = totalMapper.selectByBasicId(params);
WtSpManagementEntity managementEntity = managementMapper.selectById(basicEntity.getManagementId());
- if(null!=managementEntity){
+ if (null != managementEntity) {
basicVO.setSparePartsName(managementEntity.getName());
}
- if(null!=totalEntity){
+ if (null != totalEntity) {
List totalList = totalService.list(Wrappers.query().lambda().eq(WtSpTotalEntity::getSpBasicId, basicEntity.getId()));
- if(totalList.size() >0){
+ if (totalList.size() > 0) {
// basicVO.setStock(totalList.get(0).getStock());
basicVO.setStock(totalEntity.getStock());
- }else{
+ } else {
basicVO.setStock(0L);
}
- BeanUtils.copyProperties(basicEntity,basicVO);
+ BeanUtils.copyProperties(basicEntity, basicVO);
// R deptR = sysClient.getDept(basicEntity.getWaterPlantId());
// if(Func.isNotEmpty(deptR.getData())){
// basicVO.setWaterPlantName(deptR.getData().getDeptName());
// }
voList.add(basicVO);
}
- List totalEntities = totalMapper.selectByWarehouseId(warehouseId);
- pages.setTotal(totalEntities.size());
+ List totalEntities = totalMapper.selectByWarehouseId(warehouseId);
+ pages.setTotal(totalEntities.size());
}
}
voPages.setRecords(voList);
- if (Func.isNotEmpty(sign)){
+ if (Func.isNotEmpty(sign)) {
voPages.setTotal(pages.getTotal());
- }else {
+ } else {
if (null == warehouseId) {
voPages.setTotal(pages.getTotal());
} else {
@@ -243,7 +267,7 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl params) {
+ public Integer selectTotal(Map params) {
return baseMapper.selectTotal(params);
}
@@ -251,7 +275,7 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl getBasicList(String tenantId) {
WtSpBasicEntity spBasicEntity = new WtSpBasicEntity();
spBasicEntity.setTenantId(tenantId);
- QueryWrapper queryWrapper = Condition.getQueryWrapper(spBasicEntity);
+ QueryWrapper queryWrapper = Condition.getQueryWrapper(spBasicEntity);
List list = super.list(queryWrapper);
return list;
}
@@ -274,7 +298,7 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl stockList(WtSpBasicEntity spBasicEntity, Query query) {
WtSpBasicEntity entity = new WtSpBasicEntity();
- if(Func.isNotEmpty(spBasicEntity.getCode())){
+ if (Func.isNotEmpty(spBasicEntity.getCode())) {
entity.setCode(spBasicEntity.getCode());
}
QueryWrapper queryWrapper = Condition.getQueryWrapper(entity);
@@ -286,23 +310,23 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl typeList = new ArrayList<>();
- if(null!= spBasicEntity.getManagementId()){
+ if (null != spBasicEntity.getManagementId()) {
List managementEntityList = managementMapper.selectList(Wrappers.query().lambda().like(WtSpManagementEntity::getAncestors, spBasicEntity.getManagementId()));
for (int i = 0; i < managementEntityList.size(); i++) {
typeList.add(managementEntityList.get(i).getId());
}
typeList.add(spBasicEntity.getManagementId());
}
- if(typeList.size()>0){
- queryWrapper.lambda().in(WtSpBasicEntity::getManagementId,typeList);
+ if (typeList.size() > 0) {
+ queryWrapper.lambda().in(WtSpBasicEntity::getManagementId, typeList);
}
IPage pages = super.page(Condition.getPage(query), queryWrapper);
List voList = new ArrayList<>();
IPage voPages = new Page<>();
List warehouseList = wtSpWarehouseService.list(Wrappers.query().lambda().
- in(WtSpWarehouseEntity::getAffiliatedUnit,Func.toLongList(String.valueOf(waterPlantId))));
+ in(WtSpWarehouseEntity::getAffiliatedUnit, Func.toLongList(String.valueOf(waterPlantId))));
List longList = new ArrayList<>();
for (int i = 0; i < warehouseList.size(); i++) {
longList.add(warehouseList.get(i).getId());
@@ -310,14 +334,14 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl0){
+ if (longList.size() > 0) {
List totalList = totalService.list(Wrappers.query().lambda()
- .eq(WtSpTotalEntity::getSpBasicId,basicEntity.getId())
- .in(WtSpTotalEntity::getWarehouseId,longList));
- if(totalList.size() >0){
+ .eq(WtSpTotalEntity::getSpBasicId, basicEntity.getId())
+ .in(WtSpTotalEntity::getWarehouseId, longList));
+ if (totalList.size() > 0) {
SpTemporaryStockEntity stockEntity = new SpTemporaryStockEntity();
WtSpManagementEntity managementEntity = managementMapper.selectById(basicEntity.getManagementId());
- if(null!=managementEntity){
+ if (null != managementEntity) {
stockEntity.setManagementId(managementEntity.getId());
stockEntity.setSparePartsName(managementEntity.getName());
}
@@ -327,21 +351,21 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl deptR = sysClient.getDept(basicEntity.getCreateDept());
- if(Func.isNotEmpty(deptR.getData())){
+ R deptR = sysClient.getDept(basicEntity.getCreateDept());
+ if (Func.isNotEmpty(deptR.getData())) {
stockEntity.setWaterPlantName(deptR.getData().getDeptName());
}
stockEntity.setQuantity(totalList.get(0).getStock().intValue());
stockEntity.setWarehouseId(totalList.get(0).getWarehouseId());
WtSpWarehouseEntity wtSpWarehouseEntity = wtSpWarehouseService.getById(totalList.get(0).getWarehouseId());
- if(null != wtSpWarehouseEntity){
+ if (null != wtSpWarehouseEntity) {
stockEntity.setWarehouseName(wtSpWarehouseEntity.getName());
}
//查询出库数量
Integer deliveryQuantity = totalMapper.getListByBasicId(basicEntity.getId());
- if(null !=deliveryQuantity){
+ if (null != deliveryQuantity) {
stockEntity.setDeliveryQuantity(deliveryQuantity);
- }else{
+ } else {
stockEntity.setDeliveryQuantity(0);
}
//查询工单消耗数量
@@ -356,13 +380,13 @@ public class WtSpBasicServiceImpl extends BaseServiceImpl data = new ArrayList<>();
+ data.add(wtSpBasicEntity);
+ List list = wtSpManagementMapper.findAll(AuthUtil.getTenantId());
+ response.setContentType("application/vnd.ms-excel");
+ response.setCharacterEncoding("UTF-8");
+ // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
+ String fileName = URLEncoder.encode("库存填写模板", "UTF-8");
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
+ response.setContentType("application/vnd.ms-excel");
+ response.setCharacterEncoding("UTF-8");
+ //ExcelWriter初始化
+ ExcelWriter excelWriter = EasyExcel
+ .write(response.getOutputStream())
+ .autoCloseStream(Boolean.TRUE)
+ .registerConverter(new LongStringConverter())
+ .registerWriteHandler(new SimpleColumnWidthStyleStrategy(25))
+ .build();
+ Set excludeColumnFiledNames = new HashSet();
+ excludeColumnFiledNames.add("managementId");
+ excludeColumnFiledNames.add("remarks");
+ excludeColumnFiledNames.add("codeNumber");
+ excludeColumnFiledNames.add("tenantId");
+ excludeColumnFiledNames.add("id");
+ excludeColumnFiledNames.add("createUser");
+ excludeColumnFiledNames.add("createDept");
+ excludeColumnFiledNames.add("createTime");
+ excludeColumnFiledNames.add("updateUser");
+ excludeColumnFiledNames.add("updateTime");
+ excludeColumnFiledNames.add("status");
+ excludeColumnFiledNames.add("isDeleted");
+ WriteSheet writeSheet = EasyExcel.writerSheet(1, "库存台账表头模板").head(WtSpBasicEntity.class).registerWriteHandler(new SheetWriteStyleHandler()).excludeColumnFiledNames(excludeColumnFiledNames).build();
+ excelWriter.write(data, writeSheet);
+ WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "物品类别代码").head(WtSpManagementeExcelEntity.class).build();
+ excelWriter.write(list, writeSheet2);
+ excelWriter.finish();
+// // 这里需要设置不关闭流
+// EasyExcel.write(response.getOutputStream(), WtSpBasicEntity.class).autoCloseStream(Boolean.TRUE).sheet(1,"库存台账表头模板")
+// .head(WtSpBasicEntity.class)
+// .registerWriteHandler(new SheetWriteStyleHandler());
+//// .doWrite(data);
+// EasyExcel.write(response.getOutputStream(), WtSpManagementeExcelEntity.class).autoCloseStream(Boolean.TRUE).sheet(2,"库存填写模板")
+// .doWrite(list);
+// response.getOutputStream().close();
+ } catch (Exception e) {
+ System.out.println("exportExcel = " + e.toString());
+ // 重置response
+ response.reset();
+ response.setContentType("application/json");
+ response.setCharacterEncoding("utf-8");
+ Map map = new HashMap<>();
+ map.put("success", "false");
+ map.put("msg", "库存填写模板导出失败" + e.getMessage());
+ try {
+ response.getWriter().println(JSONUtils.toJSONString(map));
+ } catch (IOException e3) {
+ throw new ServiceException("发生异常 :" + e.toString());
+ }
+ }
+ }
+
+
+ @Transactional(rollbackFor = Exception.class)
+ @Override
+ public R importExcel(MultipartFile file) throws Exception {
+ ImportParams params = new ImportParams();
+ params.setHeadRows(2);
+ List list = wtSpManagementMapper.findAll(AuthUtil.getTenantId());
+ List codeList = list.stream().map(s -> s.getOneCode()).collect(Collectors.toList());
+ List twoCodeList = list.stream().map(s -> s.getTwoCode()).collect(Collectors.toList());
+ codeList.addAll(twoCodeList);
+ String filename = file.getOriginalFilename();
+ ExcelToolListener excelToolListener = new ExcelToolListener();
+ if (org.springframework.util.StringUtils.isEmpty(filename)) {
+ throw new ExcelException("请上传文件!");
+ } else if (!org.springframework.util.StringUtils.endsWithIgnoreCase(filename, ".xls") && !org.springframework.util.StringUtils.endsWithIgnoreCase(filename, ".xlsx")) {
+ throw new ExcelException("请上传正确的excel文件!");
+ } else {
+ try {
+ InputStream inputStream = file.getInputStream();
+ asyncReadModel(inputStream, excelToolListener, WtSpBasicEntity.class, 0, 1);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ List entityList = excelToolListener.getDataList();
+ List data = new ArrayList<>();
+ data = JSONArray.parseArray(JSON.toJSONString(entityList), WtSpBasicEntity.class).stream()
+ .filter(entity -> Func.isNotEmpty(entity.getName())).collect(Collectors.toList());
+ Assert.isTrue(CollectionUtil.isNotEmpty(data), () -> {
+ throw new ServiceException("填入工器具编码不能为空");
+ });
+// List data = ExcelImportUtil.importExcel(file.getInputStream(), WtSpBasicEntity.class, params);
+// List data = ExcelImportUtil.importExcel(file.getInputStream(), WtSpBasicEntity.class, params);
+
+
+ try {
+ for (WtSpBasicEntity libraryExcel : data) {
+ Integer counter = 2;
+ //数据校验
+ R r = this.validExcel(libraryExcel, codeList, counter);
+ if (r == null && r.getCode() == 500) {
+ return r;
+ }
+ }
+ boolean flag = this.saveBatch(data);
+ return R.status(flag);
+ } catch (Exception ex) {
+ throw new RuntimeException( ex);
+// String.format("导入数据异常!"),
+ }
+
+ }
+
+ private InputStream copyStream(InputStream source) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] buffer = new byte[1024];
+ int len;
+ while ((len = source.read(buffer)) > -1) {
+ baos.write(buffer, 0, len);
+ }
+ baos.flush();
+ return new ByteArrayInputStream(baos.toByteArray());
+ }
+
+ /**
+ * 校验Excel数据
+ **/
+ private R validExcel(WtSpBasicEntity libraryExcel, List list, Integer counter) {
+ List errors = new ArrayList<>();
+ //数据库操作
+ List wtSpBasicEntityList = this.list(Wrappers.lambdaQuery()
+ .eq(WtSpBasicEntity::getCode, libraryExcel.getCode()));
+ Assert.isTrue(CollectionUtil.isEmpty(wtSpBasicEntityList), () -> {
+ throw new ServiceException("物资台账物品编码不能重复,保存失败!");
+ });
+
+ if (StringUtils.isBlank(libraryExcel.getCode())) {
+ errors.add("物品编码不能为空");
+ }
+ if (StringUtils.isBlank(libraryExcel.getName())) {
+ errors.add("物品名称不能为空");
+ }
+ if (StringUtils.isBlank(libraryExcel.getBrand())) {
+ errors.add("物品品牌不能为空");
+ }
+ if (StringUtils.isBlank(libraryExcel.getSpecifications())) {
+ errors.add("规格型号不能为空");
+ }
+ if (StringUtils.isBlank(libraryExcel.getParameter())) {
+ errors.add("物品类别代码不能为空");
+ } else {
+ if (!list.contains(libraryExcel.getParameter())) {
+ errors.add("物品类别代码不规范");
+ } else {
+ //物品类别代码规范的情况下,把managerID写入
+ WtSpManagementEntity wtSpManagementeExcelEntity = wtSpManagementMapper.selectOne(new LambdaQueryWrapper() {{
+ eq(WtSpManagementEntity::getCode, libraryExcel.getParameter());
+ }});
+ libraryExcel.setManagementId(wtSpManagementeExcelEntity.getId());
+ libraryExcel.setParameter(null);
+ }
+ }
+ if (StringUtils.isBlank(libraryExcel.getUnit())) {
+ errors.add("物品单位不能为空");
+ }
+ if (ObjectUtil.isEmpty(libraryExcel.getUpperLimit()) && libraryExcel.getUpperLimit() < 0) {
+ errors.add("库存上限不能为空");
+ }
+ if (ObjectUtil.isEmpty(libraryExcel.getLowerLimit()) && libraryExcel.getLowerLimit() < 0) {
+ errors.add("库存下限不能为空");
+ }
+ if (ObjectUtil.isEmpty(libraryExcel.getLowerLimit()) && ObjectUtil.isEmpty(libraryExcel.getLowerLimit())) {
+ if (libraryExcel.getLowerLimit() > libraryExcel.getLowerLimit()) {
+ errors.add("库存下限不能大于库存上限");
+ }
+ }
+ if (StringUtils.isBlank(libraryExcel.getIsWaste())) {
+ errors.add("回收废品不能为空");
+ }
+ if (ObjectUtil.isEmpty(libraryExcel.getPrice())) {
+ errors.add("单价不能为空");
+ }
+ counter++;
+ if (CollectionUtils.isEmpty(errors)) {
+ return R.success("OK");
+ } else {
+ return R.data(500, errors, String.format("第%s行导入异常", counter));
+ }
+ }
+
// @Override
// public String getTaskUser(Long deptId, String role, String tenantId) {
// R> userR = userClient.userByDeptAndRole(deptId, role, tenantId);
diff --git a/hzims-service/assets/src/main/java/com/hnac/hzims/spare/utils/ExcelToolListener.java b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/utils/ExcelToolListener.java
new file mode 100644
index 0000000..6dfa6df
--- /dev/null
+++ b/hzims-service/assets/src/main/java/com/hnac/hzims/spare/utils/ExcelToolListener.java
@@ -0,0 +1,33 @@
+package com.hnac.hzims.spare.utils;
+
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.event.AnalysisEventListener;
+import com.alibaba.fastjson.JSONObject;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+@Slf4j
+@Data
+public class ExcelToolListener extends AnalysisEventListener {
+
+ /**保存数据**/
+ private List