<?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.scheduling.ShiftWorkRulesMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.NAME,
		f.CORPINFO_ID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.PARENTID,
		f.LEVEL,
		f.ISCUSTOM,
		f.SHIFTWORKRULES_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		NAME,
		CORPINFO_ID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		PARENTID,
		LEVEL,
		ISCUSTOM,
		SHIFTWORKRULES_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{NAME},
		#{CORPINFO_ID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{PARENTID},
		#{LEVEL},
		#{ISCUSTOM},
		#{SHIFTWORKRULES_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',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			SHIFTWORKRULES_ID = #{SHIFTWORKRULES_ID}
	</delete>

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

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			<if test="NAME != null and NAME != ''"><!-- 关键词检索 -->
				NAME = #{NAME},
			</if>
			<if test="ISCUSTOM != null and ISCUSTOM != ''"><!-- 关键词检索 -->
				ISCUSTOM = #{ISCUSTOM},
			</if>
			<if test="OPERATOR != null and OPERATOR != ''"><!-- 关键词检索 -->
				OPERATOR = #{OPERATOR},
			</if>
			<if test="OPERATTIME != null and OPERATTIME != ''"><!-- 关键词检索 -->
				OPERATTIME = #{OPERATTIME},
			</if>

			SHIFTWORKRULES_ID = SHIFTWORKRULES_ID
		where
			SHIFTWORKRULES_ID = #{SHIFTWORKRULES_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.SHIFTWORKRULES_ID = #{SHIFTWORKRULES_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.PARENTID != null and pd.PARENTID != ''"><!-- 关键词检索 -->
			and f.PARENTID = #{pd.PARENTID}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.LEVEL != null and pd.LEVEL != ''"><!-- 关键词检索 -->
			and f.LEVEL = #{pd.LEVEL}
		</if>

	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include> f
		WHERE f.ISDELETE = '0'
			AND f.CORPINFO_ID = #{CORPINFO_ID}
		<if test="PARENTID != null and PARENTID != ''"><!-- 关键词检索 -->
			AND f.PARENTID = #{PARENTID}
		</if>
	</select>

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


	<!-- 排班类型1是否使用 -->
	<select id="isUseOne" parameterType="pd" resultType="int">
		SELECT COUNT(1) FROM SYS_USER WHERE SHIFTDUTYONE = #{SHIFTWORKRULES_ID}
	</select>
	<!-- 排班类型2是否使用 -->
	<select id="isUseTwo" parameterType="pd" resultType="int">
		SELECT COUNT(1) FROM SYS_USER WHERE SHIFTDUTYTWO = #{SHIFTWORKRULES_ID}
	</select>
</mapper>
