<?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.relatedparty.UnitsMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.UNITS_NAME,
		f.CODE,
		f.COMPANY_AREA,
		f.ADDRESS,
		f.CONTACTS,
		f.CONTACTS_PHONE,
		f.LR_MOBILE,
		f.EMPLOYEES,
		f.CREATE_DATE,
		f.REGCAPITAL,
		f.TOTALASSETS,
		f.UNITS_TYPE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.UNITS_ID,
		f.PROVINCE,
		f.CITY,
		f.COUNTY,
		f.VILLAGE,
		f.STREET,
		f.FILEPATH

	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		UNITS_NAME,
		CODE,
		COMPANY_AREA,
		ADDRESS,
		CONTACTS,
		CONTACTS_PHONE,
		LR_MOBILE,
		EMPLOYEES,
		CREATE_DATE,
		REGCAPITAL,
		TOTALASSETS,
		UNITS_TYPE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		UNITS_ID,
		PROVINCE,
		CITY,
		COUNTY,
		VILLAGE,
		STREET,
		FILEPATH
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{UNITS_NAME},
		#{CODE},
		#{COMPANY_AREA},
		#{ADDRESS},
		#{CONTACTS},
		#{CONTACTS_PHONE},
		#{LR_MOBILE},
		#{EMPLOYEES},
		#{CREATE_DATE},
		#{REGCAPITAL},
		#{TOTALASSETS},
		#{UNITS_TYPE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{UNITS_ID},
		#{PROVINCE},
		#{CITY},
		#{COUNTY},
		#{VILLAGE},
		#{STREET},
		#{FILEPATH}
	</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
			UNITS_ID = #{UNITS_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			UNITS_NAME = #{UNITS_NAME},
			CODE = #{CODE},
			COMPANY_AREA = #{COMPANY_AREA},
			ADDRESS = #{ADDRESS},
			CONTACTS = #{CONTACTS},
			CONTACTS_PHONE = #{CONTACTS_PHONE},
			LR_MOBILE = #{LR_MOBILE},
			EMPLOYEES = #{EMPLOYEES},
			CREATE_DATE = #{CREATE_DATE},
			REGCAPITAL = #{REGCAPITAL},
			TOTALASSETS = #{TOTALASSETS},
			UNITS_TYPE = #{UNITS_TYPE},
			UNITS_ID = UNITS_ID,
			PROVINCE = #{PROVINCE},
			CITY = #{CITY},
			COUNTY = #{COUNTY},
			VILLAGE = #{VILLAGE},
			STREET = #{STREET},
			FILEPATH = #{FILEPATH}
		where
			UNITS_ID = #{UNITS_ID}
	</update>

	<!-- 列出重复的相关方名称 -->
	<select id="findByName"  parameterType="pd" resultType="pd" >
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.UNITS_NAME = #{UNITS_NAME}
		<if test="UNITS_ID != null and UNITS_ID !=''">
			and f.UNITS_ID != #{UNITS_ID}
		</if>
	</select>

	<!-- 通过社会信用代码获取数据 -->
	<select id="findByCode" parameterType="pd" resultType="pd" >
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.CODE = #{CODE}
		<if test="UNITS_ID != null and UNITS_ID !=''">
			and f.UNITS_ID != #{UNITS_ID}
		</if>
	</select>
	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		f.UNITS_NAME,
		f.CODE,
		f.COMPANY_AREA,
		f.ADDRESS,
		f.CONTACTS,
		f.CONTACTS_PHONE,
		f.LR_MOBILE,
		f.EMPLOYEES,
		f.CREATE_DATE,
		f.REGCAPITAL,
		f.TOTALASSETS,
		f.UNITS_TYPE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.UNITS_ID,
		f.PROVINCE,
		f.CITY,
		f.COUNTY,
		f.VILLAGE,
		f.STREET,
		f.FILEPATH
		from
		<include refid="tableName"></include> f
		where
			f.UNITS_ID = #{UNITS_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		u.`NAME` CREATOR_NAME,
		c.CORP_NAME
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_user u on f.CREATOR = u.USERNAME
		LEFT JOIN bus_corp_info c on u.CORPINFO_ID = c.CORPINFO_ID
		where f.ISDELETE = '0' and u.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				<!--	根据需求自己加检索条件-->
					f.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
			        f.CONTACTS LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')

				)
		</if>
		<if test="pd.UNITS_TYPE != null and pd.UNITS_TYPE !=''">
			and f.UNITS_TYPE = #{pd.UNITS_TYPE}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.ORPINFO_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' and f.CORPINFO_ID = #{CORPINFO_ID}
	</select>

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

</mapper>
