Bläddra i källkod

1.优化货权转移逻辑

2.删除前新增判断该垛位下是否存在钢板
dw 2 veckor sedan
förälder
incheckning
fdc07c8d14

+ 4
- 0
src/main/java/com/th/demo/mapping/ware/StoreMapper.java Visa fil

@@ -72,4 +72,8 @@ public interface StoreMapper {
72 72
      * 修改入库表收货地址
73 73
      */
74 74
     int  updateInAddressById(@Param( "inId") List<String> inId,@Param( "address") String  address);
75
+    /**
76
+     * 根据垛位id查询当前垛位下是否存在未出库钢板
77
+     */
78
+    List<String> queryStoreById(@Param("stackId") String stackId);
75 79
 }

+ 17
- 0
src/main/java/com/th/demo/service/impl/maint/StackServiceImpl.java Visa fil

@@ -7,14 +7,18 @@ import com.github.pagehelper.PageHelper;
7 7
 import com.github.pagehelper.PageInfo;
8 8
 import com.th.demo.mapping.maint.StackMapper;
9 9
 import com.th.demo.mapping.maint.WareMapper;
10
+import com.th.demo.mapping.ware.StoreMapper;
10 11
 import com.th.demo.model.maint.Stack;
11 12
 import com.th.demo.model.maint.Ware;
12 13
 import com.th.demo.model.system.Type;
14
+import com.th.demo.model.ware.Store;
13 15
 import com.th.demo.service.maint.StackService;
14 16
 import com.th.demo.tools.JSONTools;
15 17
 import com.th.demo.tools.Tools;
18
+import org.apache.commons.lang3.StringUtils;
16 19
 import org.springframework.beans.factory.annotation.Autowired;
17 20
 import org.springframework.stereotype.Service;
21
+import org.springframework.util.ObjectUtils;
18 22
 
19 23
 import java.util.ArrayList;
20 24
 import java.util.Date;
