<?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.custom.dao.impl.CustomPlanEmailDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.custom.entity.CustomPlanEmail">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="custom_plan_id" property="customPlanId" jdbcType="INTEGER"/>
        <result column="emails" property="emails" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="robot_id" property="robotId" jdbcType="VARCHAR"/>
        <result column="pcloud_robot_classify_name" property="pcloudRobotClassifyName" jdbcType="VARCHAR"/>
        <result column="user_wx_id" property="userWxId" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, custom_plan_id, emails, description, robot_id, pcloud_robot_classify_name, user_wx_id, create_time        
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM custom_plan_email
        WHERE id = #{id}
    </select>

    <select id="getCustomPlanEmailByPlanId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM custom_plan_email
        WHERE custom_plan_id = #{customPlanId}
        ORDER BY id DESC
        LIMIT 0,1
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM custom_plan_email
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO custom_plan_email(
            custom_plan_id, 
            emails, 
            description, 
            robot_id, 
            pcloud_robot_classify_name, 
            user_wx_id, 
            create_time
        ) VALUES (
            #{customPlanId, jdbcType=INTEGER},
            #{emails, jdbcType=VARCHAR}, 
            #{description, jdbcType=VARCHAR}, 
            #{robotId, jdbcType=VARCHAR}, 
            #{pcloudRobotClassifyName, jdbcType=VARCHAR}, 
            #{userWxId, jdbcType=VARCHAR}, 
            NOW()
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO custom_plan_email (
            custom_plan_id, 
            emails, 
            description, 
            robot_id, 
            pcloud_robot_classify_name, 
            user_wx_id, 
            create_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.customPlanId, jdbcType=INTEGER},
            #{item.emails, jdbcType=VARCHAR}, 
            #{item.description, jdbcType=VARCHAR}, 
            #{item.robotId, jdbcType=VARCHAR}, 
            #{item.pcloudRobotClassifyName, jdbcType=VARCHAR}, 
            #{item.userWxId, jdbcType=VARCHAR}, 
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE custom_plan_email
        <set>
            <if test="customPlanId != null">
                custom_plan_id = #{customPlanId},
            </if>
            <if test="emails != null and emails != ''">
                emails = #{emails},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="robotId != null and robotId != ''">
                robot_id = #{robotId},
            </if>
            <if test="pcloudRobotClassifyName != null and pcloudRobotClassifyName != ''">
                pcloud_robot_classify_name = #{pcloudRobotClassifyName},
            </if>
            <if test="userWxId != null and userWxId != ''">
                user_wx_id = #{userWxId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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

</mapper>