|
@@ -0,0 +1,233 @@
|
|
|
+package com.abi.qms.platform.service.impl;
|
|
|
+
|
|
|
+import com.abi.qms.platform.dao.entity.QrBoxMapping;
|
|
|
+import com.abi.qms.platform.dao.entity.UserInfo;
|
|
|
+import com.abi.qms.platform.dao.enums.BoxMappingActiveStatusEnum;
|
|
|
+import com.abi.qms.platform.dao.mapper.QrBoxMappingMapper;
|
|
|
+import com.abi.qms.platform.dao.tablestore.entity.QrCode;
|
|
|
+import com.abi.qms.platform.dao.vo.result.QrBoxMappingVO;
|
|
|
+import com.abi.qms.platform.dto.req.ActiveBoxCodeReq;
|
|
|
+import com.abi.qms.platform.dto.req.GenerateBarCodeReq;
|
|
|
+import com.abi.qms.platform.dto.req.ListQrBoxCodeMappingReq;
|
|
|
+import com.abi.qms.platform.dto.res.ListQrBoxCodeMappingRes;
|
|
|
+import com.abi.qms.platform.dto.res.QrBoxCodeUploadRes;
|
|
|
+import com.abi.qms.platform.dto.res.UploadFileRes;
|
|
|
+import com.abi.qms.platform.infrastructure.util.AssertUtil;
|
|
|
+import com.abi.qms.platform.infrastructure.util.PageUtil;
|
|
|
+import com.abi.qms.platform.infrastructure.util.UserUtil;
|
|
|
+import com.abi.qms.platform.service.QrBoxMappingService;
|
|
|
+import com.abi.task.common.api.exception.BusinessException;
|
|
|
+import com.abi.task.common.tablestore.TableStorePlusUtils;
|
|
|
+import com.abi.task.common.utils.PojoConverterUtils;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: fangxinjian
|
|
|
+ * @date: 2021/05/27 11:19
|
|
|
+ * @description:
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class QrBoxMappingServiceImpl implements QrBoxMappingService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QrBoxMappingMapper qrBoxMappingMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TableStorePlusUtils tableStorePlusUtils;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserUtil userUtil;
|
|
|
+
|
|
|
+ private static final String SUCCESS = "success";
|
|
|
+
|
|
|
+ private static final String FAIL = "fail";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void generateBarCode(GenerateBarCodeReq req) {
|
|
|
+
|
|
|
+ //获取登录用户信息
|
|
|
+ UserInfo user = userUtil.getUser();
|
|
|
+
|
|
|
+ //获取起始序号和结束序号
|
|
|
+ Long indexBeagin = getCodeIndex(req.getBeginQrCode());
|
|
|
+ Long indexEnd = getCodeIndex(req.getEndQrCode());
|
|
|
+ //条数
|
|
|
+ Long qrCodeCount = indexEnd - indexBeagin;
|
|
|
+ if (qrCodeCount < 0) {
|
|
|
+ throw new BusinessException("起始序号大于结束序号,无法生成条形码");
|
|
|
+ }
|
|
|
+
|
|
|
+ QrBoxMapping qbm = new QrBoxMapping();
|
|
|
+ qbm.setBoxCode(String.valueOf(IdWorker.getId()));
|
|
|
+ qbm.setIndexBegin(indexBeagin);
|
|
|
+ qbm.setIndexEnd(indexEnd);
|
|
|
+ qbm.setBeginQrCode(req.getBeginQrCode());
|
|
|
+ qbm.setEndQrCode(req.getEndQrCode());
|
|
|
+ qbm.setQrCodeCount(qrCodeCount);
|
|
|
+ qbm.setActiveStatus(BoxMappingActiveStatusEnum.NOT_ACTIVE.getCode());
|
|
|
+ qbm.setProjectNo(req.getProjectNo());
|
|
|
+ qbm.setBoardNo(req.getBoardNo());
|
|
|
+ qbm.setRemark(req.getRemark());
|
|
|
+ qbm.setCreateUserName(user.getUserName());
|
|
|
+ qbm.setCreateBy(user.getId());
|
|
|
+ qrBoxMappingMapper.insert(qbm);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ListQrBoxCodeMappingRes listQrBoxCodeMapping(ListQrBoxCodeMappingReq req) {
|
|
|
+
|
|
|
+ // 分页查询
|
|
|
+ IPage<QrBoxMappingVO> iPage = qrBoxMappingMapper.listQrBoxCodeMapping(PageUtil.createPage(req), req);
|
|
|
+ List<QrBoxMappingVO> qrboxMappingList = iPage.getRecords();
|
|
|
+
|
|
|
+ // 封装出参、放入分页信息
|
|
|
+ ListQrBoxCodeMappingRes res = new ListQrBoxCodeMappingRes();
|
|
|
+ PageUtil.copyPageInfo(res, iPage);
|
|
|
+ List<ListQrBoxCodeMappingRes.QrBoxMappingBean> beanList = PojoConverterUtils.copyList(qrboxMappingList, ListQrBoxCodeMappingRes.QrBoxMappingBean.class);
|
|
|
+ res.setQrBoxMappingBeanList(beanList);
|
|
|
+
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public QrBoxCodeUploadRes uploadCodeFiles(Map<String, MultipartFile> map) {
|
|
|
+
|
|
|
+ QrBoxCodeUploadRes res = new QrBoxCodeUploadRes();
|
|
|
+ Map<String, UploadFileRes> resultMap = Maps.newHashMap();
|
|
|
+ map.entrySet().stream().forEach(o -> {
|
|
|
+ MultipartFile file = o.getValue();
|
|
|
+ //核心!!!上传文件
|
|
|
+ try {
|
|
|
+ //上传箱码文件
|
|
|
+ doUpload(file);
|
|
|
+ resultMap.put(o.getKey(), new UploadFileRes(SUCCESS, ""));
|
|
|
+ } catch (BusinessException e) {
|
|
|
+ log.info("文件上传失败");
|
|
|
+ resultMap.put(o.getKey(), new UploadFileRes(FAIL, e.getMessage()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("文件上传异常", e);
|
|
|
+ resultMap.put(o.getKey(), new UploadFileRes(FAIL, ""));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ res.put("data", resultMap);
|
|
|
+
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void activeBoxCode(ActiveBoxCodeReq req) {
|
|
|
+
|
|
|
+ //获取登录用户信息
|
|
|
+ UserInfo user = userUtil.getUser();
|
|
|
+
|
|
|
+ QrBoxMapping qrBoxMapping = qrBoxMappingMapper.selectById(req.getId());
|
|
|
+ AssertUtil.isNull(qrBoxMapping, "该箱码不存在!");
|
|
|
+
|
|
|
+ qrBoxMapping.setActiveStatus(req.getActiveStatus());
|
|
|
+ qrBoxMapping.setActiveTime(LocalDateTime.now());
|
|
|
+ qrBoxMapping.setActiveUserId(user.getId());
|
|
|
+ qrBoxMapping.setActiveUserName(user.getUserName());
|
|
|
+ qrBoxMapping.setUpdateBy(user.getId());
|
|
|
+ qrBoxMapping.setUpdateTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ qrBoxMappingMapper.updateById(qrBoxMapping);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键查询已生成码包位置
|
|
|
+ *
|
|
|
+ * @param index 主键
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Long getCodeIndex(String index) {
|
|
|
+ QrCode qrCode = tableStorePlusUtils.getRow(QrCode.class, index);
|
|
|
+ return qrCode.getCodeIndex();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void doUpload(MultipartFile file) {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ throw new BusinessException("文件为空,请重新选择文件");
|
|
|
+ }
|
|
|
+ InputStream inputStream = null;
|
|
|
+ BufferedReader reader = null;
|
|
|
+ String filename = file.getName();
|
|
|
+ String encryptCodeStr;
|
|
|
+
|
|
|
+ String first = null;
|
|
|
+ String last = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ //解决服务器上乱码
|
|
|
+ reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
|
|
+
|
|
|
+ //1-逐行读取文件的码
|
|
|
+ while ((encryptCodeStr = reader.readLine()) != null) {
|
|
|
+ log.debug("{} line : {}", filename, encryptCodeStr);
|
|
|
+ if (StringUtils.isBlank(encryptCodeStr)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //截取 去掉uri
|
|
|
+ encryptCodeStr = encryptCodeStr.substring(encryptCodeStr.lastIndexOf("/") + 1);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(first)) {
|
|
|
+ first = encryptCodeStr;
|
|
|
+ }
|
|
|
+ last = encryptCodeStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成条形码
|
|
|
+ GenerateBarCodeReq req = new GenerateBarCodeReq();
|
|
|
+ req.setBeginQrCode(first);
|
|
|
+ req.setEndQrCode(last);
|
|
|
+ generateBarCode(req);
|
|
|
+
|
|
|
+ } catch (BusinessException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("上传失败", e);
|
|
|
+ throw new BusinessException("上传失败");
|
|
|
+ } finally {
|
|
|
+ //关闭流
|
|
|
+ closeStream(inputStream, reader);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void closeStream(InputStream inputStream, BufferedReader reader) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("inputStream关闭失败");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (reader != null) {
|
|
|
+ try {
|
|
|
+ reader.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("reader关闭失败");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|