Просмотр исходного кода

1.优化货权转移逻辑

2.删除前新增判断该垛位下是否存在钢板
dw 2 недель назад
Родитель
Сommit
fdc07c8d14

+ 4
- 0
src/main/java/com/th/demo/mapping/ware/StoreMapper.java Просмотреть файл

72
      * 修改入库表收货地址
72
      * 修改入库表收货地址
73
      */
73
      */
74
     int  updateInAddressById(@Param( "inId") List<String> inId,@Param( "address") String  address);
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 Просмотреть файл

7
 import com.github.pagehelper.PageInfo;
7
 import com.github.pagehelper.PageInfo;
8
 import com.th.demo.mapping.maint.StackMapper;
8
 import com.th.demo.mapping.maint.StackMapper;
9
 import com.th.demo.mapping.maint.WareMapper;
9
 import com.th.demo.mapping.maint.WareMapper;
10
+import com.th.demo.mapping.ware.StoreMapper;
10
 import com.th.demo.model.maint.Stack;
11
 import com.th.demo.model.maint.Stack;
11
 import com.th.demo.model.maint.Ware;
12
 import com.th.demo.model.maint.Ware;
12
 import com.th.demo.model.system.Type;
13
 import com.th.demo.model.system.Type;
14
+import com.th.demo.model.ware.Store;
13
 import com.th.demo.service.maint.StackService;
15
 import com.th.demo.service.maint.StackService;
14
 import com.th.demo.tools.JSONTools;
16
 import com.th.demo.tools.JSONTools;
15
 import com.th.demo.tools.Tools;
17
 import com.th.demo.tools.Tools;
18
+import org.apache.commons.lang3.StringUtils;
16
 import org.springframework.beans.factory.annotation.Autowired;
19
 import org.springframework.beans.factory.annotation.Autowired;
17
 import org.springframework.stereotype.Service;
20
 import org.springframework.stereotype.Service;
21
+import org.springframework.util.ObjectUtils;
18
 
22
 
19
 import java.util.ArrayList;
23
 import java.util.ArrayList;
20
 import java.util.Date;
24
 import java.util.Date;
27
     public StackMapper stackMapper;
31
     public StackMapper stackMapper;
28
     @Autowired
32
     @Autowired
29
     public WareMapper wareMapper;
33
     public WareMapper wareMapper;
34
+    @Autowired
35
+    public StoreMapper storeMapper;
30
 
36
 
31
 
37
 
32
 
38
 
76
 
82
 
77
     @Override
83
     @Override
