RepairConsumablesController.java 17 KB

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