From 669fe984048785873c20fbd0ce8abb3cd52c1fce Mon Sep 17 00:00:00 2001
From: skyouc
Date: 星期二, 29 四月 2025 15:22:24 +0800
Subject: [PATCH] 新增出库单优化

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java |  223 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 217 insertions(+), 6 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java
index 8ffcc4f..6c5fa1b 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java
@@ -2,33 +2,244 @@
 
 import cn.afterturn.easypoi.excel.ExcelImportUtil;
 import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.vincent.rsf.framework.common.R;
+import com.vincent.rsf.framework.exception.CoolException;
+import com.vincent.rsf.server.common.domain.BaseParam;
+import com.vincent.rsf.server.common.domain.PageParam;
+import com.vincent.rsf.server.common.utils.CommonUtil;
 import com.vincent.rsf.server.common.utils.ExcelUtil;
+import com.vincent.rsf.server.common.utils.FieldsUtils;
+import com.vincent.rsf.server.manager.controller.params.MatnrToGroupParams;
+import com.vincent.rsf.server.manager.entity.MatnrGroup;
 import com.vincent.rsf.server.manager.entity.excel.MatnrsTemplate;
 import com.vincent.rsf.server.manager.mapper.MatnrMapper;
 import com.vincent.rsf.server.manager.entity.Matnr;
+import com.vincent.rsf.server.manager.service.MatnrGroupService;
 import com.vincent.rsf.server.manager.service.MatnrService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.vincent.rsf.server.system.constant.SerialRuleCode;
