<?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.RelevantUnitMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.RELEVANT_NAME,
		f.RELEVANT_CODE,
		f.PROVINCE,
		f.CITY,
		f.COUNTRY,
		f.VILLAGE,
		f.PRINCIPAL_USER,
		f.PRINCIPAL_NUMBER,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.SPARE1,
		f.SPARE2,
		f.SPARE3,
		f.SPARE4,
		f.SPARE5,
		f.ISDELETE,
		f.RELEVANTUNIT_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		RELEVANT_NAME,
		RELEVANT_CODE,
		PROVINCE,
		CITY,
		COUNTRY,
		VILLAGE,
		PRINCIPAL_USER,
		PRINCIPAL_NUMBER,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		SPARE1,
		SPARE2,
		SPARE3,
		SPARE4,
		SPARE5,
		ISDELETE,
		RELEVANTUNIT_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{RELEVANT_NAME},
		#{RELEVANT_CODE},
		#{PROVINCE},
		#{CITY},
		#{COUNTRY},
		#{VILLAGE},
		#{PRINCIPAL_USER},
		#{PRINCIPAL_NUMBER},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{SPARE1},
		#{SPARE2},
		#{SPARE3},
		#{SPARE4},
		#{SPARE5},
		#{ISDELETE},
		#{RELEVANTUNIT_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
		RELEVANTUNIT_ID = #{RELEVANTUNIT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		RELEVANT_NAME = #{RELEVANT_NAME},
		RELEVANT_CODE = #{RELEVANT_CODE},
		PROVINCE = #{PROVINCE},
		CITY = #{CITY},
		COUNTRY = #{COUNTRY},
		VILLAGE = #{VILLAGE},
		PRINCIPAL_USER = #{PRINCIPAL_USER},
		PRINCIPAL_NUMBER = #{PRINCIPAL_NUMBER},
		CREATOR = #{CREATOR},
		CREATTIME = #{CREATTIME},
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		SPARE1 = #{SPARE1},
		SPARE2 = #{SPARE2},
		SPARE3 = #{SPARE3},
		SPARE4 = #{SPARE4},
		SPARE5 = #{SPARE5},
		ISDELETE = #{ISDELETE},
		RELEVANTUNIT_ID = RELEVANTUNIT_ID
		where
		RELEVANTUNIT_ID = #{RELEVANTUNIT_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>, c.CORP_NAME as corpName , u.USERNAME as userName
		from
		<include refid="tableName"></include> f
		left join sys_user u on u.USER_ID = f.CREATOR
		left join bus_corp_info c on c.CORPINFO_ID = u.CORPINFO_ID
		where
		f.RELEVANTUNIT_ID = #{RELEVANTUNIT_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>, c.CORP_NAME as corpName , u.USERNAME as userName
		from
		<include refid="tableName"></include> f
		 left join sys_user u on u.USER_ID = f.CREATOR
		left join bus_corp_info c on c.CORPINFO_ID = u.CORPINFO_ID
		where f.ISDELETE = '0'
		<if test="pd.keycorpName != null and pd.keycorpName != ''"><!-- 关键词检索 -->
			and( c.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.keycorpName}),'%'))
		</if>
		<if test="pd.keyUnitName != null and pd.keyUnitName != ''"><!-- 关键词检索 -->
			and ( f.RELEVANT_NAME LIKE CONCAT(CONCAT('%', #{pd.keyUnitName}),'%'))
		</if>
		<if test="pd.keyprincipalUser != null and pd.keyprincipalUser != ''"><!-- 关键词检索 -->
			and ( f.PRINCIPAL_USER LIKE CONCAT(CONCAT('%', #{pd.keyprincipalUser}),'%'))
		</if>
		order by f.CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		order by f.CREATTIME desc
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1'
		where
		RELEVANTUNIT_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="RELEVANT_NAME != null and RELEVANT_NAME != ''" ><!-- 关键词检索 -->
			and f.RELEVANT_NAME =  #{RELEVANT_NAME}
		</if>
		<if test="RELEVANT_CODE != null and RELEVANT_CODE != ''"><!-- 关键词检索 -->
			and f.RELEVANT_CODE = #{RELEVANT_CODE}
		</if>
	</select>
	<!-- 列表(全部) -->
	<select id="listNameAll" parameterType="pd" resultType="pd">
		select
		RELEVANT_NAME ,RELEVANTUNIT_ID
		from
		<include refid="tableName"></include> f
	</select>
</mapper>
