<?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.jeeplus.modules.iim.mapper.LayGroupUserMapper">
    
	<sql id="layGroupUserColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.user_id AS "user.id",
		a.group_id AS "group.id",
		t_user.name AS "user.name",
		t_user.photo AS "user.photo",
		t_user.login_name AS "user.loginName"
	</sql>
	
	<sql id="layGroupUserJoins">
		LEFT JOIN t_group b ON b.id = a.group_id
		LEFT JOIN sys_user t_user ON t_user.id = a.user_id
	</sql>
	
    
	<select id="get" resultType="LayGroupUser" >
		SELECT 
			<include refid="layGroupUserColumns"/>
		FROM group_user a
		<include refid="layGroupUserJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="LayGroupUser" >
		SELECT 
			<include refid="layGroupUserColumns"/>
		FROM group_user a
		<include refid="layGroupUserJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="user != null and user.id != null and user.id != ''">
				AND a.user_id = #{user.id}
			</if>
			<if test="group != null and group.id != null and group.id != ''">
				AND a.group_id = #{group.id}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="LayGroupUser" >
		SELECT 
			<include refid="layGroupUserColumns"/>
		FROM group_user a
		<include refid="layGroupUserJoins"/>
		<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.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO group_user(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			user_id,
			group_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{user.id},
			#{group.id}
		)
	</insert>
	
	<update id="update">
		UPDATE group_user SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			user_id = #{user.id},
			group_id = #{group.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM group_user
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE group_id = #{group.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE group_user SET 
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE group_id = #{group.id}
			</otherwise>
		</choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LayGroupUser" statementType="STATEMENT">
		select * FROM group_user  where ${propertyName} = '${value}'
	</select>
	
</mapper>