<?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.ruoyi.cc.mapper.CcExtNumMapper">
    
    <resultMap type="CcExtNum" id="CcExtNumResult">
        <result property="extId"    column="ext_id"    />
        <result property="extNum"    column="ext_num"    />
        <result property="extPass"    column="ext_pass"    />
        <result property="userCode"    column="user_code"    />
    </resultMap>

    <sql id="selectCcExtNumVo">
        select ext_id, ext_num, ext_pass, user_code from cc_ext_num
    </sql>

    <select id="selectCcExtNumList" parameterType="CcExtNum" resultMap="CcExtNumResult">
        <include refid="selectCcExtNumVo"/>
        <where>  
            <if test="extNum != null "> and ext_num = #{extNum}</if>
            <if test="extPass != null  and extPass != ''"> and ext_pass = #{extPass}</if>
<!--            <if test="userCode != null  and userCode != ''"> and user_code = #{userCode}</if>-->
            <if test="userCode != null  and userCode != ''"> and user_code like concat('%', #{userCode}, '%')</if>
            <if test="sipExtNumList != null and sipExtNumList.size() > 0">
                and ext_num in
                <foreach collection="sipExtNumList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by ext_num asc
    </select>
    
    <select id="selectCcExtNumByExtId" parameterType="Long" resultMap="CcExtNumResult">
        <include refid="selectCcExtNumVo"/>
        where ext_id = #{extId}
    </select>

    <insert id="insertCcExtNum" parameterType="CcExtNum" useGeneratedKeys="true" keyProperty="extId">
        insert into cc_ext_num
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ext_num,
            ext_pass,
            user_code,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{extNum},
            #{extPass},
            #{userCode},
         </trim>
    </insert>

    <update id="updateCcExtNum" parameterType="CcExtNum">
        update cc_ext_num
        <trim prefix="SET" suffixOverrides=",">
            ext_num = #{extNum},
            ext_pass = #{extPass},
            user_code = #{userCode},
        </trim>
        where ext_id = #{extId}
    </update>

    <delete id="deleteCcExtNumByExtId" parameterType="Long">
        delete from cc_ext_num where ext_id = #{extId}
    </delete>

    <delete id="deleteCcExtNumByExtIds" parameterType="String">
        delete from cc_ext_num where ext_id in 
        <foreach item="extId" collection="array" open="(" separator="," close=")">
            #{extId}
        </foreach>
    </delete>

    <select id="selectUnBindCcExtNumList" resultMap="CcExtNumResult">
        <include refid="selectCcExtNumVo"/>
        where user_code = ''
    </select>
    <update id="cleanCcExtNumByUserCode" parameterType="String">
        update cc_ext_num set user_code = '' where user_code = #{userCode}
    </update>
    <update id="updateCcExtNumByUserCode" parameterType="CcExtNum">
        update cc_ext_num set user_code = #{userCode} where ext_num = #{extNum}
    </update>

    <select id="selectUnBindCcExtNumListPage" parameterType="CcExtNum" resultMap="CcExtNumResult">
        <include refid="selectCcExtNumVo"/>
        where user_code = ''
        <if test="extNum != null">and ext_num = #{extNum}</if>
        <if test="extPass != null and extPass != ''">and ext_pass = #{extPass}</if>
        <if test="startExtNum != null">and ext_num &gt;= #{startExtNum}</if>
        <if test="endExtNum != null">and ext_num  &lt;= #{endExtNum}</if>
        order by ext_num asc
    </select>

    <update id="updateCompanyBindExtNum">
        update cc_ext_num set user_code = #{userCode} where ext_num in
        <foreach item="extNum" collection="sipExtNumList" open="(" separator="," close=")">
            #{extNum}
        </foreach>
    </update>
    <update id="companyUnbindExtNum">
        update cc_ext_num set user_code = '' where user_code in
        <foreach item="userCode" collection="list" open="(" separator="," close=")">
            #{userCode}
        </foreach>
    </update>

    <insert id="batchInsertCcExtNum" parameterType="java.util.List">
        insert into cc_ext_num (ext_num, ext_pass, user_code)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.extNum}, #{item.extPass}, #{item.userCode})
        </foreach>
    </insert>

    <select id="selectExtNumsGreaterThan" parameterType="java.lang.Long" resultType="java.lang.Long">
        select ext_num from cc_ext_num where ext_num &gt; #{minExtNum}
    </select>

    <select id="selectMaxExtNum" resultType="java.lang.Long">
        select max(ext_num) from cc_ext_num
    </select>

    <update id="companyBatchUnbindSipExt">
        update cc_ext_num set user_code = '' where ext_num in
        <foreach item="extNum" collection="extNums" open="(" separator="," close=")">
            #{extNum}
        </foreach>
    </update>
    <update id="updateUserCodeByLoginName">
        update cc_ext_num set user_code = #{companyName} where user_code = #{loginName}
    </update>
</mapper>