diff --git a/src/main/java/com/glxp/udi/admin/controller/product/StackCodeController.java b/src/main/java/com/glxp/udi/admin/controller/product/StackCodeController.java index e37decd..4b56657 100644 --- a/src/main/java/com/glxp/udi/admin/controller/product/StackCodeController.java +++ b/src/main/java/com/glxp/udi/admin/controller/product/StackCodeController.java @@ -10,6 +10,7 @@ import com.glxp.udi.admin.req.inout.DeleteRequest; import com.glxp.udi.admin.req.product.AddStackCodeRequest; import com.glxp.udi.admin.req.product.FilterStackCodeRequest; import com.glxp.udi.admin.res.PageSimpleResponse; +import com.glxp.udi.admin.res.product.StackCodeResponse; import com.glxp.udi.admin.service.product.StackCodeService; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.BindingResult; @@ -82,7 +83,8 @@ public class StackCodeController { return ResultVOUtils.error(500, "条码重复!"); } } - StackCode stackCode = stackCodeService.addCode(addStackCodeRequest); + + StackCodeResponse stackCode = stackCodeService.addCode(addStackCodeRequest); return ResultVOUtils.success(stackCode); } diff --git a/src/main/java/com/glxp/udi/admin/controller/product/StackOrderController.java b/src/main/java/com/glxp/udi/admin/controller/product/StackOrderController.java index 4a4da57..ae1b9bf 100644 --- a/src/main/java/com/glxp/udi/admin/controller/product/StackOrderController.java +++ b/src/main/java/com/glxp/udi/admin/controller/product/StackOrderController.java @@ -4,21 +4,24 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.StrUtil; +import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.metadata.IPage; import com.glxp.udi.admin.annotation.AuthRuleAnnotation; +import com.glxp.udi.admin.common.enums.ResultEnum; import com.glxp.udi.admin.common.res.BaseResponse; +import com.glxp.udi.admin.common.util.ResultVOUtils; +import com.glxp.udi.admin.constant.ConstantStatus; +import com.glxp.udi.admin.entity.product.StackOrder; import com.glxp.udi.admin.req.info.DeleteRequest; import com.glxp.udi.admin.req.product.ImportStackOrderRequest; +import com.glxp.udi.admin.req.product.StackOrderFilterRequest; import com.glxp.udi.admin.req.product.StackUploadRequest; +import com.glxp.udi.admin.res.PageSimpleResponse; import com.glxp.udi.admin.res.product.StackOrderResponse; import com.glxp.udi.admin.service.auth.CustomerService; -import com.glxp.udi.admin.common.util.ResultVOUtils; -import com.glxp.udi.admin.entity.product.StackOrder; -import com.glxp.udi.admin.req.product.StackOrderFilterRequest; -import com.glxp.udi.admin.res.PageSimpleResponse; import com.glxp.udi.admin.service.product.StackCodeService; import com.glxp.udi.admin.service.product.StackOrderService; -import com.glxp.udi.admin.common.enums.ResultEnum; +import com.glxp.udi.admin.vo.StackCodeVo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.validation.BindingResult; @@ -26,7 +29,13 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedInputStream; import java.io.File; +import java.io.InputStream; +import java.net.URLEncoder; +import java.nio.file.Files; import java.util.*; @Slf4j @@ -56,7 +65,7 @@ public class StackOrderController { BeanUtil.copyProperties(stackOrder, response); //查询此垛的扫码数量 - Long codeNum = stackOrderService.countCode(stackOrder.getOrderId()); + Long codeNum = stackOrderService.countCode(stackOrder.getId().toString()); response.setCodeNum(codeNum); responses.add(response); }); @@ -109,11 +118,11 @@ public class StackOrderController { } stackOrderEntity.setCustomerId(customerService.getCustomerId() + ""); - String stackOrderId = stackOrderService.insertOrder(stackOrderEntity); + String orderId = stackOrderService.insertOrder(stackOrderEntity); //绑定垛号 - stackUploadRequest.getStackCodeEntityList().forEach(stackCode -> stackCode.setOrderIdFk(stackOrderId)); + stackUploadRequest.getStackCodeEntityList().forEach(stackCode -> stackCode.setOrderIdFk(orderId)); stackCodeService.batchInsert(stackUploadRequest.getStackCodeEntityList()); - return ResultVOUtils.success(stackOrderId); + return ResultVOUtils.success(orderId); } /** @@ -188,5 +197,47 @@ public class StackOrderController { return ResultVOUtils.success("导入成功"); } + /** + * 导出垛码 + * + * @param response + */ + @GetMapping("/udims/stack/order/exportStackOrder") + public void exportStackOrder(@RequestParam String orderId, @RequestParam String customerId, + HttpServletResponse response) { + try { + //查询垛码 + List codeList = stackCodeService.getExportData(orderId); + if (CollUtil.isEmpty(codeList)) { + return; + } + stackOrderService.updateExportStatus(ConstantStatus.ORDER_EXPORT_ED, Integer.valueOf(orderId)); + String fileName = codeList.get(0).getOrderId() + "码详情.xlsx"; + //保存导出文件的文件夹 + String folderPath = filePath + "/stack/order/export/" + customerId + "/"; + File file = new File(folderPath); + if (!file.exists()) { + file.mkdirs(); + } + file = new File(folderPath + fileName); + EasyExcel.write(file, StackCodeVo.class).sheet().doWrite(codeList); + + InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath())); + byte[] bytes = new byte[inputStream.available()]; + inputStream.read(bytes); + inputStream.close(); + + response.reset(); + response.setContentType("application/octet-stream"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); + ServletOutputStream os = response.getOutputStream(); + os.write(bytes); + os.flush(); + os.close(); + } catch (Exception e) { + log.error("导出垛码数据异常", e); + } + } + } diff --git a/src/main/java/com/glxp/udi/admin/dao/product/StackCodeDao.java b/src/main/java/com/glxp/udi/admin/dao/product/StackCodeDao.java index 2107fb0..ed388fd 100644 --- a/src/main/java/com/glxp/udi/admin/dao/product/StackCodeDao.java +++ b/src/main/java/com/glxp/udi/admin/dao/product/StackCodeDao.java @@ -2,6 +2,7 @@ package com.glxp.udi.admin.dao.product; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.glxp.udi.admin.entity.product.StackCode; +import com.glxp.udi.admin.vo.StackCodeVo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @@ -39,4 +40,12 @@ public interface StackCodeDao extends BaseMapper { * @param list */ void batchInsert(List list); + + /** + * 查询导出垛码数据 + * + * @param orderId + * @return + */ + List getExportData(@Param("orderId") String orderId); } diff --git a/src/main/java/com/glxp/udi/admin/dao/product/StackOrderDao.java b/src/main/java/com/glxp/udi/admin/dao/product/StackOrderDao.java index 50c0dd7..3cc5ace 100644 --- a/src/main/java/com/glxp/udi/admin/dao/product/StackOrderDao.java +++ b/src/main/java/com/glxp/udi/admin/dao/product/StackOrderDao.java @@ -15,4 +15,12 @@ public interface StackOrderDao extends BaseMapper { * @return */ String selectOrderIdById(@Param("id") Integer id); + + /** + * 更新导出状态 + * + * @param exportStatus + * @param id + */ + void updateExportStatus(@Param("exportStatus") Integer exportStatus, @Param("id") Integer id); } diff --git a/src/main/java/com/glxp/udi/admin/res/product/StackCodeResponse.java b/src/main/java/com/glxp/udi/admin/res/product/StackCodeResponse.java new file mode 100644 index 0000000..306281d --- /dev/null +++ b/src/main/java/com/glxp/udi/admin/res/product/StackCodeResponse.java @@ -0,0 +1,17 @@ +package com.glxp.udi.admin.res.product; + +import com.glxp.udi.admin.entity.product.StackCode; +import lombok.Data; + +/** + * 垛码响应结果 + */ +@Data +public class StackCodeResponse extends StackCode { + + /** + * 垛号 + */ + private String stackId; + +} diff --git a/src/main/java/com/glxp/udi/admin/service/product/StackCodeService.java b/src/main/java/com/glxp/udi/admin/service/product/StackCodeService.java index dda1172..77deb43 100644 --- a/src/main/java/com/glxp/udi/admin/service/product/StackCodeService.java +++ b/src/main/java/com/glxp/udi/admin/service/product/StackCodeService.java @@ -13,8 +13,10 @@ import com.glxp.udi.admin.entity.product.StackOrder; import com.glxp.udi.admin.entity.udid.UdiEntity; import com.glxp.udi.admin.req.product.AddStackCodeRequest; import com.glxp.udi.admin.req.product.FilterStackCodeRequest; +import com.glxp.udi.admin.res.product.StackCodeResponse; import com.glxp.udi.admin.util.FilterUdiUtils; import com.glxp.udi.admin.util.IdUtil; +import com.glxp.udi.admin.vo.StackCodeVo; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; @@ -83,13 +85,12 @@ public class StackCodeService { * @param addStackCodeRequest * @return */ - public StackCode addCode(AddStackCodeRequest addStackCodeRequest) { + public StackCodeResponse addCode(AddStackCodeRequest addStackCodeRequest) { StackCode stackCode = new StackCode(); stackCode.setCode(addStackCodeRequest.getCode()); if (StrUtil.isBlank(addStackCodeRequest.getOrderId())) { //垛号为空,此条码为这个垛的第一个条码数据,先生垛号和垛数据,再添加条码 String stackId = idUtil.getStackId(addStackCodeRequest.getCustomerId()); - stackCode.setOrderIdFk(stackId); //创建垛数据 StackOrder stackOrder = new StackOrder(); //垛码 @@ -98,6 +99,7 @@ public class StackCodeService { stackOrder.setExportStatus(0); stackOrder.setCustomerId(addStackCodeRequest.getCustomerId()); stackOrderDao.insert(stackOrder); + stackCode.setOrderIdFk(String.valueOf(stackOrder.getId())); } else { stackCode.setOrderIdFk(addStackCodeRequest.getOrderId()); } @@ -105,7 +107,11 @@ public class StackCodeService { BeanUtil.copyProperties(udi, stackCode); int id = stackCodeDao.insert(stackCode); stackCode.setId(id); - return stackCode; + + StackCodeResponse response = new StackCodeResponse(); + BeanUtil.copyProperties(stackCode, response); + response.setStackId(stackOrderDao.selectOrderIdById(Integer.valueOf(stackCode.getOrderIdFk()))); + return response; } /** @@ -123,4 +129,16 @@ public class StackCodeService { } return false; } + + /** + * 查询导出数据 + * + * @param orderId + * @param customerId + * @return + */ + public List getExportData(String orderId) { + return stackCodeDao.getExportData(orderId); + } + } diff --git a/src/main/java/com/glxp/udi/admin/service/product/StackOrderService.java b/src/main/java/com/glxp/udi/admin/service/product/StackOrderService.java index c1cd1ca..960048f 100644 --- a/src/main/java/com/glxp/udi/admin/service/product/StackOrderService.java +++ b/src/main/java/com/glxp/udi/admin/service/product/StackOrderService.java @@ -17,15 +17,11 @@ import com.glxp.udi.admin.entity.product.StackOrder; import com.glxp.udi.admin.entity.udid.UdiEntity; import com.glxp.udi.admin.req.product.StackOrderFilterRequest; import com.glxp.udi.admin.req.product.StackUploadRequest; -import com.glxp.udi.admin.service.auth.CustomerService; import com.glxp.udi.admin.util.FilterUdiUtils; import com.glxp.udi.admin.util.IdUtil; import com.glxp.udi.admin.vo.StackCodeVo; import lombok.extern.slf4j.Slf4j; -import org.apache.ibatis.session.ExecutorType; -import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.TransactionIsolationLevel; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -51,10 +47,8 @@ public class StackOrderService { private StackCodeDao stackCodeDao; @Resource private SqlSessionFactory sqlSessionFactory; - @Resource - private CustomerService customerService; - private ExecutorService executorService = ThreadUtil.newExecutor(8); + private final ExecutorService executorService = ThreadUtil.newExecutor(8); public IPage filterList(StackOrderFilterRequest stackOrderFilterRequest) { @@ -72,26 +66,7 @@ public class StackOrderService { public void updateOrder(StackUploadRequest stackUploadRequest) { - //更新垛数据 stackOrderDao.updateById(stackUploadRequest.getStackOrderEntity()); - //更新码详情 - if (CollUtil.isNotEmpty(stackUploadRequest.getStackCodeEntityList())) { - stackUploadRequest.getStackCodeEntityList().forEach(stackCode -> { - stackCode.setOrderIdFk(stackUploadRequest.getStackOrderEntity().getOrderId()); - }); - SqlSession batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, TransactionIsolationLevel.READ_COMMITTED); - StackCodeDao mapper = batchSession.getMapper(StackCodeDao.class); - List stackCodeEntityList = stackUploadRequest.getStackCodeEntityList(); - for (StackCode stackCode : stackCodeEntityList) { - if (null != stackCode.getId()) { - mapper.updateById(stackCode); - } else { - mapper.insert(stackCode); - } - } - batchSession.commit(); - batchSession.close(); - } } public boolean isExit(String orderId) { @@ -105,7 +80,7 @@ public class StackOrderService { stackOrderEntity.setCreateTime(new Date()); } stackOrderDao.insert(stackOrderEntity); - return stackId; + return String.valueOf(stackOrderEntity.getId()); } public void deleteById(Integer id) { @@ -114,7 +89,7 @@ public class StackOrderService { //删除垛数据 stackOrderDao.deleteById(id); //删除关联的条码数据 - stackCodeDao.deleteByOrderId(orderId); + stackCodeDao.deleteByOrderId(String.valueOf(id)); } /** @@ -151,7 +126,7 @@ public class StackOrderService { for (int i = 1; i <= batchNum; i++) { int start = (i - 1) * 1000; - int end = i * 1000; + int end = i == 1 ? (codeList.size() - 1) : i * 1000; List codePage = codeList.subList(start, end); QueryWrapper wrapper = new QueryWrapper<>(); @@ -208,7 +183,7 @@ public class StackOrderService { //插入垛数据 stackOrderDao.insert(stackOrder); List data = stacks.get(serialNum); - data.forEach(stackCode -> stackCode.setOrderIdFk(stackId)); + data.forEach(stackCode -> stackCode.setOrderIdFk(String.valueOf(stackOrder.getId()))); //按照指定长度切分集合,批量插入 List> listList = ListUtil.split(data, 5000); @@ -221,4 +196,13 @@ public class StackOrderService { FileUtil.del(filePath); } + /** + * 更新垛导出状态 + * + * @param exportStatus + * @param id + */ + public void updateExportStatus(Integer exportStatus, Integer id) { + stackOrderDao.updateExportStatus(exportStatus, id); + } } diff --git a/src/main/resources/mybatis/mapper/product/StackCodeDao.xml b/src/main/resources/mybatis/mapper/product/StackCodeDao.xml index 5909e94..39135e3 100644 --- a/src/main/resources/mybatis/mapper/product/StackCodeDao.xml +++ b/src/main/resources/mybatis/mapper/product/StackCodeDao.xml @@ -22,15 +22,21 @@ insert into stack_code (code, orderIdFk, batchNo, produceDate, expireDate, serialNo, nameCode) values - ( - #{item.code}, + (#{item.code}, #{item.orderIdFk}, #{item.batchNo}, #{item.produceDate}, #{item.expireDate}, #{item.serialNo}, - #{item.nameCode} - ) + #{item.nameCode}) + + \ No newline at end of file diff --git a/src/main/resources/mybatis/mapper/product/StackOrderDao.xml b/src/main/resources/mybatis/mapper/product/StackOrderDao.xml index d25a04c..bb2bb55 100644 --- a/src/main/resources/mybatis/mapper/product/StackOrderDao.xml +++ b/src/main/resources/mybatis/mapper/product/StackOrderDao.xml @@ -6,4 +6,8 @@ from stack_order where id = #{id} + + + update stack_order set exportStatus = #{exportStatus} where id = #{id} + \ No newline at end of file