<?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.bus.SpecialUserMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.USERNAME,
		f.SEX,
		f.PHONE,
		f.USER_ID_CARD,
		f.SPECIAL_TYPE,
		f.OPERATION_TYPE,
		f.ISSUING_AUTHORITY,
		f.REVIEW_TIME,
		f.VALIDITY_TIME_END,
		f.VALIDITY_TIME_START,
		f.USER_ID,
		f.USER_TYPE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.SPARE1,
		f.SPARE2,
		f.SPARE3,
		f.SPARE4,
		f.SPARE5,
		f.SPECIAL_NUMBER,
		f.UNIT_ID,
		f.SPECIALUSER_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		USERNAME,
		SEX,
		PHONE,
		USER_ID_CARD,
		SPECIAL_TYPE,
		OPERATION_TYPE,
		ISSUING_AUTHORITY,
		REVIEW_TIME,
		USER_ID,
		USER_TYPE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		SPARE1,
		SPARE2,
		SPARE3,
		SPARE4,
		SPARE5,
		SPECIAL_NUMBER,
		VALIDITY_TIME_END,
		VALIDITY_TIME_START,
		ISDELETE,
		UNIT_ID,
		SPECIALUSER_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USERNAME},
		#{SEX},
		#{PHONE},
		#{USER_ID_CARD},
		#{SPECIAL_TYPE},
		#{OPERATION_TYPE},
		#{ISSUING_AUTHORITY},
		#{REVIEW_TIME},
		#{USER_ID},
		#{USER_TYPE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{SPARE1},
		#{SPARE2},
		#{SPARE3},
		#{SPARE4},
		#{SPARE5},
		#{SPECIAL_NUMBER},
		#{VALIDITY_TIME_END},
		#{VALIDITY_TIME_START},
		#{ISDELETE},
		#{UNIT_ID},
		#{SPECIALUSER_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
			SPECIALUSER_ID = #{SPECIALUSER_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			USERNAME = #{USERNAME},
			SEX = #{SEX},
			PHONE = #{PHONE},
			USER_ID_CARD = #{USER_ID_CARD},
			SPECIAL_TYPE = #{SPECIAL_TYPE},
			OPERATION_TYPE = #{OPERATION_TYPE},
			ISSUING_AUTHORITY = #{ISSUING_AUTHORITY},
			VALIDITY_TIME_END = #{VALIDITY_TIME_END},
			VALIDITY_TIME_START = #{VALIDITY_TIME_START},
			REVIEW_TIME = #{REVIEW_TIME},
			SPECIAL_NUMBER = #{SPECIAL_NUMBER},
			USER_ID = #{USER_ID},
			USER_TYPE = #{USER_TYPE},
			CREATOR = #{CREATOR},
			CREATTIME = #{CREATTIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			SPARE1 = #{SPARE1},
			SPARE2 = #{SPARE2},
			SPARE3 = #{SPARE3},
			SPARE4 = #{SPARE4},
			SPARE5 = #{SPARE5},
			SPECIALUSER_ID = SPECIALUSER_ID
		where
			SPECIALUSER_ID = #{SPECIALUSER_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		IF(f.USER_TYPE='1',corp.CORP_NAME,unit.RELEVANT_NAME) unitName,
		cuser.USERNAME as cuserName ,
		ccorp.CORP_NAME as corpName ,
		poDic.NAME as operationName
		from
		<include refid="tableName"></include> f
		left join bus_relevantunit unit on unit.RELEVANTUNIT_ID = f.UNIT_ID<!-- 相关单位 -->
		left join bus_corp_info corp on corp.CORPINFO_ID = f.UNIT_ID<!-- 相关单位 企业-->
		left join sys_user cuser on  cuser.USER_ID = f.CREATOR<!-- 添加人信息-->
		left join bus_corp_info ccorp on ccorp.CORPINFO_ID = cuser.CORPINFO_ID <!-- 添加人信息 企业-->
		left join sys_dictionaries poDic on poDic.DICTIONARIES_ID = f.OPERATION_TYPE <!-- 操作项目-->
		where
			f.SPECIALUSER_ID = #{SPECIALUSER_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		IF(f.USER_TYPE='1',corp.CORP_NAME,unit.RELEVANT_NAME) unitName,
		cuser.USERNAME as cuserName ,
		ccorp.CORP_NAME as corpName ,
		poDic.NAME as operationName
		from
		<include refid="tableName"></include> f
		left join bus_relevantunit unit on unit.RELEVANTUNIT_ID = f.UNIT_ID<!-- 相关单位 -->
		left join bus_corp_info corp on corp.CORPINFO_ID = f.UNIT_ID<!-- 相关单位 企业-->
		left join sys_user cuser on  cuser.USER_ID = f.CREATOR<!-- 添加人信息-->
		left join bus_corp_info ccorp on ccorp.CORPINFO_ID = cuser.CORPINFO_ID <!-- 添加人信息 企业-->
		left join sys_dictionaries poDic on poDic.DICTIONARIES_ID = f.OPERATION_TYPE <!-- 操作项目-->
		where f.ISDELETE = '0'
		<if test="pd.keySpecialType != null and pd.keySpecialType != ''"><!-- 作业类别 -->
			and f.SPECIAL_TYPE = #{pd.keySpecialType}
		</if>
		<if test="pd.keyTypeIn1And2 != null and pd.keyTypeIn1And2 != ''"><!-- 人员类型 -->
			and f.USER_TYPE  in ('1','2')
		</if>
		<if test="pd.keyUserType != null and pd.keyUserType != ''"><!-- 人员类型 -->
			and f.USER_TYPE = #{pd.keyUserType}
		</if>
		<if test="pd.keyUserName != null and pd.keyUserName != ''"><!-- 姓名 -->
			and(f.USERNAME LIKE CONCAT(CONCAT('%', #{pd.keyUserName}),'%'))
		</if>
		<if test="pd.keyUnitName != null and pd.keyUnitName != ''"><!-- 单位名称 -->
			and(unit.RELEVANT_NAME LIKE CONCAT(CONCAT('%', #{pd.keyUnitName}),'%')
			 or corp.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.keyUnitName}),'%')  )
		</if>
		<if test="pd.keySpecialNumber != null and pd.keySpecialNumber != ''"><!-- 特种作业证书编号 -->
			and(f.SPECIAL_NUMBER LIKE CONCAT(CONCAT('%', #{pd.keySpecialNumber}),'%'))
		</if>
		order by f.CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,u.NAME as userName,dept.NAME as deptName
		from
		<include refid="tableName"></include> f
		left join SYS_USER u on u.USER_ID = f.USER_ID
		left join oa_department dept on dept.DEPARTMENT_ID = u.DEPARTMENT_ID
		 where f.ISDELETE = '0'
		<if test="UNIT_ID != null and UNIT_ID != ''"><!-- 人员类型 -->
			and f.UNIT_ID = #{UNIT_ID}
		</if>
		<if test="endday != null and endday !=''">
			and f.VALIDITY_TIME_END <![CDATA[ >= ]]> #{endday}
		</if>
		order by f.CREATTIME desc
	</select>

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

	<select id="hasState" parameterType="pd" resultType="Integer">
		SELECT count(*) as count
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="USER_ID_CARD != null and USER_ID_CARD != ''" ><!-- 关键词检索 -->
			and  f.USER_ID_CARD =  #{USER_ID_CARD}
		</if>
		<if test="SPECIAL_NUMBER != null and SPECIAL_NUMBER != ''" ><!-- 关键词检索 -->
			and  f.SPECIAL_NUMBER =  #{SPECIAL_NUMBER}
		</if>
	</select>
	<!-- 删除-->
	<delete id="deleteTem" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		SPECIALUSER_ID = #{SPECIALUSER_ID}
	</delete>
</mapper>
