Bladeren bron

fix:销售计划

lumaojun 1 maand geleden
bovenliggende
commit
4a775850ad

+ 10 - 83
easydo-mes/src/main/java/easydo/technology/controller/SalePlanController.java

@@ -1,11 +1,8 @@
 package easydo.technology.controller;
 
-import easydo.technology.components.JdbcClient;
-import easydo.technology.enums.MESEnum;
 import easydo.technology.exception.BizException;
 import easydo.technology.model.SalePlan;
-import easydo.technology.service.FlowNoService;
-import easydo.technology.utils.StringUtil;
+import easydo.technology.service.SalePlanService;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -22,105 +19,35 @@ import java.util.Map;
 @RequestMapping("/salePlan")
 public class SalePlanController {
     @Resource
-    JdbcClient jdbcClient;
-    @Resource
-    DataSource dataSource;
-    @Resource
-    FlowNoService flowNoService;
+    SalePlanService salePlanService;
 
 
     @RequestMapping(value = "/getList")
     public Object getList(@RequestBody Map<String, Object> map) throws Exception {
-        List<SalePlan> list = jdbcClient.getJdbcList(map, SalePlan.class);
+        List<SalePlan> list = salePlanService.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, SalePlan.class, connection);
-            List<SalePlan> records = (List<SalePlan>) recordsPage.get("records");
-
-            return new ResponseEntity<>(recordsPage, HttpStatus.OK);
-        } catch (Exception e) {
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
+        Map<String, Object> recordsPage = salePlanService.getPage(map);
+        return new ResponseEntity<>(recordsPage, HttpStatus.OK);
     }
 
     @RequestMapping(value = "/save")
     public Object save(@RequestBody SalePlan model) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-            connection.setAutoCommit(false);
-            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();
-        }
+        return new ResponseEntity<>(salePlanService.save(model), HttpStatus.OK);
     }
 
     @RequestMapping(value = "/update")
     public Object update(@RequestBody SalePlan model) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-            connection.setAutoCommit(false);
-
-            jdbcClient.jdbcUpdateById(model, connection);
-
-            connection.commit();
-            return new ResponseEntity<>(HttpStatus.OK);
-        } catch (Exception e) {
-            connection.rollback();
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
+        salePlanService.update(model);
+        return new ResponseEntity<>(HttpStatus.OK);
     }
 
     @RequestMapping(value = "/remove")
     public Object deleteProgram(@RequestBody SalePlan model) throws Exception {
-        Connection connection = dataSource.getConnection();
-        try {
-
-            jdbcClient.jdbcRemoveById(model, connection);
-
-            return new ResponseEntity<>(HttpStatus.OK);
-        } catch (Exception e) {
-            throw new BizException(e.getMessage());
-        } finally {
-            connection.close();
-        }
-    }
-
-
-    private synchronized void generateCode(SalePlan model, Connection connection) throws Exception {
-        if (StringUtil.isEmpty(model.getCode())) {
-            while (true) {
-                String flowNo = flowNoService.getFlowNo(MESEnum.FLOW_NO_TYPE_SALE_PLAN.getValue(), connection);
-                SalePlan param = new SalePlan();
-                param.setCode(flowNo);
-                int count = jdbcClient.getJdbcCount(param, connection);
-                if (count == 0) {
-                    model.setCode(flowNo);
-                    break;
-                }
-            }
-        } else {
-            SalePlan param = new SalePlan();
-            param.setCode(model.getCode());
-            int count = jdbcClient.getJdbcCount(param, connection);
-            if (count > 0) {
-                throw new BizException("编号已存在,请重新填写");
-            }
-        }
+        salePlanService.remove(model);
+        return new ResponseEntity<>(HttpStatus.OK);
     }
 }

+ 4 - 0
easydo-mes/src/main/java/easydo/technology/model/SaleOrder.java

@@ -29,6 +29,10 @@ public class SaleOrder extends CommonModel{
     private String contractNo;
     private String orderDate;
     private String remark;
+    private String tenantId;
+    private Long createId;
+    private Long updateId;
+    private String updateTime;
 
     @NotTableField
     private List<SaleOrderDetail> childrenList = new ArrayList<>();

+ 8 - 0
easydo-mes/src/main/java/easydo/technology/model/SalePlan.java

@@ -1,5 +1,6 @@
 package easydo.technology.model;
 
+import easydo.technology.annotation.NotTableField;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 
@@ -18,4 +19,11 @@ public class SalePlan extends CommonModel{
     private BigDecimal saleAmount;
     private String createTime;
     private String remark;
+    private String tenantId;
+    private Long createId;
+    private Long updateId;
+    private String updateTime;
+
+    @NotTableField
+    private BigDecimal totalActualPrice;
 }

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

@@ -33,6 +33,7 @@ public class FlowNoService {
     private final Object productMaterialLock = new Object();
     private final Object customerLock = new Object();
     private final Object productBomLock = new Object();
+    private final Object salePlanLock = new Object();
 
 
     /**
@@ -420,6 +421,61 @@ public class FlowNoService {
         }
     }
 
+    /**
+     * 生成销售计划编码 (SalePlan) - 独立解耦实现
+     */
+    public String generateSalePlanCode(SalePlan model, Connection connection) throws Exception {
+        synchronized (salePlanLock) {
+            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, SalePlan.class, connection);
+                if (count > 0) {
+                    throw new BizException("销售计划编号已存在: " + manualCode);
+                }
+                return manualCode;
+            }
+            while (true) {
+                FlowNo flowNo = new FlowNo();
+                flowNo.setType(MESEnum.FLOW_NO_TYPE_SALE_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_SALE_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, SalePlan.class, connection);
+                if (count == 0) return no;
+            }
+        }
+    }
+
     /**
      * 原始流水号生成方法
      */

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

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

