RepairConsumablesController.java 15 KB

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