<?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.ruoyi.system.mapper.VipQuanyiMapper">

    <resultMap type="VipQuanyi" id="VipQuanyiResult">
        <result property="id"    column="id"    />
        <result property="vip"    column="vip"    />
        <result property="yhfangs"    column="yhfangs"    />
        <result property="yhnum"    column="yhnum"    />
        <result property="name"    column="name"    />
        <result property="shuoming"    column="shuoming"    />
        <result property="tiaojian"    column="tiaojian"    />
        <result property="num"    column="num"    />
        <result property="total"    column="total"    />
        <result property="validity"    column="validity"    />
        <result property="lqxz"    column="lqxz"    />
        <result property="state"    column="state"    />
        <result property="shId"    column="sh_id"    />
        <result property="spId"    column="sp_id"    />
        <result property="claimedCount"    column="claimed_count"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deleted"    column="deleted"    />
        <result property="language"    column="language"    />
        <result property="poster"    column="poster"    />
        <result property="type" column="type"/>
        <result property="merchantId" column="merchant_id"/>
    </resultMap>



    <sql id="selectVipQuanyiVo">
        select id, vip, yhfangs, yhnum, name, shuoming, tiaojian, num,
               total, validity, lqxz, state, sh_id, sp_id, claimed_count, create_time, update_time, deleted, language, poster,type,merchant_id
        from vip_quanyi
    </sql>
    <select id="userUnclaimedList" resultMap="VipQuanyiResult">
        select * from vip_quanyi v  LEFT JOIN (SELECT
                                                   user_id,
                                                   quanyi_id,
                                                   COUNT(*) AS receive_count
                                               FROM vip_user_quanyi
                                               WHERE deleted = '0' and user_id=#{userId}
                                               GROUP BY user_id, quanyi_id) u  on v.id=u.quanyi_id  where v.state='0' and v.deleted='0' and v.total> v.claimed_count and v.language=#{language} and (u.receive_count IS NULL OR v.lqxz > u.receive_count)
                                                                                                    <if test="mdId != null and mdId != ''">
                                                                                                      and (v.sh_id = #{mdId} or v.sh_id is null)
                                                                                                    </if>
                                                                                                    ORDER BY create_time
    </select>
    <select id="selectVipQuanyiList" parameterType="VipQuanyi" resultMap="VipQuanyiResult">
        <include refid="selectVipQuanyiVo"/>
        <where>
            deleted != '1'
            <if test="vip != null "> and vip = #{vip}</if>
            <if test="yhfangs != null "> and yhfangs = #{yhfangs}</if>
            <if test="yhnum != null "> and yhnum = #{yhnum}</if>
            <if test="name != null  and name != ''"> and BINARY  name like concat('%', #{name}, '%')</if>
            <if test="shuoming != null  and shuoming != ''"> and shuoming = #{shuoming}</if>
            <if test="tiaojian != null "> and tiaojian = #{tiaojian}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="total != null and total!=''"> and total = #{total}</if>
            <if test="validity != null and validity!='' "> and validity = #{validity}</if>
            <if test="lqxz != null and lqxz !='' "> and lqxz = #{lqxz}</if>
            <if test="state != null and state != ''"> and state = #{state}</if>
            <if test="shId != null"> and sh_id = #{shId}</if>
            <if test="spId != null"> and sp_id = #{spId}</if>
            <if test="claimedCount != null and claimedCount !='' "> and claimed_count = #{claimedCount}</if>
            <if test="createTime != null and createTime !='' "> and create_time = #{createTime}</if>
            <if test="updateTime != null and updateTime != '' "> and update_time = #{updateTime}</if>
            <if test="deleted != null and deleted!=''"> and deleted = #{deleted}</if>
            <if test="language != null and language != ''"> and language = #{language}</if>
            <if test="poster != null and poster != ''"> and poster = #{poster}</if>
            <if test="type != null and type != ''"> and type = #{type}</if>
            <if test="merchantId != null and merchantId != ''"> and merchant_id = #{merchantId}</if>
        </where>
        order by id desc
    </select>

    <select id="selectVipQuanyiById" parameterType="Long" resultMap="VipQuanyiResult">
        <include refid="selectVipQuanyiVo"/>
        where id = #{id}
    </select>

    <insert id="insertVipQuanyi" parameterType="VipQuanyi" useGeneratedKeys="true" keyProperty="id">
        insert into vip_quanyi
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="vip != null">vip,</if>
            <if test="yhfangs != null">yhfangs,</if>
            <if test="yhnum != null">yhnum,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="shuoming != null">shuoming,</if>
            <if test="tiaojian != null">tiaojian,</if>
            <if test="num != null">num,</if>
            <if test="total != null">total,</if>
            <if test="validity != null">validity,</if>
            <if test="lqxz != null">lqxz,</if>
            <if test="state != null">state,</if>
            <if test="shId != null">sh_id,</if>
            <if test="spId != null">sp_id,</if>
            <if test="claimedCount != null">claimed_count,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleted != null">deleted,</if>
            <if test="language != null and language != ''">language,</if>
            <if test="poster != null and poster != ''">poster,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="merchantId != null and merchantId != ''">merchant_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="vip != null">#{vip},</if>
            <if test="yhfangs != null">#{yhfangs},</if>
            <if test="yhnum != null">#{yhnum},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="shuoming != null">#{shuoming},</if>
            <if test="tiaojian != null">#{tiaojian},</if>
            <if test="num != null">#{num},</if>
            <if test="total != null">#{total},</if>
            <if test="validity != null">#{validity},</if>
            <if test="lqxz != null">#{lqxz},</if>
            <if test="state != null">#{state},</if>
            <if test="shId != null">#{shId},</if>
            <if test="spId != null">#{spId},</if>
            <if test="claimedCount != null">#{claimedCount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="language != null and language != ''">#{language},</if>
            <if test="poster != null and poster != ''">#{poster},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="merchantId != null and merchantId != ''">#{merchantId},</if>
         </trim>
    </insert>

    <update id="updateVipQuanyi" parameterType="VipQuanyi">
        update vip_quanyi
        <trim prefix="SET" suffixOverrides=",">
            <if test="vip != null">vip = #{vip},</if>
            <if test="yhfangs != null">yhfangs = #{yhfangs},</if>
            <if test="yhnum != null">yhnum = #{yhnum},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="shuoming != null">shuoming = #{shuoming},</if>
            <if test="tiaojian != null">tiaojian = #{tiaojian},</if>
            <if test="num != null">num = #{num},</if>
            <if test="total != null">total = #{total},</if>
            <if test="validity != null">validity = #{validity},</if>
            <if test="lqxz != null">lqxz = #{lqxz},</if>
            <if test="state != null">state = #{state},</if>
            <if test="shId != null">sh_id = #{shId},</if>
            <if test="spId != null">sp_id = #{spId},</if>
            <if test="claimedCount != null">claimed_count = #{claimedCount},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
            <if test="language != null and language != ''">language = #{language},</if>
            <if test="poster != null and poster != ''">poster = #{poster},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="merchantId != null and merchantId != ''">merchant_id = #{merchantId},</if>

        </trim>
        where id = #{id}
    </update>

    <update id="deleteVipQuanyiById" parameterType="Long">
        update vip_quanyi set deleted = '1' where id = #{id}
    </update>

    <update id="deleteVipQuanyiByIds" parameterType="String">
        update vip_quanyi set deleted = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
