|
|
@@ -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
|
|