<?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.iformall.mapper.WxMsgLimitMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxMsgLimit">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="limit_id" jdbcType="VARCHAR" property="limitId"/>
        <result column="limit_num" jdbcType="INTEGER" property="limitNum"/>
        <result column="limit_date" jdbcType="TIMESTAMP" property="limitDate"/>

    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`type`,`limit_id`,`limit_num`,`limit_date`
	</sql>

    <sql id="dynamicWhereConditions">
        where 1 = 1

        <if test=" null != id ">
            and `id` = #{id}
        </if>

        <if test=" null != tenantId ">
            and `tenant_id`=#{tenantId}
        </if>

        <if test=" null != type ">
            and `type` = #{type}
        </if>

        <if test=" null != limitId ">
            and `limit_id` = #{limitId}
        </if>

        <if test=" null != limitNum ">
            and `limitNum` = #{limitNum}
        </if>

        <if test=" null != limitDate ">
            and `limit_date` = #{limitDate}
        </if>

        <if test=" null != expireDate ">
            and `limit_date` > #{expireDate}
        </if>
        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.WxMsgLimit" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_msg_limit
        <include refid="dynamicWhereConditions"/>
    </select>

    <update id="updateLimitNum" parameterType="com.iformall.domain.po.WxMsgLimit">
        update wx_msg_limit set limit_num = limit_num - 1 where `id` = #{id} and `tenant_id` = #{tenantId} and limit_num > 0
    </update>

    <delete id="delLimitNum" parameterType="com.iformall.domain.po.WxMsgLimit">
        delete from wx_msg_limit
        where `tenant_id` = #{tenantId} and `type`= #{type} and `limit_id` = #{limitId}
    </delete>

</mapper>
