Prechádzať zdrojové kódy

feat:物料管理

lumaojun 1 mesiac pred
rodič
commit
dfc044539d

+ 13 - 104
easydo-mes/src/main/java/easydo/technology/controller/ProductMaterialController.java

@@ -1,13 +1,7 @@
 package easydo.technology.controller;
 
-import easydo.technology.components.JdbcClient;
-import easydo.technology.enums.MESEnum;
-import easydo.technology.exception.BizException;
-import easydo.technology.model.ProductBom;
 import easydo.technology.model.ProductMaterial;
-import easydo.technology.service.FlowNoService;
-import easydo.technology.utils.SecurityUtils;
-import easydo.technology.utils.StringUtil;
+import easydo.technology.service.ProductMaterialService;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -15,127 +9,42 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
-import javax.sql.DataSource;
-import java.sql.Connection;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 @RestController
 @RequestMapping("/processMaterial")
 public class ProductMaterialController {
+
     @Resource
-    JdbcClient jdbcClient;
-    @Resource
-    DataSource dataSource;
-    @Resource
-    FlowNoService flowNoService;
+    ProductMaterialService productMaterialService;
 
     @RequestMapping(value = "/getList")
     public Object getList(@RequestBody Map<String, Object> map) throws Exception {
-        List<ProductMaterial> list = jdbcClient.getJdbcList(map, ProductMaterial.class);
+        Object list = productMaterialService.getList(map);
         return new ResponseEntity<>(list, HttpStatus.OK);
     }
 
     @RequestMapping(value = "/getPage")
     public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-
-            Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, ProductMaterial.class, connection);
-            return new ResponseEntity<>(recordsPage, HttpStatus.OK);
-        } catch (Exception e) {
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
+        Object page = productMaterialService.getPage(map);
+        return new ResponseEntity<>(page, HttpStatus.OK);
     }
 
     @RequestMapping(value = "/save")
     public Object save(@RequestBody ProductMaterial model) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-            connection.setAutoCommit(false);
-            model.setStatus(MESEnum.PROCESS_OF_STATUS_ENABLE.getValue());
-            model.setCreateId(SecurityUtils.getCurrentUserId());
-            generateCode(model, connection);
-            jdbcClient.jdbcInsert(model, connection);
-            connection.commit();
-            return new ResponseEntity<>(model, HttpStatus.OK);
-        } catch (Exception e) {
-            connection.rollback();
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
+        Object result = productMaterialService.save(model);
+        return new ResponseEntity<>(result, HttpStatus.OK);
     }
 
     @RequestMapping(value = "/update")
     public Object update(@RequestBody ProductMaterial model) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-            connection.setAutoCommit(false);
-
-            jdbcClient.jdbcUpdateById(model, connection);
-
-            Map<String, Object> paramMap = new HashMap<>();
-            paramMap.put("materialCode", model.getCode());
-            ProductBom bom = new ProductBom();
-            bom.setMaterialName(model.getName());
-            jdbcClient.jdbcUpdate(bom, paramMap, connection);
-
-            connection.commit();
-            return new ResponseEntity<>(HttpStatus.OK);
-        } catch (Exception e) {
-            connection.rollback();
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
+        Object result = productMaterialService.update(model);
+        return new ResponseEntity<>(result, HttpStatus.OK);
     }
 
     @RequestMapping(value = "/remove")
