You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					214 lines
				
				10 KiB
			
		
		
			
		
	
	
					214 lines
				
				10 KiB
			| 
											3 years ago
										 | package com.ynxbd.ali.helper;
 | ||
|  | 
 | ||
|  | import com.alibaba.fastjson.JSONArray;
 | ||
|  | import com.alibaba.fastjson.JSONObject;
 | ||
|  | import com.alipay.api.AlipayClient;
 | ||
|  | import com.alipay.api.domain.CommerceAppUploadRequestContent;
 | ||
|  | import com.alipay.api.request.AlipayCommerceAppAuthUploadRequest;
 | ||
|  | import com.alipay.api.request.AlipayEcoCityserviceCityserviceEnergySendRequest;
 | ||
|  | import com.alipay.api.response.AlipayCommerceAppAuthUploadResponse;
 | ||
|  | import com.alipay.api.response.AlipayEcoCityserviceCityserviceEnergySendResponse;
 | ||
|  | import com.ynxbd.ali.config.AliConfig;
 | ||
|  | import com.ynxbd.ali.enums.AliEnergyEnum;
 | ||
|  | import com.ynxbd.ali.enums.AliMsgEnum;
 | ||
|  | import com.ynxbd.common.bean.Patient;
 | ||
|  | import com.ynxbd.common.helper.common.DateHelper;
 | ||
|  | import com.ynxbd.common.helper.common.JsonHelper;
 | ||
|  | import com.ynxbd.common.result.ServiceException;
 | ||
|  | import com.ynxbd.common.service.PatientService;
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | 
 | ||
|  | import java.math.BigDecimal;
 | ||
|  | import java.math.RoundingMode;
 | ||
|  | import java.util.Date;
 | ||
|  | import java.util.UUID;
 | ||
|  | 
 | ||
|  | @Slf4j
 | ||
