<?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.srs.dormitory.mapper.DmsDormitoryDeptMapper">
	
	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsDormitoryDept">
		<id column="id" property="id"/>
		<result column="XYDM" property="xydm"/>
		<result column="dormitory_id" property="dormitoryId"/>
	</resultMap>
	
	<select id="selectRegByXydm" resultType="java.util.Map">
		SELECT dormitory_id,ssr.dept_code,ssr.dept_name XY,bed_count,is_lock FROM dms_dormitory_dept
		left join sys_dept ssr on dms_dormitory_dept.XYDM = ssr.dept_code
		WHERE ssr.dept_code IN
		<foreach collection="xydm" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		group by dormitory_id, ssr.dept_code, XY,bed_count,is_lock
	</select>
	<select id="selectCountByXYDM" resultType="java.util.Map">
		select XYDM,count(*) count
		from dms_dormitory_dept
		where XYDM in
		<foreach collection="collect" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		group by XYDM
	</select>

	<select id="getDeptBedCount" resultType="DomInfo">
		select t1.*,t2.dept_name
		from
			(
				select sum(a.bed_count) as bed_count,a.XYDM
				from dms_dormitory_dept as a
				GROUP BY a.XYDM
			) as t1
				left join sys_dept as t2 on t1.XYDM = t2.dept_code
	</select>

	<select id="deptBuildingCount" resultType="DomInfo" parameterType="DmsSearch">
		select t1.*,t2.dept_name,t3.`name` as building_name,t4.`name` as park_name
		from
			(
				select sum(a.bed_count) as bed_count,a.XYDM,c.building_id
				from dms_dormitory_dept as a
						 left join dms_dormitory as b on a.dormitory_id = b.id
						 left join dms_dormitory_floor as c on b.floor_id = c.id
				GROUP BY a.XYDM,c.building_id
			) as t1
				left join sys_dept as t2 on t1.XYDM = t2.dept_code
				left join dms_dormitory_building as t3 on t1.building_id = t3.id
				left join dms_dormitory_park as t4 on t3.park_id = t4.id
		<where>
			<if test="deptCode != null">AND t1.XYDM = #{deptCode}</if>
		</where>
	</select>

	<select id="listSum" resultType="ExportDmsReg">
		select c.dept_name,b.campus_name,b.park_name,b.building_name,b.room_no,a.bed_count,
			   (
				   select GROUP_CONCAT( a3.class_name,'(',a1.bed_count,'人)' )
				   from dms_dormitory_class as a1
							left join view_dorm_info as a2 on a1.dormitory_id = a2.id
							left join srs_class as a3 on a1.BJDM = a3.class_code
							left join srs_majors as a4 on a3.major_id = a4.major_id
							left join sys_dept as a5 on a4.college_id = a5.dept_id
				   where a5.dept_code = a.XYDM and a1.dormitory_id = a.dormitory_id
			   ) as class_sum
		from dms_dormitory_dept as a
				 left join view_dorm_info as b on a.dormitory_id = b.id
				 left join sys_dept as c on a.XYDM = c.dept_code
		order by a.XYDM;
	</select>

	<update id="lockAllDept">
		update dms_dormitory_dept as a set a.is_lock = 1 where a.is_lock != 1
	</update>

</mapper>