78
     public String remove(String id, String userId) {
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
         Stack stack = stackMapper.selectByPrimaryKey(id);
96
         Stack stack = stackMapper.selectByPrimaryKey(id);
80
         stack.setCancelFlag("9");
97
         stack.setCancelFlag("9");
81
         stack.setCancelId(userId);
98
         stack.setCancelId(userId);

+ 108
- 72
src/main/java/com/th/demo/service/impl/ware/ChangeServiceImpl.java Просмотреть файл

28
 import org.springframework.beans.factory.annotation.Autowired;
28
 import org.springframework.beans.factory.annotation.Autowired;
29
 import org.springframework.stereotype.Service;
29
 import org.springframework.stereotype.Service;
30
 import org.springframework.transaction.annotation.Transactional;
30
 import org.springframework.transaction.annotation.Transactional;
31
+import org.springframework.util.StringUtils;
31
 
32
 
32
 import java.util.Collections;
33
 import java.util.Collections;
33
 import java.util.Date;
34
 import java.util.Date;
53
     int num = 0;
54
     int num = 0;
54
     String result = Type.FAIL;
55
     String result = Type.FAIL;
55
 
56
 
56
-
57
+    /**
58
+     * 货权转移
59
+     * 解决:货权转移后Cargo重量不减少、中途return不扣重量、空指针、事务部分提交BUG
60
+     */
57
     @Transactional(rollbackFor = Exception.class)
61
     @Transactional(rollbackFor = Exception.class)
58
     @Override
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
         Customer c1 = JSON.parseObject(customer, new TypeReference<Customer>() {});
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
                 recordType = "0";
125
                 recordType = "0";
90
-            }else {
126
+            } else {
91
                 recordType = "1";
127
                 recordType = "1";
92
-                AccountDetail accountDetail = getAccountDetail(list.get(i),  userId, belongId);
128
+                // 插入账户明细
129
+                AccountDetail accountDetail = getAccountDetail(store, userId, belongId);
93
                 num += accountDetailMapper.insert(accountDetail);
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
             num += changeRecordMapper.insert(changeRecord);
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
                 } else {
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
             CargoChangeRecord cargoChangeRecord = new CargoChangeRecord();
168
             CargoChangeRecord cargoChangeRecord = new CargoChangeRecord();
117
             cargoChangeRecord.setUperCustomer(cargo.getUperCustomer());
169
             cargoChangeRecord.setUperCustomer(cargo.getUperCustomer());
118
             cargoChangeRecord.setCustomer(cargo.getCustomer());
170
             cargoChangeRecord.setCustomer(cargo.getCustomer());
119
             cargoChangeRecord.setChangeFrom("货权转移");
171
             cargoChangeRecord.setChangeFrom("货权转移");
120
-            cargoChangeRecord.setWeight(use_weight);
172
+            cargoChangeRecord.setWeight(useWeight);
121
             cargoChangeRecord.setSubStr("减少");
173
             cargoChangeRecord.setSubStr("减少");
122
-
123
             cargoChangeRecord.setModifyTime(new Date());
174
             cargoChangeRecord.setModifyTime(new Date());
124
             cargoChangeRecord.setModifyUser(new SysUser(userId));
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
         ChangeRecord record = new ChangeRecord();
186
         ChangeRecord record = new ChangeRecord();
135
         record.setRecordType(recordType);
187
         record.setRecordType(recordType);
136
         record.setWareName(store.getWare().getName());
188
         record.setWareName(store.getWare().getName());
139
         record.setMaterialName(store.getMaterial().getName());
191
         record.setMaterialName(store.getMaterial().getName());
140
         record.setStandard(store.getMaterial().getStandard());
192
         record.setStandard(store.getMaterial().getStandard());
141
         record.setModel(store.getModel());
193
         record.setModel(store.getModel());
142
-        if(customer==null) {
194
+        if (customer == null) {
143
             record.setCustomerNameNew("");
195
             record.setCustomerNameNew("");
144
-        }else {
196
+        } else {
145
             record.setCustomerNameNew(customer.getName());
197
             record.setCustomerNameNew(customer.getName());
146
         }
198
         }
147
         record.setCustomerNameOld(store.getCustomer().getName());
199
         record.setCustomerNameOld(store.getCustomer().getName());
159
         return record;
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
         int changeTime = 0;
215
         int changeTime = 0;
164
         int changeTimeForPrice = 0;
216
         int changeTimeForPrice = 0;
165
-        Double plateSuttle = 0.0;
166
         Double storagePrice = 0.0;
217
         Double storagePrice = 0.0;
167
         Double weightPrice = 0.0;
218
         Double weightPrice = 0.0;
168
 
219
 
169
         List<ChangeRecord> list = changeRecordMapper.selectByPlantNoForAccount(store.getPlateNo(), belongId);
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
             changeTime = list.size();
222
             changeTime = list.size();
175
             changeTimeForPrice = 1;
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
         AccountDetail accountDetail = new AccountDetail();
226
         AccountDetail accountDetail = new AccountDetail();
182
         accountDetail.setChangeTime(changeTime);
227
         accountDetail.setChangeTime(changeTime);
183
         accountDetail.setRecordType("货权转移");
228
         accountDetail.setRecordType("货权转移");
198
         accountDetail.setCancelFlag("0");
243
         accountDetail.setCancelFlag("0");
199
         accountDetail.setBelongId(belongId);
244
         accountDetail.setBelongId(belongId);
200
 
245
 
201
-        //storagePrice = price.getStoragePrice() * store.getWeight();
202
-
203
-        //accountDetail.setStorageUnitPrice(price.getStoragePrice());
204
         accountDetail.setStoragePrice(storagePrice);
246
         accountDetail.setStoragePrice(storagePrice);
205
-
206
-        //accountDetail.setWeightUnitPrice(price.getWeightPrice());
207
         accountDetail.setWeightPrice(0.0);
247
         accountDetail.setWeightPrice(0.0);
208
         accountDetail.setSuttle(0.0);
248
         accountDetail.setSuttle(0.0);
209
-
210
         accountDetail.setReduceUnitPrice(0.0);
249
         accountDetail.setReduceUnitPrice(0.0);
211
         accountDetail.setReducePrice(0.0);
250
         accountDetail.setReducePrice(0.0);
212
-
213
-        accountDetail.setTotalPrice(storagePrice+weightPrice);
214
-
215
-
251
+        accountDetail.setTotalPrice(storagePrice + weightPrice);
216
         return accountDetail;
252
         return accountDetail;
217
     }
253
     }
218
 
254
 

+ 1
- 1
src/main/java/com/th/demo/service/impl/ware/OutServiceImpl.java Просмотреть файл

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

+ 5
- 1
src/main/resource/mapper/ware/StoreMapper.xml Просмотреть файл

444
     and  t.model like concat('%',#{param3,jdbcType=VARCHAR},'%')
444
     and  t.model like concat('%',#{param3,jdbcType=VARCHAR},'%')
445
     and  c.name like concat('%',#{param4,jdbcType=VARCHAR},'%')
445
     and  c.name like concat('%',#{param4,jdbcType=VARCHAR},'%')
446
     and  c.standard like concat('%',#{param5,jdbcType=VARCHAR},'%')
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
     and  t.remark1 like concat('%',#{param13,jdbcType=VARCHAR},'%')
448
     and  t.remark1 like concat('%',#{param13,jdbcType=VARCHAR},'%')
449
     and  (t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
449
     and  (t.plate_no like concat('%',#{param7,jdbcType=VARCHAR},'%')
450
         <if test="param9 != null and param9.size() !=0" >
450
         <if test="param9 != null and param9.size() !=0" >
502
 
502
 
503
 
503
 
504
 
504
 
505
+
505
   <select id="selectForCustomer" resultMap="BaseResultMap" >
506
   <select id="selectForCustomer" resultMap="BaseResultMap" >
506
     select e.name as fk_compony_id,t.*
507
     select e.name as fk_compony_id,t.*
507
     from t_ware_store t left join t_maint_customer e
508
     from t_ware_store t left join t_maint_customer e
599
             #{item,jdbcType=VARCHAR}
600
             #{item,jdbcType=VARCHAR}
600
         </foreach>
601
         </foreach>
601
     </update>
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
 </mapper>
606
 </mapper>

Загрузка…
Отмена
Сохранить