<?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.CompanyVoiceRoboticMapper">
    
    <resultMap type="CompanyVoiceRobotic" id="CompanyVoiceRoboticResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="taskName"    column="task_name"    />
        <result property="taskId"    column="task_id"    />
        <result property="robot"    column="robot"    />
        <result property="dialogId"    column="dialog_id"    />
        <result property="mode"    column="mode"    />
        <result property="addType"    column="add_type"    />
        <result property="multiplier"    column="multiplier"    />
        <result property="autoRecall"    column="auto_recall"    />
        <result property="recallTimes"    column="recall_times"    />
        <result property="cidGroupId"    column="cid_group_id"    />
        <result property="weekDay1"    column="week_day1"    />
        <result property="startTime1"    column="start_time1"    />
        <result property="endTime1"    column="end_time1"    />
        <result property="weekDay2"    column="week_day2"    />
        <result property="startTime2"    column="start_time2"    />
        <result property="endTime2"    column="end_time2"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCompanyVoiceRoboticVo">
        select id, name, task_name, task_id, add_type, robot, dialog_id, mode, multiplier, auto_recall, recall_times, cid_group_id, week_day1, start_time1, end_time1, week_day2, start_time2, end_time2, create_time, create_user from company_voice_robotic
    </sql>

    <select id="selectCompanyVoiceRoboticList" parameterType="CompanyVoiceRobotic" resultType="CompanyVoiceRobotic">
        select
            a.*,
            (select count(1) from company_voice_robotic_callees b where a.id = b.robotic_id and b.result = 1 and b.distribute = 0) num
        from
        company_voice_robotic a
        <where>  
            <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>
            <if test="taskName != null  and taskName != ''"> and a.task_name like concat('%', #{taskName}, '%')</if>
            <if test="taskId != null "> and a.task_id = #{taskId}</if>
            <if test="robot != null "> and a.robot = #{robot}</if>
            <if test="dialogId != null "> and a.dialog_id = #{dialogId}</if>
            <if test="mode != null "> and a.mode = #{mode}</if>
            <if test="addType != null "> and a.add_type = #{addType}</if>
            <if test="multiplier != null "> and a.multiplier = #{multiplier}</if>
            <if test="autoRecall != null "> and a.auto_recall = #{autoRecall}</if>
            <if test="recallTimes != null "> and a.recall_times = #{recallTimes}</if>
            <if test="cidGroupId != null "> and a.cid_group_id = #{cidGroupId}</if>
            <if test="weekDay1 != null  and weekDay1 != ''"> and a.week_day1 = #{weekDay1}</if>
            <if test="startTime1 != null  and startTime1 != ''"> and a.start_time1 = #{startTime1}</if>
            <if test="endTime1 != null  and endTime1 != ''"> and a.end_time1 = #{endTime1}</if>
            <if test="weekDay2 != null  and weekDay2 != ''"> and a.week_day2 = #{weekDay2}</if>
            <if test="startTime2 != null  and startTime2 != ''"> and a.start_time2 = #{startTime2}</if>
            <if test="endTime2 != null  and endTime2 != ''"> and a.end_time2 = #{endTime2}</if>
            <if test="createUser != null "> and a.create_user = #{createUser}</if>
        </where>
        order by id desc
    </select>
    <select id="selectCompanyVoiceRoboticListCompany" parameterType="CompanyVoiceRobotic" resultMap="CompanyVoiceRoboticResult">
        select a.* from company_voice_robotic a
        inner join company_user u on a.create_user = u.user_id
        inner join company_dept d on u.dept_id = d.dept_id
        <where>
            <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>
            <if test="taskName != null  and taskName != ''"> and a.task_name like concat('%', #{taskName}, '%')</if>
            <if test="taskId != null "> and a.task_id = #{taskId}</if>
            <if test="robot != null "> and a.robot = #{robot}</if>
            <if test="dialogId != null "> and a.dialog_id = #{dialogId}</if>
            <if test="mode != null "> and a.mode = #{mode}</if>
            <if test="addType != null "> and a.add_type = #{addType}</if>
            <if test="multiplier != null "> and a.multiplier = #{multiplier}</if>
            <if test="autoRecall != null "> and a.auto_recall = #{autoRecall}</if>
            <if test="recallTimes != null "> and a.recall_times = #{recallTimes}</if>
            <if test="cidGroupId != null "> and a.cid_group_id = #{cidGroupId}</if>
            <if test="weekDay1 != null  and weekDay1 != ''"> and a.week_day1 = #{weekDay1}</if>
            <if test="startTime1 != null  and startTime1 != ''"> and a.start_time1 = #{startTime1}</if>
            <if test="endTime1 != null  and endTime1 != ''"> and a.end_time1 = #{endTime1}</if>
            <if test="weekDay2 != null  and weekDay2 != ''"> and a.week_day2 = #{weekDay2}</if>
            <if test="startTime2 != null  and startTime2 != ''"> and a.start_time2 = #{startTime2}</if>
            <if test="endTime2 != null  and endTime2 != ''"> and a.end_time2 = #{endTime2}</if>
            <if test="createUser != null "> and a.create_user = #{createUser}</if>
            <if test="params != null">
                ${params.dataScope}
            </if>
        </where>
    </select>
    
    <select id="selectCompanyVoiceRoboticById" parameterType="Long" resultMap="CompanyVoiceRoboticResult">
        <include refid="selectCompanyVoiceRoboticVo"/>
        where id = #{id}
    </select>
    <select id="qwUserList" resultType="com.fs.company.vo.CompanyVoiceRoboticQwUserListVo">
        select a.id, a.wx_nick_name, a.wx_no, b.nick_name as companyUserName  from company_wx_account a
       inner join company_user b on a.company_user_id = b.user_id
    </select>
    <select id="qwUserListCompany" resultType="com.fs.company.vo.CompanyVoiceRoboticQwUserListVo">
        select a.id, a.wx_nick_name, a.wx_no, u.nick_name as companyUserName
        from company_wx_account a
       inner join company_user u on a.company_user_id = u.user_id
       inner join company_dept d on u.dept_id = d.dept_id
        <where>
            <if test="params != null">${params.dataScope}</if>
        </where>
    </select>

    <insert id="insertCompanyVoiceRobotic" parameterType="CompanyVoiceRobotic" useGeneratedKeys="true" keyProperty="id">
        insert into company_voice_robotic
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="taskName != null">task_name,</if>
            <if test="addType != null">add_type,</if>
            <if test="taskId != null">task_id,</if>
            <if test="robot != null">robot,</if>
            <if test="dialogId != null">dialog_id,</if>
            <if test="mode != null">mode,</if>
            <if test="multiplier != null">multiplier,</if>
            <if test="autoRecall != null">auto_recall,</if>
            <if test="recallTimes != null">recall_times,</if>
            <if test="cidGroupId != null">cid_group_id,</if>
            <if test="weekDay1 != null">week_day1,</if>
            <if test="startTime1 != null">start_time1,</if>
            <if test="endTime1 != null">end_time1,</if>
            <if test="weekDay2 != null">week_day2,</if>
            <if test="startTime2 != null">start_time2,</if>
            <if test="endTime2 != null">end_time2,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUser != null">create_user,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="addType != null">#{addType},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="robot != null">#{robot},</if>
            <if test="dialogId != null">#{dialogId},</if>
            <if test="mode != null">#{mode},</if>
            <if test="multiplier != null">#{multiplier},</if>
            <if test="autoRecall != null">#{autoRecall},</if>
            <if test="recallTimes != null">#{recallTimes},</if>
            <if test="cidGroupId != null">#{cidGroupId},</if>
            <if test="weekDay1 != null">#{weekDay1},</if>
            <if test="startTime1 != null">#{startTime1},</if>
            <if test="endTime1 != null">#{endTime1},</if>
            <if test="weekDay2 != null">#{weekDay2},</if>
            <if test="startTime2 != null">#{startTime2},</if>
            <if test="endTime2 != null">#{endTime2},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUser != null">#{createUser},</if>
         </trim>
    </insert>

    <update id="updateCompanyVoiceRobotic" parameterType="CompanyVoiceRobotic">
        update company_voice_robotic
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="addType != null">add_type = #{addType},</if>
            <if test="robot != null">robot = #{robot},</if>
            <if test="dialogId != null">dialog_id = #{dialogId},</if>
            <if test="mode != null">mode = #{mode},</if>
            <if test="multiplier != null">multiplier = #{multiplier},</if>
            <if test="autoRecall != null">auto_recall = #{autoRecall},</if>
            <if test="recallTimes != null">recall_times = #{recallTimes},</if>
            <if test="cidGroupId != null">cid_group_id = #{cidGroupId},</if>
            <if test="weekDay1 != null">week_day1 = #{weekDay1},</if>
            <if test="startTime1 != null">start_time1 = #{startTime1},</if>
            <if test="endTime1 != null">end_time1 = #{endTime1},</if>
            <if test="weekDay2 != null">week_day2 = #{weekDay2},</if>
            <if test="startTime2 != null">start_time2 = #{startTime2},</if>
            <if test="endTime2 != null">end_time2 = #{endTime2},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyVoiceRoboticById" parameterType="Long">
        delete from company_voice_robotic where id = #{id}
    </delete>

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