<?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.CompanyVoiceCallerMapper">

    <resultMap type="CompanyVoiceCaller" id="CompanyVoiceCallerResult">
        <result property="callerId"    column="caller_id"    />
        <result property="callerLine"    column="caller_line"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="callerNo"    column="caller_no"    />
        <result property="mobile"    column="mobile"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCompanyVoiceCallerVo">
        select caller_id,caller_line, company_id, company_user_id, caller_no, mobile, status, remark from company_voice_caller
    </sql>

    <select id="selectCompanyVoiceCallerList" parameterType="CompanyVoiceCaller" resultMap="CompanyVoiceCallerResult">
        <include refid="selectCompanyVoiceCallerVo"/>
        <where>
            <if test="callerLine != null "> and caller_line = #{callerLine}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="callerNo != null  and callerNo != ''"> and caller_no = #{callerNo}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="selectCompanyVoiceCallerById" parameterType="Long" resultMap="CompanyVoiceCallerResult">
        <include refid="selectCompanyVoiceCallerVo"/>
        where caller_id = #{callerId}
    </select>
    <select id="selectCompanyVoiceCallerByUserId" resultType="com.fs.company.domain.CompanyVoiceCaller">
        select * from company_voice_caller where company_id=#{companyId} and company_user_id=#{companyUserId}
        <if test = "callerId != null ">
            and caller_id = #{callerId}
        </if>
    </select>
    <select id="selectCompanyVoiceCallerListVO" resultType="com.fs.company.vo.CompanyVoiceCallerListVO">
        select va.api_name as caller_line,vc.*,u.nick_name as company_user_nick_name,c.company_name
        from company_voice_caller vc
        left join company_voice_api va on vc.caller_line = va.api_id
        left join company c on c.company_id=vc.company_id
        left join  company_user u on u.user_id=vc.company_user_id
       <where>
           <if test="maps.callerLine != null">
               and vc.caller_line = #{maps.callerLine}
           </if>
           <if test="maps.companyId != null">
               and vc.company_id = #{maps.companyId}
           </if>
           <if test="maps.companyUserId != null">
               and vc.company_user_id = #{maps.companyUserId}
           </if>
           <if test="maps.callerNo != null and maps.callerNo != ''">
               and vc.caller_no like '%${maps.callerNo}%'
           </if>
           <if test="maps.mobile != null and maps.mobile != ''">
               and vc.mobile like '%${maps.mobile}%'
           </if>
           <if test="maps.companyUserNickName != null and maps.companyUserNickName != ''">
               and u.nick_name like '%${maps.companyUserNickName}%'
           </if>
           <if test="maps.status != null">
               and vc.status = #{maps.status}
           </if>
       </where>
        order by vc.caller_id desc
    </select>
    <select id="selectCompanyVoiceCallerByEnable" resultType="com.fs.company.domain.CompanyVoiceCaller">
        select * from company_voice_caller where  status=1
        <if test="callerLine != null">
            and caller_line = #{callerLine}
        </if>
    </select>

    <insert id="insertCompanyVoiceCaller" parameterType="CompanyVoiceCaller" useGeneratedKeys="true" keyProperty="callerId">
        insert into company_voice_caller
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="callerLine != null">caller_line,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="callerNo != null">caller_no,</if>
            <if test="mobile != null">mobile,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="callerLine != null">#{callerLine},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="callerNo != null">#{callerNo},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCompanyVoiceCaller" parameterType="CompanyVoiceCaller">
        update company_voice_caller
        <trim prefix="SET" suffixOverrides=",">
            <if test="callerLine != null">caller_line = #{callerLine},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="callerNo != null">caller_no = #{callerNo},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where caller_id = #{callerId}
    </update>

    <delete id="deleteCompanyVoiceCallerById" parameterType="Long">
        delete from company_voice_caller where caller_id = #{callerId}
    </delete>

    <delete id="deleteCompanyVoiceCallerByIds" parameterType="String">
        delete from company_voice_caller where caller_id in
        <foreach item="callerId" collection="array" open="(" separator="," close=")">
            #{callerId}
        </foreach>
    </delete>

</mapper>
