<?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.iamberry.wechat.service.mapper.WechatShareMapper">

	<sql id="allField">
		SHARE_ID shareID,
		SHARE_URL shareUrl,
		SHARE_URL_HASH shareUrlHash,
		SHARE_TITLE shareTitle,
		SHARE_DESC shareDesc,
		SHARE_DATA_NUM shareDataNum,
		SHARE_NUM shareNum,
		SHARE_END_DATE shareEndDate,
		SHARE_CREATE_DATE shareCreateDate
	</sql>
	<!-- 添加一个分享 -->
	<insert id="addWechatShare" parameterType="WechatShare" useGeneratedKeys="true" keyProperty="shareID">
		INSERT INTO TB_IAMBERRY_WX_SHARE
		(
			SHARE_ID ,
			SHARE_URL ,
			SHARE_URL_HASH ,
			SHARE_TITLE ,
			SHARE_DESC ,
			SHARE_DATA_NUM ,
			SHARE_NUM ,
			SHARE_END_DATE ,
			SHARE_CREATE_DATE
		)
		VALUES
		(
			#{shareID},
			#{shareUrl},
			#{shareUrlHash},
			#{shareTitle},
			#{shareDesc},
			#{shareDataNum},
			#{shareNum},
			#{shareEndDate},
			#{shareCreateDate}
		)
	</insert>
	
	<!-- 根据url 的 hash值，获取对应的规则 -->
	<select id="selectWechatShareByHash" parameterType="Integer" resultType="WechatShare">
		SELECT 
			<include refid="allField"/>
		FROM 
			TB_IAMBERRY_WX_SHARE
		WHERE
			SHARE_URL_HASH = #{shareUrlHash}
		LIMIT
			0, 1
	</select>
	
	<!-- 根据hash更新数据 -->
	<update id="updateWechatShareByHash" parameterType="WechatShare">
		UPDATE 
			TB_IAMBERRY_WX_SHARE 
		SET 
			SHARE_DATA_NUM = (SHARE_DATA_NUM + #{shareDataNum}), SHARE_NUM= (SHARE_NUM + #{shareNum}) 
		WHERE 
			SHARE_URL_HASH = #{shareUrlHash}
	</update>
</mapper>