<?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.qw.mapper.QwContactMapper">

    <resultMap type="QwContact" id="QwContactResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="sex"    column="sex"    />
        <result property="nickName"    column="nick_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="avatar"    column="avatar"    />
        <result property="corpId"    column="corp_id"    />
        <result property="corpRemark"    column="corp_remark"    />
        <result property="remarkPhone"    column="remark_phone"    />
        <result property="describes"    column="describes"    />
        <result property="deleted"    column="deleted"    />
        <result property="labels"    column="labels"    />
        <result property="position"    column="position"    />
        <result property="aliasName"    column="alias_name"    />
        <result property="remark"    column="remark"    />
        <result property="accountId"    column="account_id"    />
        <result property="homeUserId"    column="home_user_id"    />
    </resultMap>

    <sql id="selectQwContactVo">
        select id, user_id, sex, nick_name, create_time, avatar, corp_id, corp_remark, remark_phone, describes, deleted, labels, position, alias_name, remark, account_id, home_user_id from qw_contact
    </sql>

    <select id="selectQwContactList" parameterType="QwContact" resultMap="QwContactResult">
        <include refid="selectQwContactVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="sex != null "> and sex = #{sex}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
            <if test="corpRemark != null  and corpRemark != ''"> and corp_remark = #{corpRemark}</if>
            <if test="remarkPhone != null  and remarkPhone != ''"> and remark_phone = #{remarkPhone}</if>
            <if test="describes != null  and describes != ''"> and describes = #{describes}</if>
            <if test="deleted != null "> and deleted = #{deleted}</if>
            <if test="labels != null  and labels != ''"> and labels = #{labels}</if>
            <if test="position != null  and position != ''"> and position = #{position}</if>
            <if test="aliasName != null  and aliasName != ''"> and alias_name like concat('%', #{aliasName}, '%')</if>
            <if test="accountId != null  and accountId != ''"> and account_id = #{accountId}</if>
            <if test="homeUserId != null  and homeUserId != ''"> and home_user_id = #{homeUserId}</if>
        </where>
    </select>

    <select id="selectQwContactById" parameterType="Long" resultMap="QwContactResult">
        <include refid="selectQwContactVo"/>
        where id = #{id}
    </select>


    <insert id="insertQwContact" parameterType="QwContact" useGeneratedKeys="true" keyProperty="id">
        insert into qw_contact
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="sex != null">sex,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="avatar != null">avatar,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="corpRemark != null">corp_remark,</if>
            <if test="remarkPhone != null">remark_phone,</if>
            <if test="describes != null">describes,</if>
            <if test="deleted != null">deleted,</if>
            <if test="labels != null">labels,</if>
            <if test="position != null">position,</if>
            <if test="aliasName != null">alias_name,</if>
            <if test="remark != null">remark,</if>
            <if test="accountId != null">account_id,</if>
            <if test="homeUserId != null">home_user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="sex != null">#{sex},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="corpRemark != null">#{corpRemark},</if>
            <if test="remarkPhone != null">#{remarkPhone},</if>
            <if test="describes != null">#{describes},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="labels != null">#{labels},</if>
            <if test="position != null">#{position},</if>
            <if test="aliasName != null">#{aliasName},</if>
            <if test="remark != null">#{remark},</if>
            <if test="accountId != null">#{accountId},</if>
            <if test="homeUserId != null">#{homeUserId},</if>
        </trim>
    </insert>

    <update id="updateQwContact" parameterType="QwContact">
        update qw_contact
        <trim prefix="SET" suffixOverrides=",">
            <if test="sex != null">sex = #{sex},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="corpRemark != null">corp_remark = #{corpRemark},</if>
            <if test="remarkPhone != null">remark_phone = #{remarkPhone},</if>
            <if test="describes != null">describes = #{describes},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
            <if test="labels != null">labels = #{labels},</if>
            <if test="position != null">position = #{position},</if>
            <if test="aliasName != null">alias_name = #{aliasName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="accountId != null">account_id = #{accountId},</if>
        </trim>
        where user_id = #{userId} and home_user_id = #{homeUserId}
    </update>

    <delete id="deleteQwContactById" parameterType="Long">
        delete from qw_contact where id = #{id}
    </delete>

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

</mapper>
