package com.poteviohealth.cgp.statistics.controller; import com.google.common.collect.Maps; import com.poteviohealth.cgp.common.controller.BaseWebController; import com.poteviohealth.cgp.common.exception.BusinessException; import com.poteviohealth.cgp.common.facade.cache.CacheService; import com.poteviohealth.cgp.common.facade.constrain.OrgConstrainService; import com.poteviohealth.cgp.common.facade.lock.DistributedLock; import com.poteviohealth.cgp.common.facade.log.OperateType; import com.poteviohealth.cgp.common.facade.log.OperationLog; import com.poteviohealth.cgp.common.facade.time.MethodTime; import com.poteviohealth.cgp.common.filter.TokenContext; import com.poteviohealth.cgp.common.filter.repeatSubmit.RepeatSubmit; import com.poteviohealth.cgp.common.integrated.finance.model.CardAccTransRecharge; import com.poteviohealth.cgp.common.integrated.finance.model.MemberPlanDTO; import com.poteviohealth.cgp.common.integrated.finance.model.MemberPlanPageDTO; import com.poteviohealth.cgp.common.model.VaultsResponse; import com.poteviohealth.cgp.statistics.model.indto.*; import com.poteviohealth.cgp.statistics.model.outdto.DishesOrderWebOutDTO; import com.poteviohealth.cgp.statistics.model.outdto.DistinctionDTO; import com.poteviohealth.cgp.statistics.model.outdto.OrderCostWebOutDTO; import com.poteviohealth.cgp.statistics.model.outdto.OrderExcelDTO; import com.poteviohealth.cgp.statistics.service.IDistinctionService; import com.poteviohealth.cgp.statistics.service.IOrderService; import com.poteviohealth.cgp.statistics.service.IWatermarkImgService; import com.poteviohealth.cgp.statistics.utils.ExcelUtils2; import com.poteviohealth.cgp.statistics.utils.OssUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.util.Map; /** * 服务工单 前端控制器 * @author Qin */ @Controller @RequestMapping("/statistics/web/order") @Api(tags={"WEB端-工单相关接口"}) @Log4j2 public class OrderController extends BaseWebController { @Resource private OrgConstrainService orgConstrainService; @Resource private CacheService cacheService; @Resource private IOrderService orderService; @Resource private OssUtils ossUtils; @Resource private IWatermarkImgService watermarkImgService; @Resource private IDistinctionService distinctionService; @PostMapping(value = "/orderServiceExport") @ApiOperation(httpMethod = "POST", value = "工单Excel导出") @RepeatSubmit @MethodTime public void orderServiceExport(HttpServletResponse resp, @RequestBody OrderServiceWebInDTO orderServiceWebInDTO){ String key ="orderServiceExport"; String error = this.checkExcelExport(key); try { if ("".equals(error)) { orgConstrainService.fillOrgParams(orderServiceWebInDTO); List list = orderService.orderExcel(orderServiceWebInDTO); ExcelUtils2.easyDownload("classpath:excel/order_service.xlsx", Maps.newHashMap(), list, "", resp); }else{ throw new BusinessException(error); } }catch (Exception e){ e.printStackTrace(); throw new BusinessException(e.getMessage()); }finally { if(cacheService.exists(key)){ if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){ cacheService.del(key); } } } } @PostMapping(value = "/orderDishesExport") @ApiOperation(httpMethod = "POST", value = "餐单Excel导出") @RepeatSubmit @MethodTime public void orderDishesExport(HttpServletResponse resp, @RequestBody DishesOrderWebInDTO dto){ String key ="orderDishesExport"; String error = this.checkExcelExport(key); try{ if ("".equals(error)) { orgConstrainService.fillOrgParams(dto); List list = orderService.orderDishes(dto); ExcelUtils2.easyDownload("classpath:excel/order_dishes.xlsx", Maps.newHashMap(), list, "", resp); }else{ throw new BusinessException(error); } }catch (Exception e){ e.printStackTrace(); throw new BusinessException(e.getMessage()); }finally { if(cacheService.exists(key)){ if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){ cacheService.del(key); } } } } @PostMapping(value = "/revenueExport") @ApiOperation(httpMethod = "POST", value = "成本核算Excel导出") @RepeatSubmit @MethodTime public void revenueExport(HttpServletResponse resp, @RequestBody OrderCostWebInDTO orderCostWebInDTO){ String key ="revenueExport"; String error = this.checkExcelExport(key); try{ if ("".equals(error)) { String templateLocation = "classpath:excel/revenue.xlsx"; String filename = ""; Map data = Maps.newHashMap(); String dateStr = "工单成本核算"; if(orderCostWebInDTO.getStartDate() != null){ dateStr = DateFormatUtils.format(orderCostWebInDTO.getStartDate(),"yyyy-MM-dd")+"~"+ DateFormatUtils.format(orderCostWebInDTO.getEndDate(),"yyyy-MM-dd")+dateStr; } orgConstrainService.fillOrgParams(orderCostWebInDTO); List list = orderService.OrderCostList(orderCostWebInDTO); double totalActualCost = list.stream().mapToDouble(a-> Double.parseDouble(a.getStandard())).sum(); double totalActualIncome =list.stream().mapToDouble(a-> Double.parseDouble(a.getActual())).sum(); data.put("title",dateStr); data.put("title2","标准成本合计:"+totalActualCost+"元;成本金额合计:"+totalActualIncome+"元"); ExcelUtils2.easyDownload(templateLocation,data , list, filename, resp); }else{ throw new BusinessException(error); } }catch (Exception e){ e.printStackTrace(); throw new BusinessException(e.getMessage()); }finally { if(cacheService.exists(key)){ if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){ cacheService.del(key); } } } } @PostMapping(value = "/orderExport") @ApiOperation(httpMethod = "POST", value = "订单Excel导出") public void orderExport(HttpServletResponse resp,@RequestBody OrderWebInDTO orderWebInDTO){ String key ="orderExport"; String error = this.checkExcelExport(key); try{ if ("".equals(error)) { String templateLocation = "classpath:excel/order.xlsx"; String filename = ""; Map data = Maps.newHashMap(); List list = orderService.excelList(orderWebInDTO); ExcelUtils2.easyDownload(templateLocation,data , list, filename, resp); }else{ throw new BusinessException(error); } }catch (Exception e){ e.printStackTrace(); throw new BusinessException(e.getMessage()); }finally { if(cacheService.exists(key)){ if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){ cacheService.del(key); } } } } @DistributedLock(fairLock =true) private String checkExcelExport(String key){ String returnVal = ""; String user = TokenContext.cureWebUser().getUserId().toString(); if (!cacheService.exists(key)) { cacheService.setex(key,300,user); } else { String val = cacheService.get(key); if(val.equals(user)){ returnVal = "正在导出数据,请耐心等待..."; }else{ returnVal = "其他用户正在导出数据,请稍后再试"; } } return returnVal; } @RequestMapping(value = "/uploadArrayFile", method = RequestMethod.POST) @ApiOperation(value = "阿里云图片上传") @MethodTime @ResponseBody public VaultsResponse uploadArrayFile(@RequestParam("file") MultipartFile file,@RequestParam("orderId") Long orderId,@RequestParam("uidList") List uidList){ log.info(orderId); //图片检查 try { DistinctionDTO dto; Integer pos = file.getOriginalFilename().lastIndexOf('.'); String suffix = ""; if (pos != -1) { suffix = file.getOriginalFilename().substring(pos); } if(suffix.indexOf("mp4") != -1){ //视频处理 File tempFile = File.createTempFile("temp_", ".mp4"); file.transferTo(tempFile); MultipartFile videoFile = new MockMultipartFile(tempFile.getName(), new FileInputStream(tempFile)); File imageFile = ossUtils.generateCover(tempFile); MultipartFile tempMultipartFile = new MockMultipartFile(imageFile.getName(), new FileInputStream(imageFile)); dto = distinctionService.checkImg(tempMultipartFile,orderId,uidList); if(dto.getOrderId() ==null){ VaultsResponse vaultsResponse = ossUtils.uploadOneFile(videoFile,orderId); dto.setUrl(vaultsResponse.getData()); } tempFile.delete(); imageFile.delete(); }else{ //图片处理 dto = distinctionService.checkImg(file,orderId,uidList); if(dto.getOrderId() ==null){ VaultsResponse vaultsResponse = ossUtils.uploadOneFile(file,orderId); dto.setUrl(vaultsResponse.getData()); } } return VaultsResponse.success(dto); } catch (IOException e) { e.printStackTrace(); } return VaultsResponse.failed(); } @PostMapping("/orderFile") @ApiOperation(value = "工单照片") @ResponseBody @OperationLog(type = OperateType.UPDATE, description = "工单照片处理") @MethodTime public VaultsResponse orderFile(@RequestBody List list) { //处理水印 VaultsResponse> vaultsResponse = watermarkImgService.printingWatermark(list); if(!vaultsResponse.validate()){ return vaultsResponse; } list = vaultsResponse.getData(); //处理照片 watermarkImgService.updateOrderUrl(list); return VaultsResponse.success(); } @PostMapping(value = "/pageCardExport") @ApiOperation(httpMethod = "POST", value = "后台账户查询Excel导出") public void orderExport(HttpServletResponse resp,@RequestBody MemberPlanPageDTO dto){ String key ="pageCardExport"; String error = this.checkExcelExport(key); try{ if ("".equals(error)) { String templateLocation = "classpath:excel/pageCard.xlsx"; String filename = "客户账户.xlsx"; Map data = Maps.newHashMap(); orgConstrainService.fillOrgParams(dto); List list = orderService.pageCardExcel(dto); ExcelUtils2.easyDownload(templateLocation,data , list, filename, resp); }else{ throw new BusinessException(error); } }catch (Exception e){ e.printStackTrace(); throw new BusinessException(e.getMessage()); }finally { if(cacheService.exists(key)){ if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){ cacheService.del(key); } } } } @PostMapping(value = "/transRechargeExcel") @ApiOperation(httpMethod = "POST", value = "账户充值Excel导出") @RepeatSubmit @MethodTime public void transRechargeExcel(HttpServletResponse resp, @RequestBody CardWebInDTO dto){ String key ="transRechargeExcel"; String error = this.checkExcelExport(key); try{ if ("".equals(error)) { orgConstrainService.fillOrgParams(dto); Map maps = Maps.newHashMap(); String start = DateFormatUtils.format(dto.getStartDate(), "yyyy-MM"); String end = DateFormatUtils.format(dto.getEndDate(), "yyyy-MM"); if(start.equals(end)){ maps.put("title", start+"充值记录"); }else{ maps.put("title", start+"到"+end+"充值记录"); } List list = orderService.transRechargeExcel(dto); ExcelUtils2.easyDownload("classpath:excel/trans_recharge.xlsx",maps, list, "", resp); }else{ throw new BusinessException(error); } }catch (Exception e){ e.printStackTrace(); throw new BusinessException(e.getMessage()); }finally { if(cacheService.exists(key)){ if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){ cacheService.del(key); } } } } }