OrderController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package com.poteviohealth.cgp.statistics.controller;
  2. import com.google.common.collect.Maps;
  3. import com.poteviohealth.cgp.common.controller.BaseWebController;
  4. import com.poteviohealth.cgp.common.exception.BusinessException;
  5. import com.poteviohealth.cgp.common.facade.cache.CacheService;
  6. import com.poteviohealth.cgp.common.facade.constrain.OrgConstrainService;
  7. import com.poteviohealth.cgp.common.facade.lock.DistributedLock;
  8. import com.poteviohealth.cgp.common.facade.log.OperateType;
  9. import com.poteviohealth.cgp.common.facade.log.OperationLog;
  10. import com.poteviohealth.cgp.common.facade.time.MethodTime;
  11. import com.poteviohealth.cgp.common.filter.TokenContext;
  12. import com.poteviohealth.cgp.common.filter.repeatSubmit.RepeatSubmit;
  13. import com.poteviohealth.cgp.common.integrated.finance.model.CardAccTransRecharge;
  14. import com.poteviohealth.cgp.common.integrated.finance.model.MemberPlanDTO;
  15. import com.poteviohealth.cgp.common.integrated.finance.model.MemberPlanPageDTO;
  16. import com.poteviohealth.cgp.common.model.VaultsResponse;
  17. import com.poteviohealth.cgp.statistics.model.indto.*;
  18. import com.poteviohealth.cgp.statistics.model.outdto.DishesOrderWebOutDTO;
  19. import com.poteviohealth.cgp.statistics.model.outdto.DistinctionDTO;
  20. import com.poteviohealth.cgp.statistics.model.outdto.OrderCostWebOutDTO;
  21. import com.poteviohealth.cgp.statistics.model.outdto.OrderExcelDTO;
  22. import com.poteviohealth.cgp.statistics.service.IDistinctionService;
  23. import com.poteviohealth.cgp.statistics.service.IOrderService;
  24. import com.poteviohealth.cgp.statistics.service.IWatermarkImgService;
  25. import com.poteviohealth.cgp.statistics.utils.ExcelUtils2;
  26. import com.poteviohealth.cgp.statistics.utils.OssUtils;
  27. import io.swagger.annotations.Api;
  28. import io.swagger.annotations.ApiOperation;
  29. import lombok.extern.log4j.Log4j2;
  30. import org.apache.commons.lang3.time.DateFormatUtils;
  31. import org.springframework.mock.web.MockMultipartFile;
  32. import org.springframework.stereotype.Controller;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.web.multipart.MultipartFile;
  35. import javax.annotation.Resource;
  36. import javax.servlet.http.HttpServletResponse;
  37. import java.io.File;
  38. import java.io.FileInputStream;
  39. import java.io.IOException;
  40. import java.util.List;
  41. import java.util.Map;
  42. /**
  43. * 服务工单 前端控制器
  44. * @author Qin
  45. */
  46. @Controller
  47. @RequestMapping("/statistics/web/order")
  48. @Api(tags={"WEB端-工单相关接口"})
  49. @Log4j2
  50. public class OrderController extends BaseWebController {
  51. @Resource
  52. private OrgConstrainService orgConstrainService;
  53. @Resource
  54. private CacheService cacheService;
  55. @Resource
  56. private IOrderService orderService;
  57. @Resource
  58. private OssUtils ossUtils;
  59. @Resource
  60. private IWatermarkImgService watermarkImgService;
  61. @Resource
  62. private IDistinctionService distinctionService;
  63. @PostMapping(value = "/orderServiceExport")
  64. @ApiOperation(httpMethod = "POST", value = "工单Excel导出")
  65. @RepeatSubmit
  66. @MethodTime
  67. public void orderServiceExport(HttpServletResponse resp, @RequestBody OrderServiceWebInDTO orderServiceWebInDTO){
  68. String key ="orderServiceExport";
  69. String error = this.checkExcelExport(key);
  70. try {
  71. if ("".equals(error)) {
  72. orgConstrainService.fillOrgParams(orderServiceWebInDTO);
  73. List<OrderExcelDTO> list = orderService.orderExcel(orderServiceWebInDTO);
  74. ExcelUtils2.easyDownload("classpath:excel/order_service.xlsx", Maps.newHashMap(), list, "", resp);
  75. }else{
  76. throw new BusinessException(error);
  77. }
  78. }catch (Exception e){
  79. e.printStackTrace();
  80. throw new BusinessException(e.getMessage());
  81. }finally {
  82. if(cacheService.exists(key)){
  83. if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){
  84. cacheService.del(key);
  85. }
  86. }
  87. }
  88. }
  89. @PostMapping(value = "/orderDishesExport")
  90. @ApiOperation(httpMethod = "POST", value = "餐单Excel导出")
  91. @RepeatSubmit
  92. @MethodTime
  93. public void orderDishesExport(HttpServletResponse resp, @RequestBody DishesOrderWebInDTO dto){
  94. String key ="orderDishesExport";
  95. String error = this.checkExcelExport(key);
  96. try{
  97. if ("".equals(error)) {
  98. orgConstrainService.fillOrgParams(dto);
  99. List<DishesOrderWebOutDTO> list = orderService.orderDishes(dto);
  100. ExcelUtils2.easyDownload("classpath:excel/order_dishes.xlsx", Maps.newHashMap(), list, "", resp);
  101. }else{
  102. throw new BusinessException(error);
  103. }
  104. }catch (Exception e){
  105. e.printStackTrace();
  106. throw new BusinessException(e.getMessage());
  107. }finally {
  108. if(cacheService.exists(key)){
  109. if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){
  110. cacheService.del(key);
  111. }
  112. }
  113. }
  114. }
  115. @PostMapping(value = "/revenueExport")
  116. @ApiOperation(httpMethod = "POST", value = "成本核算Excel导出")
  117. @RepeatSubmit
  118. @MethodTime
  119. public void revenueExport(HttpServletResponse resp, @RequestBody OrderCostWebInDTO orderCostWebInDTO){
  120. String key ="revenueExport";
  121. String error = this.checkExcelExport(key);
  122. try{
  123. if ("".equals(error)) {
  124. String templateLocation = "classpath:excel/revenue.xlsx";
  125. String filename = "";
  126. Map<String, Object> data = Maps.newHashMap();
  127. String dateStr = "工单成本核算";
  128. if(orderCostWebInDTO.getStartDate() != null){
  129. dateStr = DateFormatUtils.format(orderCostWebInDTO.getStartDate(),"yyyy-MM-dd")+"~"+
  130. DateFormatUtils.format(orderCostWebInDTO.getEndDate(),"yyyy-MM-dd")+dateStr;
  131. }
  132. orgConstrainService.fillOrgParams(orderCostWebInDTO);
  133. List<OrderCostWebOutDTO> list = orderService.OrderCostList(orderCostWebInDTO);
  134. double totalActualCost = list.stream().mapToDouble(a-> Double.parseDouble(a.getStandard())).sum();
  135. double totalActualIncome =list.stream().mapToDouble(a-> Double.parseDouble(a.getActual())).sum();
  136. data.put("title",dateStr);
  137. data.put("title2","标准成本合计:"+totalActualCost+"元;成本金额合计:"+totalActualIncome+"元");
  138. ExcelUtils2.easyDownload(templateLocation,data , list, filename, resp);
  139. }else{
  140. throw new BusinessException(error);
  141. }
  142. }catch (Exception e){
  143. e.printStackTrace();
  144. throw new BusinessException(e.getMessage());
  145. }finally {
  146. if(cacheService.exists(key)){
  147. if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){
  148. cacheService.del(key);
  149. }
  150. }
  151. }
  152. }
  153. @PostMapping(value = "/orderExport")
  154. @ApiOperation(httpMethod = "POST", value = "订单Excel导出")
  155. public void orderExport(HttpServletResponse resp,@RequestBody OrderWebInDTO orderWebInDTO){
  156. String key ="orderExport";
  157. String error = this.checkExcelExport(key);
  158. try{
  159. if ("".equals(error)) {
  160. String templateLocation = "classpath:excel/order.xlsx";
  161. String filename = "";
  162. Map<String, Object> data = Maps.newHashMap();
  163. List<OrderExcelDTO> list = orderService.excelList(orderWebInDTO);
  164. ExcelUtils2.easyDownload(templateLocation,data , list, filename, resp);
  165. }else{
  166. throw new BusinessException(error);
  167. }
  168. }catch (Exception e){
  169. e.printStackTrace();
  170. throw new BusinessException(e.getMessage());
  171. }finally {
  172. if(cacheService.exists(key)){
  173. if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){
  174. cacheService.del(key);
  175. }
  176. }
  177. }
  178. }
  179. @DistributedLock(fairLock =true)
  180. private String checkExcelExport(String key){
  181. String returnVal = "";
  182. String user = TokenContext.cureWebUser().getUserId().toString();
  183. if (!cacheService.exists(key)) {
  184. cacheService.setex(key,300,user);
  185. } else {
  186. String val = cacheService.get(key);
  187. if(val.equals(user)){
  188. returnVal = "正在导出数据,请耐心等待...";
  189. }else{
  190. returnVal = "其他用户正在导出数据,请稍后再试";
  191. }
  192. }
  193. return returnVal;
  194. }
  195. @RequestMapping(value = "/uploadArrayFile", method = RequestMethod.POST)
  196. @ApiOperation(value = "阿里云图片上传")
  197. @MethodTime
  198. @ResponseBody
  199. public VaultsResponse<DistinctionDTO> uploadArrayFile(@RequestParam("file") MultipartFile file,@RequestParam("orderId") Long orderId,@RequestParam("uidList") List<String> uidList){
  200. log.info(orderId);
  201. //图片检查
  202. try {
  203. DistinctionDTO dto;
  204. Integer pos = file.getOriginalFilename().lastIndexOf('.');
  205. String suffix = "";
  206. if (pos != -1) {
  207. suffix = file.getOriginalFilename().substring(pos);
  208. }
  209. if(suffix.indexOf("mp4") != -1){
  210. //视频处理
  211. File tempFile = File.createTempFile("temp_", ".mp4");
  212. file.transferTo(tempFile);
  213. MultipartFile videoFile = new MockMultipartFile(tempFile.getName(), new FileInputStream(tempFile));
  214. File imageFile = ossUtils.generateCover(tempFile);
  215. MultipartFile tempMultipartFile = new MockMultipartFile(imageFile.getName(), new FileInputStream(imageFile));
  216. dto = distinctionService.checkImg(tempMultipartFile,orderId,uidList);
  217. if(dto.getOrderId() ==null){
  218. VaultsResponse<String> vaultsResponse = ossUtils.uploadOneFile(videoFile,orderId);
  219. dto.setUrl(vaultsResponse.getData());
  220. }
  221. tempFile.delete();
  222. imageFile.delete();
  223. }else{
  224. //图片处理
  225. dto = distinctionService.checkImg(file,orderId,uidList);
  226. if(dto.getOrderId() ==null){
  227. VaultsResponse<String> vaultsResponse = ossUtils.uploadOneFile(file,orderId);
  228. dto.setUrl(vaultsResponse.getData());
  229. }
  230. }
  231. return VaultsResponse.success(dto);
  232. } catch (IOException e) {
  233. e.printStackTrace();
  234. }
  235. return VaultsResponse.failed();
  236. }
  237. @PostMapping("/orderFile")
  238. @ApiOperation(value = "工单照片")
  239. @ResponseBody
  240. @OperationLog(type = OperateType.UPDATE, description = "工单照片处理")
  241. @MethodTime
  242. public VaultsResponse orderFile(@RequestBody List<PriceUploadDto> list) {
  243. //处理水印
  244. VaultsResponse<List<PriceUploadDto>> vaultsResponse = watermarkImgService.printingWatermark(list);
  245. if(!vaultsResponse.validate()){
  246. return vaultsResponse;
  247. }
  248. list = vaultsResponse.getData();
  249. //处理照片
  250. watermarkImgService.updateOrderUrl(list);
  251. return VaultsResponse.success();
  252. }
  253. @PostMapping(value = "/pageCardExport")
  254. @ApiOperation(httpMethod = "POST", value = "后台账户查询Excel导出")
  255. public void orderExport(HttpServletResponse resp,@RequestBody MemberPlanPageDTO dto){
  256. String key ="pageCardExport";
  257. String error = this.checkExcelExport(key);
  258. try{
  259. if ("".equals(error)) {
  260. String templateLocation = "classpath:excel/pageCard.xlsx";
  261. String filename = "客户账户.xlsx";
  262. Map<String, Object> data = Maps.newHashMap();
  263. orgConstrainService.fillOrgParams(dto);
  264. List<MemberPlanDTO> list = orderService.pageCardExcel(dto);
  265. ExcelUtils2.easyDownload(templateLocation,data , list, filename, resp);
  266. }else{
  267. throw new BusinessException(error);
  268. }
  269. }catch (Exception e){
  270. e.printStackTrace();
  271. throw new BusinessException(e.getMessage());
  272. }finally {
  273. if(cacheService.exists(key)){
  274. if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){
  275. cacheService.del(key);
  276. }
  277. }
  278. }
  279. }
  280. @PostMapping(value = "/transRechargeExcel")
  281. @ApiOperation(httpMethod = "POST", value = "账户充值Excel导出")
  282. @RepeatSubmit
  283. @MethodTime
  284. public void transRechargeExcel(HttpServletResponse resp, @RequestBody CardWebInDTO dto){
  285. String key ="transRechargeExcel";
  286. String error = this.checkExcelExport(key);
  287. try{
  288. if ("".equals(error)) {
  289. orgConstrainService.fillOrgParams(dto);
  290. Map<String,Object> maps = Maps.newHashMap();
  291. String start = DateFormatUtils.format(dto.getStartDate(), "yyyy-MM");
  292. String end = DateFormatUtils.format(dto.getEndDate(), "yyyy-MM");
  293. if(start.equals(end)){
  294. maps.put("title", start+"充值记录");
  295. }else{
  296. maps.put("title", start+"到"+end+"充值记录");
  297. }
  298. List<CardAccTransRecharge> list = orderService.transRechargeExcel(dto);
  299. ExcelUtils2.easyDownload("classpath:excel/trans_recharge.xlsx",maps, list, "", resp);
  300. }else{
  301. throw new BusinessException(error);
  302. }
  303. }catch (Exception e){
  304. e.printStackTrace();
  305. throw new BusinessException(e.getMessage());
  306. }finally {
  307. if(cacheService.exists(key)){
  308. if(cacheService.get(key).equals(TokenContext.cureWebUser().getUserId().toString())){
  309. cacheService.del(key);
  310. }
  311. }
  312. }
  313. }
  314. }