<?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="MdeFriendRelationMapper">
	<resultMap type="com.wechat.model.dbEntity.MdeFriendRelation" id="MdeFriendRelation">
		<id column="user_id_1" property="userId1" />
		<result column="user_id_2" property="userId2" />
		<result column="to_be_confirmed" property="toBeConfirmed" />
		<result column="confirm_time" property="confirmTime" />
		<result column="version_no" property="versionNo" />
		<result column="remarks" property="remarks" />
		<result column="del_flag" property="delFlag" />
		<result column="create_by" property="createBy" />
		<result column="create_date" property="createDate" />
		<result column="update_by" property="updateBy" />
		<result column="update_date" property="updateDate" />
		
		<result column="user_code" property="userCode" />
		<result column="name" property="name" />
		<result column="head_portrait" property="headPortrait"/>
	</resultMap>
	<sql id="mdeFriendRelationColumns">
		a.user_id_1 AS "userId1",
		a.user_id_2 AS "userId2",
		a.to_be_confirmed AS "toBeConfirmed",
		a.confirm_time AS "confirmTime",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate"
	</sql>

	<sql id="mdeFriendRelationJoins">
	</sql>

	<select id="getInfoByCode" resultMap="MdeFriendRelation">
		SELECT
		<include refid="mdeFriendRelationColumns" />
		FROM mde_friend_relation a
		<include refid="mdeFriendRelationJoins" />
		WHERE a.user_id_1 = LEAST(cast(#{userId1} as signed),cast(#{userId2} as signed)) 
		and a.user_id_2 = GREATEST(cast(#{userId1} as signed),cast(#{userId2} as signed))
		and a.DEL_FLAG ="0" 
	</select>

	<select id="findList" resultMap="MdeFriendRelation">
		SELECT
		<include refid="mdeFriendRelationColumns" />
		FROM mde_friend_relation a
		<include refid="mdeFriendRelationJoins" />
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when
				test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultMap="MdeFriendRelation">
		SELECT
		<include refid="mdeFriendRelationColumns" />
		FROM mde_friend_relation a
		<include refid="mdeFriendRelationJoins" />
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when
				test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<insert id="insert">
		INSERT INTO mde_friend_relation(
		user_id_1,
		user_id_2,
		to_be_confirmed,
		version_no,
		remarks,
		del_flag,
		create_by,
		create_date,
		update_by,
		update_date
		) VALUES (
		
		LEAST(cast(#{userId1} as signed),cast(#{userId2} as signed)),
		GREATEST(cast(#{userId1} as signed),cast(#{userId2} as signed)),
		#{toBeConfirmed},
		#{versionNo},
		#{remarks},
		#{delFlag},
		#{createBy},
		#{createDate},
		#{updateBy},
		#{updateDate}
		)
	</insert>

	<update id="update">
		UPDATE mde_friend_relation SET
		USER_ID_1 = LEAST(cast(#{userId1} as signed),cast(#{userId2} as signed)),
		USER_ID_2 = GREATEST(cast(#{userId1} as signed),cast(#{userId2} as signed)),
		TO_BE_CONFIRMED = #{toBeConfirmed},
		CONFIRM_TIME = #{confirmTime},
		VERSION_NO = version_no +1,
		REMARKS = #{remarks},
		DEL_FLAG = #{delFlag},
		UPDATE_BY = #{updateBy},
		UPDATE_DATE = #{updateDate}
		WHERE user_id_1 = LEAST(cast(#{userId1} as signed),cast(#{userId2} as signed)) and
		user_id_2 = GREATEST(cast(#{userId1} as signed),cast(#{userId2} as signed))
		and version_no = #{versionNo} and DEL_FLAG ="0"
	</update>

	 <update id="delete">
		UPDATE mde_friend_relation SET
		version_no = version_no +1,
		del_flag = #{DEL_FLAG_DELETE}
		WHERE a.user_id_1 = #{id1} and
		a.user_id_2 = #{id2} 
		and version_no = #{versionNo}
	</update> 
	<delete id="deleteRecords">
		DELETE FROM mde_friend_relation 
		WHERE 
		USER_ID_1 = #{userId1} and USER_ID_2=#{userId2}
		and TO_BE_CONFIRMED=#{toBeConfirmed} 
		and DEL_FLAG='0' 
		and CONFIRM_TIME is null
	</delete>

<!-- 	<select id="getRequestForFriends" resultMap="MdeFriendRelation">
		SELECT
		<include refid="mdeFriendRelationColumns" />
		FROM mde_friend_relation a
		where a.DEL_FLAG ="0" 
		and a.TO_BE_CONFIRMED= #{userCode} 
		and a.CONFIRM_TIME is null
	</select> -->
	
	<!-- 获取交友信息  - 申请人信息 -->
	<select id="getBriefInfoOfFriendInvitation" resultMap="MdeFriendRelation">
		select u.user_code , u.name ,a.update_date,u.head_portrait 
		FROM mde_user u 
		inner join (		
			select case TO_BE_CONFIRMED when USER_ID_1 then USER_ID_2 when USER_ID_2 then  USER_ID_1 end as user_code ,UPDATE_DATE
			FROM mde_friend_relation 
			where DEL_FLAG ="0" and TO_BE_CONFIRMED= #{userCode} and CONFIRM_TIME is null 
		)  a on a.user_code = u.user_code
		where u.DEL_FLAG =0
	</select>

	<select id="findRequestRecordByCodes" resultMap="MdeFriendRelation">
		SELECT
		a.user_id_1 AS "userId1",
		a.user_id_2 AS "userId2",
		a.to_be_confirmed AS "toBeConfirmed",
		a.confirm_time AS "confirmTime",
		a.version_no AS "versionNo",
		a.del_flag AS "delFlag"
		FROM mde_friend_relation a
		where
		(USER_ID_1 = #{userId1} or USER_ID_2=#{userId1})
		and a.DEL_FLAG ="0" 
		and a.TO_BE_CONFIRMED = #{userId2} 
		and a.CONFIRM_TIME is null
		limit 1
	</select>
	<!-- 删除好友信息 -->
	<delete id="delFriends">
		delete from mde_friend_relation 
		where user_id_1 = LEAST(cast(#{userId1} as signed),cast(#{userId2} as signed))
		and user_id_2 = GREATEST(cast(#{userId1} as signed),cast(#{userId2} as signed))
	</delete>


</mapper>