<?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.applet.dao.impl.AppletGroupManageDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletGroupManage">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="group_activity_id" property="groupActivityId" jdbcType="BIGINT"/>
        <result column="enable" property="enable" jdbcType="INTEGER"/>
        <result column="recommend" property="recommend" jdbcType="INTEGER"/>
        <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="agent_id" property="agentId" jdbcType="BIGINT" />
    </resultMap>

    <sql id="Base_Column_List">
        id, group_activity_id, enable, recommend, seq_num, create_time, agent_id
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_group_manage
        WHERE id = #{id}
    </select>
    
    <select id="getList" resultType="com.pcloud.book.applet.dto.AppletGroupManageDTO">
        SELECT
            a.id,
            a.group_activity_id groupActivityId,
            a.`enable`,
            a.recommend,
            a.seq_num seqNum,
            b.`name`,
            b.description as `desc`,
            c.id AS classifyId,
            b.pro_label_id AS proLabelId,
            b.dep_label_id AS depLabelId,
            b.pur_label_id AS purLabelId,
            c.classify AS classifyName
        FROM
            applet_group_manage a
            INNER JOIN pcloud_group_activity b ON a.group_activity_id = b.id AND b.is_delete = 0
            LEFT JOIN book_group_classify c on b.book_group_classify_id = c.id AND c.is_delete = 0
        WHERE 1=1
        <if test="name!=null">
            AND b.name like concat('%',#{name},'%')
        </if>
        <if test="enable!=null">
            AND a.enable=#{enable}
        </if>
        <if test="recommend!=null">
            AND a.recommend=#{recommend}
        </if>
        <choose>
            <when test="agentId != null">
                and a.agent_id = #{agentId}
            </when>
            <otherwise>
                and a.agent_id = 0
            </otherwise>
        </choose>
        ORDER BY a.seq_num, a.id DESC
    </select>

    <select id="getList4Applet" resultType="com.pcloud.book.applet.dto.AppletGroupManageDTO">
        SELECT
        a.id,
        a.group_activity_id groupActivityId,
        b.`name`,
        b.description as `desc`,
        b.book_group_classify_id AS classifyId,
        c.book_group_id bookGroupId,
        c.price,
        g.related_book_group_id relatedBookGroupId,
        b.group_type as groupType,
        b.group_extLink as groupLink
        FROM
        applet_group_manage a
        left JOIN pcloud_group_activity b ON a.group_activity_id = b.id
        left JOIN book_group_classify c ON b.book_group_classify_id = c.id
        LEFT JOIN book_group g ON g.id = c.book_group_id
        WHERE a.enable=1
        AND b.is_delete = 0
        AND (c.is_delete = 0 or c.is_delete is NULL)
            <if test="recommend!=null">
              AND a.recommend=#{recommend}
            </if>
            <if test="name!=null">
                AND b.name like concat('%',#{name},'%')
            </if>
        <choose>
            <when test="agentId != null">
                and a.agent_id = #{agentId}
            </when>
            <otherwise>
                and a.agent_id = 0
            </otherwise>
        </choose>
        ORDER BY a.id DESC
    </select>

    <select id="getMaxSeqNum" resultType="integer">
        SELECT
            IFNULL(MAX(seq_num), 0)
        FROM applet_group_manage
        WHERE agent_id = #{agentId}
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_group_manage(
            group_activity_id, 
            enable, 
            recommend,
            seq_num,
            create_time,
            agent_id
        ) VALUES (
            #{groupActivityId, jdbcType=BIGINT}, 
            #{enable, jdbcType=INTEGER}, 
            #{recommend, jdbcType=INTEGER},
            #{seqNum, jdbcType=INTEGER},
           NOW(),
           #{agentId}
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_group_manage (
            group_activity_id, 
            enable, 
            recommend,
            seq_num,
            create_time,
            agent_id
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.groupActivityId, jdbcType=BIGINT}, 
            #{item.enable, jdbcType=INTEGER}, 
            #{item.recommend, jdbcType=INTEGER},
            #{item.seqNum, jdbcType=INTEGER},
            NOW(),
            #{item.agentId, jdbcType=BIGINT}
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_group_manage
        <set>
            <if test="groupActivityId != null">
                group_activity_id = #{groupActivityId},
            </if>
            <if test="enable != null">
                enable = #{enable},
            </if>
            <if test="recommend != null">
                recommend = #{recommend},
            </if>
            <if test="seqNum != null">
                seq_num = #{seqNum},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM applet_group_manage where id = #{id}
    </delete>

</mapper>