|
@ -0,0 +1,411 @@
|
|
|
package com.yihu.jw.door.util;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
|
|
|
import com.yihu.jw.entity.base.wx.BasePatientWechatDo;
|
|
|
import com.yihu.jw.entity.base.wx.WxAccessTokenDO;
|
|
|
import com.yihu.jw.entity.base.wx.WxTemplateConfigDO;
|
|
|
import com.yihu.jw.entity.base.wx.WxWechatDO;
|
|
|
import com.yihu.jw.entity.hospital.message.SystemMessageDO;
|
|
|
import com.yihu.jw.exception.ApiException;
|
|
|
import com.yihu.jw.exception.code.ExceptionCode;
|
|
|
import com.yihu.jw.rm.base.WechatRequestMapping;
|
|
|
import com.yihu.jw.util.common.IdCardUtil;
|
|
|
import com.yihu.jw.util.date.DateUtil;
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
import com.yihu.jw.util.wechat.WeixinMessagePushUtils;
|
|
|
import com.yihu.jw.util.wechat.wxhttp.HttpUtil;
|
|
|
import com.yihu.jw.wechat.dao.BasePatientWechatDao;
|
|
|
import com.yihu.jw.wechat.dao.WxTemplateConfigDao;
|
|
|
import com.yihu.jw.wechat.service.WxAccessTokenService;
|
|
|
import com.yihu.utils.network.HttpUtils;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by liub on 2020/12/28.
|
|
|
*/
|
|
|
@Component
|
|
|
public class MessageUtil {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(MessageUtil.class);
|
|
|
|
|
|
@Autowired
|
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
|
@Value("${hospital.url}")
|
|
|
private String serverUrl;
|
|
|
@Autowired
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
@Autowired
|
|
|
private WeixinMessagePushUtils weixinMessagePushUtils;
|
|
|
@Autowired
|
|
|
private WxTemplateConfigDao wxTemplateConfigDao;
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
//发送微信模板消息
|
|
|
public static String sendMessageUrl ="http://172.16.100.37:8090/hospitalPortal-sms/sms/sendMessage";
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param wxId
|
|
|
* @param patient 患者id
|
|
|
* @param cardNo
|
|
|
* @param first
|
|
|
* @param noticeContent
|
|
|
* @param remark
|
|
|
* @param redirectUrl
|
|
|
* @return
|
|
|
*/
|
|
|
public String sendWXMes(String wxId,String patient,String cardNo,String first,String noticeContent,String remark,String redirectUrl){
|
|
|
String msg="first:"+first+"contentMsg:"+noticeContent+"remark:"+remark;
|
|
|
logger.info("发送的信息="+msg);
|
|
|
JSONObject params = new JSONObject();
|
|
|
params.put("transType","sms.hospital.notice");
|
|
|
params.put("merchId","3501000014");
|
|
|
JSONObject p = new JSONObject();
|
|
|
String openId = "";
|
|
|
if(StringUtils.isNotBlank(patient)){
|
|
|
String sql = "select * from base.base_patient_wechat where wechat_id='"+wxId+"'and patient_id='"+patient+"' ";
|
|
|
List<BasePatientWechatDo> paientWechatDos = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BasePatientWechatDo.class));
|
|
|
if(paientWechatDos!=null&&paientWechatDos.size()>0){
|
|
|
openId = paientWechatDos.get(0).getOpenid();
|
|
|
p.put("openId",openId);
|
|
|
}
|
|
|
}else {
|
|
|
p.put("cardNo",cardNo);
|
|
|
|
|
|
}
|
|
|
p.put("first",first);
|
|
|
p.put("noticeTime", DateUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
p.put("noticeContent",noticeContent);
|
|
|
if(StringUtils.isNotBlank(redirectUrl)){
|
|
|
p.put("redirectUrl",redirectUrl);
|
|
|
}
|
|
|
p.put("remark",remark);
|
|
|
params.put("param",p);
|
|
|
|
|
|
logger.info("params :"+params.toString());
|
|
|
|
|
|
if(StringUtils.isNotBlank(openId)||StringUtils.isNotBlank(cardNo)){
|
|
|
String rs = HttpUtil.sendPost(sendMessageUrl,params.toJSONString());
|
|
|
JSONObject rsJson = JSON.parseObject(rs);
|
|
|
String resCode = rsJson.getString("respCode");
|
|
|
if("000000".equals(resCode)){
|
|
|
return "1";
|
|
|
}
|
|
|
return "0";
|
|
|
}else {
|
|
|
return "-1";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String ehospitalNotice(String userName, String idCard, String phone, String title, String content, String contentString,String url) {
|
|
|
String msg="first:"+title+"contentMsg:"+content+"remark:"+contentString;
|
|
|
logger.info("发送的信息="+msg);
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put("userName", userName);
|
|
|
jsonObject.put("idCard", idCard);
|
|
|
jsonObject.put("phone", phone);
|
|
|
jsonObject.put("title", title);
|
|
|
jsonObject.put("url", url);
|
|
|
jsonObject.put("content", content);
|
|
|
jsonObject.put("contentString", contentString);
|
|
|
System.out.println(serverUrl);
|
|
|
String responseMsg = httpClientUtil.sendPost(serverUrl + "/interface/ehospitalNoticePush.htm", jsonObject.toString());
|
|
|
logger.info("ehospitalNoticePushResult:" + responseMsg);
|
|
|
return responseMsg;
|
|
|
}
|
|
|
|
|
|
public void putTemplateWxMessage(String wechatId,String templateName,String scene,String openId,String first,String remark,String url,String ...keywords){
|
|
|
WxAccessTokenDO wxAccessTokenDO = getWxAccessTokenById(wechatId);
|
|
|
if (wxAccessTokenDO==null){
|
|
|
return;
|
|
|
}
|
|
|
WxTemplateConfigDO newConfig = new WxTemplateConfigDO();
|
|
|
String sql="select w.* from base.wx_template_config w where w.wechat_id='"+wechatId+"' and w.template_name='"+templateName+"' and w.scene='"+scene+"' and w.status=1";
|
|
|
List<WxTemplateConfigDO> configList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(WxTemplateConfigDO.class));
|
|
|
WxTemplateConfigDO config =null;
|
|
|
if (configList.size()>=0){
|
|
|
config = configList.get(0);
|
|
|
}
|
|
|
BeanUtils.copyProperties(config, newConfig);
|
|
|
if (StringUtils.isNoneBlank(url)){
|
|
|
newConfig.setUrl(url);
|
|
|
}
|
|
|
if (StringUtils.isNoneBlank(remark)){
|
|
|
newConfig.setRemark(remark);
|
|
|
}
|
|
|
newConfig.setFirst(first);
|
|
|
int keyLength = keywords.length;
|
|
|
if (keyLength>=1){
|
|
|
newConfig.setKeyword1(keywords[0]);
|
|
|
}
|
|
|
if (keyLength>=2){
|
|
|
newConfig.setKeyword1(keywords[1]);
|
|
|
}
|
|
|
if (keyLength>=3){
|
|
|
newConfig.setKeyword1(keywords[2]);
|
|
|
}
|
|
|
if (keyLength>=4){
|
|
|
newConfig.setKeyword1(keywords[3]);
|
|
|
}
|
|
|
if (keyLength>=5){
|
|
|
newConfig.setKeyword1(keywords[4]);
|
|
|
}
|
|
|
if (keyLength>=6){
|
|
|
newConfig.setKeyword1(keywords[5]);
|
|
|
}
|
|
|
if (keyLength>=7){
|
|
|
newConfig.setKeyword1(keywords[6]);
|
|
|
}
|
|
|
//发起微信消息模板推送
|
|
|
weixinMessagePushUtils.putWxMsg(wxAccessTokenDO.getAccessToken(), openId, newConfig);
|
|
|
}
|
|
|
|
|
|
public WxAccessTokenDO getWxAccessTokenById(String wechatId) {
|
|
|
try {
|
|
|
//根据wechatCode查找出appid和appSecret
|
|
|
String sql ="select * from base.wx_wechat w where w.id = '"+wechatId+"' and w.status!=-1";
|
|
|
List<WxWechatDO> wxWechatList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(WxWechatDO.class));
|
|
|
WxWechatDO wxWechat=null;
|
|
|
if (wxWechatList.size()>0){
|
|
|
wxWechat = wxWechatList.get(0);
|
|
|
}
|
|
|
sql="select * from base.wx_access_token w where w.wechat_id ='"+wechatId+"' order by w.add_timestamp desc ";
|
|
|
List<WxAccessTokenDO> wxAccessTokens = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(WxAccessTokenDO.class));
|
|
|
if(wxWechat==null){
|
|
|
throw new ApiException(WechatRequestMapping.WxConfig.message_fail_wxWechat_is_no_exist, ExceptionCode.common_error_params_code);
|
|
|
}
|
|
|
if(wxAccessTokens!=null&&wxAccessTokens.size()>0){
|
|
|
for (WxAccessTokenDO accessToken : wxAccessTokens) {
|
|
|
if ((System.currentTimeMillis() - accessToken.getAddTimestamp()) < (accessToken.getExpiresIn() * 500)) {
|
|
|
return accessToken;
|
|
|
} else {
|
|
|
sql="DELETE from base.wx_access_token where wechat_id='"+accessToken.getWechatId()+"' and access_token='"+accessToken.getAccessToken()+"'";
|
|
|
jdbcTemplate.execute(sql);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
String token_url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
String appId="";
|
|
|
String appSecret="";
|
|
|
appId = wxWechat.getAppId();
|
|
|
appSecret = wxWechat.getAppSecret();
|
|
|
if (org.springframework.util.StringUtils.isEmpty(appId)){
|
|
|
throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appId_is_null, ExceptionCode.common_error_params_code);
|
|
|
}
|
|
|
if (org.springframework.util.StringUtils.isEmpty(appSecret)){
|
|
|
throw new ApiException(WechatRequestMapping.WxConfig.message_fail_appSecret_is_null, ExceptionCode.common_error_params_code);
|
|
|
}
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
|
params.put("grant_type", "client_credential");
|
|
|
params.put("appid", appId);
|
|
|
params.put("secret", appSecret);
|
|
|
String result = HttpUtils.doGet(token_url, params).getContent();
|
|
|
logger.info("--------------wechat token return:"+result+"---------------");
|
|
|
org.json.JSONObject json = new org.json.JSONObject(result);
|
|
|
if (json.has("access_token")) {
|
|
|
sql="INSERT INTO `base`.`wx_access_token` (`id`, `wechat_id`, `access_token`, `add_timestamp`, `expires_in`, `czrq`, `code`) \n" +
|
|
|
"VALUES ('?1', '?2', '?3', '?4', '?5', '?6', '?7') ";
|
|
|
String token = json.get("access_token").toString();
|
|
|
String expires_in = json.get("expires_in").toString();
|
|
|
WxAccessTokenDO newaccessToken = new WxAccessTokenDO();
|
|
|
newaccessToken.setAccessToken(token);
|
|
|
newaccessToken.setExpiresIn(Long.parseLong(expires_in));
|
|
|
newaccessToken.setAddTimestamp(System.currentTimeMillis());
|
|
|
newaccessToken.setCzrq(new Date());
|
|
|
newaccessToken.setCode(UUID.randomUUID().toString().replace("-",""));
|
|
|
newaccessToken.setWechatId(wechatId);
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
sql.replace("?1",UUID.randomUUID().toString().replace("-",""));
|
|
|
sql.replace("?2",newaccessToken.getWechatId());
|
|
|
sql.replace("?3",newaccessToken.getAccessToken());
|
|
|
sql.replace("?4",newaccessToken.getAddTimestamp()+"");
|
|
|
sql.replace("?5",newaccessToken.getExpiresIn()+"");
|
|
|
sql.replace("?6",format.format(newaccessToken.getCzrq()));
|
|
|
sql.replace("?7",newaccessToken.getCode());
|
|
|
jdbcTemplate.execute(sql);
|
|
|
return newaccessToken;
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
public SystemMessageDO saveSystemMessage(String messageId,String relationCode,String title,String type,String sender,String senderName,String receiver,String receiverName,String idCard,String msg,String over) {
|
|
|
SystemMessageDO messageDO = new SystemMessageDO();
|
|
|
if (StringUtils.isBlank(messageId)){
|
|
|
messageDO.setId(UUID.randomUUID().toString().replace("-",""));
|
|
|
}
|
|
|
messageDO.setTitle(title);
|
|
|
messageDO.setType(type);
|
|
|
messageDO.setSender(sender);
|
|
|
messageDO.setSenderName(senderName);
|
|
|
messageDO.setRelationCode(relationCode);
|
|
|
messageDO.setReceiver(receiver);
|
|
|
messageDO.setReceiverName(receiverName);
|
|
|
net.sf.json.JSONObject data = new net.sf.json.JSONObject();
|
|
|
data.put("name", receiverName);
|
|
|
data.put("age", IdCardUtil.getAgeForIdcard(idCard));
|
|
|
try {
|
|
|
data.put("gender", IdCardUtil.getSexForIdcard(idCard));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
data.put("msg", msg);
|
|
|
messageDO.setData(data.toString());
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
Date date = messageDO.getCreateTime();
|
|
|
String dateTime=format.format(date);
|
|
|
String sql="INSERT INTO `base`.`base_system_message` (`id`, `type`, `title`, `sender`, `sender_name`, `receiver`, `receiver_name`, `relation_code`, `data`, `is_read`, `del`, `create_time`,`over`) \n" +
|
|
|
"VALUES ('?!', '?2', '?3', '?4', '?5', '?6', '?7', '?8', '?9', '0', '1', '?10')";
|
|
|
messageDO.setCreateTime(date);
|
|
|
sql.replace("?1",messageDO.getId());
|
|
|
sql.replace("?2",messageDO.getType());
|
|
|
sql.replace("?3",messageDO.getTitle());
|
|
|
sql.replace("?4",messageDO.getSender());
|
|
|
sql.replace("?5",messageDO.getSenderName());
|
|
|
sql.replace("?6",messageDO.getReceiver());
|
|
|
sql.replace("?7",messageDO.getReceiverName());
|
|
|
sql.replace("?8",messageDO.getRelationCode());
|
|
|
sql.replace("?9",messageDO.getData());
|
|
|
sql.replace("?10",dateTime);
|
|
|
sql.replace("?11",over);
|
|
|
jdbcTemplate.execute(sql);
|
|
|
return messageDO;
|
|
|
}
|
|
|
|
|
|
public SystemMessageDO saveSystemMessage(SystemMessageDO messageDO){
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
if (StringUtils.isBlank(messageDO.getId())){
|
|
|
messageDO.setId(UUID.randomUUID().toString().replace("-",""));
|
|
|
}
|
|
|
Date date = messageDO.getCreateTime();
|
|
|
String dateTime=format.format(date);
|
|
|
String sql="INSERT INTO `base`.`base_system_message` (`id`, `type`, `title`, `sender`, `sender_name`, `receiver`, `receiver_name`, `relation_code`, `data`, `is_read`, `del`, `create_time`,`over`) \n" +
|
|
|
"VALUES ('?!', '?2', '?3', '?4', '?5', '?6', '?7', '?8', '?9', '0', '1', '?10','?11')";
|
|
|
messageDO.setCreateTime(date);
|
|
|
sql.replace("?1",messageDO.getId());
|
|
|
sql.replace("?2",messageDO.getType());
|
|
|
sql.replace("?3",messageDO.getTitle());
|
|
|
sql.replace("?4",messageDO.getSender());
|
|
|
sql.replace("?5",messageDO.getSenderName());
|
|
|
sql.replace("?6",messageDO.getReceiver());
|
|
|
sql.replace("?7",messageDO.getReceiverName());
|
|
|
sql.replace("?8",messageDO.getRelationCode());
|
|
|
sql.replace("?9",messageDO.getData());
|
|
|
sql.replace("?10",dateTime);
|
|
|
sql.replace("?11",messageDO.getOver());
|
|
|
jdbcTemplate.execute(sql);
|
|
|
return messageDO;
|
|
|
}
|
|
|
|
|
|
// public void updateMessageByList(String orderId,String typeCondition,String title) {
|
|
|
// List<SystemMessageDO> messages = findSystemMesage(orderId, "403,407");
|
|
|
// if (CollectionUtils.isEmpty(messages)) {
|
|
|
// logger.error("当前工单没有医生待接单消息!orderId:" + orderId);
|
|
|
// } else {
|
|
|
// messages.forEach(message -> {
|
|
|
// orderMessageDel(message.getId(), message.getRelationCode(), message.getType(), message.getReceiver());
|
|
|
// if (message.getType().equals("403")) {
|
|
|
// message.setTitle("医生接单");
|
|
|
// message.setType(434 + "");
|
|
|
// message.setReceiver(doorServiceOrder.getDispatcher());
|
|
|
// message.setReceiverName(doorServiceOrder.getDispatcherName());
|
|
|
// message.setSender(doorServiceOrder.getDoctor());
|
|
|
// message.setSenderName(doorServiceOrder.getDoctorName());
|
|
|
// message.setCreateTime(new Date());
|
|
|
// JSONObject data = JSONObject.parseObject(message.getData());
|
|
|
// String msg = doorServiceOrder.getDispatcherName() + "接受了服务工单" + doorServiceOrder.getNumber();
|
|
|
// BaseDoctorDO baseDoctorDO = baseDoctorDao.findById(doorServiceOrder.getDispatcher());//·
|
|
|
// data.put("name", message.getReceiverName());
|
|
|
// data.put("age", IdCardUtil.getAgeForIdcard(baseDoctorDO.getIdcard()));
|
|
|
// try {
|
|
|
// data.put("gender", IdCardUtil.getSexForIdcard(baseDoctorDO.getIdcard()));
|
|
|
// } catch (Exception e) {
|
|
|
// e.printStackTrace();
|
|
|
// }
|
|
|
// data.put("msg", msg);
|
|
|
// message.setData(data.toJSONString());
|
|
|
// }
|
|
|
//// else{
|
|
|
//// message.setOver("0");
|
|
|
//// }
|
|
|
// saveSystemMessage(message);
|
|
|
//
|
|
|
// });
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
public List<SystemMessageDO> findSystemMesage(String relationCode,String type){
|
|
|
StringBuilder sql =new StringBuilder("select * from base.base_system_message where 1=1 ");
|
|
|
if (StringUtils.isNoneBlank(relationCode)){
|
|
|
sql.append(" and relation_code='"+relationCode+"' ");
|
|
|
}
|
|
|
if (StringUtils.isNoneBlank(type)){
|
|
|
sql.append(" and type in ('").append(type.replace(",","','")).append("') ");
|
|
|
}
|
|
|
List<SystemMessageDO> list = jdbcTemplate.query(sql.toString(),new BeanPropertyRowMapper<>(SystemMessageDO.class));
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
public void orderMessageDel(String id,String relationCode,String type,String receiver){
|
|
|
StringBuilder sql = new StringBuilder(" DELETE from base.base_system_message where 1=1 ");
|
|
|
int runStatus=0;
|
|
|
if (StringUtils.isNoneBlank(id)){
|
|
|
sql.append(" and id='"+id+"' ");
|
|
|
runStatus=1;
|
|
|
}
|
|
|
if (StringUtils.isNoneBlank(relationCode)){
|
|
|
sql.append(" and relation_code='"+relationCode+"' ");
|
|
|
runStatus=1;
|
|
|
}
|
|
|
if (StringUtils.isNoneBlank(type)){
|
|
|
sql.append(" and type='"+type+"' ");
|
|
|
runStatus=1;
|
|
|
}
|
|
|
if (StringUtils.isNoneBlank(receiver)){
|
|
|
sql.append(" and receiver='"+receiver+"' ");
|
|
|
runStatus=1;
|
|
|
}
|
|
|
if (runStatus==1){
|
|
|
jdbcTemplate.execute(sql.toString());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void test(){
|
|
|
String sql="select w.* from base.wx_template_config w where w.wechat_id='xm_ykyy_wx' and w.template_name='template_evaluate_notice' and w.scene='fwqjtx' and w.status=1";
|
|
|
List<Map<String,Object>> config = jdbcTemplate.queryForList(sql);
|
|
|
// wxTemplateConfigDao.findByWechatIdAndTemplateNameAndSceneAndStatus("xm_ykyy_wx", "template_pay_notice_jz", "mzxxtx", 1)
|
|
|
System.out.println("!");
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args){
|
|
|
try {
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
System.out.println(format.format(new Date()));
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
System.out.println("1");
|
|
|
}
|
|
|
}
|