<?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.specialoperation.SpecialOperationsMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CREATOR,
		f.UNITS_ID,
		f.PTYPE,
		f.JOB_TYPE,
		f.OPERATIONITEM,
		f.NAME,
		f.SEX,
		f.CERTIFICATE_NUM,
		f.REVIEWTIME,
		f.PHONENUM,
		f.IDENTITYCARD,
		f.LICENSING,
		f.VALID_ETIME,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.SPECIALOPERATIONS_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CREATOR,
		UNITS_ID,
		PTYPE,
		JOB_TYPE,
		OPERATIONITEM,
		NAME,
		SEX,
		CERTIFICATE_NUM,
		REVIEWTIME,
		PHONENUM,
		PASSWORD,
		IDENTITYCARD,
		LICENSING,
		VALID_ETIME,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		SPECIALOPERATIONS_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CREATOR},
		#{UNITS_ID},
		#{PTYPE},
		#{JOB_TYPE},
		#{OPERATIONITEM},
		#{NAME},
		#{SEX},
		#{CERTIFICATE_NUM},
		#{REVIEWTIME},
		#{PHONENUM},
		#{PASSWORD},
		#{IDENTITYCARD},
		#{LICENSING},
		#{VALID_ETIME},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{SPECIALOPERATIONS_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
			SPECIALOPERATIONS_ID = #{SPECIALOPERATIONS_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		UNITS_ID = #{UNITS_ID},
			PTYPE = #{PTYPE},
			JOB_TYPE = #{JOB_TYPE},
			OPERATIONITEM = #{OPERATIONITEM},
			NAME = #{NAME},
			SEX = #{SEX},
			CERTIFICATE_NUM = #{CERTIFICATE_NUM},
			REVIEWTIME = #{REVIEWTIME},
			PHONENUM = #{PHONENUM},
			IDENTITYCARD = #{IDENTITYCARD},
			LICENSING = #{LICENSING},
		VALID_ETIME = #{VALID_ETIME}
		where
			SPECIALOPERATIONS_ID = #{SPECIALOPERATIONS_ID}
	</update>

	<!-- 重置密码 -->
	<update id="editPassword" parameterType="pd">
		update
		<include refid="tableName"></include>
		set PASSWORD = #{PASSWORD}
		where
		SPECIALOPERATIONS_ID = #{SPECIALOPERATIONS_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findForLogin" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.PHONENUM = #{PHONENUM} and f.PASSWORD = #{PASSWORD}
		and f.ISDELETE='0'
	</select>

	<!-- 通过社会信用代码获取数据 -->
	<select id="findByCertificate" parameterType="pd" resultType="pd" >
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.CERTIFICATE_NUM = #{CERTIFICATE_NUM}
		<if test="SPECIALOPERATIONS_ID != null and SPECIALOPERATIONS_ID !=''">
			and f.SPECIALOPERATIONS_ID != #{SPECIALOPERATIONS_ID}
		</if>
	</select>
	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		u.CORPINFO_ID
		from
		<include refid="tableName"></include> f
		left join bus_units u on f.UNITS_ID = u.UNITS_ID
		where
			f.SPECIALOPERATIONS_ID = #{SPECIALOPERATIONS_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		f.*,
		a.NAME as JOBTYPE,
		o.NAME AS OPERATIONITEM_NAME,
		r.UNITS_NAME as UNITS_NAME,
		u.`NAME` CREATOR_NAME,
		c.CORP_NAME as CREATOR_CORP_NAME
		from
		<include refid="tableName"></include> f
		left join sys_dictionaries a on a.DICTIONARIES_ID = f.JOB_TYPE
		left join sys_dictionaries o on o.DICTIONARIES_ID = f.OPERATIONITEM
		left join bus_corp_info b on b.CORPINFO_ID = f.CORPINFO_ID
		left join bus_units r on r.UNITS_ID = f.UNITS_ID
		LEFT JOIN sys_user u on f.CREATOR = u.USERNAME and u.ISDELETE = '0'
		LEFT JOIN bus_corp_info c on u.CORPINFO_ID = c.CORPINFO_ID
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
			f.CERTIFICATE_NUM LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			f.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			r.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.JOB_TYPE != null and pd.JOB_TYPE !=''">
			and f.JOB_TYPE = #{pd.JOB_TYPE}
		</if>
		<if test="pd.PTYPE != null and pd.PTYPE !=''">
			and f.PTYPE = #{pd.PTYPE}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''">
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		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'
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''">
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="UNITS_ID != null and UNITS_ID !=''">
			and f.UNITS_ID = #{UNITS_ID}
		</if>
		<if test="IDS != null and IDS.size()>0">
			and f.UNITS_ID in
			<foreach collection="IDS" index="index" item="item"   open="(" separator="," close=")">
				#{IDS[${index}]}
			</foreach>
		</if>
	</select>

	<select id="namelist" parameterType="pd" resultType="pd">
		select
		concat("姓名：",f.NAME,'，',"编号：",f.CERTIFICATE_NUM) NAME
		from
		<include refid="tableName"></include> f

		left join sys_dictionaries a on a.DICTIONARIES_ID = f.JOB_TYPE
		left join bus_corp_info b on b.CORPINFO_ID = f.CORPINFO_ID
		left join bus_units r on r.UNITS_ID = f.UNITS_ID
		where f.ISDELETE = '0'
		<if test="CORPINFO_ID != null and ORPINFO_ID != ''">
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		ORDER BY f.OPERATTIME DESC
	</select>

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

</mapper>
