<?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.BizMaterialMapper">

    <resultMap type="com.pcloud.book.group.entity.BizMaterial" id="BizMaterialMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="mediaId" column="media_id" jdbcType="VARCHAR"/>
        <result property="materialUrl" column="material_url" jdbcType="VARCHAR"/>
        <result property="accountId" column="account_id" jdbcType="INTEGER"/>
        <result property="bizType" column="biz_type" jdbcType="INTEGER"/>
        <result property="bizId" column="biz_id" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="BizMaterialMap">
        select id,
               media_id,
               material_url,
               account_id,
               biz_type,
               biz_id,
               create_time,
               update_time
        from book.biz_material
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="BizMaterialMap">
        select id,
               media_id,
               material_url,
               account_id,
               biz_type,
               biz_id,
               create_time,
               update_time
        from book.biz_material
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="BizMaterialMap">
        select
        id, media_id, material_url, account_id, biz_type, biz_id, create_time, update_time
        from book.biz_material
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="mediaId != null and mediaId != ''">
                and media_id = #{mediaId}
            </if>
            <if test="materialUrl != null and materialUrl != ''">
                and material_url = #{materialUrl}
            </if>
            <if test="accountId != null">
                and account_id = #{accountId}
            </if>
            <if test="bizType != null">
                and biz_type = #{bizType}
            </if>
            <if test="bizId != null">
                and biz_id = #{bizId}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.biz_material(media_id, material_url, account_id, biz_type, biz_id, create_time, update_time)
        values (#{mediaId}, #{materialUrl}, #{accountId}, #{bizType}, #{bizId}, #{createTime}, #{updateTime})
    </insert>

    <insert id="batchInsert" parameterType="list">
        insert into book.biz_material (media_id, material_url, account_id, biz_type, biz_id, create_time, update_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.mediaId}, #{item.materialUrl}, #{item.accountId}, #{item.bizType}, #{item.bizId}, #{item.createTime}, NOW())
        </foreach>
        on duplicate key update
        media_id = values(media_id),
        material_url = values(material_url),
        account_id = values(account_id),
        biz_type = values(biz_type),
        biz_id = values(biz_id),
        create_time = values(create_time),
        update_time = values(update_time)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.biz_material
        <set>
            <if test="mediaId != null and mediaId != ''">
                media_id = #{mediaId},
            </if>
            <if test="materialUrl != null and materialUrl != ''">
                material_url = #{materialUrl},
            </if>
            <if test="accountId != null">
                account_id = #{accountId},
            </if>
            <if test="bizType != null">
                biz_type = #{bizType},
            </if>
            <if test="bizId != null">
                biz_id = #{bizId},
            </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.biz_material
        where id = #{id}
    </delete>

    <select id="listBizMaterialsByGroupId" resultType="com.pcloud.book.group.dto.UpdateGroupMaterialDTO" parameterType="list">
        SELECT
            b.id AS id,
            a.group_name AS groupName,
            a.weixin_group_id AS wxGroupId,
            a.id AS bookGroupQrcodeId,
            a.qrcode_url AS qrcodeUrl,
            b.account_id AS accountId,
            b.media_id AS mediaId
        FROM
            book_group_qrcode a
            LEFT JOIN biz_material b ON a.id = b.biz_id
        WHERE
            a.weixin_group_id IN
        <foreach collection="list" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>

    </select>

    <insert id="insertOrUpdate">
        insert into book.biz_material (media_id, material_url, account_id, biz_type, biz_id,
            <if test="createTime!=null">
                create_time,
            </if>
             update_time)
        values
            (#{mediaId}, #{materialUrl}, #{accountId}, #{bizType}, #{bizId},
             <if test="createTime!=null">
                #{createTime},
            </if>
             NOW())
        on duplicate key update
        media_id = values(media_id),
        material_url = values(material_url),
        account_id = values(account_id),
        update_time = values(update_time)
    </insert>

    <select id="selectMaterialUrl4Biz" resultType="java.lang.String">
        select material_url from biz_material where biz_id = #{bizId} AND biz_type=#{bizType} limit 1
    </select>

</mapper>