| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 |
- package easydo.technology.service;
- import easydo.technology.components.JdbcClient;
- import easydo.technology.enums.MESEnum;
- import easydo.technology.exception.BizException;
- import easydo.technology.model.*;
- import easydo.technology.utils.StringUtil;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.HashMap;
- import java.util.Map;
- @Service
- public class FlowNoService {
- private static final DateTimeFormatter DFY_MD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- private static final DateTimeFormatter DFY_MD_2 = DateTimeFormatter.ofPattern("yyyyMMdd");
- @Resource
- private JdbcClient jdbcClient;
- @Resource
- private DataSource dataSource;
- // 为每个类定义独立的私有锁,实现细粒度并发控制 (Warehouse 锁 Warehouse)
- private final Object warehouseLock = new Object();
- private final Object qualityLock = new Object();
- private final Object processStageLock = new Object();
- private final Object processRouteLock = new Object();
- private final Object productMaterialLock = new Object();
- private final Object customerLock = new Object();
- private final Object productBomLock = new Object();
- private final Object salePlanLock = new Object();
- private final Object purchasePlanLock = new Object();
- private final Object outsourcingPlanLock = new Object();
- private final Object productPlanLock = new Object();
- private final Object purchaseOrderLock = new Object();
- private final Object productOrderLock = new Object();
- private final Object productOrderDispatchLock = new Object();
- private final Object materialRequisitionLock = new Object();
- private final Object warehouseOutboundLock = new Object();
- /**
- * 生成仓库编码 (Warehouse) - 独立解耦实现
- */
- public String generateWarehouseCode(Warehouse model, Connection connection) throws Exception {
- synchronized (warehouseLock) {
- 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, Warehouse.class, connection);
- if (count > 0) {
- throw new BizException("仓库编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_WAREHOUSE.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_WAREHOUSE.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, Warehouse.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成质检方案编码 (QualityInspectProgram) - 独立解耦实现
- */
- public String generateQualityCode(QualityInspectProgram model, Connection connection) throws Exception {
- synchronized (qualityLock) {
- 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, QualityInspectProgram.class, connection);
- if (count > 0) {
- throw new BizException("质检编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_QUALITY_INSPECT_PROGRAM.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_QUALITY_INSPECT_PROGRAM.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, QualityInspectProgram.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成工序管理编码 (ProcessStage) - 独立解耦实现
- */
- public String generateProcessStageCode(ProcessStage model, Connection connection) throws Exception {
- synchronized (processStageLock) {
- 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, ProcessStage.class, connection);
- if (count > 0) {
- throw new BizException("工序编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PROCESS_STAGE.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_PROCESS_STAGE.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, ProcessStage.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成工艺路线编码 (ProcessRoute) - 独立解耦实现
- */
- public String generateProcessRouteCode(ProcessRoute model, Connection connection) throws Exception {
- synchronized (processRouteLock) {
- 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, ProcessRoute.class, connection);
- if (count > 0) {
- throw new BizException("工艺路线编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PROCESS_ROUTE.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_PROCESS_ROUTE.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, ProcessRoute.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成物料编码 (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(MESEnum.FLOW_NO_TYPE_PRODUCT_MATERIAL.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_PRODUCT_MATERIAL.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, ProductMaterial.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成客户编码 (Customer) - 独立解耦实现
- */
- public String generateCustomerCode(Customer model, Connection connection) throws Exception {
- synchronized (customerLock) {
- 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, Customer.class, connection);
- if (count > 0) {
- throw new BizException("客户编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_CUSTOMER.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_CUSTOMER.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, Customer.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成物料BOM编码 (ProductBom) - 独立解耦实现
- */
- public String generateProductBomCode(ProductBom model, Connection connection) throws Exception {
- synchronized (productBomLock) {
- String manualCode = model.getBomCode();
- String tenantId = model.getTenantId();
- if (StringUtil.isNotEmpty(manualCode)) {
- Map<String, Object> checkMap = new HashMap<>();
- checkMap.put("bomCode", manualCode);
- checkMap.put("tenantId", tenantId);
- int count = (int) jdbcClient.getJdbcCountByMap(checkMap, ProductBom.class, connection);
- if (count > 0) {
- throw new BizException("BOM编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PRODUCT_BOM.getValue());
- flowNo.setTenantId(tenantId);
- flowNo = jdbcClient.getJdbcModel(flowNo, connection);
- if (flowNo == null) throw new BizException("未配置物料BOM流水号规则");
- 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_PRODUCT_BOM.getValue());
- updateMap.put("tenantId", tenantId);
- jdbcClient.jdbcUpdate(flowNo, updateMap, connection);
- Map<String, Object> checkMap = new HashMap<>();
- checkMap.put("bomCode", no);
- checkMap.put("tenantId", tenantId);
- int count = (int) jdbcClient.getJdbcCountByMap(checkMap, ProductBom.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成销售计划编码 (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;
- }
- }
- }
- /**
- * 生成生产计划编码 (ProductPlan) - 独立解耦实现
- */
- public String generateProductPlanCode(ProductPlan model, Connection connection) throws Exception {
- synchronized (productPlanLock) {
- 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, ProductPlan.class, connection);
- if (count > 0) {
- throw new BizException("生产计划编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PRODUCT_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_PRODUCT_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, ProductPlan.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成采购计划编码 (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;
- }
- }
- }
- /**
- * 生成委外计划编码 (OutsourcingPlan) - 独立解耦实现
- */
- public String generateOutsourcingPlanCode(OutsourcingPlan model, Connection connection) throws Exception {
- synchronized (outsourcingPlanLock) {
- 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, OutsourcingPlan.class, connection);
- if (count > 0) {
- throw new BizException("委外计划编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_OUTSOURCING_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_OUTSOURCING_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, OutsourcingPlan.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成采购订单编码 (PurchaseOrder) - 独立解耦实现
- */
- public String generatePurchaseOrderCode(PurchaseOrder model, Connection connection) throws Exception {
- synchronized (purchaseOrderLock) {
- 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, PurchaseOrder.class, connection);
- if (count > 0) {
- throw new BizException("采购订单编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PURCHASE_ORDER.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_ORDER.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, PurchaseOrder.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成工单编码 (ProductOrder) - 独立解耦实现
- */
- public String generateProductOrderCode(ProductOrder model, Connection connection) throws Exception {
- synchronized (productOrderLock) {
- 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, ProductOrder.class, connection);
- if (count > 0) {
- throw new BizException("工单编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PRODUCT_ORDER.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_PRODUCT_ORDER.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, ProductOrder.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成派工单编码 (ProductOrderDispatch) - 独立解耦实现
- */
- public String generateProductOrderDispatchCode(ProductOrderDispatch model, Connection connection) throws Exception {
- synchronized (productOrderDispatchLock) {
- 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, ProductOrderDispatch.class, connection);
- if (count > 0) {
- throw new BizException("派工单编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_PRODUCT_ORDER_DISPATCH.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_PRODUCT_ORDER_DISPATCH.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, ProductOrderDispatch.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成领料单编码 (MaterialRequisition) - 独立解耦实现
- */
- public String generateMaterialRequisitionCode(MaterialRequisition model, Connection connection) throws Exception {
- synchronized (materialRequisitionLock) {
- 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, MaterialRequisition.class, connection);
- if (count > 0) {
- throw new BizException("领料单编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_MATERIAL_REQUISITION.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_MATERIAL_REQUISITION.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, MaterialRequisition.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 生成出库单编码 (WarehouseOutbound) - 独立解耦实现
- */
- public String generateWarehouseOutboundCode(WarehouseOutbound model, Connection connection) throws Exception {
- synchronized (warehouseOutboundLock) {
- 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, WarehouseOutbound.class, connection);
- if (count > 0) {
- throw new BizException("出库单编号已存在: " + manualCode);
- }
- return manualCode;
- }
- while (true) {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(MESEnum.FLOW_NO_TYPE_WAREHOUSE_OUTBOUND.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_WAREHOUSE_OUTBOUND.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, WarehouseOutbound.class, connection);
- if (count == 0) return no;
- }
- }
- }
- /**
- * 原始流水号生成方法
- */
- public String getFlowNo(String type, Connection... connections) throws Exception {
- Connection connection = null;
- if (connections.length > 0) {
- connection = connections[0];
- } else {
- connection = dataSource.getConnection();
- }
- try {
- FlowNo flowNo = new FlowNo();
- flowNo.setType(type);
- flowNo = jdbcClient.getJdbcModel(flowNo, connection);
- DateTimeFormatter DFY_MD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- DateTimeFormatter DFY_MD_2 = DateTimeFormatter.ofPattern("yyyyMMdd");
- String currDate = DFY_MD.format(LocalDateTime.now());
- String currDate2 = DFY_MD_2.format(LocalDateTime.now());
- String no;
- if (StringUtil.isNotEmpty(flowNo.getCurrDate()) && !flowNo.getCurrDate().equals(currDate)) {
- flowNo.setCurrDate(currDate);
- flowNo.setCurrSeq(1);
- } else {
- flowNo.setCurrSeq(flowNo.getCurrSeq() + 1);
- }
- if (StringUtil.isEmpty(flowNo.getCurrDate())) {
- String currSeq = String.format("%06d", flowNo.getCurrSeq());
- no = flowNo.getPrefix() + currSeq;
- } else {
- String currSeq = String.format("%05d", flowNo.getCurrSeq());
- no = flowNo.getPrefix() + currDate2 + currSeq;
- }
- flowNo.setCurrNo(no);
- Map<String, Object> paramMap = new HashMap<>();
- paramMap.put("type", type);
- jdbcClient.jdbcUpdate(flowNo, paramMap, connection);
- return no;
- } catch (Exception e) {
- throw new BizException(e.getMessage());
- } finally {
- if (connections.length == 0) {
- connection.close();
- }
- }
- }
- }
|