+import com.vincent.rsf.server.system.utils.SerialRuleUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 @Service("matnrService")
 public class MatnrServiceImpl extends ServiceImpl<MatnrMapper, Matnr> implements MatnrService {
 
+    @Autowired
+    private MatnrGroupService matnrGroupService;
+
     /**
+     * @return
+     * @throws
      * @author Ryan
      * @description 鐗╂枡鏁版嵁瀵煎叆鎺ュ鐞嗗疄鐜�
-     * @throws
-     * @return
      * @time 2025/3/3 13:08
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void importExcels(MultipartFile file) throws Exception {
+    public R importExcels(MultipartFile file) throws Exception {
         //璇诲彇涓婁紶鏂囦欢鍐呭
-        ExcelImportResult<MatnrsTemplate> result = ExcelImportUtil.importExcelMore(file.getInputStream(), MatnrsTemplate.class, ExcelUtil.getDefaultImportParams());
-        //TODO 鐗╂枡鍐欏叆澶勭悊
+        ExcelImportResult result = ExcelImportUtil.importExcelMore(file.getInputStream(), MatnrsTemplate.class, ExcelUtil.getDefaultImportParams());
+        if (result.getList().isEmpty()) {
+            throw new CoolException("鐗╂枡瀵煎叆澶辫触锛侊紒");
+        }
+        List<Matnr> matnrs = new ArrayList<>();
+        List<Map<String, String>> list = result.getList();
+        list.forEach(template -> {
+            Matnr matnr = new Matnr();
+            matnr.setBarcode(template.get("barcode"))
+                    .setCode(template.get("code"))
+                    .setDescrible(template.get("describle"))
+                    .setColor(template.get("color"))
+                    .setFlagCheck(!Objects.isNull(template.get("flagCheck")) ? Short.parseShort(template.get("flagCheck")) : 0)
+                    .setWeight(!Objects.isNull(template.get("weight")) ? Double.parseDouble(template.get("weight")) : 0.0)
+                    .setValidWarn(!Objects.isNull(template.get("validWarn")) ? Integer.parseInt(template.get("validWarn")): 0)
+                    .setValid(!Objects.isNull(template.get("valid")) ? Integer.parseInt( template.get("valid")) : 0)
+                    .setUnit(template.get("unit"))
+                    .setStockUnit(template.get("purUnit"))
+                    .setSpec(template.get("spec"))
+                    .setStagn(!Objects.isNull(template.get("stagn")) ? Integer.parseInt(template.get("stagn")) : 0)
+                    .setModel(template.get("model"))
+                    .setGroupCode(template.get("groupCode"))
+                    .setPurUnit(template.get("purUnit"))
+                    .setStockLevel(!Objects.isNull(template.get("stockLevel")) ? Short.parseShort(template.get("stockLevel")) : 0)
+                    .setSafeQty(!Objects.isNull(template.get("safeQty")) ? Double.parseDouble(template.get("safeQty")) : 0)
+                    .setMinQty(!Objects.isNull(template.get("safeQty")) ? Double.parseDouble(template.get("minQty")) : 0);
+            if (Objects.isNull(template.get("groupCode")) && Objects.isNull(template.get("groupName"))) {
+                MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>()
+                        .eq(!Objects.isNull(template.get("groupCode")), MatnrGroup::getCode, template.get("groupCode"))
+                        .eq(!Objects.isNull(template.get("groupName")),MatnrGroup::getName, template.get("groupName")));
+                matnr.setGroupId(matnrGroups.getId());
+            }
+            //鑾峰彇鍔ㄦ�佸瓧娈碉紝骞朵繚瀛樻槑缁嗗唴瀹�
+            if (!FieldsUtils.getFieldsSta().isEmpty()) {
+                //淇濆瓨鐗╂枡鎵╁睍灞炴�у��
+                try {
+                    String uuid = CommonUtil.randomUUID16();
+                    boolean b = FieldsUtils.saveFields(template, uuid);
+                    if (b) {
+                        matnr.setFieldsIndex(uuid);
+                    }
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            matnrs.add(matnr);
+        });
+
+        if (!this.saveBatch(matnrs)) {
+            throw new CoolException("瀵煎叆澶辫触锛侊紒");
+        }
+        return R.ok("瀵煎叆鎴愬姛锛侊紒");
+    }
+
+    @Override
+    public PageParam<Matnr, BaseParam> getMatnrPage(PageParam<Matnr, BaseParam> pageParam, Map<String, Object> params) {
+        QueryWrapper<Matnr> queryWrapper = pageParam.buildWrapper(true);
+        if (params.containsKey("groupId")) {
+            Object groupId = params.get("groupId");
+            if (!Objects.isNull(groupId)) {
+                List<MatnrGroup> matnrGroups = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getParentId, Long.parseLong(groupId.toString())).select(MatnrGroup::getId));
+                if (!matnrGroups.isEmpty()) {
+                    List<Long> longs = matnrGroups.stream().map(MatnrGroup::getId).collect(Collectors.toList());
+                    queryWrapper.or().in("group_id", longs);
+                }
+            }
+        }
+        /**鎷兼帴鎵╁睍瀛楁*/
+        PageParam<Matnr, BaseParam> page = this.page(pageParam, queryWrapper);
+        List<Matnr> records = page.getRecords();
+        for (Matnr record : records) {
+            if (!Objects.isNull(record.getFieldsIndex())) {
+                Map<String, String> fields = FieldsUtils.getFields(record.getFieldsIndex());
+                record.setExtendFields(fields);
+            }
+        }
+        page.setRecords(records);
+        return page;
+    }
+
+    @Override
+    public Matnr selectMatnrById(Long id) {
+        Matnr matnr = this.baseMapper.selectById(id);
+        if (Objects.isNull(matnr)) {
+            throw new CoolException("褰撳墠鐗╂枡涓嶅瓨鍦紒锛�");
+        }
+        if (!Objects.isNull(matnr.getFieldsIndex())) {
+            Map<String, String> fields = FieldsUtils.getFields(matnr.getFieldsIndex());
+            matnr.setExtendFields(fields);
+        }
+        return matnr;
+    }
+
+    /**
+     * 鐗╂枡缁戝畾
+     * @param params
+     * @return
+     */
+    @Override
+    public boolean bindMatnrs(MatnrToGroupParams params) {
+        if (Objects.isNull(params) && params.getIds().isEmpty()) {
+            throw new CoolException("鐗╂枡涓嶈兘涓虹┖锛侊紒");
+        }
+        if (Objects.isNull(params.getGroupId())) {
+            throw new CoolException("鍒嗙粍涓嶈兘涓虹┖锛侊紒");
+        }
+        MatnrGroup groups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getId, params.getGroupId()));
+        if (Objects.isNull(groups)) {
+            throw new CoolException("鐗╂枡鍒嗙粍涓嶅瓨鍦紒锛�");
+        }
+        if (!this.update(new LambdaUpdateWrapper<Matnr>()
+                .in(Matnr::getId, params.getIds())
+                .set(Matnr::getGroupId, groups.getId())
+                .set(Matnr::getGroupCode, groups.getCode()))) {
+            throw new CoolException("缁戝畾澶辫触锛侊紒");
+        }
+        return true;
+    }
+
+    /**
+     * 鎵归噺淇敼鐗╂枡
+     * @param params
+     * @return
+     */
+    @Override
+    public boolean batchUpdate(MatnrToGroupParams params) {
+        if (Objects.isNull(params.getMatnr())) {
+            throw new CoolException("鐗╂枡灞炴�т笉鑳戒负绌猴紒锛�");
+        }
+        if (Objects.isNull(params.getIds()) || params.getIds().isEmpty()) {
+            throw new CoolException("鎵�閫夌墿鏂欎笉鑳戒负绌猴紒锛�");
+        }
+        Matnr matnr = params.getMatnr();
+        if (!this.update(new LambdaUpdateWrapper<Matnr>().in(Matnr::getId, params.getIds())
+                .set(!Objects.isNull(matnr.getStatus()), Matnr::getStatus, matnr.getStatus())
+                .set(!Objects.isNull(matnr.getFlagCheck()), Matnr::getFlagCheck, matnr.getFlagCheck())
+                .set(!Objects.isNull(matnr.getFlagLabelMange()), Matnr::getFlagLabelMange, matnr.getFlagLabelMange())
+                .set(!Objects.isNull(matnr.getStockLevel()), Matnr::getStockLevel, matnr.getStockLevel())
+                .set(!Objects.isNull(matnr.getShipperId()), Matnr::getShipperId, matnr.getShipperId())
+                .set(!Objects.isNull(matnr.getMinQty()), Matnr::getMinQty, matnr.getMinQty())
+                .set(!Objects.isNull(matnr.getMaxQty()), Matnr::getMaxQty, matnr.getMaxQty())
+                .set(!Objects.isNull(matnr.getValid()), Matnr::getValid, matnr.getValid())
+                .set(!Objects.isNull(matnr.getValidWarn()), Matnr::getValidWarn, matnr.getValidWarn())
+                .set(!Objects.isNull(matnr.getStockUnit()), Matnr::getStockUnit, matnr.getStockUnit())
+        )) {
+            throw new CoolException("鎵归噺鎿嶄綔澶辫触锛侊紒");
+        }
+
+        return true;
+    }
+
+    @Override
+    public PageParam<Matnr, BaseParam> getPages(Map<String, Object> map) {
+
+
+        return null;
+    }
+
+    /**
+     * @desc 鏇存柊鎵╁睍鐗╂枡鎵╁睍瀛楁鍊�
+     * @param matnr
+     * @return
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R saveMatnrs(Map<String, Object> matnr) {
+        Matnr matnr1 = JSONObject.parseObject(JSONObject.toJSONString(matnr), Matnr.class);
+        if (Objects.isNull(matnr1.getCode())) {
+            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_MATNR_CODE, null);
+            matnr1.setCode(ruleCode);
+        }
+        /**
+         * 鎵╁睍瀛楁瀛樺叆搴�
+         */
+        String uuid16 = CommonUtil.randomUUID16();
+        if (!FieldsUtils.getFieldsSta().isEmpty()) {
+//            Map<String, ?> extendFields = (Map<String, ?>) matnr.get("extendFields");
+            try {
+                FieldsUtils.saveFields(matnr, uuid16);
+                matnr1.setFieldsIndex(uuid16);
+            } catch (Exception ex) {
+                log.error(ex.toString());
+                throw new CoolException("鍔ㄦ�佸瓧娈典繚瀛樺け璐�");
+            }
+        }
+        if (!this.saveOrUpdate(matnr1)) {
+            return R.error("Save Fail");
+        }
+        return R.ok(matnr1);
     }
 }

--
Gitblit v1.9.1