<?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.pcloud.book.group.mapper.GroupMaterialAccountMapper">

    <resultMap type="com.pcloud.book.group.entity.GroupMaterialAccount" id="GroupMaterialAccountMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="accountId" column="account_id" jdbcType="INTEGER"/>
        <result property="coverMediaId" column="cover_media_id" jdbcType="VARCHAR"/>
        <result property="headMediaId" column="head_media_id" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="GroupMaterialAccountMap">
        select id,
               account_id,
               cover_media_id,
               head_media_id,
               status,
               create_time,
               update_time
        from book.group_material_account
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="GroupMaterialAccountMap">
        select id,
               account_id,
               cover_media_id,
               head_media_id,
               status,
               create_time,
               update_time
        from book.group_material_account
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="GroupMaterialAccountMap">
        select
        id, account_id, cover_media_id, head_media_id, status, create_time, update_time
        from book.group_material_account
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="accountId != null">
                and account_id = #{accountId}
            </if>
            <if test="coverMediaId != null and coverMediaId != ''">
                and cover_media_id = #{coverMediaId}
            </if>
            <if test="headMediaId != null and headMediaId != ''">
                and head_media_id = #{headMediaId}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>
    <select id="getActiveAccount" resultMap="GroupMaterialAccountMap">
        SELECT
            a.id,
            a.account_id,
            a.cover_media_id,
            a.head_media_id,
            a.`status`,
            a.create_time,
            a.update_time,
            count( 0 ) sum
        FROM
            group_material_account a
            LEFT JOIN biz_material b ON a.account_id = b.account_id
        WHERE
            (a.`status` = 1 or (a.`status` = 0 and a.update_time <![CDATA[ < ]]> DATE(NOW())))
            <if test="useType != null">
                and a.use_type = #{useType}
            </if>
        GROUP BY a.account_id
        ORDER BY sum
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.group_material_account(account_id, cover_media_id, head_media_id, status, create_time,
                                                update_time)
        values (#{accountId}, #{coverMediaId}, #{headMediaId}, #{status}, #{createTime}, #{updateTime})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.group_material_account
        <set>
            <if test="accountId != null">
                account_id = #{accountId},
            </if>
            <if test="coverMediaId != null and coverMediaId != ''">
                cover_media_id = #{coverMediaId},
            </if>
            <if test="headMediaId != null and headMediaId != ''">
                head_media_id = #{headMediaId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from book.group_material_account
        where id = #{id}
    </delete>

    <select id="getByAccountId" resultMap="GroupMaterialAccountMap">
        select
            id,
            account_id,
            cover_media_id,
            head_media_id,
            `status`,
            create_time,
            update_time
        from group_material_account
        where account_id = #{accountId}
    </select>

</mapper>