RepairConsumablesController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package com.repair.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.repair.api.RepairConsumablesControllerAPI;
  4. import com.repair.common.utils.*;
  5. import com.repair.model.enumModel.eIsSuper;
  6. import com.repair.model.enumModel.eRecordStatu;
  7. import com.repair.model.enumModel.eSchool;
  8. import com.repair.model.pojo.*;
  9. import com.repair.model.request.MaintenanceConsumeablesRequest;
  10. import com.repair.model.request.insertConsumablesRequest;
  11. import com.repair.model.result.CommonResult;
  12. import com.repair.model.result.PageUtils;
  13. import com.repair.model.vo.ChangeConsumesVo;
  14. import com.repair.model.vo.ChangePriceConsumeVo;
  15. import com.repair.model.vo.ConsumableExcelVo;
  16. import com.repair.model.vo.ConsumablePageVo;
  17. import com.repair.services.*;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import org.springframework.validation.BindingResult;
  22. import org.springframework.web.bind.annotation.RequestHeader;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.math.BigDecimal;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.stream.Collectors;
  32. /**
  33. * <p>
  34. * 前端控制器
  35. * </p>
  36. *
  37. * @author ceshi
  38. * @since 2023-07-20
  39. */
  40. @RestController
  41. public class RepairConsumablesController implements RepairConsumablesControllerAPI {
  42. @Autowired
  43. private RepairUserService repairUserService;
  44. @Autowired
  45. private RepairAdminService repairAdminService;
  46. @Autowired
  47. private RepairRecordService repairRecordService;
  48. @Autowired
  49. private RepairConsumablesService repairConsumablesService;
  50. @Autowired
  51. private RepairTrackRecordService repairTrackRecordService;
  52. @Autowired
  53. private RepairSystemMessagesService repairSystemMessagesService;
  54. /**
  55. * 新增耗材记录
  56. *
  57. * @param rc 耗材记录
  58. * @param bindingResult 是否为空判断
  59. * @return
  60. */
  61. @Override
  62. public CommonResult InsertRepairConsumables(RepairConsumables rc, BindingResult bindingResult) {
  63. if (bindingResult.hasErrors()) {
  64. String st = paramUtils.getParamError(bindingResult);
  65. return CommonResult.fail(st);
  66. }
  67. int result = repairConsumablesService.insertRepairConsumables(rc);
  68. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  69. }
  70. /**
  71. * 根据数据ID更新耗材记录
  72. *
  73. * @param rc 耗材记录
  74. * @param bindingResult 是否为空判断
  75. * @return
  76. */
  77. @Override
  78. public CommonResult updateRepairConsumablesById(RepairConsumables rc, BindingResult bindingResult) {
  79. if (bindingResult.hasErrors()) {
  80. String st = paramUtils.getParamError(bindingResult);
  81. return CommonResult.fail(st);
  82. }
  83. int result = repairConsumablesService.updateRepairConsumables(rc);
  84. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  85. }
  86. /**
  87. * 耗材记录分页数据查询
  88. *
  89. * @param currentPage 当前页
  90. * @param pageCount 一页数据条数
  91. * @param name 查询名称
  92. * @return
  93. */
  94. @Override
  95. public CommonResult queryPageRepairConsumabless(int currentPage, int pageCount, String name) {
  96. PageUtils<RepairConsumables> result = repairConsumablesService.queryPageRepairConsumabless(currentPage, pageCount, name);
  97. return CommonResult.ok(result);
  98. }
  99. /**
  100. * 根据数据ID删除耗材记录
  101. *
  102. * @param id 数据ID
  103. * @return
  104. */
  105. @Override
  106. public CommonResult deleteRepairConsumablesById(int id) {
  107. RepairConsumables data = repairConsumablesService.getRepairById(id);
  108. if (data == null) {
  109. return CommonResult.fail("当前数据不存在,删除失败!");
  110. }
  111. int result = repairConsumablesService.deleteRepairConsumablesById(id);
  112. return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  113. }
  114. /**
  115. * 耗材记录分页数据查询
  116. *
  117. * @param currentPage 当前页
  118. * @param pageCount 一页数据条数
  119. * @param keyWord 关键字查询
  120. * @param startTime 起始时间
  121. * @param endTime 结束时间
  122. * @return
  123. */
  124. @Override
  125. public CommonResult queryRepairConsumablePageList(int currentPage, int pageCount, String keyWord, String startTime, String endTime) {
  126. if (startTime != null && endTime != null) {
  127. try {
  128. startTime = TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
  129. endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
  130. } catch (Exception e) {
  131. }
  132. }
  133. PageUtils<ConsumablePageVo> result = repairConsumablesService.queryConsumablePageList(currentPage, pageCount, keyWord, startTime, endTime);
  134. for (ConsumablePageVo data : result.getList()) {
  135. //校区名称
  136. data.setSchoolName(eSchool.stringOf(data.getSchoolId()));
  137. //当返回值小于0时,表示BigDecimal对象小于指定的数值;
  138. //当返回值等于0时,表示BigDecimal对象等于指定的数值;
  139. //当返回值大于0时,表示BigDecimal对象大于指定的数值。
  140. data.setStatu(data.getTotalPrice().compareTo(BigDecimal.ZERO) > 0 ? "有偿" : "无偿");
  141. }
  142. return CommonResult.ok(result);
  143. }
  144. /**
  145. * 耗材记录导出
  146. *
  147. * @param keyWord 关键字查询
  148. * @param startTime 起始时间
  149. * @param endTime 结束时间
  150. * @param response 文件响应流
  151. * @throws NoSuchMethodException
  152. * @throws InstantiationException
  153. * @throws IllegalAccessException
  154. * @throws InvocationTargetException
  155. */
  156. @Override
  157. public void downloadRepairConsumableExcel(String keyWord, String startTime, String endTime, HttpServletResponse response) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
  158. Map<String, String> map = new HashMap<>();
  159. //表头与键值对的映射关系
  160. map.put("id", "数据ID");
  161. map.put("consumeName", "耗材名称");
  162. map.put("price", "单价(元)");
  163. map.put("number", "数量");
  164. map.put("totalPrice", "金额(元)");
  165. map.put("articleName", "工单");
  166. map.put("reportTimeStr", "报修时间");
  167. map.put("updateTimeStr", "维修时间");
  168. if (startTime != null && endTime != null) {
  169. try {
  170. startTime = TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
  171. endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
  172. } catch (Exception e) {
  173. }
  174. }
  175. List<ConsumableExcelVo> datas = repairConsumablesService.queryConsumablePageList(keyWord, startTime, endTime);
  176. List<String> orderList = new ArrayList<>();
  177. orderList.add("id");
  178. orderList.add("consumeName");
  179. orderList.add("price");
  180. orderList.add("number");
  181. orderList.add("totalPrice");
  182. orderList.add("articleName");
  183. orderList.add("reportTimeStr");
  184. orderList.add("updateTimeStr");
  185. //导出逻辑 这里的list是从导入里面哪来的 map1与map不一样 orderList这里为空
  186. Workbook workbook = ExcelExportUtil.createWorkbook(datas, map, 1, "耗材记录表", orderList);
  187. ExcelUtils.excelDownload(workbook, "耗材记录.xlsx", response);
  188. }
  189. /**
  190. * 报价-确认提交
  191. *
  192. * @param data 报价耗材数据
  193. * @param userhead userhead请求头数据
  194. * @param bindingResult 是否为空判断
  195. * @return
  196. * @throws Exception
  197. */
  198. @Override
  199. @Transactional(rollbackFor = {Exception.class})
  200. public CommonResult InsertMaintenanceConsumables(MaintenanceConsumeablesRequest data, @RequestHeader("user_head") String userhead, BindingResult bindingResult) throws Exception {
  201. System.out.println("报价确认提交" + JSON.toJSON(data));
  202. try {
  203. if (bindingResult.hasErrors()) {
  204. String st = paramUtils.getParamError(bindingResult);
  205. return CommonResult.fail(st);
  206. }
  207. if (data.getConsumes().size() <= 0) {
  208. return CommonResult.fail("无耗材数据,报价失败");
  209. }
  210. List<RepairConsumables> rcDatas = new ArrayList<>();
  211. for (insertConsumablesRequest rc : data.getConsumes()) {
  212. if (rc.getConsumeId() == null) {
  213. return CommonResult.fail("耗材ID不能为空");
  214. }
  215. if (rc.getConsumeName() == null) {
  216. return CommonResult.fail("耗材名称不能为空");
  217. }
  218. if (rc.getNumber() == null || rc.getNumber() <= 0) {
  219. return CommonResult.fail("耗材数量不能为空");
  220. }
  221. if (rc.getPrice() == null) {
  222. return CommonResult.fail("耗材单价不能为空");
  223. }
  224. RepairConsumables rcData = new RepairConsumables();
  225. rcData.setRecordId(data.getRecordId());
  226. rcData.setArticleId(rc.getArticleId());
  227. rcData.setConsumeId(rc.getConsumeId());
  228. rcData.setConsumeName(rc.getConsumeName());
  229. rcData.setNumber(rc.getNumber());
  230. rcData.setPrice(rc.getPrice());
  231. rcData.setTotalPrice(new BigDecimal(rcData.getNumber()).multiply(rcData.getPrice()));
  232. rcDatas.add(rcData);
  233. }
  234. RepairRecord rr = repairRecordService.getRepairById(data.getRecordId());
  235. if (rr == null) {
  236. return CommonResult.fail("报修工单不存在,报价失败");
  237. }
  238. //判断是否已报价
  239. if (rr.getPrice() != null) {
  240. return CommonResult.fail("报修工单已报过价,请勿重复报价");
  241. }
  242. if (rr.getMaintenanceState() == eRecordStatu.Finished.getValue()
  243. || rr.getMaintenanceState() == eRecordStatu.Canceled.getValue()
  244. || rr.getMaintenanceState() == eRecordStatu.Closed.getValue()) {
  245. return CommonResult.fail("当前报修工单状态发生变化,报价失败");
  246. }
  247. boolean result = repairConsumablesService.insertConsumableBatch(rcDatas);
  248. if (!result) {
  249. throw new Exception("报价失败!");
  250. }
  251. //只有用户支付完了之后,维修师傅才能继续看到这个单子
  252. rr.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
  253. rr.setPrice(data.getTotalPrice());
  254. int updateRecord = repairRecordService.updateRepairRecord(rr);
  255. if (updateRecord <= 0) {
  256. throw new Exception("报价失败!");
  257. }
  258. RepairSystemMessages rsm = new RepairSystemMessages();
  259. rsm.setRecordId(data.getRecordId());
  260. rsm.setRecipientId(rr.getUserId());
  261. rsm.setIsRead(0);
  262. rsm.setContent("工单需要支付" + data.getTotalPrice() + "元,请尽快确认!");
  263. int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
  264. if (insertRsm <= 0) {
  265. throw new Exception("报价失败!");
  266. }
  267. String userID = AesUtils.decrypt(userhead);
  268. RepairAdmin operateData = repairAdminService.getRepairById(userID);
  269. if (operateData == null) {
  270. throw new Exception("操作人信息不合法,无法进行回复!");
  271. }
  272. //发送人
  273. Integer senderId = 0;//超级管理员用ID为0
  274. if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
  275. RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
  276. if (user == null) {
  277. throw new Exception("管理端账号未与移动端账号绑定,无法进行审核!");
  278. }
  279. senderId = user.getId();
  280. }
  281. //添加跟踪记录
  282. RepairTrackRecord rtr = new RepairTrackRecord();
  283. rtr.setRecordId(data.getRecordId());
  284. rtr.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
  285. rtr.setContent("待确认");
  286. rtr.setUserId(senderId);
  287. rtr.setUserZzstr("维修师傅");
  288. int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
  289. if (insertRtr <= 0) {
  290. throw new Exception("报价失败");
  291. }
  292. } catch (Exception e) {
  293. throw new Exception("报价失败!");
  294. }
  295. return CommonResult.ok("报价成功");
  296. }
  297. /**
  298. * 改价-获取耗材记录数据
  299. *
  300. * @param recordId 维修单记录ID
  301. * @return
  302. */
  303. @Override
  304. public CommonResult queryChangePriceConsumables(Integer recordId) {
  305. RepairRecord rr = repairRecordService.getRepairById(recordId);
  306. if (rr == null) {
  307. return CommonResult.fail("维修单已失效,改价失败");
  308. }
  309. if (rr.getMaintenanceState() != eRecordStatu.ToLogistics.getValue()) {
  310. return CommonResult.fail("维修单不是待处理状态,无法进行改价操作");
  311. }
  312. ChangePriceConsumeVo result = new ChangePriceConsumeVo();
  313. result.setRecordId(recordId);
  314. result.setTotalPrice(rr.getPrice());
  315. result.setMaintenancerName(rr.getMaintenancerName());
  316. result.setMaintenancerPhone(rr.getMaintenancerPhone());
  317. List<ChangeConsumesVo> consumables = repairConsumablesService.repairChangeConsumes(recordId);
  318. result.setConsumes(consumables);
  319. return CommonResult.ok(result);
  320. }
  321. /**
  322. * 改价-确认提交
  323. *
  324. * @param data 改价耗材数据
  325. * @param userhead userhead请求头
  326. * @param bindingResult 是否为空判断
  327. * @return
  328. * @throws Exception
  329. */
  330. @Override
  331. @Transactional(rollbackFor = {Exception.class})
  332. public CommonResult changeMaintenanceConsumables(MaintenanceConsumeablesRequest data, @RequestHeader("user_head") String userhead, BindingResult bindingResult) throws Exception {
  333. System.out.println("改价确认提交" + JSON.toJSON(data));
  334. try {
  335. if (bindingResult.hasErrors()) {
  336. String st = paramUtils.getParamError(bindingResult);
  337. return CommonResult.fail(st);
  338. }
  339. if (data.getConsumes().size() <= 0) {
  340. return CommonResult.fail("无耗材数据,改价失败");
  341. }
  342. String userID = AesUtils.decrypt(userhead);
  343. RepairAdmin operateData = repairAdminService.getRepairById(userID);
  344. if (operateData == null) {
  345. //return CommonResult.fail("操作人信息不合法,无法进行回复!");
  346. throw new Exception("操作人信息不合法,无法进行改价!");
  347. }
  348. List<RepairConsumables> rcDatas = new ArrayList<>();
  349. for (insertConsumablesRequest rc : data.getConsumes()) {
  350. if (rc.getArticleId() == null) {
  351. return CommonResult.fail("报修物品ID不能为空");
  352. }
  353. if (rc.getConsumeId() == null) {
  354. return CommonResult.fail("耗材ID不能为空");
  355. }
  356. if (rc.getConsumeName() == null) {
  357. return CommonResult.fail("耗材名称不能为空");
  358. }
  359. if (rc.getNumber() == null || rc.getNumber() <= 0) {
  360. return CommonResult.fail("耗材数量不能为空");
  361. }
  362. if (rc.getPrice() == null) {
  363. return CommonResult.fail("耗材单价不能为空");
  364. }
  365. RepairConsumables rcData = new RepairConsumables();
  366. if (rc.getId() != null && rc.getId() > 0) {
  367. rcData.setId(rc.getId());
  368. }
  369. rcData.setChangeUser(operateData.getUsername());
  370. rcData.setRecordId(data.getRecordId());
  371. rcData.setArticleId(rc.getArticleId());
  372. rcData.setConsumeId(rc.getConsumeId());
  373. rcData.setConsumeName(rc.getConsumeName());
  374. rcData.setNumber(rc.getNumber());
  375. rcData.setPrice(rc.getPrice());
  376. rcData.setTotalPrice(new BigDecimal(rcData.getNumber()).multiply(rcData.getPrice()));
  377. rcDatas.add(rcData);
  378. }
  379. //获取原有的耗材记录数据
  380. List<ChangeConsumesVo> oldConsumables = repairConsumablesService.repairChangeConsumes(data.getRecordId());
  381. RepairRecord rr = repairRecordService.getRepairById(data.getRecordId());
  382. if (rr == null) {
  383. return CommonResult.fail("报修工单不存在,改价失败");
  384. }
  385. if (rr.getMaintenanceState() == eRecordStatu.Finished.getValue()
  386. || rr.getMaintenanceState() == eRecordStatu.Canceled.getValue()
  387. || rr.getMaintenanceState() == eRecordStatu.Closed.getValue()) {
  388. return CommonResult.fail("当前报修工单状态发生变化,改价失败");
  389. }
  390. boolean result = repairConsumablesService.saveorUpdateConsumableBatch(rcDatas);
  391. if (!result) {
  392. throw new Exception("改价失败!");
  393. }
  394. //只有用户支付完了之后,维修师傅才能继续看到这个单子
  395. rr.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
  396. rr.setPrice(data.getTotalPrice());
  397. int updateRecord = repairRecordService.updateRepairRecord(rr);
  398. if (updateRecord <= 0) {
  399. throw new Exception("改价失败!");
  400. }
  401. List<Integer> newDatas = rcDatas.stream().map(RepairConsumables::getId).collect(Collectors.toList());
  402. List<Integer> oldDatas = oldConsumables.stream().map(ChangeConsumesVo::getId).collect(Collectors.toList());
  403. //oldData集合不在newDatas集合中的内容
  404. List<Integer> ids = oldDatas.stream().filter(item -> !newDatas.contains(item)).collect(Collectors.toList());
  405. if (ids.size() > 0) {
  406. int deleted = repairConsumablesService.deletedConsumableByIds(ids);
  407. if (deleted <= 0) {
  408. throw new Exception("改价失败!");
  409. }
  410. }
  411. //发送人
  412. Integer senderId = 0;//超级管理员用ID为0
  413. if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
  414. RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
  415. if (user == null) {
  416. //return CommonResult.fail("管理端账号未与移动端账号绑定,无法进行审核!");
  417. throw new Exception("管理端账号未与移动端账号绑定,无法进行改价!");
  418. }
  419. senderId = user.getId();
  420. }
  421. //追踪记录表
  422. RepairTrackRecord repairTrackRecord = new RepairTrackRecord();
  423. repairTrackRecord.setRecordId(data.getRecordId());
  424. repairTrackRecord.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
  425. repairTrackRecord.setContent("改价");
  426. repairTrackRecord.setUserId(senderId);
  427. repairTrackRecord.setUserZzstr("维修师傅");
  428. int save = repairTrackRecordService.insertRepairTrackRecord(repairTrackRecord);
  429. if (save <= 0) {
  430. throw new Exception("改价失败!");
  431. }
  432. } catch (Exception e) {
  433. throw new Exception("改价失败!");
  434. }
  435. return CommonResult.ok("改价成功");
  436. }
  437. }