Sfoglia il codice sorgente

fix:采购计划

lumaojun 3 settimane fa
parent
commit
2d276240f7

+ 41 - 0
easydo-mes/src/main/java/easydo/technology/controller/PurchasePlanController.java

@@ -0,0 +1,41 @@
+package easydo.technology.controller;
+
+import easydo.technology.model.PurchasePlan;
+import easydo.technology.service.PurchasePlanService;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/purchasePlan")
+public class PurchasePlanController {
+
+    @Resource
+    private PurchasePlanService purchasePlanService;
+
+    @RequestMapping(value = "/getPage")
+    public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
+        Map<String, Object> result = purchasePlanService.getPage(map);
+        return new ResponseEntity<>(result, HttpStatus.OK);
+    }
+
+    @RequestMapping(value = "/save")
+    public Object save(@RequestBody PurchasePlan model) throws Exception {
+        return new ResponseEntity<>(purchasePlanService.save(model), HttpStatus.OK);
+    }
+
+    @RequestMapping(value = "/update")
+    public Object update(@RequestBody PurchasePlan model) throws Exception {
+        return new ResponseEntity<>(purchasePlanService.update(model), HttpStatus.OK);
+    }
+
+    @RequestMapping(value = "/remove")
+    public Object remove(@RequestBody PurchasePlan model) throws Exception {
+        return new ResponseEntity<>(purchasePlanService.remove(model), HttpStatus.OK);
+    }
+}

+ 1 - 0
easydo-mes/src/main/java/easydo/technology/enums/MESEnum.java

@@ -16,6 +16,7 @@ public enum MESEnum {
     FLOW_NO_TYPE_SALE_PLAN("sale_plan", "销售计划"),
     FLOW_NO_TYPE_SALE_ORDER("sale_order", "销售订单"),
     FLOW_NO_TYPE_WAREHOUSE("warehouse", "仓库管理"),
+    FLOW_NO_TYPE_PURCHASE_PLAN("purchase_plan", "采购计划"),
 
     PROCESS_STAGE_OF_CATEGORY_PREPARATION("preparation","准备工序"),
     PROCESS_STAGE_OF_CATEGORY_PROCESSING("processing","加工工序"),

+ 31 - 0
easydo-mes/src/main/java/easydo/technology/model/PurchasePlan.java

@@ -0,0 +1,31 @@
+package easydo.technology.model;
+
+import easydo.technology.annotation.Minio;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.util.List;
+
+@Data
+@Minio
+@EqualsAndHashCode(callSuper = true)
+public class PurchasePlan extends CommonModel {
+    private String id;
+    private String code;
+    private String name;
+    private String saleOrderId;
+    private String createTime;
+    private Long purchaseUserId;
+    private Long createId;
+    private Long updateId;
+    private String status;
+    private String tenantId;
+    private String updateTime;
+    private String beginDate;
+    private String endDate;
+
+    // 非数据库字段,用于级联组装
+    private List<PurchasePlanDetail> childrenList;
+    private SaleOrder saleOrder;
+    private String purchaseUserName;
+}

+ 14 - 0
easydo-mes/src/main/java/easydo/technology/model/PurchasePlanDetail.java

@@ -0,0 +1,14 @@
+package easydo.technology.model;
+
+import lombok.Data;
+
+@Data
+public class PurchasePlanDetail {
+    private String id;
+    private String planId;
+    private String materialCode;
+    private java.math.BigDecimal number;
+
+    // 非数据库字段
+    private ProductMaterial material;
+}

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

@@ -34,6 +34,7 @@ public class FlowNoService {
     private final Object customerLock = new Object();
     private final Object productBomLock = new Object();
     private final Object salePlanLock = new Object();
+    private final Object purchasePlanLock = new Object();
 
 
     /**
@@ -476,6 +477,62 @@ public class FlowNoService {
         }
     }
 
+    /**
+     * 生成采购计划编码 (PurchasePlan) - 独立解耦实现
+     */
+    public String generatePurchasePlanCode(PurchasePlan model, Connection connection) throws Exception {
+        synchronized (purchasePlanLock) {
+            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, PurchasePlan.class, connection);
+                if (count > 0) {
+                    throw new BizException("采购计划编号已存在: " + manualCode);
+                }
+                return manualCode;
+            }
+
+            while (true) {
+                FlowNo flowNo = new FlowNo();
+                flowNo.setType(MESEnum.FLOW_NO_TYPE_PURCHASE_PLAN.getValue());
+                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", MESEnum.FLOW_NO_TYPE_PURCHASE_PLAN.getValue());
+                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, PurchasePlan.class, connection);
+                if (count == 0) return no;
+            }
+        }
+    }
+
     /**
      * 原始流水号生成方法
      */

+ 15 - 0
easydo-mes/src/main/java/easydo/technology/service/PurchasePlanService.java

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

+ 192 - 0
easydo-mes/src/main/java/easydo/technology/service/impl/PurchasePlanServiceImpl.java

