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.time.MethodTime; import com.poteviohealth.cgp.common.filter.TokenContext; import com.poteviohealth.cgp.common.filter.repeatSubmit.RepeatSubmit; 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.OrderCostWebOutDTO; import com.poteviohealth.cgp.statistics.model.outdto.OrderExcelDTO; import com.poteviohealth.cgp.statistics.service.IOrderService; 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.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.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; @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 = "阿里云图片上传") @ResponseBody @MethodTime public VaultsResponse uploadArrayFile(@RequestParam("files") List files, @RequestBody List list){ log.info(files); log.info(list); /* VaultsResponse> vaultsResponse = ossUtils.uploadArrayFile(list); if(vaultsResponse.validate()){ return orderService.updateOrderUrl(list); }*/ return VaultsResponse.success(); } }