<?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.fs.company.mapper.CompanyVoiceRoboticWxMapper">
    
    <resultMap type="CompanyVoiceRoboticWx" id="CompanyVoiceRoboticWxResult">
        <result property="id"    column="id"    />
        <result property="accountId"    column="account_id"    />
        <result property="intention"    column="intention"    />
        <result property="accountId"    column="account_id"    />
        <result property="wxDialogId"    column="wx_dialog_id"    />
        <result property="num"    column="num"    />
        <result property="addNum"    column="add_num"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCompanyVoiceRoboticWxVo">
        select id, intention, account_id, account_id, wx_dialog_id, num, add_num, create_time, create_user from company_voice_robotic_wx
    </sql>

    <select id="selectCompanyVoiceRoboticWxList" parameterType="CompanyVoiceRoboticWx" resultMap="CompanyVoiceRoboticWxResult">
        <include refid="selectCompanyVoiceRoboticWxVo"/>
        <where>  
            <if test="intention != null  and intention != ''"> and intention = #{intention}</if>
            <if test="accountId != null "> and account_id = #{accountId}</if>
            <if test="accountId != null "> and account_id = #{accountId}</if>
            <if test="wxDialogId != null "> and wx_dialog_id = #{wxDialogId}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="addNum != null "> and add_num = #{addNum}</if>
        </where>
    </select>
    
    <select id="selectCompanyVoiceRoboticWxById" parameterType="Long" resultMap="CompanyVoiceRoboticWxResult">
        <include refid="selectCompanyVoiceRoboticWxVo"/>
        where id = #{id}
    </select>
    <select id="selectByRoboticId" resultType="com.fs.company.domain.CompanyVoiceRoboticWx">
        select a.*,account.wx_nick_name,account.wx_no,b.nick_name as companyUserName,c.name dialogName
        from company_voice_robotic_wx a
        left join company_wx_account account on account.id = a.account_id
        left join company_user b on account.company_user_id = b.user_id
        left join company_wx_dialog c on a.wx_dialog_id = c.id
        where a.robotic_id = #{id}
    </select>

    <insert id="insertCompanyVoiceRoboticWx" parameterType="CompanyVoiceRoboticWx" useGeneratedKeys="true" keyProperty="id">
        insert into company_voice_robotic_wx
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="intention != null">intention,</if>
            <if test="accountId != null">account_id,</if>
            <if test="accountId != null">account_id,</if>
            <if test="wxDialogId != null">wx_dialog_id,</if>
            <if test="num != null">num,</if>
            <if test="addNum != null">add_num,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="intention != null">#{intention},</if>
            <if test="accountId != null">#{accountId},</if>
            <if test="accountId != null">#{accountId},</if>
            <if test="wxDialogId != null">#{wxDialogId},</if>
            <if test="num != null">#{num},</if>
            <if test="addNum != null">#{addNum},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>
    <insert id="insertList">
        insert into company_voice_robotic_wx (intention, account_id, account_id, wx_dialog_id, create_time) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.intention},#{item.accountId},#{item.accountId},#{item.wxDialogId},now())
        </foreach>
    </insert>

    <update id="updateCompanyVoiceRoboticWx" parameterType="CompanyVoiceRoboticWx">
        update company_voice_robotic_wx
        <trim prefix="SET" suffixOverrides=",">
            <if test="intention != null">intention = #{intention},</if>
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="wxDialogId != null">wx_dialog_id = #{wxDialogId},</if>
            <if test="num != null">num = #{num},</if>
            <if test="addNum != null">add_num = #{addNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyVoiceRoboticWxById" parameterType="Long">
        delete from company_voice_robotic_wx where id = #{id}
    </delete>

    <delete id="deleteCompanyVoiceRoboticWxByIds" parameterType="String">
        delete from company_voice_robotic_wx where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
</mapper>