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

192 lines
8.5 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.glxp.api.util.alihealth;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import com.glxp.api.dao.collect.RelCodeDetailMapper;
import com.glxp.api.entity.auth.CustomerInfoEntity;
import com.glxp.api.entity.collect.RelCodeBatch;
import com.glxp.api.entity.collect.RelCodeDetail;
import com.glxp.api.req.alihealth.AlihealthKytCommonParametersReqeust;
import com.glxp.api.req.alihealth.AlihealthKytGetentinfoReqeust;
import com.glxp.api.req.alihealth.AlihealthKytSinglerelationReqeust;
import com.glxp.api.res.alihealth.AlihealthKytCommonParametersResponse;
import com.glxp.api.res.alihealth.AlihealthKytGetentinfoResponse;
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;
import com.taobao.api.Constants;
import com.taobao.api.internal.util.StringUtils;
import com.taobao.api.response.AlibabaAlihealthDrugtraceTopLsydQueryRelationResponse;
import io.swagger.models.auth.In;
import org.apache.poi.ss.formula.functions.T;
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.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.taobao.api.internal.util.TaobaoUtils.byte2hex;
@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
//
// }
@Transactional(rollbackFor = Exception.class)
public BaseResponse relCodeInsert(String customerId , String code){
CustomerInfoEntity customerInfoEntity = customerInfoService.selectById(customerId);
AlihealthKytSinglerelationResponse alihealthKytGetentinfoResponse = null;
if(customerInfoEntity != null){
try {
AlihealthKytSinglerelationReqeust alihealthKytSinglerelationReqeust =new AlihealthKytSinglerelationReqeust();
alihealthKytSinglerelationReqeust.setRef_ent_id(customerInfoEntity.getRefEntId());
alihealthKytSinglerelationReqeust.setDes_ref_ent_id(customerInfoEntity.getRefEntId());
alihealthKytSinglerelationReqeust.setCode(code);
alihealthKytSinglerelationReqeust.setApp_key(customerInfoEntity.getAppKey());
Map map = alihealthUtils.disposeSign(alihealthKytSinglerelationReqeust,customerInfoEntity.getAppSecret());
String json = HttpUtil.get(alihealthUrl,map);
alihealthKytGetentinfoResponse =new AlihealthKytSinglerelationResponse(json);
}catch (Exception e){
return ResultVOUtils.error("阿里健康接口调用失败===请检查厂商的key和密文是否对");
}
}
if(org.apache.commons.lang3.StringUtils.isNotEmpty(alihealthKytGetentinfoResponse.getMsg_info())
&& alihealthKytGetentinfoResponse.getMsg_info().equals("调用成功")
){
RelCodeBatch relCodeBatch = alihealthKytGetentinfoResponse.disposeRelCodeBatch( customerId);
RelCodeBatch relCodeBatchOne = relCodeBatchService.getOne(new QueryWrapper<RelCodeBatch>().eq("productCode",relCodeBatch.getProductCode())
.last("limit 1")
);
if(Objects.isNull(relCodeBatchOne)){
relCodeBatch.setCreateUser(customerId);
relCodeBatch.setCreateTime(new Date());
relCodeBatchService.save(relCodeBatch);
}else {
relCodeBatchService.update(relCodeBatch,new QueryWrapper<RelCodeBatch>().eq("productCode",relCodeBatch.getProductCode()));
}
Integer id = Objects.isNull(relCodeBatch.getId()) ? relCodeBatchOne.getId() :relCodeBatch.getId();
// 进行查询
List<RelCodeDetail> list = alihealthKytGetentinfoResponse.disposeRelRodeDetailList(id);
if(list!=null && list.size()>0){
List curCodeList = new ArrayList();
for (RelCodeDetail relCodeDetail : list) {
curCodeList.add(relCodeDetail.getCurCode());
}
relCodeDetailService.remove(new QueryWrapper<RelCodeDetail>().in("curCode",curCodeList));
relCodeDetailService.saveBatch(list);
}
}else {
return ResultVOUtils.error("阿里健康接口调用失败==="+alihealthKytGetentinfoResponse.getMsg_info());
}
return ResultVOUtils.success("码关联关系获取成功");
}
public Map disposeSign(AlihealthKytCommonParametersReqeust alihealthKytCommonParametersReqeust, String secret){
Map map = BeanUtil.beanToMap(alihealthKytCommonParametersReqeust);
String sign;
try {
sign = new AlihealthUtils().signTopRequest(map, secret, "md5");
alihealthKytCommonParametersReqeust.setSign(sign);
return BeanUtil.beanToMap(alihealthKytCommonParametersReqeust);
} catch (IOException e) {
}
return null;
}
public String signTopRequest(Map<String, String> params, String secret, String signMethod) throws IOException {
// 第一步:检查参数是否已经排序
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
if (Constants.SIGN_METHOD_MD5.equals(signMethod)) { //签名的摘要算法可选值为hmacmd5hmac-sha256
query.append(secret);
}
for (String key : keys) {
String value = params.get(key);
if (StringUtils.areNotEmpty(key, value)) {
query.append(key).append(value);
}
}
// 第三步使用MD5/HMAC加密
byte[] bytes;
if (Constants.SIGN_METHOD_HMAC.equals(signMethod)) {
bytes = encryptHMAC(query.toString(), secret);
} else {
query.append(secret);
bytes = encryptMD5(query.toString());
}
// 第四步:把二进制转化为大写的十六进制(正确签名应该为32大写字符串此方法需要时使用)
return byte2hex(bytes);
}
public byte[] encryptHMAC(String data, String secret) throws IOException {
byte[] bytes = null;
try {
SecretKey secretKey = new SecretKeySpec(secret.getBytes(Constants.CHARSET_UTF8), "HmacMD5");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
bytes = mac.doFinal(data.getBytes(Constants.CHARSET_UTF8));
} catch (GeneralSecurityException gse) {
throw new IOException(gse.toString());
}
return bytes;
}
public byte[] encryptMD5(String data) throws IOException {
return encryptMD5(data.getBytes(Constants.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;
}
}