@@ -0,0 +1,192 @@
+package easydo.technology.service.impl;
+
+import easydo.technology.components.JdbcClient;
+import easydo.technology.exception.BizException;
+import easydo.technology.model.ProductMaterial;
+import easydo.technology.model.PurchasePlan;
+import easydo.technology.model.PurchasePlanDetail;
+import easydo.technology.model.SaleOrder;
+import easydo.technology.service.FlowNoService;
+import easydo.technology.service.PurchasePlanService;
+import easydo.technology.system.model.SysUser;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class PurchasePlanServiceImpl implements PurchasePlanService {
+
+    @Resource
+    private JdbcClient jdbcClient;
+
+    @Resource
+    private DataSource dataSource;
+
+    @Resource
+    private FlowNoService flowNoService;
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public Map<String, Object> getPage(Map<String, Object> map) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            // 1. 获取主表分页数据
+            Map<String, Object> result = jdbcClient.getJdbcPage(map, PurchasePlan.class, connection);
+            List<PurchasePlan> list = (List<PurchasePlan>) result.get("records");
+            if (list == null || list.isEmpty()) {
+                if (result != null) {
+                    result.put("records", new ArrayList<>());
+                }
+                return result;
+            }
+
+            for (PurchasePlan model : list) {
+                // 2. 补全文件列表 FileList
+                try {
+                    jdbcClient.getMinioFile(model, connection);
+                } catch (Exception e) {
+                    // 打印错误但继续执行,参考 Go 版 utils.PrintSearchFileErr
+                }
+
+                // 3. 补全销售订单 SaleOrder
+                if (model.getSaleOrderId() != null) {
+                    SaleOrder saleOrderParam = new SaleOrder();
+                    saleOrderParam.setId(model.getSaleOrderId());
+                    model.setSaleOrder(jdbcClient.getJdbcModelById(saleOrderParam, connection));
+                }
+
+                // 4. 补全采购员姓名 PurchaseUserName (从 SysUser 获取 NickName)
+                if (model.getPurchaseUserId() != null) {
+                    SysUser userParam = new SysUser();
+                    userParam.setId(model.getPurchaseUserId());
+                    SysUser user = jdbcClient.getJdbcModelById(userParam, connection);
+                    if (user != null) {
+                        model.setPurchaseUserName(user.getNickName());
+                    }
+                }
+
+                // 5. 补全明细列表 ChildrenList
+                PurchasePlanDetail detailParam = new PurchasePlanDetail();
+                detailParam.setPlanId(model.getId());
+                List<PurchasePlanDetail> detailList = jdbcClient.getJdbcList(detailParam, connection);
+                if (detailList != null) {
+                    for (PurchasePlanDetail detail : detailList) {
+                        // 5.1 补全明细中的物料信息 ProductMaterial
+                        if (detail.getMaterialCode() != null) {
+                            ProductMaterial materialParam = new ProductMaterial();
+                            materialParam.setCode(detail.getMaterialCode());
+                            // 参考 Go 版 services.JdbcClient.GetJdbcModel(material)
+                            detail.setMaterial(jdbcClient.getJdbcModel(materialParam, connection));
+                        }
+                    }
+                    model.setChildrenList(detailList);
+                } else {
+                    model.setChildrenList(new ArrayList<>());
+                }
+            }
+
+            result.put("records", list);
+            return result;
+        } catch (Exception e) {
+            throw new BizException(e.getMessage());
+        } finally {
+            if (connection != null) {
+                connection.close();
+            }
+        }
+    }
+
+    @Override
+    public PurchasePlan save(PurchasePlan model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        connection.setAutoCommit(false);
+        try {
+            // 1) 生成编码(支持手填校验 + 自动生成)
+            String code = flowNoService.generatePurchasePlanCode(model, connection);
+            model.setCode(code);
+
+            // 2) 初始状态 pending(与 Go 端一致)
+            model.setStatus("pending");
+
+            // 3) 插入主表(会自动保存附件 fileList)
+            jdbcClient.jdbcInsert(model, connection);
+
+            // 4) 插入明细
+            List<PurchasePlanDetail> detailList = model.getChildrenList();
+            if (detailList != null) {
+                for (PurchasePlanDetail detail : detailList) {
+                    detail.setPlanId(model.getId());
+                    jdbcClient.jdbcInsert(detail, connection);
+                }
+            }
+
+            connection.commit();
+            return model;
+        } catch (Exception e) {
+            connection.rollback();
+            throw new BizException(e.getMessage());
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public PurchasePlan update(PurchasePlan model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        connection.setAutoCommit(false);
+        try {
+            // 1) 更新主表(会自动保存附件 fileList)
+            jdbcClient.jdbcUpdateById(model, connection);
+
+            // 2) 删除旧明细
+            PurchasePlanDetail removeParam = new PurchasePlanDetail();
+            removeParam.setPlanId(model.getId());
+            jdbcClient.jdbcRemove(removeParam, connection);
+
+            // 3) 插入新明细
+            List<PurchasePlanDetail> detailList = model.getChildrenList();
+            if (detailList != null) {
+                for (PurchasePlanDetail detail : detailList) {
+                    detail.setPlanId(model.getId());
+                    jdbcClient.jdbcInsert(detail, connection);
+                }
+            }
+
+            connection.commit();
+            return model;
+        } catch (Exception e) {
+            connection.rollback();
+            throw new BizException(e.getMessage());
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public PurchasePlan remove(PurchasePlan model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        connection.setAutoCommit(false);
+        try {
+            // 1) 删除主表(JdbcClient 会自动删除附件)
+            jdbcClient.jdbcRemoveById(model, connection);
+
+            // 2) 删除明细
+            PurchasePlanDetail removeParam = new PurchasePlanDetail();
+            removeParam.setPlanId(model.getId());
+            jdbcClient.jdbcRemove(removeParam, connection);
+
+            connection.commit();
+            return model;
+        } catch (Exception e) {
+            connection.rollback();
+            throw new BizException(e.getMessage());
+        } finally {
+            connection.close();
+        }
+    }
+}