<?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.system.FHlogMapper">

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

	<!-- 字段 -->
	<sql id="Field">
		USERNAME,
		CZTIME,
		CONTENT,
		FHLOG_ID,
		IP,
		SOURCE,
		TYPE,
		NAME,
		CORPINFO_ID,
		DEPARTMENT,
		DEPARTMENT_ID,
		USER_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USERNAME},
		#{CZTIME},
		#{CONTENT},
		#{FHLOG_ID},
		#{IP},
		#{SOURCE},
		#{TYPE},
		#{NAME},
		#{CORPINFO_ID},
		#{DEPARTMENT},
		#{DEPARTMENT_ID},
		#{USER_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>
    <insert id="saveGobleLog" parameterType="pd">
		INSERT INTO log_${HTTP_UR2L}
		    (`ID`, `HTTP_URL`, `TIME_CONSUMING`, `PARAMETER`, `RESULT_INFO`, `SUCCESS`, `CREATED_TIME`,CREATOR)
		VALUES (
		        #{ID},
		        #{HTTP_URL},
		        #{TIME_CONSUMING},
		        #{PARAMETER},
		        #{RESULT_INFO},
		        #{SUCCESS},
		        #{CREATED_TIME},
		        #{CREATOR}
			   )
	</insert>

    <!-- 删除-->
	<delete id="delete" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
			FHLOG_ID = #{FHLOG_ID}
	</delete>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where 1=1
		<if test="pd.KEYWORDS!= null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					USERNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.STARTTIME!=null and pd.STARTTIME!=''"><!-- 开始时间检索 -->
			and CZTIME &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME!=null and pd.ENDTIME!=''"><!-- 结束时间检索 -->
			and CZTIME &lt;= #{pd.ENDTIME}
		</if>
		order by CZTIME desc
	</select>

		<!-- 列表(无IP） -->
	<select id="getlistPage" parameterType="page" resultType="pd">
		select
			f.USERNAME,
			f.CZTIME,
			f.CONTENT,
			f.FHLOG_ID,
			f.SOURCE,
			f.TYPE,
			f.CORPINFO_ID,
			f.NAME,
			f.DEPARTMENT,
			getFullName(f.DEPARTMENT_ID) DEPARTMENT_ALL,
			f.DEPARTMENT_ID,
			f.USER_ID
		from
		<include refid="tableName"></include> f
		where 1=1
		<if test="pd.KEYWORDS!= null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.USERNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.STARTTIME!=null and pd.STARTTIME!=''"><!-- 开始时间检索 -->
			and f.CZTIME &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME!=null and pd.ENDTIME!=''"><!-- 结束时间检索 -->
			and f.CZTIME &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.SOURCE!=null and pd.SOURCE!=''">
			and f.SOURCE = #{pd.SOURCE}
		</if>
		<if test="pd.TYPE!=null and pd.TYPE!=''">
			and f.TYPE = #{pd.TYPE}
		</if>
		<if test="pd.CORPINFO_ID!=null and pd.CORPINFO_ID!=''">
			and f.CORPINFO_ID = #{pd.CORPINFO_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
					f.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
										f.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="pd.DEPT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
									</when>
									<otherwise>
										and
										((f.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.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.USER_ID = #{pd.USER_ID}
								and
								f.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>

								and
								f.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})
							</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
								f.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								f.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 f.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
		                and f.USER_ID = #{pd.USER_ID}
		            </when>
		            <otherwise>
		            </otherwise>
		        </choose>
			</otherwise>
		</choose>
		order by f.CZTIME desc
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		delete from
		<include refid="tableName"></include>
		where
			FHLOG_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