+ 168 - 0
easydo-mes/src/main/java/easydo/technology/service/impl/SalePlanServiceImpl.java

@@ -0,0 +1,168 @@
+package easydo.technology.service.impl;
+
+import easydo.technology.components.JdbcClient;
+import easydo.technology.enums.MESEnum;
+import easydo.technology.exception.BizException;
+import easydo.technology.model.SalePlan;
+import easydo.technology.service.FlowNoService;
+import easydo.technology.service.SalePlanService;
+import easydo.technology.utils.SecurityUtils;
+import easydo.technology.utils.StringUtil;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class SalePlanServiceImpl implements SalePlanService {
+
+    @Resource
+    JdbcClient jdbcClient;
+    @Resource
+    DataSource dataSource;
+    @Resource
+    FlowNoService flowNoService;
+
+    @Override
+    public List<SalePlan> getList(Map<String, Object> map) throws Exception {
+        List<SalePlan> list = jdbcClient.getJdbcList(map, SalePlan.class);
+        fillTotalActualPrice(list);
+        return list;
+    }
+
+    @Override
+    public Map<String, Object> getPage(Map<String, Object> map) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, SalePlan.class, connection);
+            List<SalePlan> records = (List<SalePlan>) recordsPage.get("records");
+            fillTotalActualPrice(records);
+            return recordsPage;
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public SalePlan save(SalePlan model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            connection.setAutoCommit(false);
+
+            // 1. 对齐 Go 逻辑:校验计划是否已存在 (BeginDate + EndDate + Type)
+            SalePlan existPlan = new SalePlan();
+            existPlan.setBeginDate(model.getBeginDate());
+            existPlan.setEndDate(model.getEndDate());
+            existPlan.setType(model.getType());
+            int count = jdbcClient.getJdbcCount(existPlan, connection);
+            if (count > 0) {
+                throw new BizException("该计划已存在,请勿重复添加");
+            }
+
+            // 2. 生成编码
+            model.setCode(flowNoService.generateSalePlanCode(model, connection));
+
+            // 3. 设置创建人
+            model.setCreateId(SecurityUtils.getCurrentUserId());
+
+            // 4. 保存
+            jdbcClient.jdbcInsert(model, connection);
+
+            connection.commit();
+            return model;
+        } catch (Exception e) {
+            connection.rollback();
+            throw e;
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public void update(SalePlan model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            connection.setAutoCommit(false);
+
+            // 1. 校验计划是否已存在 (BeginDate + EndDate + Type),排除自身
+            SalePlan param = new SalePlan();
+            param.setBeginDate(model.getBeginDate());
+            param.setEndDate(model.getEndDate());
+            param.setType(model.getType());
+            List<SalePlan> existList = jdbcClient.getJdbcList(param, connection);
+            if (existList != null && !existList.isEmpty()) {
+                for (SalePlan exist : existList) {
+                    if (!exist.getId().equals(model.getId())) {
+                        throw new BizException("该计划已存在,请勿重复添加");
+                    }
+                }
+            }
+
+            model.setUpdateId(SecurityUtils.getCurrentUserId());
+            jdbcClient.jdbcUpdateById(model, connection);
+            connection.commit();
+        } catch (Exception e) {
+            connection.rollback();
+            throw e;
+        } finally {
+            connection.close();
+        }
+    }
+
+    @Override
+    public void remove(SalePlan model) throws Exception {
+        Connection connection = dataSource.getConnection();
+        try {
+            jdbcClient.jdbcRemoveById(model, connection);
+        } finally {
+            connection.close();
+        }
+    }
+
+    private void fillTotalActualPrice(List<SalePlan> records) throws Exception {
+        if (records == null || records.isEmpty()) {
+            return;
+        }
+        for (SalePlan model : records) {
+            calculateTotalActualPrice(model);
+        }
+    }
+
+    private void calculateTotalActualPrice(SalePlan model) throws Exception {
+        if (model == null) {
+            return;
+        }
+        if (StringUtil.isEmpty(model.getBeginDate()) || StringUtil.isEmpty(model.getEndDate())) {
+            model.setTotalActualPrice(BigDecimal.ZERO);
+            return;
+        }
+
+        String sql = String.format("SELECT sum(actual_price) as actualPrice FROM sale_order where order_date > '%s' and order_date < '%s' and status = '%s'",
+                model.getBeginDate(), model.getEndDate(), MESEnum.SALE_ORDER_OF_STATUS_COMPLETE.getValue());
+
+        Connection connection = dataSource.getConnection();
+        Statement statement = null;
+        ResultSet resultSet = null;
+        try {
+            statement = connection.createStatement();
+            resultSet = statement.executeQuery(sql);
+            if (resultSet.next()) {
+                BigDecimal total = resultSet.getBigDecimal("actualPrice");
+                model.setTotalActualPrice(total != null ? total : BigDecimal.ZERO);
+            } else {
+                model.setTotalActualPrice(BigDecimal.ZERO);
+            }
+        } finally {
+            if (resultSet != null) resultSet.close();
+            if (statement != null) statement.close();
+            connection.close();
+        }
+    }
+}
+