quota-user-mapper.xml 3.63 KB
<?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">

<!-- 用户权益  @author tongxiaochuan -->
<mapper namespace="com.cjs.cms.dao.quota.QuotaUserDao">

	<sql id="quota_user_field">
		id, user_id, otc_code, otc_name, quota, 
		create_at, create_by, update_at, update_by
	</sql>
	
	<sql id="searchCondition">
		<if test="otcCode != null and otcCode != ''">
			and u.otc_code = #{otcCode} 
		</if>
		<if test="userId != null and userId != ''">
			and u.user_id = #{userId} 
		</if>
		<if test="quota == 1">
		   and u.quota = 0
		</if> 
		<if test="quota == 2">
		   and u.quota > 0
		</if>
		<if test="endTime != null and endTime != ''">
			and date(valid_end_date) = #{endTime} 
		</if>
	</sql>
	
	<sql id="searchQuotaCondition">
		<if test="otcCode != null and otcCode != ''">
			and otc_code like CONCAT('%',#{otcCode},'%')
		</if>
		
		<if test="otcName != null and otcName != ''">
			and otc_name like CONCAT('%',#{otcName},'%')
		</if>
		
		<if test="userId != null and userId != ''">
			and user_id  like CONCAT('%',#{userId},'%') 
		</if>
		
	</sql>
	
	<!-- 查询用户权益 -->
	<select  id="searchQuota"  parameterType="Map" resultType="map">
	   select <include refid="quota_user_field" /> from quota_user 
	   where 1=1
	   <include refid="searchQuotaCondition"/>
	   <include refid="common.pageLimit" />
	</select>
	
	<select  id="searchQuotaTotal" parameterType="map" resultType="int">
	   select count(*) from quota_user 
	   where 1=1
	   <include refid="searchQuotaCondition"/>
	</select>
	
	<!-- 查询指定用户权益 -->
	<select id="queryByUserId" parameterType="Map" resultType="QuotaUserInfo">
		select <include refid="quota_user_field" />
		from quota_user
		where user_id = #{userId} and otc_code = #{otcCode}
	</select>
	
	<insert id="insert" parameterType="QuotaUserInfo" useGeneratedKeys="true" keyProperty="id">
		insert into quota_user(user_id, otc_code, otc_name, create_at, create_by) 
		values(#{userId},#{otcCode},#{otcName},CURRENT_TIMESTAMP,#{createBy})
	</insert>
	
	<update id="updateQuota" parameterType="QuotaUserInfo">
		update quota_user set quota = #{quota}, 
			update_by = #{updateBy}, update_at = CURRENT_TIMESTAMP 
			where id = #{id}
	</update>
	
	<update id="updateQuotaByUserId" parameterType="map">
	    update quota_user set quota =quota+#{quota}, 
	    update_at = CURRENT_TIMESTAMP
	    where user_id = #{userId} and otc_code = #{otcCode}
	</update>
	
	<!-- 查询超过有效期时间的用户额度 -->
	<select id="search" parameterType="map" resultType="QuotaUserInfo">
	   select u.id, u.user_id, u.otc_code, u.otc_name, u.quota, 
		u.update_at, u.update_by, valid_end_date
		from quota_user u, quota_config c
		where u.otc_code = c.otc_code
	    and  <![CDATA[now() >= c.valid_end_date]]>
	    and  c.allot = 1
		<include refid="searchCondition" />
		<include refid="common.pageLimit" />
	</select>
    
    
    
    <select id="searchTotal" parameterType="map"
		resultType="int">
		select count(1) from quota_user u, quota_config c  
		where u.otc_code = c.otc_code
	    and  <![CDATA[now() >= c.valid_end_date]]>
	    and  c.allot = 1
		<include refid="searchCondition" />
	</select>
	
	
	<!-- 清除超过有效期时间的用户额度-->
	<update id="emptyQuota" parameterType="map">
	     update quota_user set quota = 0 , update_by = #{updateBy} , update_at = now()
	     where otc_code = #{otcCode} and quota > 0
	</update>
	
	<select id="queryQuotaByotcCode" parameterType="String" resultType = "QuotaUserInfo">
	     select user_id, otc_code, otc_name, quota 
	     from quota_user
	     where otc_code = #{otcCode} and quota>0
	</select>
	
</mapper>