| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- package com.repair.controller;
- import com.repair.api.RepairConsumablesControllerAPI;
- import com.repair.common.utils.ExcelExportUtil;
- import com.repair.common.utils.ExcelUtils;
- import com.repair.common.utils.TimeExchange;
- import com.repair.common.utils.paramUtils;
- import com.repair.model.enumModel.eRecordStatu;
- import com.repair.model.pojo.RepairConsumables;
- import com.repair.model.pojo.RepairRecord;
- import com.repair.model.request.insertConsumablesRequest;
- import com.repair.model.result.CommonResult;
- import com.repair.model.result.PageUtils;
- import com.repair.model.vo.ChangeConsumesVo;
- import com.repair.model.vo.ChangePriceConsumeVo;
- import com.repair.model.vo.ConsumableExcelVo;
- import com.repair.model.vo.ConsumablePageVo;
- import com.repair.services.RepairConsumablesService;
- import com.repair.services.RepairRecordService;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpServletResponse;
- import java.lang.reflect.InvocationTargetException;
- import java.math.BigDecimal;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author ceshi
- * @since 2023-07-20
- */
- @RestController
- public class RepairConsumablesController implements RepairConsumablesControllerAPI {
- @Autowired
- private RepairConsumablesService repairConsumablesService;
- @Autowired
- private RepairRecordService repairRecordService;
- @Override
- public CommonResult InsertrepairConsumables(RepairConsumables rc, BindingResult bindingResult) {
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- int result = repairConsumablesService.insertRepairConsumables(rc);
- return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
- }
- @Override
- public CommonResult updaterepairConsumablesById(RepairConsumables rc, BindingResult bindingResult) {
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- int result = repairConsumablesService.updateRepairConsumables(rc);
- return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
- }
- @Override
- public CommonResult queryPagerepairConsumabless(int currentPage, int pageCount, String name) {
- PageUtils<RepairConsumables> result = repairConsumablesService.queryPageRepairConsumabless(currentPage, pageCount, name);
- return CommonResult.ok(result);
- }
- @Override
- public CommonResult deleterepairConsumablesById(int id) {
- RepairConsumables data = repairConsumablesService.getRepairById(id);
- if (data == null) {
- return CommonResult.fail("当前数据不存在,删除失败!");
- }
- int result = repairConsumablesService.deleteRepairConsumablesById(id);
- return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
- }
- @Override
- public CommonResult queryRepairConsumablePageList(int currentPage, int pageCount, String keyWord, String startTime, String endTime) {
- if (startTime != null && endTime != null) {
- try {
- startTime = TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
- endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
- } catch (Exception e) {
- }
- }
- PageUtils<ConsumablePageVo> result = repairConsumablesService.queryConsumablePageList(currentPage, pageCount, keyWord, startTime, endTime);
- return CommonResult.ok(result);
- }
- @Override
- public void downloadRepairConsumableExcel(String keyWord, String startTime, String endTime, HttpServletResponse response) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
- Map<String, String> map = new HashMap<>();
- //表头与键值对的映射关系
- map.put("id", "数据ID");
- map.put("consumeName", "耗材名称");
- map.put("price", "单价(元)");
- map.put("number", "数量");
- map.put("totalPrice", "金额(元)");
- map.put("articleName", "工单");
- map.put("reportTimeStr", "报修时间");
- map.put("updateTimeStr", "维修时间");
- if (startTime != null && endTime != null) {
- try {
- startTime = TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
- endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
- } catch (Exception e) {
- }
- }
- List<ConsumableExcelVo> datas = repairConsumablesService.queryConsumablePageList(keyWord, startTime, endTime);
- List<String> orderList = new ArrayList<>();
- orderList.add("id");
- orderList.add("consumeName");
- orderList.add("price");
- orderList.add("number");
- orderList.add("totalPrice");
- orderList.add("articleName");
- orderList.add("reportTimeStr");
- orderList.add("updateTimeStr");
- //导出逻辑 这里的list是从导入里面哪来的 map1与map不一样 orderList这里为空
- Workbook workbook = ExcelExportUtil.createWorkbook(datas, map, 1, "耗材记录表", orderList);
- ExcelUtils.excelDownload(workbook, "耗材记录.xlsx", response);
- }
- @Override
- @Transactional(rollbackFor = {Exception.class})
- public CommonResult InsertMaintenanceConsumables(MaintenanceConsumeablesRequest data, BindingResult bindingResult) throws Exception {
- try {
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- if (data.getConsumes().size() <= 0) {
- return CommonResult.fail("无耗材数据,报价失败");
- }
- List<RepairConsumables> rcDatas = new ArrayList<>();
- for (insertConsumablesRequest rc : data.getConsumes()) {
- if (rc.getConsumeId() == null) {
- return CommonResult.fail("耗材ID不能为空");
- }
- if (rc.getConsumeName() == null) {
- return CommonResult.fail("耗材名称不能为空");
- }
- if (rc.getNumber() == null || rc.getNumber() <= 0) {
- return CommonResult.fail("耗材数量不能为空");
- }
- if (rc.getPrice() == null) {
- return CommonResult.fail("耗材单价不能为空");
- }
- RepairConsumables rcData = new RepairConsumables();
- rcData.setRecordId(data.getRecordId());
- rcData.setConsumeId(rc.getConsumeId());
- rcData.setConsumeName(rc.getConsumeName());
- rcData.setNumber(rc.getNumber());
- rcData.setPrice(rc.getPrice());
- rcData.setTotalPrice(new BigDecimal(rcData.getNumber()).multiply(rcData.getPrice()));
- rcDatas.add(rcData);
- }
- RepairRecord rr = repairRecordService.getRepairById(data.getRecordId());
- if (rr == null) {
- return CommonResult.fail("报修工单不存在,报价失败");
- }
- if (rr.getMaintenanceState() == eRecordStatu.Finished.getValue()
- || rr.getMaintenanceState() == eRecordStatu.Canceled.getValue()
- || rr.getMaintenanceState() == eRecordStatu.Closed.getValue()) {
- return CommonResult.fail("当前报修工单状态发生变化,报价失败");
- }
- boolean result = repairConsumablesService.insertConsumableBatch(rcDatas);
- if (!result) {
- throw new Exception("报价失败!");
- }
- //只有用户支付完了之后,维修师傅才能继续看到这个单子
- rr.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
- rr.setPrice(data.getTotalPrice());
- int updateRecord = repairRecordService.updateRepairRecord(rr);
- if (updateRecord <= 0) {
- throw new Exception("报价失败!");
- }
- } catch (Exception e) {
- throw new Exception("报价失败!");
- }
- return CommonResult.ok("报价成功");
- }
- @Override
- public CommonResult queryChangePriceConsumables(Integer recordId) {
- RepairRecord rr = repairRecordService.getRepairById(recordId);
- if (rr == null) {
- return CommonResult.fail("维修单已失效,改价失败");
- }
- if (rr.getMaintenanceState() != eRecordStatu.ToConfirmed.getValue()) {
- return CommonResult.fail("维修单不是待确认状态,无法进行改价操作");
- }
- ChangePriceConsumeVo result = new ChangePriceConsumeVo();
- result.setRecordId(recordId);
- result.setTotalPrice(rr.getPrice());
- result.setMaintenancerName(rr.getMaintenancerName());
- result.setMaintenancerPhone(rr.getMaintenancerPhone());
- List<ChangeConsumesVo> consumables = repairConsumablesService.repairChangeConsumes(recordId);
- result.setConsumes(consumables);
- return CommonResult.ok(result);
- }
- @Override
- @Transactional(rollbackFor = {Exception.class})
- public CommonResult changeMaintenanceConsumables(MaintenanceConsumeablesRequest data, BindingResult bindingResult) throws Exception {
- try {
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- if (data.getConsumes().size() <= 0) {
- return CommonResult.fail("无耗材数据,改价失败");
- }
- List<RepairConsumables> rcDatas = new ArrayList<>();
- for (insertConsumablesRequest rc : data.getConsumes()) {
- if (rc.getConsumeId() == null) {
- return CommonResult.fail("耗材ID不能为空");
- }
- if (rc.getConsumeName() == null) {
- return CommonResult.fail("耗材名称不能为空");
- }
- if (rc.getNumber() == null || rc.getNumber() <= 0) {
- return CommonResult.fail("耗材数量不能为空");
- }
- if (rc.getPrice() == null) {
- return CommonResult.fail("耗材单价不能为空");
- }
- RepairConsumables rcData = new RepairConsumables();
- if (rc.getId() != null && rc.getId() > 0) {
- rcData.setId(rc.getId());
- }
- rcData.setRecordId(data.getRecordId());
- rcData.setConsumeId(rc.getConsumeId());
- rcData.setConsumeName(rc.getConsumeName());
- rcData.setNumber(rc.getNumber());
- rcData.setPrice(rc.getPrice());
- rcData.setTotalPrice(new BigDecimal(rcData.getNumber()).multiply(rcData.getPrice()));
- rcDatas.add(rcData);
- }
- RepairRecord rr = repairRecordService.getRepairById(data.getRecordId());
- if (rr == null) {
- return CommonResult.fail("报修工单不存在,改价失败");
- }
- if (rr.getMaintenanceState() == eRecordStatu.Finished.getValue()
- || rr.getMaintenanceState() == eRecordStatu.Canceled.getValue()
- || rr.getMaintenanceState() == eRecordStatu.Closed.getValue()) {
- return CommonResult.fail("当前报修工单状态发生变化,改价失败");
- }
- boolean result = repairConsumablesService.insertConsumableBatch(rcDatas);
- if (!result) {
- throw new Exception("改价失败!");
- }
- //只有用户支付完了之后,维修师傅才能继续看到这个单子
- rr.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
- rr.setPrice(data.getTotalPrice());
- int updateRecord = repairRecordService.updateRepairRecord(rr);
- if (updateRecord <= 0) {
- throw new Exception("改价失败!");
- }
- //获取原有的耗材记录数据
- List<ChangeConsumesVo> oldConsumables = repairConsumablesService.repairChangeConsumes(rr.getId());
- List<Integer> newDatas = data.getConsumes().stream().map(insertConsumablesRequest::getId).collect(Collectors.toList());
- List<Integer> oldDatas = oldConsumables.stream().map(ChangeConsumesVo::getId).collect(Collectors.toList());
- //oldData集合不在newDatas集合中的内容
- List<Integer> ids = newDatas.stream().filter(item -> !oldDatas.contains(item)).collect(Collectors.toList());
- if(ids.size() > 0){
- int deleted = repairConsumablesService.deletedConsumableByIds(ids);
- if (updateRecord <= 0) {
- throw new Exception("改价失败!");
- }
- }
- } catch (Exception e) {
- throw new Exception("改价失败!");
- }
- return CommonResult.ok("改价成功");
- }
- }
|