Authored by tanling

小程序消息模板

package com.yoho.message.dal;
import com.yoho.message.dal.model.MiniappMsgForm;
public interface IMiniappMsgFormDAO {
int deleteByPrimaryKey(Integer id);
int insert(MiniappMsgForm record);
int insertSelective(MiniappMsgForm record);
MiniappMsgForm selectByPrimaryKey(Integer id);
MiniappMsgForm selectByOrderCode(String orderCode);
MiniappMsgForm selectLastedAvailabByOrderCode(String orderCode);
int updateByPrimaryKeySelective(MiniappMsgForm record);
int updateByPrimaryKey(MiniappMsgForm record);
}
\ No newline at end of file
... ...
package com.yoho.message.dal.model;
public class MiniappMsgForm {
private Integer id;
private String formId;
private Integer availableTimes;
private Integer createTime;
private String openId;
private String orderCode;
private Integer miniappType;
private Integer uid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFormId() {
return formId;
}
public void setFormId(String formId) {
this.formId = formId == null ? null : formId.trim();
}
public Integer getAvailableTimes() {
return availableTimes;
}
public void setAvailableTimes(Integer availableTimes) {
this.availableTimes = availableTimes;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId == null ? null : openId.trim();
}
public String getOrderCode() {
return orderCode;
}
public void setOrderCode(String orderCode) {
this.orderCode = orderCode;
}
public Integer getMiniappType() {
return miniappType;
}
public void setMiniappType(Integer miniappType) {
this.miniappType = miniappType;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
}
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yoho.message.dal.IMiniappMsgFormDAO" >
<resultMap id="BaseResultMap" type="com.yoho.message.dal.model.MiniappMsgForm" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="form_id" property="formId" jdbcType="VARCHAR" />
<result column="available_times" property="availableTimes" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="open_id" property="openId" jdbcType="VARCHAR" />
<result column="order_code" property="orderCode" jdbcType="VARCHAR" />
<result column="miniapp_type" property="miniappType" jdbcType="INTEGER" />
<result column="uid" property="uid" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, form_id, available_times, create_time, open_id, order_code, miniapp_type, uid
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from miniapp_msg_form
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByOrderCode" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from miniapp_msg_form
where order_code = #{orderCode,jdbcType=VARCHAR}
order by create_time
limit 1
</select>
<select id="selectLastedAvailabByOrderCode" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from miniapp_msg_form
where order_code = #{orderCode,jdbcType=VARCHAR}
and available_times > 0
and create_time > unix_timestamp(now()) - 7*24*60*60
order by create_time
limit 1
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from miniapp_msg_form
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.message.dal.model.MiniappMsgForm" >
insert into miniapp_msg_form (id, form_id, available_times,
create_time, open_id, order_code,
miniapp_type, uid)
values (#{id,jdbcType=INTEGER}, #{formId,jdbcType=VARCHAR}, #{availableTimes,jdbcType=INTEGER},
#{createTime,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}, #{orderCode,jdbcType=VARCHAR},
#{miniappType,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yoho.message.dal.model.MiniappMsgForm" >
insert into miniapp_msg_form
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="formId != null" >
form_id,
</if>
<if test="availableTimes != null" >
available_times,
</if>
<if test="createTime != null" >
create_time,
</if>
<if test="openId != null" >
open_id,
</if>
<if test="orderCode != null" >
order_code,
</if>
<if test="miniappType != null" >
miniapp_type,
</if>
<if test="uid != null" >
uid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="formId != null" >
#{formId,jdbcType=VARCHAR},
</if>
<if test="availableTimes != null" >
#{availableTimes,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
<if test="openId != null" >
#{openId,jdbcType=VARCHAR},
</if>
<if test="orderCode != null" >
#{orderCode,jdbcType=VARCHAR},
</if>
<if test="miniappType != null" >
#{miniappType,jdbcType=INTEGER},
</if>
<if test="uid != null" >
#{uid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.message.dal.model.MiniappMsgForm" >
update miniapp_msg_form
<set >
<if test="formId != null" >
form_id = #{formId,jdbcType=VARCHAR},
</if>
<if test="availableTimes != null" >
available_times = #{availableTimes,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
<if test="openId != null" >
open_id = #{openId,jdbcType=VARCHAR},
</if>
<if test="orderCode != null" >
order_code = #{orderCode,jdbcType=VARCHAR},
</if>
<if test="miniappType != null" >
miniapp_type = #{miniappType,jdbcType=INTEGER},
</if>
<if test="uid != null" >
uid = #{uid,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yoho.message.dal.model.MiniappMsgForm" >
update miniapp_msg_form
set form_id = #{formId,jdbcType=VARCHAR},
available_times = #{availableTimes,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=INTEGER},
open_id = #{openId,jdbcType=VARCHAR},
order_code = #{orderCode,jdbcType=VARCHAR},
miniapp_type = #{miniappType,jdbcType=INTEGER},
uid = #{uid,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -29,7 +29,7 @@ rabbit.cacheclean.producer.host=192.168.102.211:5672
rabbit.cacheclean.consumer.host=192.168.102.211:5672
#message_center_rabbit_mq_config
rabbit_host=192.168.102.211:5672
rabbit_host=192.168.103.58:5672
rabbit_user=yoho
rabbit_password=yoho
rabbit_vhost=yoho
... ...
#zookeeper address
zkAddress=192.168.102.205:2181
zkAddress=127.0.0.1:2181
# web context
web.context=messagesender
\ No newline at end of file
... ...
... ... @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yoho.message.dal.IMiniappMsgFormDAO;
import com.yoho.message.dal.model.MiniappMsgForm;
import com.yoho.service.model.msgcenter.wechat.MiniappMsgReqBO;
import com.yoho.yhmessage.wechat.service.IMiniAppAccessTokenService;
import com.yoho.yhmessage.wechat.service.IMiniAppService;
... ... @@ -32,6 +34,8 @@ public class MiniServiceAppImpl implements IMiniAppService {
@Value("${miniapp.send.tempalte:/cgi-bin/message/wxopen/template/send?}")
private String sendMsgTemplate;
@Autowired
IMiniappMsgFormDAO miniappMsgFormDAO;
@Autowired
SendWechatRequest sendWechatRequest;
... ... @@ -68,9 +72,21 @@ public class MiniServiceAppImpl implements IMiniAppService {
throw new ServiceException(ServiceError.WECHAT_PARAM_ISNULL);
}
// 查询时间(可用次数>1 && 时间在7天之内的消息)
MiniappMsgForm miniappMsgForm = miniappMsgFormDAO.selectLastedAvailabByOrderCode(reqBO.getOrderCode());
if (miniappMsgForm == null){
logger.warn("MiniSendServiceImpl selectLastedAvailabByOrderCode, give up msg send. orderCode is {}", reqBO.getOrderCode());
return new JSONObject();
}
// 发送消息
JSONObject result = sendTemplateMsg(reqBO);
if (result.getInteger("errcode") == 0){
miniappMsgForm.setAvailableTimes(miniappMsgForm.getAvailableTimes()-1);
miniappMsgFormDAO.updateByPrimaryKey(miniappMsgForm);
}
return result;
}
... ...