Browse Source

采购入库

chenc 3 years ago
parent
commit
9365cc6b2f

+ 53 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/transactional/ComboTransaction.java

@@ -0,0 +1,53 @@
+package org.jeecg.config.transactional;
+
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.Callable;
+import java.util.stream.Stream;
+
+/**
+ * 功能描述:
+ *
+ * @Author: chenchuang
+ * @Date: 2022/3/31 16:56
+ */
+
+@Component
+public class ComboTransaction {
+    @Autowired
+    private Db1TxBroker db1TxBroker;
+
+    @Autowired
+    private Db2TxBroker db2TxBroker;
+
+    public <V> V inCombinedTx(Callable<V> callable, String[] transactions) {
+        if (callable == null) {
+            return null;
+        }
+
+        Callable<V> combined = Stream.of(transactions)
+                .filter(ele -> !StringUtils.isEmpty(ele))
+                .distinct()
+                .reduce(callable, (r, tx) -> {
+                    switch (tx) {
+                        case DbTxConstants.DB1_TX:
+                            return () -> db1TxBroker.inTransaction(r);
+                        case DbTxConstants.DB2_TX:
+                            return () -> db2TxBroker.inTransaction(r);
+                        default:
+                            return null;
+                    }
+                }, (r1, r2) -> r2);
+
+        try {
+            return combined.call();
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}

+ 28 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/transactional/Db1TxBroker.java

@@ -0,0 +1,28 @@
+package org.jeecg.config.transactional;
+
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.concurrent.Callable;
+
+/**
+ * 功能描述:
+ *
+ * @Author: chenchuang
+ * @Date: 2022/3/31 16:57
+ */
+
+@Component
+public class Db1TxBroker {
+    @Transactional(DbTxConstants.DB1_TX)
+    public <V> V inTransaction(Callable<V> callable) {
+        try {
+            return callable.call();
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}

+ 27 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/transactional/Db2TxBroker.java

@@ -0,0 +1,27 @@
+package org.jeecg.config.transactional;
+
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.concurrent.Callable;
+
+/**
+ * 功能描述:
+ *
+ * @Author: chenchuang
+ * @Date: 2022/3/31 16:58
+ */
+
+@Component
+public class Db2TxBroker {
+    @Transactional(value = DbTxConstants.DB2_TX)
+    public <V> V inTransaction(Callable<V> callable) {
+        try {
+            return callable.call();
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 13 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/transactional/DbTxConstants.java

@@ -0,0 +1,13 @@
+package org.jeecg.config.transactional;
+
+/**
+ * 功能描述:
+ *
+ * @Author: chenchuang
+ * @Date: 2022/3/31 16:50
+ */
+public class DbTxConstants {
+    public static final String DB1_TX = "master";
+
+    public static final String DB2_TX = "multidatasource1";
+}

+ 44 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/transactional/MultiTransactionAop.java

@@ -0,0 +1,44 @@
+package org.jeecg.config.transactional;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 功能描述:
+ *
+ * @Author: chenchuang
+ * @Date: 2022/3/31 16:50
+ */
+
+@Aspect
+@Component
+public class MultiTransactionAop {
+    private final ComboTransaction comboTransaction;
+
+    @Autowired
+    public MultiTransactionAop(ComboTransaction comboTransaction) {
+        this.comboTransaction = comboTransaction;
+    }
+
+    @Pointcut("@annotation(org.jeecg.config.transactional.MultiTransactional)")
+    public void pointCut() {
+    }
+
+    @Around("pointCut() && @annotation(multiTransactional)")
+    public Object inMultiTransactions(ProceedingJoinPoint pjp, MultiTransactional multiTransactional) {
+        return comboTransaction.inCombinedTx(() -> {
+            try {
+                return pjp.proceed();
+            } catch (Throwable throwable) {
+                if (throwable instanceof RuntimeException) {
+                    throw (RuntimeException) throwable;
+                }
+                throw new RuntimeException(throwable);
+            }
+        }, multiTransactional.value());
+    }
+}

+ 16 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/config/transactional/MultiTransactional.java

@@ -0,0 +1,16 @@
+package org.jeecg.config.transactional;
+
+import java.lang.annotation.*;
+
+/**
+ * 功能描述:
+ *
+ * @Author: chenchuang
+ * @Date: 2022/3/31 16:49
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface MultiTransactional {
+    String[] value() default {};
+}

+ 55 - 4
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/controller/PurchaseWarehousingController.java

@@ -1,20 +1,22 @@
 package org.jeecg.modules.openApi.controller;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.system.query.QueryGenerator;
 import org.jeecg.common.aspect.annotation.AutoLog;
 import org.jeecg.common.util.oConvertUtils;
+import org.jeecg.config.transactional.DbTxConstants;
+import org.jeecg.config.transactional.MultiTransactional;
 import org.jeecg.modules.openApi.entity.PurchaseWarehousing;
 import org.jeecg.modules.openApi.service.IPurchaseWarehousingService;
-import java.util.Date;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -164,4 +166,53 @@ public class PurchaseWarehousingController extends JeecgController<PurchaseWareh
       return super.importExcel(request, response, PurchaseWarehousing.class);
   }
 
+  @PostMapping(value = "/saveBath")
+//  @MultiTransactional(value = {DbTxConstants.DB1_TX, DbTxConstants.DB2_TX})
+  public Result saveBath(@RequestBody JSONArray json){
+  	Result result=new Result();
+
+	if(json!=null&&json.size()>0){
+		//账套1
+		List<Map<String,Object>> mapListOne=new ArrayList<>();
+		//账套2
+		List<Map<String,Object>> mapListTwo=new ArrayList<>();
+		//账套3
+		List<Map<String,Object>> mapListThree=new ArrayList<>();
+		for(int i=0;i<json.size();i++){
+			//获取对象
+			JSONObject jsonObject=json.getJSONObject(i);
+			//获取账套
+			String cAccId=jsonObject.get("cAccId").toString();
+			//转map
+			Map<String,Object> map=updateMap(jsonObject);
+			if(cAccId.equals("101")){
+				mapListOne.add(map);
+			}else if(cAccId.equals("102")){
+				mapListTwo.add(map);
+			}else if(cAccId.equals("103")){
+				mapListThree.add(map);
+			}
+		}
+//		purchaseWarehousingService.aa();
+//		purchaseWarehousingService.bb();
+		purchaseWarehousingService.savaOne(mapListOne);
+//		result.setResult(mapList);
+	}
+
+  	return result;
+  }
+
+  public Map<String,Object> updateMap(JSONObject jsonObject){
+
+	//map对象
+	  Map<String, Object> data =new HashMap<>();
+	//循环转换
+	  Iterator it =jsonObject.entrySet().iterator();
+	  while (it.hasNext()) {
+		  Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
+		  data.put(entry.getKey(), entry.getValue());
+	  }
+	  return data;
+  }
+
 }

+ 27 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/mapper/PurchaseWarehousingMapper.java

@@ -1,6 +1,7 @@
 package org.jeecg.modules.openApi.mapper;
 
 import java.util.List;
+import java.util.Map;
 
 import org.apache.ibatis.annotations.Param;
 import org.jeecg.modules.openApi.entity.PurchaseWarehousing;
@@ -14,4 +15,30 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  */
 public interface PurchaseWarehousingMapper extends BaseMapper<PurchaseWarehousing> {
 
+    /**
+    * @Author chenchuang
+    * @Description //TODO 获取最新id
+    * @Date 2022/4/1 14:34
+    * @Param [table]
+    * @return java.lang.String
+    */
+    Integer getMaxId(@Param("id")String id,@Param("tableName")String tableName);
+
+    /**
+    * @Author chenchuang
+    * @Description //TODO 采购入库主表新增
+    * @Date 2022/4/1 16:30
+    * @Param [map]
+    * @return void
+    */
+    void saveRdRecord01One(@Param("RdRecord01")Map<String,Object> map);
+
+    /**
+    * @Author chenchuang
+    * @Description //TODO 采购入库子表新增
+    * @Date 2022/4/1 18:54
+    * @Param [list]
+    * @return void
+    */
+    void saveRdrecords01One(@Param("list")List<Map<String,Object>> list);
 }

+ 97 - 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/mapper/xml/PurchaseWarehousingMapper.xml

@@ -1,5 +1,101 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="org.jeecg.modules.demo.openApi.mapper.PurchaseWarehousingMapper">
+<mapper namespace="org.jeecg.modules.openApi.mapper.PurchaseWarehousingMapper">
+
+    <select id="getMaxId" resultType="java.lang.Integer">
+        select max(${id}) from ${tableName}
+    </select>
+
+    <insert id="saveRdRecord01One">
+        INSERT INTO RdRecord01
+        (
+          ID,
+          cVouchType,
+          cCode,
+          dDate,
+          cWhCode,
+          cOrderCode,
+          cARVCode,
+          cVenCode,
+          cDepCode,
+          cPersonCode,
+          dARVDate,
+          cBusType,
+          cPTCode,
+          cMemo,
+          cRdCode,
+          cDefine10,
+          bredvouch,<!--红蓝标识-->
+          bRdFlag,<!--收发标志-->
+          cSource,<!--单据来源-->
+          bTransFlag,<!--是否传递 -->
+          bIsSTQc<!--是否传递 -->
+        )
+        VALUES
+            (
+                #{RdRecord01.ID},
+                '01',
+                #{RdRecord01.cCode},
+                #{RdRecord01.dDate},
+                #{RdRecord01.cWhCode},
+                #{RdRecord01.cOrderCode},
+                #{RdRecord01.cARVCode},
+                #{RdRecord01.cVenCode},
+                #{RdRecord01.cDepCode},
+                #{RdRecord01.cPersonCode},
+                #{RdRecord01.dARVDate},
+                #{RdRecord01.cBusType},
+                #{RdRecord01.cPTCode},
+                #{RdRecord01.cMemo},
+                #{RdRecord01.cRdCode},
+                #{RdRecord01.exportInvoiceNo},
+                #{RdRecord01.cVouchType},
+                1,
+                '云工厂',
+                0,
+                0
+        )
+    </insert>
+    
+    <insert id="saveRdrecords01One" parameterType="java.util.List">
+        <foreach collection="list" item="item" index="index" separator=";">
+          INSERT INTO rdrecords01
+            (
+              AutoID,
+              ID,
+              cDefine22,
+              cDefine28,
+              cInvCode,
+              iQuantity,
+              cBatch,
+              cFree1,
+              cDefine30,
+              cbMemo,
+              cPOID,
+              irowno,
+              iFlag,<!--是否传递 -->
+            iMatSettleState,<!--结算状态 -->
+            iBillSettleCount<!--结算次数 -->
+            )
+            VALUES
+            (
+              #{item.AutoID},
+              #{item.ID},
+              #{item.cDefine22},
+              #{item.cDefine28},
+              #{item.cInvCode},
+              #{item.iQuantity},
+              #{item.cBatch},
+              #{item.colour},
+              #{item.cDefine30},
+              #{item.cbMemo},
+              #{item.iordercode},
+            #{item.irowno},
+            0,
+            0,
+            0
+            )
+        </foreach>
+    </insert>
 
 </mapper>

+ 9 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/service/IPurchaseWarehousingService.java

@@ -3,6 +3,9 @@ package org.jeecg.modules.openApi.service;
 import org.jeecg.modules.openApi.entity.PurchaseWarehousing;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import java.util.List;
+import java.util.Map;
+
 /**
  * @Description: 采购入库
  * @Author: jeecg-boot
@@ -11,4 +14,10 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IPurchaseWarehousingService extends IService<PurchaseWarehousing> {
 
+    void savaOne(List<Map<String,Object>> mapList);
+
+
+    Integer getMaxId(String id,String tableName);
+
+
 }

+ 73 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/service/impl/PurchaseWarehousingServiceImpl.java

@@ -1,11 +1,20 @@
 package org.jeecg.modules.openApi.service.impl;
 
+import com.baomidou.dynamic.datasource.annotation.DS;
 import org.jeecg.modules.openApi.entity.PurchaseWarehousing;
 import org.jeecg.modules.openApi.mapper.PurchaseWarehousingMapper;
 import org.jeecg.modules.openApi.service.IPurchaseWarehousingService;
+import org.jeecg.modules.system.entity.SysUser;
+import org.jeecg.modules.system.service.ISysUserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 采购入库
@@ -13,7 +22,71 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  * @Date:   2022-03-31
  * @Version: V1.0
  */
+@Transactional
 @Service
 public class PurchaseWarehousingServiceImpl extends ServiceImpl<PurchaseWarehousingMapper, PurchaseWarehousing> implements IPurchaseWarehousingService {
 
+    @Autowired
+    private ISysUserService iSysUserService;
+
+    @Autowired
+    private PurchaseWarehousingMapper purchaseWarehousingMapper;
+
+    @DS("multi-datasource1")
+    @Override
+    public void savaOne(List<Map<String, Object>> mapList) {
+        if(mapList!=null&&mapList.size()>0){
+
+            for(Map<String,Object> map:mapList){
+                //获取主表最大编码
+                map.put("ID",getMaxId("ID","RdRecord01"));
+                System.out.println(map.get("ID"));
+                //红蓝标识
+                if(map.get("cVouchType").toString().equals("1")){
+                    map.put("cVouchType",0);//采购入库
+                }else{
+                    map.put("cVouchType",1);//红字入库
+                }
+                //入库类别、采购类型
+                if(map.get("cRdCode").toString().equals("采购入库")){
+                    //采购入库
+                    map.put("cRdCode",11);
+                    map.put("cPTCode",01);
+                }else{
+                    //委外入库
+                    map.put("cRdCode",12);
+                    map.put("cPTCode",02);
+                }
+                this.baseMapper.saveRdRecord01One(map);
+                //子表
+                if(map.get("item")!=null&&!map.get("item").equals("")){
+                    List<Map<String,Object>> itemList=(List<Map<String,Object>>)map.get("item");
+                    Integer rowId=1;
+                    for(Map<String,Object> itemMap:itemList){
+                        //获取子表最大编码
+                        itemMap.put("AutoID",getMaxId("AutoID","rdrecords01"));
+                        //主表id
+                        itemMap.put("ID",map.get("ID"));
+                        //行号
+                        itemMap.put("irowno",rowId);
+                        rowId++;
+                    }
+                    this.baseMapper.saveRdrecords01One(itemList);
+                }
+            }
+        }
+    }
+
+    @Override
+    public Integer getMaxId(String id, String tableName) {
+        Integer maxId=purchaseWarehousingMapper.getMaxId(id,tableName);
+        if(maxId!=null){
+            maxId=maxId+1;
+        }else{
+            maxId=0;
+        }
+        return maxId;
+    }
+
+
 }