<?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.workcommongroup.dao.WorkCommonGroupDao">
    
	<sql id="workCommonGroupColumns">
		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.name AS "name",
		a.num AS "num",
		a.office_id AS "office.id",
		a.company_id AS "company.id"
	</sql>
	
	<sql id="workCommonGroupJoins">
        LEFT JOIN sys_office o on a.office_id = o.id
        LEFT JOIN sys_office s ON s.id = a.company_id
        LEFT JOIN sys_user u ON u.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkCommonGroup" >
		SELECT 
			<include refid="workCommonGroupColumns"/>
		FROM work_common_group a
		<include refid="workCommonGroupJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkCommonGroup" >
		SELECT 
			<include refid="workCommonGroupColumns"/>
		FROM work_common_group a
		<include refid="workCommonGroupJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="num != null and num != ''">
				AND a.num = #{num}
			</if>
			<if test="office != null and office.id != null and office.id != ''">
				AND a.office_id = #{office.id}
			</if>
			<if test="company != null and company.id != null and company.id != ''">
				AND a.company_id = #{company.id}
			</if>
            ${sqlMap.dsf}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.num ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkCommonGroup" >
		SELECT 
			<include refid="workCommonGroupColumns"/>
		FROM work_common_group a
		<include refid="workCommonGroupJoins"/>
		<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 work_common_group(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			name,
			num,
			office_id,
			company_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{name},
			#{num},
			#{office.id},
			#{company.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_common_group SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			num = #{num},
			office_id = #{office.id},
			company_id = #{company.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_common_group
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_common_group SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkCommonGroup" statementType="STATEMENT">
		select * FROM work_common_group  where ${propertyName} = '${value}'
	</select>

    <insert id="batchInsert" parameterType="java.util.Map">
        INSERT INTO work_common_group_detail
        (group_id ,user_id)
        VALUES
        <foreach collection="userList" item="user" separator=",">
            (#{groupId}, #{user.id})
        </foreach>
    </insert>

    <delete id="deleteUsers">
        DELETE FROM work_common_group_detail
        WHERE group_id = #{id}
    </delete>

    <select id="queryUserList" resultType="User">
        SELECT t1.id,t1.name,GROUP_CONCAT(t3.name ORDER BY t3.parent_ids SEPARATOR '-') AS 'officeName'
        FROM work_common_group_detail t
        LEFT JOIN sys_user t1 ON t.user_id = t1.id
        LEFT JOIN sys_office t2 ON t1.office_id = t2.id
        LEFT JOIN sys_office t3 ON (FIND_IN_SET(t3.id,t2.parent_ids) OR t3.id = t2.id)
        WHERE t.group_id = #{id}
        GROUP BY t1.id;
    </select>
</mapper>