|  | public class AliMessageHelper {
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 挂号推送
 | ||
|  |      */
 | ||
|  |     public static void hospitalOrder(String openid, String title, AliMsgEnum msgEnum, Date createOrderTime, boolean isCreate, BigDecimal totalFee, String outTradeNo, String regDate, String regTime, String deptCode, String deptName, String doctName, String link) {
 | ||
|  |         if (!AliConfig.IS_PUSH_MSG) {
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  |         if (!AliHelper.isAliUser(openid)) {
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  |         String accessToken = AliCacheHelper.getHospAccessToken(openid);
 | ||
|  |         if (accessToken == null) {
 | ||
|  |             log.error("[支付宝]消息推送 accessToken为空");
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         Patient patient = new PatientService().getMyself(openid);
 | ||
|  |         if (patient == null || patient.getName() == null) {
 | ||
|  |             log.error("[支付宝]消息推送 trueName为空");
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  |         String trueName = patient.getName();
 | ||
|  |         if (totalFee == null) {
 | ||
|  |             log.error("[支付宝]消息推送 totalFee为空");
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         if (createOrderTime == null) {
 | ||
|  |             log.error("[支付宝]消息推送 createOrderTime为空");
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         String createOrderTimeStr = DateHelper.dateToStr(createOrderTime, DateHelper.DateEnum.yyyy_MM_dd_HH_mm_ss);
 | ||
|  |         if (createOrderTimeStr == null) {
 | ||
|  |             log.error("[支付宝]消息推送 日期转换失败");
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         String updateOrderTime = isCreate ? DateHelper.getMoveSeconds(createOrderTimeStr, 2L) : DateHelper.getMoveSeconds(DateHelper.getCurDateTime(), -6L);
 | ||
|  | 
 | ||
|  |         String totalFeeStr = totalFee.setScale(2, RoundingMode.DOWN).toString();
 | ||
|  | 
 | ||
|  |         AlipayCommerceAppAuthUploadRequest request = new AlipayCommerceAppAuthUploadRequest();
 | ||
|  |         request.setServiceName("alipay.commerce.app.data"); //应用服务名称 固定值 String(256) 不可空
 | ||
|  |         request.setTargetId(AliConfig.HOSP_ID); // 目标用户 String(64) 签约商家 PID 不可空
 | ||
|  | 
 | ||
|  |         CommerceAppUploadRequestContent content = new CommerceAppUploadRequestContent(); //服务数据参数
 | ||
|  |         content.setTenantAppId(AliConfig.TENANT_APP_ID); // 租户应用ID String(64) 支付宝分配不可空
 | ||
|  | 
 | ||
|  |         content.setActivityId("upload_hospital_order"); // 业务流程ID String(64) 不可空 此处固定为“upload_hospital_order”
 | ||
|  | 
 | ||
|  |         JSONObject json_body = new JSONObject(); // 业务流程参数 String(6000) 业务流程请求参数说明
 | ||
|  |         json_body.put("out_biz_no", outTradeNo);  // 医院预约单订单号 唯一不重复(同一 家 ISV 接入的所有 医院的挂号单、检 查号、医药单都不 可重复) String(128)
 | ||
|  |         json_body.put("partner_id", AliConfig.ISV_PARTNER_ID); // ISV的PID,支付宝id(2088 开头)。同一家ISV 请保持一致!
 | ||
|  |         json_body.put("tiny_app_id", AliConfig.APP_ID);  // 医院在支付宝的小程序id
 | ||
|  | 
 | ||
|  |         json_body.put("buyer_id", openid);   // 就诊人 id,授权人id 就诊人在支付宝平 台的 2088 开头 16 位id
 | ||
|  | 
 | ||
|  |         json_body.put("order_create_time", createOrderTimeStr); // 订单创建时间
 | ||
|  |         json_body.put("order_modified_time", updateOrderTime); // 订单修改时间-后移5min(小于当前时间会报错)
 | ||
|  |         json_body.put("amount", totalFeeStr); //订单金额
 | ||
|  |         json_body.put("pay_amount", totalFeeStr); //支付金额
 | ||
|  | //        json_body.put("trade_no", tradeNo);//支付宝交易号
 | ||
|  | 
 | ||
|  |         json_body.put("order_type", "HOSPITAL_ORDER"); // 固定为 HOSPITAL_ORDER
 | ||
|  |         json_body.put("out_biz_type", "HOSPITAL_APPOINTMENT"); // 固定值 HOSPITAL_APPOINTMENT
 | ||
|  |         json_body.put("merchant_order_status", msgEnum.CODE); // 状态 String(64) 枚举详见文档
 | ||
|  | 
 | ||
|  |         JSONArray item_order_list = new JSONArray();
 | ||
|  |         JSONObject item_order = new JSONObject();
 | ||
|  |         item_order.put("item_name", title);//商品名称
 | ||
|  |         item_order.put("quantity", "1"); //商品数量
 | ||
|  |         item_order.put("sku_id", "1");  // 商品 skuId
 | ||
|  |         item_order.put("unit_price", totalFeeStr); //商品单价
 | ||
|  |         item_order_list.add(item_order);
 | ||
|  |         json_body.put("item_order_list", item_order_list);
 | ||
|  | 
 | ||
|  |         JSONObject ext_info = new JSONObject();
 | ||
|  |         ext_info.put("hospital", AliConfig.HOSP_NAME); //医院名称
 | ||
|  |         ext_info.put("hospital_register_id", AliConfig.HOSP_REGISTER_ID); // 医院登记号
 | ||
|  | 
 | ||
|  |         ext_info.put("department", deptName); //就诊科室
 | ||
|  |         ext_info.put("dept_num", deptCode); //诊室编号
 | ||
|  |         ext_info.put("dept_loc", "暂无");//科室位置
 | ||
|  | //        ext_info.put("navigation", "导航地址");//导航地址
 | ||
|  | 
 | ||
|  |         ext_info.put("doctor", doctName); //医生名称
 | ||
|  | //        ext_info.put("doctor_rank", "医生职级"); //医生职级
 | ||
|  | //        ext_info.put("doctor_id", "医生 id");//医生 id
 | ||
|  | //        ext_info.put("doctor_avatar", "医生头像 url");//医生头像 url
 | ||
|  |         ext_info.put("patient", trueName);  // 就诊人  必须与 buy_id 对 应的姓名一致
 | ||
|  | 
 | ||
|  |         if ((DateHelper.isToday(regDate))) { // 今日挂号
 | ||
|  |             if (regTime == null) {
 | ||
|  |                 ext_info.put("scheduled_time", DateHelper.getMoveMinute(DateHelper.getCurDateTime(), 10));
 | ||
|  | 
 | ||
|  |             } else {
 | ||
|  |                 if (DateHelper.isCurTimeOver(regTime)) { // 2
 | ||
|  |                     regTime = DateHelper.getMoveMinute(DateHelper.getCurDateTime(), 2);
 | ||
|  |                 } else {
 | ||
|  |                     String moveDateTime = DateHelper.getMoveMinute(DateHelper.getCurDateTime(), 2);
 | ||
|  |                     regTime = Boolean.TRUE.equals(DateHelper.isDateTimeOver(moveDateTime, regTime)) ?
 | ||
|  |                             moveDateTime : DateHelper.getCurDate() + " " + regTime;
 | ||
|  |                 }
 | ||
|  |                 ext_info.put("scheduled_time", regTime);
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |         } else {
 | ||
|  |             ext_info.put("scheduled_time", regDate + " " + (regTime == null ? "00:00:00" : regTime)); // 预约时间
 | ||
|  |         }
 | ||
|  | 
 | ||
|  | //        ext_info.put("take_num_url", "取号入口URL");   // 取号入口
 | ||
|  | //        ext_info.put("take_num_password", "取号密码"); // 取号密码
 | ||
|  | //        ext_info.put("call_num_url", "叫号进度入口");   // 叫号进度入口
 | ||
|  | //        ext_info.put("medical_order_id", "就诊单 id"); // 就诊单id
 | ||
|  | //        ext_info.put("medical_num", "就诊/检 查序号");  // 就诊/检 查序号
 | ||
|  | //        ext_info.put("merchant_order_link_page", "订单链接"); // 订单链接
 | ||
|  |         ext_info.put("merchant_order_link_page", link); // 订单链接
 | ||
|  | 
 | ||
|  |         json_body.put("ext_info", ext_info);
 | ||
|  | 
 | ||
|  |         content.setBody(JSONObject.toJSONString(json_body));
 | ||
|  |         request.setContent(content);
 | ||
|  | 
 | ||
|  |         log.info("[支付宝-消息推送]入参 accessToken={}, {}", accessToken, JSONObject.toJSONString(request));
 | ||
|  |         AlipayClient alipayClient = AliHelper.getAliClient();
 | ||
|  |         try {
 | ||
|  |             AlipayCommerceAppAuthUploadResponse response = alipayClient.execute(request, accessToken);
 | ||
|  |             if (!response.isSuccess()) {
 | ||
|  |                 log.error("[支付宝]消息推送是失败 code={}, msg={}, subMsg={}", response.getCode(), response.getMsg(), response.getSubMsg());
 | ||
|  |             } else {
 | ||
|  |                 log.info("[支付宝]消息推送成功 code={}, resp={}", response.getCode(), JsonHelper.toJsonString(response.getData()));
 | ||
|  |             }
 | ||
|  |         } catch (Exception e) {
 | ||
|  |             e.printStackTrace();
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 发送能量
 | ||
|  |      *
 | ||
|  |      * @param openid 唯一id
 | ||
|  |      */
 | ||
|  |     public static boolean sendEnergy(String openid, AliEnergyEnum energyEnum) throws ServiceException {
 | ||
|  |         if (!AliConfig.IS_PUSH_MSG) {
 | ||
|  |             throw new ServiceException("[支付宝-能量]功能未开通");
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         if (!AliHelper.isAliUser(openid)) { // 不是支付宝用户|openid为空
 | ||
|  |             throw new ServiceException("[支付宝-能量]用户id错误");
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         String accessToken = AliCacheHelper.getEnergyAccessToken(openid);
 | ||
|  |         if (accessToken == null) {
 | ||
|  |             throw new ServiceException("[支付宝-能量]授权码缺失");
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         JSONObject reqJson = new JSONObject();
 | ||
|  |         reqJson.put("scene", energyEnum.CODE); // 场景
 | ||
|  |         reqJson.put("outer_no", UUID.randomUUID().toString().replace("-", ""));
 | ||
|  | 
 | ||
|  |         JSONObject extInfoJson = new JSONObject();
 | ||
|  |         extInfoJson.put("ext_key", "ITEM_TYPE");
 | ||
|  |         extInfoJson.put("ext_value", "book");
 | ||
|  | 
 | ||
|  |         reqJson.put("ext_info", extInfoJson);
 | ||
|  | 
 | ||
|  |         AlipayClient alipayClient = AliHelper.getAliClient();
 | ||
|  |         AlipayEcoCityserviceCityserviceEnergySendRequest request = new AlipayEcoCityserviceCityserviceEnergySendRequest();
 | ||
|  |         request.setBizContent(JsonHelper.toJsonString(reqJson));
 | ||
|  |         try {
 | ||
|  |             AlipayEcoCityserviceCityserviceEnergySendResponse response = alipayClient.execute(request, accessToken);
 | ||
|  |             if (!response.isSuccess()) {
 | ||
|  |                 log.error("[支付宝-能量]发送失败 code={}, message={}, accessToken={}", response.getCode(), response.getMsg(), accessToken);
 | ||
|  |                 throw new ServiceException("[支付宝-能量]发送失败[" + response.getCode() + "]" + response.getMsg());
 | ||
|  |             }
 | ||
|  |             log.info("[支付宝-能量]发送成功");
 | ||
|  |         } catch (Exception e) {
 | ||
|  |             throw new ServiceException(e.getMessage());
 | ||
|  |         }
 | ||
|  |         return true;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | }
 |