package com.hx.phip.his.controller.his; import com.hx.phip.common.HisUrl; import com.hx.phip.his.config.SystemConfig; import com.hx.phip.dao.mapper.syn.SyncOrderTotalMapper; import com.hx.phip.model.SyncOrderTotal; import com.hx.phip.his.service.CreateTimeNoService; import com.hx.phip.his.service.SyncNewOrderService; import com.hx.phip.his.service.SyncOrderService; import com.hx.phip.his.service.SyncTransactioalService; import com.hx.phip.uti.HisHttpUtil; import com.hx.resultTool.ResponseCode; import com.hx.resultTool.Result; import com.hx.util.ExcelUtil; import com.hx.util.StringUtils; import com.mongodb.BasicDBObject; import lombok.SneakyThrows; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 同步订单 */ @RestController @RequestMapping("/sync/order/toVoid") public class SyncOrderController { private static Logger logger = LoggerFactory.getLogger(SyncOrderController.class); @Resource private SyncOrderService saveHisProject; @Resource private SyncTransactioalService syncTransactioalService; @Resource private CreateTimeNoService createTimeNoService; @Resource private SyncNewOrderService syncNewOrderService; @Resource private SystemConfig systemConfig; @PostMapping("/syncSalesOrderTest") public Result syncSalesOrderTest(@RequestBody JSONObject jsonObject){ System.out.println(createTimeNoService.getOrderNo("SYNC",4, String.valueOf(System.currentTimeMillis()))); return Result.success("初始化同步成功"); } /** 根据订单id获取订单信息*/ @GetMapping("/getOrderById") public Result getOrderById(String orderId) { return saveHisProject.getOrderById(orderId); } /** 同步授权机构下的销售订单摘要数据 - 订单摘要信息*/ @PostMapping("/syncSalesOrder/byId") public Result syncSalesOrderbyId(String orderId){ String at = saveHisProject.getToken(); Map header=new HashMap<>(); header.put("x-access-token",at); StringBuffer url=new StringBuffer(systemConfig.getApiUrl()+ HisUrl.BILLING_SALES_ORDER+"/"+orderId); String result = HisHttpUtil.HttpURLUtilJson(url.toString() ,null, null, header, "GET"); saveHisProject.syncOrderThread(JSONObject.fromObject(result)); return Result.success("初始化同步成功"); } /** 同步授权机构下的销售订单摘要数据 - 订单摘要信息*/ @PostMapping("/syncSalesOrder") public Result syncSalesOrder(@RequestBody JSONObject jsonObject) { try { new Thread(new Runnable() { @Override public void run() { SyncOrderTotal syncOrderTotal=new SyncOrderTotal(1,1,0,0); JSONObject returnJson= saveHisProject.syncSalesOrder(jsonObject,syncOrderTotal); if(returnJson==null){ logger.info("获取初始路径和页面错误"); return; }else { logger.info("获取初始路径和页面成功数据;{}",returnJson.toString()); } //开12个线程池处理其它页 ExecutorService threadPool = Executors.newFixedThreadPool(5); int MaxAgain=20;//定义请求超时重新发起请求最大次数 try { //3648 for(int i = 0;i < returnJson.optInt("totalPage");i ++) { int page=i; threadPool.execute(new Runnable() { public void run() { int num=0; do{ try { Boolean status= syncOrderThread(returnJson.optString("url"),page,syncOrderTotal); if(status){ num=MaxAgain; }else { logger.info(Thread.currentThread().getName()+" 执行第:"+page+" 页 Rtime Out 第"+num+"次"); num++; } }catch (Exception e){ logger.info(Thread.currentThread().getName()+" 执行第:"+page+" 页 Rtime Out 第"+num+"次"); num++; } }while(num header=new HashMap<>(); header.put("x-access-token",at); logger.info("同步订单处理第"+page+"页"); String result = HisHttpUtil.HttpURLUtilJson(url +"&page="+page,null, null, header, "GET"); if(!StringUtils.isEmpty(result)) { JSONObject obj = JSONObject.fromObject(result); if(obj != null) { JSONArray arr = obj.optJSONArray("content"); if(arr != null && arr.size() > 0) { for(int j = 0;j < arr.size();j ++) { try { saveHisProject.syncOrderThread(arr.getJSONObject(j)); syncOrderTotal.setSuccessNumber(syncOrderTotal.getSuccessNumber()+1); } catch (Exception e) { syncOrderTotal.setFailNumber(syncOrderTotal.getFailNumber()+1); logger.error("同步订单出错了:"+ arr.getJSONObject(j).optString("id") ,e); } } } } }else{ logger.info("同步第{}页his接口错误,返回:{}",page,result); return false; } return true; } /** 主动同步创建订单到his - 订单摘要信息*/ @PostMapping("/syncCreateOrder") public Result syncCreateOrder( @RequestBody JSONObject order) { return syncNewOrderService.createOrder(order); } /** 修改用户卡包 - 订单摘要信息*/ @PostMapping("/updateUserCard") public Result updateUserCard( @RequestBody JSONObject order) { return syncNewOrderService.updateUserCard(order); } /** 销售支付,支付后为已支付订单,受理所有订单流程*/ @PostMapping("/syncOrderPayment") public Result syncOrderPayment(@RequestBody JSONObject order) { if(StringUtils.isEmpty(order.optString("orderId"))) { return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"领建订单id必传"); } return syncNewOrderService.syncOrderPayment(order); } /** 创建储值金与预定金订单 - 顾客信息*/ @PostMapping("/prepaidOrder") public Result prepaidOrder(@RequestBody JSONObject jsonObject) { return saveHisProject.prepaidOrder(jsonObject); } /** 销售订单废弃,未支付订单可废弃,废弃后订单不可操作*/ @GetMapping("/syncOrderToVoid") public Result syncOrderToVoid(String orderId) { if(StringUtils.isEmpty(orderId)) { return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"请选择需要作废的订单id"); } return saveHisProject.syncOrderToVoid(orderId); } /** 订单退款,忽略审批流程*/ @PostMapping("/syncOrderRefund") public Result syncOrderRefund(@RequestBody JSONObject order) { return saveHisProject.syncOrderRefund(order); } /** 修改订单电商平台,电商平台订单号,备注*/ @PostMapping("/updateOrder") public Result updateOrder(@RequestBody BasicDBObject order ) { return saveHisProject.updateOrder(order); } /** * 直接修改领健订单为已退款 * @return */ @PostMapping("/refundOrder") public Result refundOrder(@RequestBody BasicDBObject order){ return saveHisProject.refundOrder(order); } /** * 直接修改领健订单为部分退款 * @return */ @PostMapping("/partialRefund") public Result partialRefund(@RequestBody BasicDBObject order){ return saveHisProject.partialRefund(order); } /** * 未支付订单直接作废 * @return */ @PostMapping("/cancelOrder") public Result cancelOrder(@RequestBody BasicDBObject order){ return saveHisProject.cancelOrder(order); } /** * 直接修改领健订单为已支付 * @return */ @PostMapping("/paidOrder") public Result paidOrder(@RequestBody BasicDBObject order){ return saveHisProject.paidOrder(order); } /** * 初始化同步phis创建订单领建结账结账记录 * @return */ @PostMapping("/orderPayRecord") public Result orderPayRecord(){ return saveHisProject.orderPayRecord(); } @PostMapping("/updateOrderOripice") public Result updateOrderOripice(MultipartFile file) throws Exception { List> datas = ExcelUtil.readExcelByeFileDataToXSSF(file,false); StringBuffer url=new StringBuffer(systemConfig.getApiUrl()+ HisUrl.BILLING_SALES_ORDER); /* String at; Map header=new HashMap<>(); String result=null; for (int i = 0; i < datas.size(); i++) { at = saveHisProject.getToken(); header.put("x-access-token",at); logger.info("开始处理第:{}数据", i); try { result = HisHttpUtil.HttpURLUtilJson(url.toString()+"/"+datas.get(i).get(0),null, null, header, "GET"); saveHisProject.syncOrderThread(JSONObject.fromObject(result)); } catch (Exception e) { logger.error("同步订单出错了:"+ datas.get(i).get(0),e); } }*/ //开20个线程池处理其它页 int threadSize=5; ExecutorService threadPool = Executors.newFixedThreadPool(threadSize); try { for (int i = 0; i < datas.size(); i++) { { Integer pageNum = i; threadPool.execute(new Runnable() { public void run() { try { logger.info("开始处理第:{}数据", pageNum); String at = saveHisProject.getToken(); Map header=new HashMap<>(); header.put("x-access-token", at); String result = HisHttpUtil.HttpURLUtilJson(url.toString() + "/" + datas.get(pageNum).get(0), null, null, header, "GET"); saveHisProject.syncOrderThread(JSONObject.fromObject(result)); } catch (Exception e) { logger.error("同步订单出错了:" + datas.get(pageNum).get(0), e); } } }); } } }catch (Exception e){ e.printStackTrace(); }finally { threadPool.shutdown(); } return Result.success(); } }