<?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.dsno1.corp.CorpTypeMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.NAME,
		f.PARENT_ID,
		f.DESCR,
		f.CORPTYPE_ORDER,
		f.CORPTYPE_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		NAME,
		PARENT_ID,
		DESCR,
		CORPTYPE_ORDER,
		CORPTYPE_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{NAME},
		#{PARENT_ID},
		#{DESCR},
		#{CORPTYPE_ORDER},
		#{CORPTYPE_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">
		delete from
		<include refid="tableName"></include>
		where
			CORPTYPE_ID = #{CORPTYPE_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			NAME = #{NAME},
			PARENT_ID = #{PARENT_ID},
			DESCR = #{DESCR},
			CORPTYPE_ORDER=#{CORPTYPE_ORDER},
			CORPTYPE_ID = CORPTYPE_ID
		where
			CORPTYPE_ID = #{CORPTYPE_ID}
	</update>
	<select id="corptypelist" parameterType="pd" resultType="pd">
		SELECT
		f.CORPTYPE_AREA_ID,
		f.CORPTYPE_ID,
		f.NAME,
		f.CORPTYPE_ORDER,
		f.PARENT_ID,
		f.LEVEL
		FROM
		bus_corptype f
		LEFT JOIN BUS_CORPTYPE_AREA t ON t.CORPTYPE_AREA_ID = f.CORPTYPE_AREA_ID
		WHERE
		f.ISDELETE = '0' and f.PARENT_ID = '0'
		<if test="PROVINCE != null and PROVINCE != ''"><!-- 关键词检索 -->
			and t.PROVINCE = #{PROVINCE}
		</if>
		<if test="CITY != null and CITY != ''"><!-- 关键词检索 -->
			and t.CITY = #{CITY}
		</if>
		<if test="COUNTY != null and COUNTY != ''"><!-- 关键词检索 -->
			and t.COUNTY = #{COUNTY}
		</if>
		<if test="CORPTYPE_AREA_ID != null and CORPTYPE_AREA_ID != ''"><!-- 关键词检索 -->
			and t.CORPTYPE_AREA_ID = #{CORPTYPE_AREA_ID}
		</if>
		order by f.CORPTYPE_ORDER
	</select>

	<select id="nextcorptypelist" parameterType="pd" resultType="pd">
		SELECT
		f.CORPTYPE_AREA_ID,
		f.CORPTYPE_ID,
		f.NAME,
		f.CORPTYPE_ORDER,
		f.PARENT_ID,
		f.LEVEL
		FROM
		bus_corptype f
		LEFT JOIN BUS_CORPTYPE_AREA t ON t.CORPTYPE_AREA_ID = f.CORPTYPE_AREA_ID
		WHERE
		f.ISDELETE = '0' and f.PARENT_ID != '0'
		<if test="PROVINCE != null and PROVINCE != ''"><!-- 关键词检索 -->
			and t.PROVINCE = #{PROVINCE}
		</if>
		<if test="CITY != null and CITY != ''"><!-- 关键词检索 -->
			and t.CITY = #{CITY}
		</if>
		<if test="COUNTY != null and COUNTY != ''"><!-- 关键词检索 -->
			and t.COUNTY = #{COUNTY}
		</if>
		<if test="CORPTYPE_AREA_ID != null and CORPTYPE_AREA_ID != ''"><!-- 关键词检索 -->
			and t.CORPTYPE_AREA_ID = #{CORPTYPE_AREA_ID}
		</if>
		order by f.CORPTYPE_ORDER
	</select>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where 1=1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				<!--	根据需求自己加检索条件
					字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				-->
				)
		</if>
	</select>

	<!--通过ID获取其子一级菜单 -->
	<select id="listAllByParentId" parameterType="pd" resultType="pd">
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include> f
		where PARENT_ID = #{PARENT_ID}
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
		</if>
		order by CORPTYPE_ORDER
	</select>

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

	<!-- 列表(全部子集) -->
	<select id="listAllChild" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.CORPTYPE_ID not in (select p.PARENT_ID from BUS_CORPTYPE p )
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		delete from
		<include refid="tableName"></include>
		where
			CORPTYPE_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
    <!--通过ID获取其子一级菜单 -->
    <select id="listMentDictionByPId" parameterType="String" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include> f
        where 1=1
        <if  test="_parameter != null and _parameter != ''">
            and PARENT_ID = #{_parameter}
        </if>
        <if  test="_parameter == null and _parameter == ''">
            and PARENT_ID  != '0'
        </if>
        order by CORPTYPE_ORDER
    </select>
    <!--根据企业id获取对应的企业类型 -->
    <select id="listByCorpInfoTypeIds" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include> f
        where 1=1
        and CORPTYPE_ID in
        <foreach item="item" index="index" collection="corpTypeIds" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>
