<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.push.dao.impl.PushGroupDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.PushGroup">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="push_id" property="pushId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="book_group_qrcode_id" property="bookGroupQrcodeId" jdbcType="INTEGER"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="BIGINT"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BaseResultDTOMap" type="com.pcloud.book.push.dto.PushGroupDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="push_id" property="pushId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="book_group_qrcode_id" property="bookGroupQrcodeId" jdbcType="INTEGER"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="BIGINT"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="book_name" property="bookName" jdbcType="VARCHAR"/>
        <result column="group_name" property="groupName" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, push_id, book_id, classify_id, book_group_qrcode_id, is_delete, create_user, create_time, update_user, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from push_group
        where is_delete=0 and id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="java.lang.Long">
        update push_group set is_delete = 1
        where id = #{id,jdbcType=BIGINT} and is_delete = 0
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.push.entity.PushGroup" useGeneratedKeys="true" keyProperty="id">
        insert into push_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            push_id,
            book_id,
            classify_id,
            book_group_qrcode_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{pushId,jdbcType=BIGINT},
            #{bookId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{bookGroupQrcodeId,jdbcType=INTEGER},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.push.entity.PushGroup">
        update push_group
        <set>
            <if test="pushId != null">
                push_id = #{pushId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=BIGINT},
            </if>
            <if test="classifyId != null">
                classify_id = #{classifyId,jdbcType=BIGINT},
            </if>
            <if test="bookGroupQrcodeId != null">
                book_group_qrcode_id = #{bookGroupQrcodeId,jdbcType=INTEGER},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=BOOLEAN},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=BIGINT},
            </if>
            update_time = NOW(),
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.push.entity.PushGroup" useGeneratedKeys="true" keyProperty="id">
        insert into push_group (
            push_id,
            book_id,
            classify_id,
            book_group_qrcode_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
        (
            #{item.pushId,jdbcType=BIGINT},
            #{item.bookId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.bookGroupQrcodeId,jdbcType=INTEGER},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
        )
        </foreach>
    </insert>

    <!--根据pushId查询列表-->
    <select id="getListByPushId" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from push_group
        where is_delete=0
        and push_id=#{pushId}
    </select>

    <!--删除群发关联-->
    <update id="deleteByPushId" parameterType="map">
        UPDATE
        push_group
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE push_id =#{pushId}
    </update>

    <!--批量删除-->
    <update id="deleteByIds" parameterType="map">
        UPDATE
        push_group
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE id in
        <foreach collection="ids" index="index" item="item" open="(" separator=","
                 close=")">
            ${item}
        </foreach>
    </update>

    <!--根据群发id集合获取群发关联集合-->
    <select id="getListByPushIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push_group
        where is_delete=0
        and push_id in
        <foreach collection="pushIds" item="pushId" open="("  separator="," close=")">
            #{pushId}
        </foreach>
    </select>

    <!--获取群发关联列表-->
    <select id="getPushGroupList" parameterType="map" resultMap="BaseResultDTOMap">
        SELECT
        t1.id,
        t1.push_id,
        t1.book_id,
        t1.classify_id,
        t1.book_group_qrcode_id,
        t1.is_delete,
        t1.create_user,
        t1.create_time,
        t1.update_user,
        t1.update_time,
        t2.book_name,
        t3.group_name
        FROM
        push_group t1
        LEFT JOIN book t2 ON t1.book_id = t2.BOOK_ID
        LEFT JOIN book_group_qrcode t3 ON t1.book_group_qrcode_id = t3.id
        WHERE 1=1
        <if test="isRecord==null or isRecord==false">
            AND t1.is_delete = 0
        </if>
        AND t1.push_id = #{pushId}
    </select>

    <!--根据群发计划id集合获取群发计划关联-->
    <select id="getPlanClassifyListByPlanIds" parameterType="map" resultType="com.pcloud.book.push.dto.PlanClassifyDTO">
        SELECT
        t.classify_id classifyId, t2.classify, t2.book_group_id bookGroupId, t.book_id bookId, t1.push_plan_id pushPlanId
        FROM
        push_group t
        LEFT JOIN push t1 ON t.push_id = t1.id
        LEFT JOIN book_group_classify t2 ON t.classify_id = t2.id
        where t.is_delete=0
        and t1.is_delete=0
        and t1.push_plan_id in
        <foreach collection="pushPlanIds" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY t.classify_id,t1.push_plan_id
    </select>

    <!--根据分类id查询-->
    <select id="getListByClassifyId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push_group t
        where t.classify_id=#{classifyId}
    </select>

    <!--根据群发计划id获取群发计划关联-->
    <select id="getPlanClassifyListByPlanId" parameterType="Long" resultType="com.pcloud.book.push.dto.PlanClassifyDTO">
        SELECT
        t.classify_id classifyId, t2.classify, t2.book_group_id bookGroupId, t.book_id bookId, t1.push_plan_id pushPlanId
        FROM
        push_group t
        LEFT JOIN push t1 ON t.push_id = t1.id
        LEFT JOIN book_group_classify t2 ON t.classify_id = t2.id
        where t.is_delete=0
        and t1.is_delete=0
        and t1.push_plan_id = #{pushPlanId}
        GROUP BY t.classify_id
    </select>
</mapper>