EquipmentOutController.java
21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package com.skua.modules.equipment.controller;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skua.aop.annotation.CustomExceptionAnno;
import com.skua.core.api.vo.LoginUser;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.query.QueryGenerator;
import com.skua.core.util.DateUtils;
import com.skua.modules.common.service.ICommonSqlService;
import com.skua.modules.equipment.dto.EquipmentOutDTO;
import com.skua.modules.equipment.entity.EquipmentOut;
import com.skua.modules.equipment.entity.EquipmentOutChild;
import com.skua.modules.equipment.entity.EquipmentSparepart;
import com.skua.modules.equipment.service.IEquipmentOutChildService;
import com.skua.modules.equipment.service.IEquipmentOutService;
import com.skua.modules.equipment.service.IEquipmentSparepartService;
import com.skua.modules.equipment.vo.EquipmentOutExcel;
import com.skua.modules.equipment.vo.EquipmentOutVO;
import com.skua.modules.flow.utils.ObjectUtil;
import com.skua.modules.supplies.entity.EquipmentSparepartSupplies;
import com.skua.modules.supplies.service.IEquipmentSparepartSuppliesService;
import com.skua.modules.system.service.ISysDictService;
import com.skua.redis.component.Key2ValueService;
import com.skua.redis.util.CustomRedisUtil;
import com.skua.tool.util.BeanExtUtils;
import com.skua.tool.util.UniqIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
* 出库
*/
@Slf4j
@Api(tags = "设备管理/库存管理/出库管理")
@RestController
@RequestMapping("/web/equipment/equipmentOut")
public class EquipmentOutController {
@Autowired
private ICommonSqlService iCommonSqlService;
@Autowired
private IEquipmentOutService equipmentOutService;
@Autowired
private IEquipmentOutChildService iEquipmentOutChildService;
@Autowired
private IEquipmentSparepartService iEquipmentSparepartService;
@Autowired
private TransactionTemplate transactionTemplate;
@Autowired
private CustomRedisUtil customRedisUtil;
@Autowired
private Key2ValueService key2ValueService;
@Autowired
private IEquipmentSparepartSuppliesService sparepartSuppliesService;
@Autowired
private ISysDictService sysCommonDictService;
@Autowired
@Qualifier("taskExecutor")
private ThreadPoolTaskExecutor taskExecutor;
@CustomExceptionAnno(description = "出库-详情")
@AutoLog(value = "出库-详情")
@ApiOperation(value = "出库-详情", notes = "出库-详情")
@GetMapping(value = "/detail")
public Result<Object> detailCtrl(EquipmentOutDTO equipmentOutDTO) {
Result<Object> result = new Result<>();
String outId = equipmentOutDTO.getId();
EquipmentOut equipmentOut = equipmentOutService.getById(outId);
String sql = "SELECT\n" +
"\teic.id, ei.id mainId, \n" +
"\tes.sparepart_code AS sparepartCode,\n" +
"\tes.sparepart_name AS sparepartName,\n" +
"\ttp.item_text AS sparepartType_dictText,\n" +
"\tes.sparepart_type AS sparepartType,\n" +
"\tes.specification,\n" +
"\tes.measuring_unit AS measuringUnit,\n" +
"(select item_text from sys_dict_item where dict_id=(select id from sys_dict where dict_code='equipment-sparepart_measuring_unit') and item_value=es.measuring_unit) as measuringUnit_dictText," +
"\teic.out_num AS outNum,\n" +
"\teic.batch_num AS batchNum\n" +
"FROM\n" +
"\tequipment_out AS ei inner join \n" +
"\tequipment_out_child AS eic on ei.id = eic.out_id \n" +
"\tleft join equipment_sparepart_supplies AS es on eic.sparepart_id = es.id\n" +
"\tleft join equipment_sparepart_type tp on es.sparepart_type = tp.id" +
" WHERE ei.id in( '" + outId + "')";
List<Map<String, Object>> mapList = iCommonSqlService.queryForList(sql);
JSONObject jsonObject = sysCommonDictService.translateTDictValue(equipmentOut);
jsonObject.put("childList", mapList);
result.setResult(jsonObject);
return result;
}
/**
* 分页列表查询
*
* @param equipmentOutDTO
* @param pageNo
* @param pageSize
* @param req
* @return
*/
@AutoLog(value = "出库-分页列表查询")
@ApiOperation(value = "出库-分页列表查询", notes = "出库-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<EquipmentOutVO>> queryPageList(EquipmentOutDTO equipmentOutDTO,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest req) throws Exception {
Result<IPage<EquipmentOutVO>> result = new Result<>();
EquipmentOut equipmentOut = BeanExtUtils.bean2Bean(equipmentOutDTO, EquipmentOut.class);
QueryWrapper<EquipmentOut> queryWrapper = QueryGenerator.initQueryWrapper(equipmentOut, req.getParameterMap());
if (StringUtils.isNotEmpty(equipmentOutDTO.getStartTime())) {
queryWrapper.ge("out_date", equipmentOutDTO.getStartTime() + " 00:00:00");
}
if (StringUtils.isNotEmpty(equipmentOutDTO.getEndTime())) {
queryWrapper.le("out_date", equipmentOutDTO.getEndTime() + " 23:59:59");
}
// 按照库存更新时间倒序
queryWrapper.orderByDesc("out_date");
Map<String, String> sysUserVal2KeyMap = key2ValueService.dictKey2Val("sysUser", false);
Map<String, String> departCodeVal2KeyMap = key2ValueService.dictKey2Val("departCode", false);
Map<String, String> suppliesWarehouseVal2KeyMap = key2ValueService.dictKey2Val("suppliesWarehouse", false);
Page<EquipmentOut> page = new Page<>(pageNo, pageSize);
IPage<EquipmentOut> pageList = equipmentOutService.page(page, queryWrapper);
Page<EquipmentOutVO> voPage = new Page<>(pageNo, pageSize);
voPage.setTotal(pageList.getTotal());
List<EquipmentOutVO> equipmentOutVOList = BeanExtUtils.beans2Beans(pageList.getRecords(), EquipmentOutVO.class, (targetFieldName, srcFieldVal) -> {
if ("departIdName".equals(targetFieldName) && srcFieldVal != null) {
return customRedisUtil.hget("sys_depart", srcFieldVal.toString(), "id", "depart_name");
} else if ("sparepartName".equals(targetFieldName)) {
String sql2 = "select es.sparepart_name as sparepartName from equipment_sparepart_supplies as es inner join (select sparepart_id from equipment_out_child where out_id = '" + srcFieldVal + "') as tmp on es.id = tmp.sparepart_id";
List<Map<String, Object>> sparePartList = iCommonSqlService.queryForList(sql2);
Map<String, Integer> name2CountMap = new HashMap<>();
if (sparePartList != null && !sparePartList.isEmpty() && sparePartList.get(0) != null) {
for (Map<String, Object> it : sparePartList) {
String name = "" + it.get("sparepartName");
name2CountMap.put(name, name2CountMap.getOrDefault(name, 0) + 1);
}
}
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, Integer> item : name2CountMap.entrySet()) {
stringBuilder.append(",").append(item.getKey());
}
return stringBuilder.toString().replaceFirst(",", "");
} else if ("useByName".equals(targetFieldName)) {
return sysUserVal2KeyMap.getOrDefault(srcFieldVal, "");
} else if ("departCode_dictText".equals(targetFieldName)) {
return departCodeVal2KeyMap.getOrDefault(srcFieldVal, "");
} else if ("suppliesWarehouseId_dictText".equals(targetFieldName)) {
return suppliesWarehouseVal2KeyMap.getOrDefault(srcFieldVal, "");
}
return "";
});
if (StringUtils.isNotEmpty(equipmentOutDTO.getSparepartName())) {
equipmentOutVOList = equipmentOutVOList.stream().filter(item -> item.getSparepartName().contains(equipmentOutDTO.getSparepartName())).collect(Collectors.toList());
}
voPage.setRecords(equipmentOutVOList);
result.setResult(voPage);
result.setSuccess(true);
return result;
}
/**
* 添加
*
* @param equipmentOutDTO
* @return
*/
@AutoLog(value = "出库-添加")
@ApiOperation(value = "出库-添加", notes = "出库-添加")
@PostMapping(value = "/add")
public Result<Object> add(@RequestBody EquipmentOutDTO equipmentOutDTO) throws Exception {
Result<Object> result = new Result<>();
if (equipmentOutDTO.getEquipmentOutChildList() == null || equipmentOutDTO.getEquipmentOutChildList().isEmpty()) {
result.error500("请选择出库备件");
}
// 出库单号
if (StringUtils.isEmpty(equipmentOutDTO.getOutOrder())) {
String outOrder = "CK-" + DateUtils.format(new Date(), "YYYYMMdd" + "-" + System.currentTimeMillis());
equipmentOutDTO.setOutOrder(outOrder);
}
// 出库表主键
String outId = UUID.randomUUID().toString().replaceAll("-", "");
equipmentOutDTO.setId(outId);
EquipmentOut equipmentOut = BeanExtUtils.bean2Bean(equipmentOutDTO, EquipmentOut.class);
equipmentOut.setInventoryUpdateTime(new Date());
equipmentOutService.save(equipmentOut);
CountDownLatch countDownLatch = new CountDownLatch(equipmentOutDTO.getEquipmentOutChildList().size());
for (EquipmentOutChild equipmentOutChild : equipmentOutDTO.getEquipmentOutChildList()) {
equipmentOutChild.setId(UniqIdUtils.getInstance().getUniqID());
equipmentOutChild.setOutId(outId);
taskExecutor.execute(() -> {
try {
String sparepartId = equipmentOutChild.getSparepartId();
String suppliesWarehouseId = equipmentOutDTO.getSuppliesWarehouseId();
//根据物资id和仓库id获取库存
QueryWrapper<EquipmentSparepart> sparepartQueryWrapper = new QueryWrapper<>();
sparepartQueryWrapper.eq("supplies_id", sparepartId).eq("supplies_warehouse_id", suppliesWarehouseId);
EquipmentSparepart equipmentSparepart = iEquipmentSparepartService.getOne(sparepartQueryWrapper);
if (ObjectUtil.isEmpty(equipmentSparepart)) {
//物料信息
EquipmentSparepartSupplies supplies = sparepartSuppliesService.getById(sparepartId);
equipmentSparepart = new EquipmentSparepart();
BeanUtils.copyProperties(supplies, equipmentSparepart);
equipmentSparepart.setId(null);
equipmentSparepart.setStorageNum(BigDecimal.ZERO);
equipmentSparepart.setSuppliesId(sparepartId);
equipmentSparepart.setSuppliesWarehouseId(suppliesWarehouseId);
//如果没有物料库存信息,新增库存信息为0
iEquipmentSparepartService.saveOrUpdate(equipmentSparepart);
equipmentSparepart = iEquipmentSparepartService.getOne(sparepartQueryWrapper);
}
BigDecimal resetStorageNum = equipmentSparepart.getStorageNum().subtract(equipmentOutChild.getOutNum());
if (resetStorageNum.doubleValue() < 0) {
return;
}
Integer version = equipmentSparepart.getVersion();
String id = equipmentSparepart.getId();
Integer executeRes = transactionTemplate.execute(transactionStatus -> {
int val0 = iEquipmentSparepartService.updateStorageNumById(id, resetStorageNum.doubleValue(), version);
if (val0 == 1 && iEquipmentOutChildService.saveOrUpdate(equipmentOutChild)) {
return 1;
} else {
transactionStatus.setRollbackOnly();
return 0;
}
});
// if (executeRes != null && executeRes == 1) {
//
// }
} catch (Exception e) {
e.printStackTrace();
log.error("异常error: {}", e.getMessage());
} finally {
countDownLatch.countDown();
}
});
}
countDownLatch.await();
result.setResult(equipmentOutDTO);
return result;
}
/**
* 编辑
*
* @param equipmentOut
* @return
*/
@AutoLog(value = "出库-编辑")
@ApiOperation(value = "出库-编辑", notes = "出库-编辑")
@PutMapping(value = "/edit")
public Result<EquipmentOut> edit(@RequestBody EquipmentOut equipmentOut) {
Result<EquipmentOut> result = new Result<EquipmentOut>();
EquipmentOut equipmentOutEntity = equipmentOutService.getById(equipmentOut.getId());
if (equipmentOutEntity == null) {
result.error500("未找到对应实体");
} else {
boolean ok = equipmentOutService.updateById(equipmentOut);
//TODO 返回false说明什么?
if (ok) {
result.success("修改成功!");
}
}
return result;
}
/**
* 通过id删除
*
* @param id
* @return
*/
@AutoLog(value = "出库-通过id删除")
@ApiOperation(value = "出库-通过id删除", notes = "出库-通过id删除")
@DeleteMapping(value = "/delete")
public Result<?> delete(@RequestParam(name = "id") String id) {
try {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
boolean val0 = equipmentOutService.removeById(id);
String sql = "delete from equipment_out_child where out_id = '" + id + "'";
int val1 = iCommonSqlService.delete(sql);
if (!val0 || val1 == 0) {
transactionStatus.setRollbackOnly();
}
}
});
} catch (Exception e) {
log.error("删除失败: {}", e.getMessage());
return Result.error("删除失败!");
}
return Result.ok("删除成功!");
}
/**
* 批量删除
*
* @param ids
* @return
*/
@AutoLog(value = "出库-批量删除")
@ApiOperation(value = "出库-批量删除", notes = "出库-批量删除")
@DeleteMapping(value = "/deleteBatch")
public Result<EquipmentOut> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
Result<EquipmentOut> result = new Result<EquipmentOut>();
if (ids == null || "".equals(ids.trim())) {
result.error500("参数不识别!");
} else {
this.equipmentOutService.removeByIds(Arrays.asList(ids.split(",")));
result.success("删除成功!");
}
return result;
}
/**
* 通过id查询
*
* @param id
* @return
*/
@AutoLog(value = "出库-通过id查询")
@ApiOperation(value = "出库-通过id查询", notes = "出库-通过id查询")
@GetMapping(value = "/queryById")
public Result<EquipmentOut> queryById(@RequestParam(name = "id", required = true) String id) {
Result<EquipmentOut> result = new Result<EquipmentOut>();
EquipmentOut equipmentOut = equipmentOutService.getById(id);
if (equipmentOut == null) {
result.error500("未找到对应实体");
} else {
result.setResult(equipmentOut);
result.setSuccess(true);
}
return result;
}
/**
* 导出excel
*
* @param request
* @param
*/
@RequestMapping(value = "/exportXls")
public ModelAndView exportXls(EquipmentOutDTO equipmentOutDTO, HttpServletRequest request, HttpServletResponse response) throws Exception {
EquipmentOut equipmentOut = BeanExtUtils.bean2Bean(equipmentOutDTO, EquipmentOut.class);
QueryWrapper<EquipmentOut> queryWrapper = QueryGenerator.initQueryWrapper(equipmentOut, request.getParameterMap());
List<EquipmentOutExcel> dataList;
//获取当前用户
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
try {
if (StringUtils.isNotEmpty(equipmentOutDTO.getStartTime())) {
queryWrapper.ge("out_date", equipmentOutDTO.getStartTime() + " 00:00:00");
}
if (StringUtils.isNotEmpty(equipmentOutDTO.getEndTime())) {
queryWrapper.le("out_date", equipmentOutDTO.getEndTime() + " 23:59:59");
}
queryWrapper.groupBy("ei.id");
queryWrapper.orderByDesc("out_date");
dataList = equipmentOutService.queryExport(queryWrapper);
} catch (Exception e) {
e.printStackTrace();
dataList = new ArrayList<>();
}
//Step.2 AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//导出文件名称
mv.addObject(NormalExcelConstants.FILE_NAME, "入库管理列表");
mv.addObject(NormalExcelConstants.CLASS, EquipmentOutExcel.class);
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("入库管理数据", "导出人:"+sysUser.getRealname(), "导出信息"));
mv.addObject(NormalExcelConstants.DATA_LIST, dataList);
return mv;
}
/**
* 通过excel导入数据
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
ImportParams params = new ImportParams();
params.setTitleRows(2);
params.setHeadRows(1);
params.setNeedSave(true);
try {
List<EquipmentOut> listEquipmentOuts = ExcelImportUtil.importExcel(file.getInputStream(), EquipmentOut.class, params);
equipmentOutService.saveBatch(listEquipmentOuts);
return Result.ok("文件导入成功!数据行数:" + listEquipmentOuts.size());
} catch (Exception e) {
log.error(e.getMessage(), e);
return Result.error("文件导入失败:" + e.getMessage());
} finally {
try {
file.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Result.ok("文件导入失败!");
}
}