-    public Object deleteProgram(@RequestBody ProductMaterial model) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-
-            model = jdbcClient.getJdbcModelById(model, connection);
-            ProductBom bom = new ProductBom();
-            bom.setMaterialCode(model.getCode());
-            int count = jdbcClient.getJdbcCount(bom, connection);
-            if (count > 0) {
-                throw new BizException("该物料在bom管理中存在,无法删除");
-            }
-
-            jdbcClient.jdbcRemoveById(model, connection);
-
-            return new ResponseEntity<>(HttpStatus.OK);
-        } catch (Exception e) {
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
-    }
-
-    private synchronized void generateCode(ProductMaterial model, Connection connection) throws Exception {
-        if (StringUtil.isEmpty(model.getCode())) {
-            while (true) {
-                String flowNo = flowNoService.getFlowNo(MESEnum.FLOW_NO_TYPE_PRODUCT_MATERIAL.getValue(), connection);
-                ProductMaterial productMaterialParam = new ProductMaterial();
-                productMaterialParam.setCode(flowNo);
-                int count = jdbcClient.getJdbcCount(productMaterialParam, connection);
-                if (count == 0) {
-                    model.setCode(flowNo);
-                    break;
-                }
-            }
-        } else {
-            ProductMaterial productMaterialParam = new ProductMaterial();
-            productMaterialParam.setCode(model.getCode());
-            int count = jdbcClient.getJdbcCount(productMaterialParam, connection);
-            if (count > 0) {
-                throw new BizException("编号已存在,请重新填写");
-            }
-        }
+    public Object remove(@RequestBody ProductMaterial model) throws Exception {
+        Object result = productMaterialService.remove(model);
+        return new ResponseEntity<>(result, HttpStatus.OK);
     }
 }

+ 2 - 0
easydo-mes/src/main/java/easydo/technology/model/ProductMaterial.java

@@ -20,6 +20,8 @@ public class ProductMaterial extends CommonModel {
     private String warehouseId;
     private String createTime;
     private Long createId;
+    private Long updateId;
+    private String tenantId;
     private BigDecimal price;
 
 }

+ 56 - 0
easydo-mes/src/main/java/easydo/technology/service/FlowNoService.java

