<?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.kexue.skills.mapper.ContentPurchaseMapper">

    <resultMap type="com.kexue.skills.entity.ContentPurchase" id="ContentPurchaseMap">
        <result property="purchaseId" column="purchase_id" jdbcType="BIGINT"/>
        <result property="userId" column="user_id" jdbcType="BIGINT"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="contentId" column="content_id" jdbcType="BIGINT"/>
        <result property="contentTitle" column="content_title" jdbcType="VARCHAR"/>
        <result property="payType" column="pay_type" jdbcType="INTEGER"/>
        <result property="amount" column="amount" jdbcType="DECIMAL"/>
        <result property="points" column="points" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="purchaseTime" column="purchase_time" jdbcType="TIMESTAMP"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="deleteFlag" column="delete_flag" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->    
    <select id="queryById" resultMap="ContentPurchaseMap">
        select 
          purchase_id, user_id, user_name, content_id, content_title, pay_type, amount, points, status, 
          purchase_time, create_time, update_time, create_by, update_by, delete_flag
        from content_purchase
        where purchase_id = #{purchaseId}
    </select>

    <!--通过用户ID和内容ID查询-->    
    <select id="queryByUserIdAndContentId" resultMap="ContentPurchaseMap">
        select 
          purchase_id, user_id, user_name, content_id, content_title, pay_type, amount, points, status, 
          purchase_time, create_time, update_time, create_by, update_by, delete_flag
        from content_purchase
        where user_id = #{userId} and content_id = #{contentId}
    </select>

    <!--分页查询-->    
    <select id="getPageList" resultMap="ContentPurchaseMap">
        select 
          purchase_id, user_id, user_name, content_id, content_title, pay_type, amount, points, status, 
          purchase_time, create_time, update_time, create_by, update_by, delete_flag
        from content_purchase
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="contentId != null">
                and content_id = #{contentId}
            </if>
            <if test="payType != null">
                and pay_type = #{payType}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="contentTitle != null and contentTitle != ''">
                and content_title like concat('%', #{contentTitle}, '%')
            </if>
            <if test="purchaseTimeStart != null">
                and purchase_time <![CDATA[ >= ]]> #{purchaseTimeStart}
            </if>
            <if test="purchaseTimeEnd != null">
                and purchase_time <![CDATA[ <= ]]> #{purchaseTimeEnd}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        <if test="sortBy != null and sortBy != ''">
            order by ${sortBy} ${sortDesc ? 'desc' : 'asc'}
        </if>
    </select>

    <!--查询列表-->    
    <select id="getList" resultMap="ContentPurchaseMap">
        select 
          purchase_id, user_id, user_name, content_id, content_title, pay_type, amount, points, status, 
          purchase_time, create_time, update_time, create_by, update_by, delete_flag
        from content_purchase
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="contentId != null">
                and content_id = #{contentId}
            </if>
            <if test="payType != null">
                and pay_type = #{payType}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="contentTitle != null and contentTitle != ''">
                and content_title like concat('%', #{contentTitle}, '%')
            </if>
            <if test="purchaseTimeStart != null">
                and purchase_time <![CDATA[ >= ]]> #{purchaseTimeStart}
            </if>
            <if test="purchaseTimeEnd != null">
                and purchase_time <![CDATA[ <= ]]> #{purchaseTimeEnd}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by purchase_time desc
    </select>

    <!--新增数据-->    
    <insert id="insert" useGeneratedKeys="true" keyProperty="purchaseId">
        insert into content_purchase
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="contentId != null">content_id,</if>
            <if test="contentTitle != null">content_title,</if>
            <if test="payType != null">pay_type,</if>
            <if test="amount != null">amount,</if>
            <if test="points != null">points,</if>
            <if test="status != null">status,</if>
            <if test="purchaseTime != null">purchase_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deleteFlag != null">delete_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="contentId != null">#{contentId},</if>
            <if test="contentTitle != null">#{contentTitle},</if>
            <if test="payType != null">#{payType},</if>
            <if test="amount != null">#{amount},</if>
            <if test="points != null">#{points},</if>
            <if test="status != null">#{status},</if>
            <if test="purchaseTime != null">#{purchaseTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deleteFlag != null">#{deleteFlag},</if>
        </trim>
    </insert>

    <!--更新数据-->    
    <update id="update">
        update content_purchase
        <set>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="contentId != null">content_id = #{contentId},</if>
            <if test="contentTitle != null">content_title = #{contentTitle},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="points != null">points = #{points},</if>
            <if test="status != null">status = #{status},</if>
            <if test="purchaseTime != null">purchase_time = #{purchaseTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
        </set>
        where purchase_id = #{purchaseId}
    </update>

    <!--更新购买状态-->    
    <update id="updateStatus">
        update content_purchase
        set status = #{status}, 
            update_time = now()
        where purchase_id = #{purchaseId}
    </update>

    <!--逻辑删除-->    
    <update id="logicDeleteById">
        update content_purchase
        set delete_flag = 1,
            update_by = #{updateBy},
            update_time = now()
        where purchase_id = #{purchaseId}
    </update>

    <!--物理删除-->    
    <delete id="deleteById">
        delete from content_purchase
        where purchase_id = #{purchaseId}
    </delete>
</mapper>