<?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.house365.ws.dao.mapper.UserWxRelationMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.UserWxRelation">
        <result property="id" column="id"/>
        <result property="wxId" column="wx_id"/>
        <result property="userId" column="user_id"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createId" column="create_id"/>
        <result property="createName" column="create_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateId" column="update_id"/>
        <result property="updateName" column="update_name"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <insert id="save" parameterType="com.house365.beans.entity.UserWxRelation">
        INSERT INTO user_wx_relation (
            wx_id,
            user_id,
            is_delete,
            create_id,
            create_name,
            create_time,
            update_id,
            update_name,
            update_time
        )
        VALUES (
            #{wxId},
            #{userId},
            #{isDelete},
            #{createId},
            #{createName},
            #{createTime},
            #{updateId},
            #{updateName},
            #{updateTime}
        )
    </insert>

    <select id="getById" resultMap="infoMap" parameterType="Integer">
        SELECT *
        FROM user_wx_relation
        where is_delete = 0
        AND id = #{id}
    </select>

    <update id="update" parameterType="com.house365.beans.entity.UserWxRelation">
        UPDATE user_wx_relation
        SET
        is_delete = #{isDelete},
        update_id = #{updateId},
        update_name = #{updateName},
        update_time = #{updateTime}
        WHERE id = #{id}
    </update>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM user_wx_relation
        where is_delete = 0
        <if test="userId != null and userId != ''">
            AND user_id = #{userId}
        </if>
        <if test="wxId != null and wxId != ''">
            AND wx_id = #{wxId}
        </if>
    </select>

    <select id="queryList" resultType="map" parameterType="map">
        SELECT
            uw.id AS id,
            uw.user_id AS  userId,
            uw.wx_id AS wxId,
            uw.create_time AS createTime,
            u.name AS name,
            u.deptId AS deptId,
            u.realName AS realName,
            wx.user_name AS userName,
            uw.create_name AS createName
        FROM
           user_wx_relation uw
        LEFT JOIN user u
           ON u.id = uw.user_id
        LEFT JOIN wx_user wx
           ON wx.wx_id = uw.wx_id
        WHERE uw.is_delete = 0
        <if test="userName != null and userName != ''">
            AND wx.user_name LIKE CONCAT('%',#{userName},'%')
        </if>
        <if test="realName != null and realName != ''">
            AND u.realName LIKE CONCAT('%',#{realName},'%')
        </if>
    </select>

</mapper>