@@ -29,6 +29,7 @@ public class FlowNoService {
     private final Object qualityLock = new Object();
     private final Object processStageLock = new Object();
     private final Object processRouteLock = new Object();
+    private final Object productMaterialLock = new Object();
 
 
     /**
@@ -251,6 +252,61 @@ public class FlowNoService {
         }
     }
 
+    /**
+     * 生成物料编码 (ProductMaterial) - 独立解耦实现
+     */
+    public String generateProductMaterialCode(ProductMaterial model, Connection connection) throws Exception {
+        synchronized (productMaterialLock) {
+            String manualCode = model.getCode();
+            String tenantId = model.getTenantId();
+            if (StringUtil.isNotEmpty(manualCode)) {
+                Map<String, Object> checkMap = new HashMap<>();
+                checkMap.put("code", manualCode);
+                checkMap.put("tenantId", tenantId);
+                int count = (int) jdbcClient.getJdbcCountByMap(checkMap, ProductMaterial.class, connection);
+                if (count > 0) {
+                    throw new BizException("物料编号已存在: " + manualCode);
+                }
+                return manualCode;
+            }
+            while (true) {
+                FlowNo flowNo = new FlowNo();
+                flowNo.setType("product_material");
+                flowNo.setTenantId(tenantId);
+                flowNo = jdbcClient.getJdbcModel(flowNo, connection);
+                if (flowNo == null) throw new BizException("未配置物料流水号规则");
+
+                String currDate = DFY_MD.format(LocalDateTime.now());
+                String currDate2 = DFY_MD_2.format(LocalDateTime.now());
+                if (StringUtil.isEmpty(flowNo.getCurrDate()) || !flowNo.getCurrDate().equals(currDate)) {
+                    flowNo.setCurrDate(currDate);
+                    flowNo.setCurrSeq(1);
+                } else {
+                    flowNo.setCurrSeq(flowNo.getCurrSeq() + 1);
+                }
+
+                String no;
+                if (StringUtil.isEmpty(flowNo.getCurrDate())) {
+                    no = (flowNo.getPrefix() != null ? flowNo.getPrefix() : "") + String.format("%06d", flowNo.getCurrSeq());
+                } else {
+                    no = (flowNo.getPrefix() != null ? flowNo.getPrefix() : "") + currDate2 + String.format("%05d", flowNo.getCurrSeq());
+                }
+                flowNo.setCurrNo(no);
+
+                Map<String, Object> updateMap = new HashMap<>();
+                updateMap.put("type", "product_material");
+                updateMap.put("tenantId", tenantId);
+                jdbcClient.jdbcUpdate(flowNo, updateMap, connection);
+
+                Map<String, Object> checkMap = new HashMap<>();
+                checkMap.put("code", no);
+                checkMap.put("tenantId", tenantId);
+                int count = (int) jdbcClient.getJdbcCountByMap(checkMap, ProductMaterial.class, connection);
+                if (count == 0) return no;
+            }
+        }
+    }
+
     /**
      * 原始流水号生成方法
      */

+ 19 - 0
easydo-mes/src/main/java/easydo/technology/service/ProductMaterialService.java

@@ -0,0 +1,19 @@
+package easydo.technology.service;
+
+import easydo.technology.model.ProductMaterial;
+
+import java.util.Map;
+
+public interface ProductMaterialService {
+
+    Object getList(Map<String, Object> map) throws Exception;
+
+    Object getPage(Map<String, Object> map) throws Exception;
+
+    Object save(ProductMaterial model) throws Exception;
+
+    Object update(ProductMaterial model) throws Exception;
+
+    Object remove(ProductMaterial model) throws Exception;
+}
+

+ 119 - 0
easydo-mes/src/main/java/easydo/technology/service/impl/ProductMaterialServiceImpl.java

@@ -0,0 +1,119 @@
+package easydo.technology.service.impl;
+
+import easydo.technology.components.JdbcClient;
+import easydo.technology.enums.MESEnum;
+import easydo.technology.exception.BizException;
+import easydo.technology.model.ProductBom;
+import easydo.technology.model.ProductMaterial;
+import easydo.technology.service.FlowNoService;
+import easydo.technology.service.ProductMaterialService;
+import easydo.technology.utils.SecurityUtils;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class ProductMaterialServiceImpl implements ProductMaterialService {
+
+    @Resource
+    JdbcClient jdbcClient;
+    @Resource
+    DataSource dataSource;
+    @Resource
+    FlowNoService flowNoService;
+
+    @Override
+    public Object getList(Map<String, Object> map) throws Exception {
+        List<ProductMaterial> list = jdbcClient.getJdbcList(map, ProductMaterial.class);
+        return list;
+    }
+
+    @Override
+    public Object getPage(Map<String, Object> map) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            return jdbcClient.getJdbcPage(map, ProductMaterial.class, connection);
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public Object save(ProductMaterial model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            connection.setAutoCommit(false);
+            model.setStatus(MESEnum.PROCESS_OF_STATUS_ENABLE.getValue());
+            model.setCreateId(SecurityUtils.getCurrentUserId());
+            model.setCode(flowNoService.generateProductMaterialCode(model, connection));
+            jdbcClient.jdbcInsert(model, connection);
+            connection.commit();
+            return model;
+        } catch (Exception e) {
+            connection.rollback();
+            throw e;
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public Object update(ProductMaterial model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            connection.setAutoCommit(false);
+            model.setUpdateId(SecurityUtils.getCurrentUserId());
+            jdbcClient.jdbcUpdateById(model, connection);
+
+            Map<String, Object> paramMap = new HashMap<>();
+            paramMap.put("materialCode", model.getCode());
+            ProductBom bom = new ProductBom();
+            bom.setMaterialName(model.getName());
+            jdbcClient.jdbcUpdate(bom, paramMap, connection);
+
+            connection.commit();
+            return model;
+        } catch (Exception e) {
+            connection.rollback();
+            throw e;
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public Object remove(ProductMaterial model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            connection.setAutoCommit(false);
+
+            ProductMaterial exist = jdbcClient.getJdbcModelById(model, connection);
+            if (exist == null) {
+                throw new BizException("物料不存在");
+            }
+
+            ProductBom bom = new ProductBom();
+            bom.setMaterialCode(exist.getCode());
+            int count = jdbcClient.getJdbcCount(bom, connection);
+            if (count > 0) {
+                throw new BizException("该物料在bom管理中存在,无法删除");
+            }
+
+            jdbcClient.jdbcRemoveById(exist, connection);
+
+            connection.commit();
+            return exist;
+        } catch (Exception e) {
+            connection.rollback();
+            throw e;
+        } finally {
+            connection.close();
+        }
+    }
+}
+