@@ -27,6 +31,8 @@ public class StackServiceImpl implements StackService {
27 31
     public StackMapper stackMapper;
28 32
     @Autowired
29 33
     public WareMapper wareMapper;
34
+    @Autowired
35
+    public StoreMapper storeMapper;
30 36
 
31 37
 
32 38
 
@@ -76,6 +82,17 @@ public class StackServiceImpl implements StackService {
76 82
 
77 83
     @Override
78 84
     public String remove(String id, String userId) {
85
+        /**
86
+         * 2026-5-07
87
+         * 删除前新增判断该垛位下是否存在 ( 未出库,未删除 ) 钢板
88
+         * 如果存在禁止删除
89
+         * */
90
+        List<String> stores = storeMapper.queryStoreById(id);
91
+        if(!ObjectUtils.isEmpty(stores)){
92
+            String plateNos = StringUtils.join(",", stores);
93
+            return JSONTools.toString(1,"当前垛位下存在钢板 "+plateNos+";");
94
+        }
95
+
79 96
         Stack stack = stackMapper.selectByPrimaryKey(id);
80 97
         stack.setCancelFlag("9");
81 98
         stack.setCancelId(userId);

+ 108
- 72
src/main/java/com/th/demo/service/impl/ware/ChangeServiceImpl.java Visa fil

@@ -28,6 +28,7 @@ import com.th.demo.tools.Tools;
28 28
 import org.springframework.beans.factory.annotation.Autowired;
29 29
 import org.springframework.stereotype.Service;
30 30
 import org.springframework.transaction.annotation.Transactional;
31
+import org.springframework.util.StringUtils;
31 32
 
32 33
 import java.util.Collections;
33 34
 import java.util.Date;
@@ -53,84 +54,135 @@ public class ChangeServiceImpl implements ChangeService {
53 54
     int num = 0;
54 55
     String result = Type.FAIL;
55 56
 
56
-
57
+    /**
58
+     * 货权转移
59
+     * 解决:货权转移后Cargo重量不减少、中途return不扣重量、空指针、事务部分提交BUG
60
+     */
57 61
     @Transactional(rollbackFor = Exception.class)
58 62
     @Override
59
-    public String changeCustomer(String json, String customer,String address,String userId, String belongId) throws Exception{
60
-        List<Store> list = JSON.parseObject(json, new TypeReference<List<Store>>() {});
63
+    public String changeCustomer(String json, String customer, String address, String userId, String belongId) throws Exception {
64
+        // 1. 基础参数非空校验
65
+        if (!StringUtils.hasText(json) || !StringUtils.hasText(customer) || !StringUtils.hasText(userId) || !StringUtils.hasText(belongId)) {
66
+            throw new Exception("传入参数不能为空");
67
+        }
68
+
69
+        // 2. 解析库存列表 & 新货主
70
+        List<Store> storeList = JSON.parseObject(json, new TypeReference<List<Store>>() {});
71
+        if (storeList == null || storeList.isEmpty()) {
72
+            throw new Exception("暂无需要转移的库存数据");
73
+        }
74
+
61 75
         Customer c1 = JSON.parseObject(customer, new TypeReference<Customer>() {});
62
-        Customer c = customerMapper.selectByPrimaryKey(c1.getId());//新货主
63
-        String recordType = "";
64
-        double sum_weight = 0;//可以进行货权转移的重量(货权转移的地方维护的)
65
-        double use_weight = 0;//已经使用的货权转移的重量
66
-        // 增加校验
67
-        Store store = storeMapper.selectByPrimaryKey(list.get(0).getId());
68
-        Customer fk_cus = customerMapper.selectByPrimaryKey(store.getCustomer().getId());//老货主
69
-        Cargo cargo = cargoMapper.selectByByFkAndCustomer(fk_cus.getId(),c.getId(),belongId);
70
-        if (cargo == null){
71
-            sum_weight=0;
72
-        }else{
73
-            if(cargo.getSumWeight() == 0){
74
-                num = 10;
75
-                return JSONTools.toString(1,"货权信息已维护,但可是用量为0");
76
-            }else {
77
-                sum_weight = cargo.getSumWeight();
76
+        if (c1 == null || c1.getId() == null) {
77
+            throw new Exception("新货主信息解析失败");
78
+        }
79
+
80
+        // 3. 查询新货主、第一条库存、老货主
81
+        Customer newCustomer = customerMapper.selectByPrimaryKey(c1.getId());
82
+        if (newCustomer == null) {
83
+            throw new Exception("新货主不存在");
84
+        }
85
+
86
+        Store firstStore = storeMapper.selectByPrimaryKey(storeList.get(0).getId());
87
+        if (firstStore == null || firstStore.getCustomer() == null) {
88
+            throw new Exception("库存信息或原货主信息不存在");
89
+        }
90
+
91
+        Customer oldCustomer = customerMapper.selectByPrimaryKey(firstStore.getCustomer().getId());
92
+        if (oldCustomer == null) {
93
+            throw new Exception("原货主不存在");
94
+        }
95
+
96
+        // 4. 查询货权配置记录
97
+        Cargo cargo = cargoMapper.selectByByFkAndCustomer(oldCustomer.getId(), newCustomer.getId(), belongId);
98
+        double sumWeight = 0;
99
+        if (cargo != null) {
100
+            sumWeight = cargo.getSumWeight();
101
+            // 货权可用重量为0直接拦截
102
+            if (sumWeight <= 0) {
103
+                throw new Exception("货权信息已维护,可用转移重量为0");
78 104
             }
79 105
         }
80 106
 
81
-        for (int i = 0; i < list.size(); i++) {
82
-            //保留可转移重量不为0的情况下,货权转移重量的计算
83
-            // 25.5.27增加规则维护货权转移信息的,只要重量为0了
84
-            if(sum_weight != 0 && (sum_weight-use_weight-list.get(i).getWeight()) < 0){
85
-                num =10;
86
-                return JSONTools.toString(1,"货权转移重量不足");
107
+        double useWeight = 0;// 已累计使用转移重量
108
+        int num = 0;// 数据库受影响行数 初始化
109
+        String recordType;
110
+
111
+        // 5. 循环逐条处理库存转移
112
+        for (Store store : storeList) {
113
+            if (store == null || store.getWeight() <= 0) {
114
+                continue;
87 115
             }
88
-            if(list.get(i).getCustomer().getName().equals(c.getName())){
116
+            double currentWeight = store.getWeight();
117
+
118
+            // 重量不足校验
119
+            if (sumWeight > 0 && (sumWeight - useWeight - currentWeight) < 0) {
120
+                throw new Exception("货权转移重量不足");
121
+            }
122
+
123
+            // 判断记录类型
124
+            if (newCustomer.getName().equals(store.getCustomer().getName())) {
89 125
                 recordType = "0";
90
-            }else {
126
+            } else {
91 127
                 recordType = "1";
92
-                AccountDetail accountDetail = getAccountDetail(list.get(i),  userId, belongId);
128
+                // 插入账户明细
129
+                AccountDetail accountDetail = getAccountDetail(store, userId, belongId);
93 130
                 num += accountDetailMapper.insert(accountDetail);
94 131
             }
95
-            ChangeRecord changeRecord = getChangeRecord(recordType, list.get(i), c, address, userId, belongId);
132
+
133
+            // 插入变更记录
134
+            ChangeRecord changeRecord = getChangeRecord(recordType, store, newCustomer, address, userId, belongId);
96 135
             num += changeRecordMapper.insert(changeRecord);
97
-            list.get(i).setCustomer(c);
98
-            list.get(i).setFkComponyId(fk_cus.getId());
99
-//            新增判断 地址为空不做处理 地址不为空时 0:清空地址 其他:更新地址
100
-            if (address!=null && !address.equals("")){
101
-                if(address.equals("0")){
102
-                    list.get(i).setReceiveAddress("");
136
+
137
+            // 修改库存归属
138
+            store.setCustomer(newCustomer);
139
+            store.setFkComponyId(oldCustomer.getId());
140
+
141
+            // 地址处理
142
+            if (StringUtils.hasText(address)) {
143
+                if ("0".equals(address)) {
144
+                    store.setReceiveAddress("");
103 145
                 } else {
104
-                    list.get(i).setReceiveAddress(address);
146
+                    store.setReceiveAddress(address);
105 147
                 }
106 148
             }
107
-            num += storeMapper.updateByPrimaryKey(list.get(i));
108
-            num += storeMapper.updateFkById(list.get(i));
109
-            use_weight = use_weight + list.get(i).getWeight();
149
+
150
+            // 更新库存
151
+            num += storeMapper.updateByPrimaryKey(store);
152
+            num += storeMapper.updateFkById(store);
153
+
154
+            // 累加已使用重量
155
+            useWeight += currentWeight;
156
+
157
+            // ========== 关键修复:逐条实时扣减货权重量,不放到循环外 ==========
158
+            if (cargo != null) {
159
+                cargo.setSumWeight(cargo.getSumWeight() - currentWeight);
160
+                cargo.setUseWeight(cargo.getUseWeight() + currentWeight);
161
+                // 立即更新数据库,每处理一条扣一条,避免中途中断完全不扣
162
+                num += cargoMapper.updateByPrimaryKey(cargo);
163
+            }
110 164
         }
111
-        if (cargo != null){
112
-            cargo.setSumWeight(cargo.getSumWeight() - use_weight);
113
-            cargo.setUseWeight(cargo.getUseWeight() + use_weight);
114
-            num = cargoMapper.updateByPrimaryKey(cargo);
115 165
 
166
+        // 6. 批量处理完后 插入货权变更记录
167
+        if (cargo != null && useWeight > 0) {
116 168
             CargoChangeRecord cargoChangeRecord = new CargoChangeRecord();
117 169
             cargoChangeRecord.setUperCustomer(cargo.getUperCustomer());
118 170
             cargoChangeRecord.setCustomer(cargo.getCustomer());
119 171
             cargoChangeRecord.setChangeFrom("货权转移");
120
-            cargoChangeRecord.setWeight(use_weight);
172
+            cargoChangeRecord.setWeight(useWeight);
121 173
             cargoChangeRecord.setSubStr("减少");
122
-
123 174
             cargoChangeRecord.setModifyTime(new Date());
124 175
             cargoChangeRecord.setModifyUser(new SysUser(userId));
125
-            num = cargoChangeRecordMapper.insert(cargoChangeRecord);
126
-
176
+//            cargoChangeRecord.setCancelFlag("0");
177
+            num += cargoChangeRecordMapper.insert(cargoChangeRecord);
127 178
         }
128
-        result = Tools.moreThanZeroResultJSON(num);
129
-        return result;
179
+
180
+        // 7. 返回结果
181
+        return Tools.moreThanZeroResultJSON(num);
130 182
     }
131 183
 
132
-    private ChangeRecord getChangeRecord(String recordType, Store store, Customer customer, String receiveAddress, String userId, String belongId) {
133 184
 
185
+    private ChangeRecord getChangeRecord(String recordType, Store store, Customer customer, String receiveAddress, String userId, String belongId) {
134 186
         ChangeRecord record = new ChangeRecord();
135 187
         record.setRecordType(recordType);
136 188
         record.setWareName(store.getWare().getName());
@@ -139,9 +191,9 @@ public class ChangeServiceImpl implements ChangeService {
139 191
         record.setMaterialName(store.getMaterial().getName());
140 192
         record.setStandard(store.getMaterial().getStandard());
141 193
         record.setModel(store.getModel());
142
-        if(customer==null) {
194
+        if (customer == null) {
143 195
             record.setCustomerNameNew("");
144
-        }else {
196
+        } else {
145 197
             record.setCustomerNameNew(customer.getName());
146 198
         }
147 199
         record.setCustomerNameOld(store.getCustomer().getName());
@@ -159,25 +211,18 @@ public class ChangeServiceImpl implements ChangeService {
159 211
         return record;
160 212
     }
161 213
 
162
-    private AccountDetail getAccountDetail(Store store,  String userId, String belongId) throws Exception {
214
+    private AccountDetail getAccountDetail(Store store, String userId, String belongId) throws Exception {
163 215
         int changeTime = 0;
164 216
         int changeTimeForPrice = 0;
165
-        Double plateSuttle = 0.0;
166 217
         Double storagePrice = 0.0;
167 218
         Double weightPrice = 0.0;
168 219
 
169 220
         List<ChangeRecord> list = changeRecordMapper.selectByPlantNoForAccount(store.getPlateNo(), belongId);
170
-        if (list == null || list.size() == 0) {
171
-            changeTime = 0;
172
-            changeTimeForPrice = 0;
173
-        } else {
221
+        if (list != null && !list.isEmpty()) {
174 222
             changeTime = list.size();
175 223
             changeTimeForPrice = 1;
176 224
         }
177
-       /* Price price = priceMapper.selectByMaterialChangeTime(store.getMaterial().getId(), changeTimeForPrice, belongId);
178
-        if (price == null) {
179
-            throw new Exception(store.getMaterial().getName() + "," + store.getMaterial().getStandard() + "没有对应价格信息");
180
-        }*/
225
+
181 226
         AccountDetail accountDetail = new AccountDetail();
182 227
         accountDetail.setChangeTime(changeTime);
183 228
         accountDetail.setRecordType("货权转移");
@@ -198,21 +243,12 @@ public class ChangeServiceImpl implements ChangeService {
198 243
         accountDetail.setCancelFlag("0");
199 244
         accountDetail.setBelongId(belongId);
200 245
 
201
-        //storagePrice = price.getStoragePrice() * store.getWeight();
202
-
203
-        //accountDetail.setStorageUnitPrice(price.getStoragePrice());
204 246
         accountDetail.setStoragePrice(storagePrice);
205
-
206
-        //accountDetail.setWeightUnitPrice(price.getWeightPrice());
207 247
         accountDetail.setWeightPrice(0.0);
208 248
         accountDetail.setSuttle(0.0);
209
-
210 249
         accountDetail.setReduceUnitPrice(0.0);
211 250
         accountDetail.setReducePrice(0.0);
212
-
213
-        accountDetail.setTotalPrice(storagePrice+weightPrice);
214
-
215
-
251
+        accountDetail.setTotalPrice(storagePrice + weightPrice);
216 252
         return accountDetail;
217 253
     }
218 254
 

+ 1
- 1
src/main/java/com/th/demo/service/impl/ware/OutServiceImpl.java Visa fil

@@ -363,7 +363,7 @@ public class OutServiceImpl implements OutService {
363 363
     /**
364 364
      * 库存状态检测方法(定时)
365 365
      */
366
-    @Scheduled(cron = "0 * * * * *")
366
+//    @Scheduled(cron = "0 * * * * *")
367 367
     public void checkStock() {
368 368
         count += 1;
369 369
 //      查询未出库状态,出库单存在的数据

+ 5
- 1
src/main/resource/mapper/ware/StoreMapper.xml Visa fil

@@ -444,7 +444,7 @@ update t_ware_store t set t.layer =   #{param7,jdbcType=INTEGER}
444 444
     and  t.model like concat('%',#{param3,jdbcType=VARCHAR},'%')
445 445
     and  c.name like concat('%',#{param4,jdbcType=VARCHAR},'%')
446 446
     and  c.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
447
-    and  d.name like concat('%',#{param6,jdbcType=VARCHAR},'%')
447
+    and  e.name like concat('%',#{param6,jdbcType=VARCHAR},'%')
448 448
     and  t.remark1 like concat('%',#{param13,jdbcType=VARCHAR},'%')
449 449
     and  (t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
450 450
         <if test="param9 != null and param9.size() !=0" >
@@ -502,6 +502,7 @@ update t_ware_store t set t.layer =   #{param7,jdbcType=INTEGER}
502 502
 
503 503
 
504 504
 
505
+
505 506
   <select id="selectForCustomer" resultMap="BaseResultMap" >
506 507
     select e.name as fk_compony_id,t.*
507 508
     from t_ware_store t left join t_maint_customer e
@@ -599,4 +600,7 @@ update t_ware_store t set t.layer =   #{param7,jdbcType=INTEGER}
599 600
             #{item,jdbcType=VARCHAR}
600 601
         </foreach>
601 602
     </update>
603
+    <select id="queryStoreById" resultType="java.lang.String" parameterType="java.lang.String">
604
+        select plate_no from t_ware_store where stack_id = #{stackId,jdbcType=VARCHAR} and out_flag = '0' and cancel_flag = '0'
605
+    </select>
602 606
 </mapper>

Loading…
Avbryt
Spara