<?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.CompanyWxClientMapper">
    
    <resultMap type="CompanyWxClient" id="CompanyWxClientResult">
        <result property="id"    column="id"    />
        <result property="roboticWxId"    column="robotic_wx_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="phone"    column="phone"    />
        <result property="wxNo"    column="wx_no"    />
        <result property="isAdd"    column="is_add"    />
        <result property="addTime"    column="add_time"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCompanyWxClientVo">
        select a.* from company_wx_client a
    </sql>

    <select id="selectCompanyWxClientList" resultType="com.fs.company.domain.CompanyWxClient">
        select a.*,b.wx_nick_name,c.name as dialogName,robotic.name roboticName from company_wx_client a
        inner join company_voice_robotic_wx wx on a.robotic_wx_id = wx.id
        inner join company_wx_account b on wx.account_id = b.id
        inner join company_wx_dialog c on a.dialog_id = c.id
        inner join company_voice_robotic robotic on a.robotic_id = robotic.id
        <where>
            <if test="roboticId != null "> and a.robotic_id = #{roboticId}</if>
            <if test="roboticWxId != null "> and b.id = #{roboticWxId}</if>
            <if test="customerId != null "> and a.customer_id = #{customerId}</if>
            <if test="dialogId != null "> and a.dialog_id = #{dialogId}</if>
            <if test="nickName != null  and nickName != ''"> and a.nick_name = #{nickName}</if>
            <if test="avatar != null  and avatar != ''"> and a.avatar = #{avatar}</if>
            <if test="phone != null  and phone != ''"> and a.phone = #{phone}</if>
            <if test="wxNo != null  and wxNo != ''"> and a.wx_no = #{wxNo}</if>
            <if test="isAdd != null "> and a.is_add = #{isAdd}</if>
            <if test="addTime != null "> and a.add_time = #{addTime}</if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                and date_format(a.add_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                AND date_format(a.add_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
        </where>
    </select>
    <select id="selectCompanyWxClientListCompany" resultType="com.fs.company.domain.CompanyWxClient">
        select a.*,b.wx_nick_name,c.name as dialogName,robotic.name roboticName from company_wx_client a
        inner join company_voice_robotic_wx wx on a.robotic_wx_id = wx.id
        inner join company_wx_account b on wx.account_id = b.id
        inner join company_user u on b.company_user_id = u.user_id
        inner join company_dept d on u.dept_id = d.dept_id
        inner join company_wx_dialog c on a.dialog_id = c.id
        inner join company_voice_robotic robotic on a.robotic_id = robotic.id
        <where>
            <if test="roboticId != null "> and a.robotic_id = #{roboticId}</if>
            <if test="roboticWxId != null "> and b.id = #{roboticWxId}</if>
            <if test="customerId != null "> and a.customer_id = #{customerId}</if>
            <if test="dialogId != null "> and a.dialog_id = #{dialogId}</if>
            <if test="nickName != null  and nickName != ''"> and a.nick_name = #{nickName}</if>
            <if test="avatar != null  and avatar != ''"> and a.avatar = #{avatar}</if>
            <if test="phone != null  and phone != ''"> and a.phone = #{phone}</if>
            <if test="wxNo != null  and wxNo != ''"> and a.wx_no = #{wxNo}</if>
            <if test="isAdd != null "> and a.is_add = #{isAdd}</if>
            <if test="addTime != null "> and a.add_time = #{addTime}</if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                and date_format(a.add_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                AND date_format(a.add_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            ${params.dataScope}
        </where>
    </select>
    
    <select id="selectCompanyWxClientById" parameterType="Long" resultMap="CompanyWxClientResult">
        <include refid="selectCompanyWxClientVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertCompanyWxClient" parameterType="CompanyWxClient" useGeneratedKeys="true" keyProperty="id">
        insert into company_wx_client
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="roboticWxId != null">robotic_wx_id,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="phone != null">phone,</if>
            <if test="wxNo != null">wx_no,</if>
            <if test="isAdd != null">is_add,</if>
            <if test="addTime != null">add_time,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="roboticWxId != null">#{roboticWxId},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="phone != null">#{phone},</if>
            <if test="wxNo != null">#{wxNo},</if>
            <if test="isAdd != null">#{isAdd},</if>
            <if test="addTime != null">#{addTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateCompanyWxClient" parameterType="CompanyWxClient">
        update company_wx_client
        <trim prefix="SET" suffixOverrides=",">
            <if test="roboticWxId != null">robotic_wx_id = #{roboticWxId},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="wxNo != null">wx_no = #{wxNo},</if>
            <if test="isAdd != null">is_add = #{isAdd},</if>
            <if test="addTime != null">add_time = #{addTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyWxClientById" parameterType="Long">
        delete from company_wx_client where id = #{id}
    </delete>

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

    <select id="toDayAddWxAccountList" resultType="com.fs.company.domain.CompanyWxClient">
        select a.*,b.wx_nick_name,c.name as dialogName,c.template_details
--                ,d.task_name roboticName
        from company_wx_client a
           inner join company_voice_robotic_wx wx on a.robotic_wx_id = wx.id
           inner join company_wx_account b on wx.account_id = b.id
           inner join company_wx_dialog c on a.dialog_id = c.id
--            inner join company_voice_robotic d on a.robotic_id = d.id
--            inner join aiob_baidu_task d on a.robotic_id = d.id
        where b.id = #{accountId} and b.company_user_id = #{companyId} and a.is_add = #{addType}
        <if test="addType == 2">
            AND (a.next_check_time IS NULL OR a.next_check_time &lt;= NOW())
        </if>
    </select>
    <select id="selectCompanyByAccountId" resultType="com.fs.company.domain.CompanyUser">
        select c.* from company_voice_robotic_wx a
        inner join company_wx_account b on a.account_id = b.id
        inner join company_user c on b.company_user_id = c.user_id
        where a.id = #{wxId}
    </select>
    <select id="listCompanyIds" resultType="com.fs.company.domain.CompanyWxClient">
        select a.*,b.company_user_id from
        company_wx_client a
        inner join company_wx_account b on a.account_id = b.id
        where b.company_user_id in <foreach collection="ids" open="(" separator="," close=")" item="item">#{item}</foreach>
    </select>
</mapper>