You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/util/alihealth/AlihealthUtils.java

389 lines
18 KiB
Java

package com.glxp.api.util.alihealth;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import com.glxp.api.constant.AliConstants;
import com.glxp.api.entity.thrsys.ThirdAliDrug;
import com.glxp.api.entity.alihealth.AliKytSearchbill;
import com.glxp.api.req.alihealth.AlihealthKytCommonParametersReqeust;
import com.glxp.api.req.alihealth.AlihealthKytDrugrescodeReqeust;
4 months ago
import com.glxp.api.req.alihealth.AlihealthKytSearchbillReqeust;
import com.glxp.api.req.alihealth.AlihealthKytSinglerelationReqeust;
import com.glxp.api.req.alihealth.local.AlihealthRelCodeInsertReqeust;
import com.glxp.api.req.alihealth.local.AlihealthThirdAliDrugInsertReqeust;
import com.glxp.api.req.alihealth.local.AlihealthKytThirdAliSearchbillInsertReqeust;
import com.glxp.api.res.alihealth.AlihealthKytDrugrescodeResponse;
4 months ago
import com.glxp.api.res.alihealth.AlihealthKytSearchbillResponse;
import com.glxp.api.res.alihealth.AlihealthKytSinglerelationResponse;
import com.glxp.api.service.auth.CustomerInfoService;
import com.glxp.api.service.collect.RelCodeBatchService;
import com.glxp.api.service.collect.RelCodeDetailService;
4 months ago
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
4 months ago
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.lang.reflect.Field;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;
//import static com.taobao.api.internal.util.TaobaoUtils.byte2hex;
4 months ago
@Slf4j
@Component
public class AlihealthUtils {
@Resource
private CustomerInfoService customerInfoService;
@Resource
private AlihealthUtils alihealthUtils;
@Resource
private RelCodeDetailService relCodeDetailService;
@Resource
private RelCodeBatchService relCodeBatchService;
@Value("${ALIHEALTH_URL:http://gw.api.taobao.com/router/rest}")
private String alihealthUrl;
// public static void main(String[] args) {
//
// alihealthKytSinglerelationReqeust.setSign(sign);
// String ss = HttpUtil.get("http://gw.api.taobao.com/router/rest",BeanUtil.beanToMap(alihealthKytSinglerelationReqeust));
// AlihealthKytSinglerelationResponse alihealthKytSinglerelationResponse = new AlihealthKytSinglerelationResponse(ss);
// System.out.println(ss);
// System.out.println(alihealthKytSinglerelationResponse);
//
//// AlibabaAlihealthDrugtraceTopLsydQueryRelationResponse alibabaAlihealthDrugtraceTopLsydQueryRelationResponse
//
// }
4 months ago
/**
*
4 months ago
*
* @param alihealthKytThirdAliSearchbillInsertReqeust
4 months ago
*/
public BaseResponse<List<ThirdAliDrug>> thirdAliSearchbillInsert(AlihealthKytThirdAliSearchbillInsertReqeust alihealthKytThirdAliSearchbillInsertReqeust) {
if (alihealthKytThirdAliSearchbillInsertReqeust.getAlihealthKytSearchbillReqeust() == null) {
4 months ago
return ResultVOUtils.error("入参不能为空");
}
AlihealthKytSearchbillResponse alihealthKytDrugrescodeResponse = null;
AlihealthKytSearchbillReqeust alihealthKytSearchbillReqeust = alihealthKytThirdAliSearchbillInsertReqeust.getAlihealthKytSearchbillReqeust();
4 months ago
if (alihealthKytSearchbillReqeust != null
&& org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthKytSearchbillReqeust.getApp_key())
&& org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthKytThirdAliSearchbillInsertReqeust.getAppSecret())
4 months ago
) {
try {
Map map = alihealthUtils.disposeSign(alihealthKytSearchbillReqeust, alihealthKytThirdAliSearchbillInsertReqeust.getAppSecret());
4 months ago
String json = HttpUtil.get(alihealthUrl, map);
alihealthKytDrugrescodeResponse = new AlihealthKytSearchbillResponse(json);
} catch (Exception e) {
e.printStackTrace();
return ResultVOUtils.error("阿里健康接口调用失败===" + e.getMessage());
}
} else {
return ResultVOUtils.error("阿里健康接口调用失败===用户找不到或者key和秘钥为空");
}
if (alihealthKytDrugrescodeResponse.judgeCall()) {
List<AliKytSearchbill> list = alihealthKytDrugrescodeResponse.disposeThirdAliSearchbill(alihealthKytThirdAliSearchbillInsertReqeust.getErpId());
4 months ago
return ResultVOUtils.success(list);
} else {
return ResultVOUtils.error("阿里健康接口调用失败===" + alihealthKytDrugrescodeResponse.getMsg_info());
4 months ago
}
}
4 months ago
public BaseResponse<List<ThirdAliDrug>> thirdAliDrugInsertAll(AlihealthThirdAliDrugInsertReqeust request) {
// 初始化变量
List<ThirdAliDrug> resultList = new ArrayList<>();
AlihealthKytDrugrescodeReqeust drugRequest = request.getAlihealthKytDrugrescodeReqeust();
String originalName = drugRequest.getPhysic_name();
// 尝试原始名称
BaseResponse<List<ThirdAliDrug>> listBaseResponse = tryInsertAndCollect(request, resultList);
// 处理括号转换
if (containsBrackets(originalName)) {
String convertedName = convertBrackets(originalName);
drugRequest.setPhysic_name(convertedName);
4 months ago
AlihealthThirdAliDrugInsertReqeust tempRequest = new AlihealthThirdAliDrugInsertReqeust();
BeanUtils.copyProperties(request, tempRequest);
tempRequest.getAlihealthKytDrugrescodeReqeust().setPhysic_name(convertedName);
listBaseResponse = tryInsertAndCollect(tempRequest, resultList);
}
// 处理罗马数字转换
if (containsRomanNumerals(originalName)) {
String convertedName = convertRomanNumerals(originalName);
drugRequest.setPhysic_name(convertedName);
4 months ago
AlihealthThirdAliDrugInsertReqeust tempRequest = new AlihealthThirdAliDrugInsertReqeust();
BeanUtils.copyProperties(request, tempRequest);
tempRequest.getAlihealthKytDrugrescodeReqeust().setPhysic_name(convertedName);
listBaseResponse = tryInsertAndCollect(tempRequest, resultList);
}
// 设置结果
if (!resultList.isEmpty()) {
return ResultVOUtils.success(resultList);
}
return listBaseResponse;
}
// 辅助方法
private boolean containsBrackets(String name) {
return name.contains("(") || name.contains("");
}
private String convertBrackets(String name) {
if (name.contains("(")) {
return name.replace("(", "").replace(")", "");
} else {
return name.replace("", "(").replace("", ")");
}
}
private boolean containsRomanNumerals(String name) {
return name.contains("") || name.contains("I")
|| name.contains("Ⅱ") || name.contains("II")
|| name.contains("Ⅲ") || name.contains("III");
}
private String convertRomanNumerals(String name) {
if (name.contains("") || name.contains("Ⅱ") || name.contains("Ⅲ")) {
return name.replace("", "I")
.replace("Ⅱ", "II")
.replace("Ⅲ", "III");
} else {
return name.replace("I", "")
.replace("II", "Ⅱ")
.replace("III", "Ⅲ");
}
}
private BaseResponse<List<ThirdAliDrug>> tryInsertAndCollect(
AlihealthThirdAliDrugInsertReqeust request,
List<ThirdAliDrug> resultList) {
4 months ago
AlihealthThirdAliDrugInsertReqeust alihealthThirdAliDrugInsertReqeust = new AlihealthThirdAliDrugInsertReqeust();
BeanUtil.copyProperties(request, alihealthThirdAliDrugInsertReqeust);
BaseResponse<List<ThirdAliDrug>> response = thirdAliDrugInsert(alihealthThirdAliDrugInsertReqeust);
if (response.getCode() == 20000 && response.getData() != null) {
resultList.addAll(response.getData());
}
return response;
}
4 months ago
/**
*
*
* @param alihealthThirdAliDrugInsertReqeust
*/
public BaseResponse<List<ThirdAliDrug>> thirdAliDrugInsert(AlihealthThirdAliDrugInsertReqeust alihealthThirdAliDrugInsertReqeust) {
if (alihealthThirdAliDrugInsertReqeust.getAlihealthKytDrugrescodeReqeust() == null) {
return ResultVOUtils.error("入参不能为空");
}
AlihealthKytDrugrescodeResponse alihealthKytDrugrescodeResponse = null;
AlihealthKytDrugrescodeReqeust alihealthKytDrugrescodeReqeust = alihealthThirdAliDrugInsertReqeust.getAlihealthKytDrugrescodeReqeust();
if (alihealthKytDrugrescodeReqeust != null
&& org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthKytDrugrescodeReqeust.getApp_key())
&& org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthThirdAliDrugInsertReqeust.getAppSecret())
) {
try {
Thread.sleep(600);
AlihealthKytDrugrescodeReqeust alihealthKytDrugrescodeReqeustNew = new AlihealthKytDrugrescodeReqeust();
BeanUtils.copyProperties(alihealthKytDrugrescodeReqeust,alihealthKytDrugrescodeReqeustNew);
Map map = alihealthUtils.disposeSign(alihealthKytDrugrescodeReqeustNew, alihealthThirdAliDrugInsertReqeust.getAppSecret());
String json = HttpUtil.get(alihealthUrl, map);
log.error("阿里健康接口调用结果===" + json);
alihealthKytDrugrescodeResponse = new AlihealthKytDrugrescodeResponse(json);
} catch (Exception e) {
e.printStackTrace();
return ResultVOUtils.error("阿里健康接口调用失败===" + e.getMessage());
}
} else {
return ResultVOUtils.error("阿里健康接口调用失败===用户找不到或者key和秘钥为空");
}
String physicName = alihealthKytDrugrescodeReqeust.getPhysic_name();
4 months ago
if (alihealthKytDrugrescodeResponse.judgeCall()) {
List<ThirdAliDrug> listNew = alihealthKytDrugrescodeResponse.disposeThirdAliDrugList(alihealthThirdAliDrugInsertReqeust.getErpId());
return ResultVOUtils.success(listNew);
} else {
//
// // 重新上传标识为true就改变一下
// if ("未查询到相关信息".equals(alihealthKytDrugrescodeResponse.getMsg_info())
// && callBool && (physicName.contains("(") || physicName.contains(""))) {
// if (physicName.contains("(")) {
// physicName = physicName.replace("(", "").replace(")", "");
//
// } else if (physicName.contains("")) {
// physicName = physicName.replace("", "(").replace("", ")");
//
// }
// alihealthKytDrugrescodeReqeust.setPhysic_name(physicName);
// alihealthThirdAliDrugInsertReqeust.setAlihealthKytDrugrescodeReqeust(alihealthKytDrugrescodeReqeust);
// thirdAliDrugInsert(alihealthThirdAliDrugInsertReqeust, false);
// }
return ResultVOUtils.error("阿里健康接口调用失败===" + alihealthKytDrugrescodeResponse.getMsg_info());
}
// if(physicName.equals("(")){
// thirdAliDrugInsert();
// }
// if(physicName.equals("")){
// thirdAliDrugInsert();
// }
}
//TODO 调用阿里接口用码进行批量处理只测试了两个包装层级的 暂时没有三个包装层级的数据做测试 到时候有数据之后要好好测试一下
@Transactional(rollbackFor = Exception.class)
public BaseResponse<AlihealthKytSinglerelationResponse> relCodeInsert(AlihealthRelCodeInsertReqeust alihealthRelCodeInsertReqeust) {
AlihealthKytSinglerelationResponse alihealthKytSinglerelationResponse = null;
if (alihealthRelCodeInsertReqeust != null
&& org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthRelCodeInsertReqeust.getAppKey())
&& org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthRelCodeInsertReqeust.getAppSecret())
) {
try {
AlihealthKytSinglerelationReqeust alihealthKytSinglerelationReqeust = new AlihealthKytSinglerelationReqeust();
alihealthKytSinglerelationReqeust.setRef_ent_id(alihealthRelCodeInsertReqeust.getRefEntId());
alihealthKytSinglerelationReqeust.setDes_ref_ent_id(alihealthRelCodeInsertReqeust.getRefEntId());
alihealthKytSinglerelationReqeust.setCode(alihealthRelCodeInsertReqeust.getCode());
alihealthKytSinglerelationReqeust.setApp_key(alihealthRelCodeInsertReqeust.getAppKey());
Map map = alihealthUtils.disposeSign(alihealthKytSinglerelationReqeust, alihealthRelCodeInsertReqeust.getAppSecret());
String json = HttpUtil.get(alihealthUrl, map);
alihealthKytSinglerelationResponse = new AlihealthKytSinglerelationResponse(json);
} catch (Exception e) {
return ResultVOUtils.error("阿里健康接口调用失败===" + e.getMessage());
}
} else {
return ResultVOUtils.error("阿里健康接口调用失败===用户找不到或者key和秘钥为空");
}
if (alihealthKytSinglerelationResponse.judgeCall()) {
alihealthKytSinglerelationResponse.setRefEntId(alihealthRelCodeInsertReqeust.getRefEntId());
return ResultVOUtils.success(alihealthKytSinglerelationResponse);
} else {
return ResultVOUtils.error("阿里健康接口调用失败===" + alihealthKytSinglerelationResponse.getMsg_info());
}
}
public Map disposeSign(AlihealthKytCommonParametersReqeust alihealthKytCommonParametersReqeust, String secret) {
alihealthKytCommonParametersReqeust.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
Map<String, Object> map = BeanUtil.beanToMap(alihealthKytCommonParametersReqeust);
String sign;
try {
4 months ago
sign = this.signTopRequest(map, secret, "md5");
4 months ago
log.info("sign====" + sign);
alihealthKytCommonParametersReqeust.setSign(sign);
} catch (IOException e) {
4 months ago
e.printStackTrace();
}
4 months ago
return BeanUtil.beanToMap(alihealthKytCommonParametersReqeust);
}
public String signTopRequest(Map<String, Object> params, String secret, String signMethod) throws IOException {
// 第一步:检查参数是否已经排序
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
if (AliConstants.SIGN_METHOD_MD5.equals(signMethod)) { //签名的摘要算法可选值为hmacmd5hmac-sha256
query.append(secret);
}
for (String key : keys) {
Object value = params.get(key);
if (value instanceof String) {
if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty((CharSequence) value)) {
query.append(key).append(value);
}
} else {
if (StringUtils.isNotEmpty(key) && !Objects.isNull(value)) {
query.append(key).append(value);
}
}
// StringUtils.areNotEmpty(key, value)
}
// 第三步使用MD5/HMAC加密
byte[] bytes;
if (AliConstants.SIGN_METHOD_HMAC.equals(signMethod)) {
bytes = encryptHMAC(query.toString(), secret);
} else {
query.append(secret);
bytes = encryptMD5(query.toString());
}
// 第四步:把二进制转化为大写的十六进制(正确签名应该为32大写字符串此方法需要时使用)
return byte2hex(bytes);
}
public static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for(int i = 0; i < bytes.length; ++i) {
String hex = Integer.toHexString(bytes[i] & 255);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
public byte[] encryptHMAC(String data, String secret) throws IOException {
byte[] bytes = null;
try {
SecretKey secretKey = new SecretKeySpec(secret.getBytes(AliConstants.CHARSET_UTF8), "HmacMD5");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
bytes = mac.doFinal(data.getBytes(AliConstants.CHARSET_UTF8));
} catch (GeneralSecurityException gse) {
throw new IOException(gse.toString());
}
return bytes;
}
public byte[] encryptMD5(String data) throws IOException {
return encryptMD5(data.getBytes(AliConstants.CHARSET_UTF8));
}
public byte[] encryptMD5(byte[] data) throws IOException {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
bytes = md.digest(data);
} catch (GeneralSecurityException gse) {
throw new IOException(gse.toString());
}
return bytes;
}
}