<?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.zcloud.mapper.datasource.offduty.OffDutyMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_OFFDUTY
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.USER_ID,
		f.CORPINFO_ID,
		f.STARTTIME,
		f.ENDTIME,
		f.DESCR,
		f.REVIEW_STATUS,
		f.REVIEW_DESC,
		f.REVIEW_USER_ID,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.OFFDUTY_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		USER_ID,
		CORPINFO_ID,
		STARTTIME,
		ENDTIME,
		DESCR,
		REVIEW_STATUS,
		REVIEW_DESC,
		REVIEW_USER_ID,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		OFFDUTY_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USER_ID},
		#{CORPINFO_ID},
		#{STARTTIME},
		#{ENDTIME},
		#{DESCR},
		#{REVIEW_STATUS},
		#{REVIEW_DESC},
		#{REVIEW_USER_ID},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{OFFDUTY_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			OFFDUTY_ID = #{OFFDUTY_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			<if test="USER_ID != null and USER_ID != ''"><!-- 关键词检索 -->
				USER_ID = #{USER_ID},
			</if>
			<if test="STARTTIME != null and STARTTIME != ''"><!-- 关键词检索 -->
				STARTTIME = #{STARTTIME},
			</if>
			<if test="ENDTIME != null and ENDTIME != ''"><!-- 关键词检索 -->
				ENDTIME = #{ENDTIME},
			</if>
			<if test="DESCR != null and DESCR != ''"><!-- 关键词检索 -->
				DESCR = #{DESCR},
			</if>
			<if test="REVIEW_STATUS != null and REVIEW_STATUS != ''"><!-- 申请状态 -->
				REVIEW_STATUS = #{REVIEW_STATUS},
			</if>
			<if test="REVIEW_DESC != null and REVIEW_DESC != ''"><!-- 审批意见 -->
				REVIEW_DESC = #{REVIEW_DESC},
			</if>
			<if test="REVIEW_USER_ID != null and REVIEW_USER_ID != ''"><!-- 审批人 -->
				REVIEW_USER_ID = #{REVIEW_USER_ID},
			</if>
			<if test="ISDELETE != null and ISDELETE != ''"><!-- 删除标记 -->
				ISDELETE = #{ISDELETE},
			</if>
			<if test="OPERATOR != null and OPERATOR != ''"><!-- 修改人 -->
				OPERATOR = #{OPERATOR},
			</if>
			<if test="OPERATTIME != null and OPERATTIME != ''"><!-- 修改时间 -->
				OPERATTIME = #{OPERATTIME},
			</if>
			OFFDUTY_ID = OFFDUTY_ID
		where
			OFFDUTY_ID = #{OFFDUTY_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		u.NAME USER_NAME,
		d.NAME DEPARTMENTNAME,
		getFullName(d.DEPARTMENT_ID) DEPARTMENTNAME_ALL,
		p.NAME POSTNAME,
		ru.NAME REVIEW_USER_NAME,
		rd.NAME REVIEW_USER_DEPARTMENTNAME
		from
		<include refid="tableName"></include> f
		left join sys_user u on u.USER_ID = f.USER_ID
		left join oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join sys_post p on p.POST_ID = u.POST_ID
		left join sys_user ru on ru.USER_ID = f.REVIEW_USER_ID
		left join oa_department rd on rd.DEPARTMENT_ID = ru.DEPARTMENT_ID
		where
			f.OFFDUTY_ID = #{OFFDUTY_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		u.NAME USER_NAME,
		d.NAME DEPARTMENTNAME,
		getFullName(d.DEPARTMENT_ID) DEPARTMENTNAME_ALL,
		p.NAME POSTNAME,
		ru.NAME REVIEW_USER_NAME,
		rd.NAME REVIEW_USER_DEPARTMENTNAME
		from
		<include refid="tableName"></include> f
		left join sys_user u on u.USER_ID = f.USER_ID
		left join oa_department d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		left join sys_post p on p.POST_ID = u.POST_ID
		left join sys_user ru on ru.USER_ID = f.REVIEW_USER_ID
		left join oa_department rd on rd.DEPARTMENT_ID = ru.DEPARTMENT_ID
		where (f.ISDELETE = '0' or (f.ISDELETE = '1' and f.REVIEW_STATUS = '-1'))
		and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					u.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.REVIEW_STATUS != null and pd.REVIEW_STATUS != ''"><!-- 关键词检索 -->
			<choose>
				<when test="pd.REVIEW_STATUS == 0">
					and f.REVIEW_STATUS = '0'
				</when>
				<when test="pd.REVIEW_STATUS == 1">
					and f.REVIEW_STATUS = '1'
				</when>
				<when test="pd.REVIEW_STATUS == 2">
					and f.REVIEW_STATUS = '2'
				</when>
				<when test="pd.REVIEW_STATUS == -1">
					and f.ISDELETE = '0' and f.REVIEW_STATUS = '-1'
				</when>
				<when test="pd.REVIEW_STATUS == 3">
					and f.CREATOR = f.OPERATOR and f.ISDELETE = '1' and f.REVIEW_STATUS = '-1'
				</when>
				<when test="pd.REVIEW_STATUS == 4">
					and f.CREATOR != f.OPERATOR and f.ISDELETE = '1' and f.REVIEW_STATUS = '-1'
				</when>
			</choose>
		</if>
		<if test="pd.REVIEW_USER_NAME != null and pd.REVIEW_USER_NAME != ''"><!-- 关键词检索 -->
			and ru.NAME LIKE CONCAT(CONCAT('%', #{pd.REVIEW_USER_NAME}),'%')
		</if>
		<if test="pd.DEPT_DOWN_ARR != null and pd.DEPT_DOWN_ARR.size() > 0">
			AND d.DEPARTMENT_ID IN
			<foreach item="item" collection="pd.DEPT_DOWN_ARR" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
<!-- 		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">关键词检索
			and d.DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
		</if>
		 -->

		<choose>
			<when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
				<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
	                and
					d.DEPARTMENT_ID in
					<foreach item="item" index="index"
						collection="pd.DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
	            </if>
			</when>
			<otherwise>
				<choose>
		            <when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
						<choose>
							<when test='pd.ISLEADER != null and pd.ISLEADER != "" and pd.ISLEADER == "1"'>
								<choose>
									<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
										and
										d.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="pd.DEPT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
									</when>
									<otherwise>
										and
										((d.DEPARTMENT_ID in (
										select
										SUB_DEPARTMENT_ID
										from
										oa_supervision_department osd
										where
										osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
										and osd.ISDELETE = '0'
										and osd.CORPINFO_ID  = #{pd.CORPINFO_ID}
										)) or (
										d.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
										))
									</otherwise>
								</choose>
							</when>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								or f.REVIEW_USER_ID = #{pd.USER_ID}
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>

								and
								d.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{pd.CORPINFO_ID})
								or f.REVIEW_USER_ID = #{pd.USER_ID}
							</otherwise>
						</choose>
					</when>
					<when test='pd.ISLEADER != null and pd.ISLEADER != "" and pd.ISLEADER == "1"'>
						<choose>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								d.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</otherwise>
						</choose>
					</when>
		            <when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
		                and d.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
		                and f.REVIEW_USER_ID = #{pd.USER_ID}
		            </when>
		            <otherwise>
		            </otherwise>
		        </choose>
			</otherwise>
		</choose>
		<choose>
			<when test="pd.STARTTIME != null and pd.STARTTIME != '' and pd.ENDTIME != null and pd.ENDTIME != ''">
				and DATE_FORMAT(#{pd.STARTTIME},'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(f.STARTTIME,'%Y-%m-%d %H:%i:%s')
				and DATE_FORMAT(#{pd.ENDTIME},'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(f.ENDTIME,'%Y-%m-%d %H:%i:%s')
			</when>
			<otherwise>
				<if test="pd.STARTTIME != null and pd.STARTTIME != ''">
					and DATE_FORMAT(#{pd.STARTTIME},'%Y-%m-%d %H:%i:%s') &lt;= DATE_FORMAT(f.ENDTIME,'%Y-%m-%d %H:%i:%s')
				</if>
				<if test="pd.ENDTIME != null and pd.ENDTIME != ''">
					and DATE_FORMAT(#{pd.ENDTIME},'%Y-%m-%d %H:%i:%s') &gt;= DATE_FORMAT(f.STARTTIME,'%Y-%m-%d %H:%i:%s')
				</if>
			</otherwise>
		</choose>
		order by f.OPERATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where (f.ISDELETE = '0' or (f.ISDELETE = '1' and f.REVIEW_STATUS = '-1'))
		<if test="USER_ID != null and USER_ID != ''"><!-- 关键词检索 -->
			and USER_ID = #{USER_ID}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="ISREST != null and ISREST != ''"><!-- 关键词检索 -->
			and now() between CONCAT(STARTTIME,' 00:00:00') and CONCAT(ENDTIME,' 23:59:59') and f.REVIEW_STATUS in ('1', '2')
		</if>
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			OFFDUTY_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
