<?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.agency.AgentNumberMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.USER_ID,
		f.CORPLEVEL,
		f.NUMBER,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.AGENT_NUMBER_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		USER_ID,
		CORPLEVEL,
		NUMBER,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		AGENT_NUMBER_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USER_ID},
		#{CORPLEVEL},
		#{NUMBER},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{AGENT_NUMBER_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
			AGENT_NUMBER_ID = #{AGENT_NUMBER_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			USER_ID = #{USER_ID},
			CORPLEVEL = #{CORPLEVEL},
			NUMBER = #{NUMBER},
			AGENT_NUMBER_ID = AGENT_NUMBER_ID
		where
			AGENT_NUMBER_ID = #{AGENT_NUMBER_ID}
	</update>

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

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

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

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		r.ROLE_NAME
		from
		<include refid="tableName"></include> f
		left join sys_role r on r.ROLE_ID = f.CORPLEVEL
		where
		f.ISDELETE = '0'
		<if test="USER_ID != null and USER_ID != ''">
			and f.USER_ID = #{USER_ID}
		</if>
	</select>


	<!-- 列表(全部) -->
	<select id="listRoleAgentAll" parameterType="pd" resultType="pd">
		select
		r.ROLE_NAME,
		IFNULL(f.NUMBER,0) NUMBER,
		f.AGENT_NUMBER_ID,
		r.ROLE_ID,
		r.HAS_PLS
		from
		sys_role r
		LEFT JOIN BUS_AGENT_NUMBER f ON f.CORPLEVEL = r.ROLE_ID AND f.isdelete = '0' and f.USER_ID = #{USER_ID}
		where
		r.PARENT_ID = '0'
		 and r.ISMAIN = '0'
		ORDER BY r.SORT
	</select>

	<!-- 查询代理人所有分组可创建企业数 -->
	<select id="getRoleAgentNumList" parameterType="pd" resultType="pd">
		SELECT
			r.ROLE_ID,
			r.ROLE_NAME,
			r.SORT,
			r.PARENT_ID,
			a.AGENT_NUMBER_ID,
			a.USER_ID,
			IFNULL(a.NUMBER, 0) NUMBER
		FROM
			SYS_ROLE r
		LEFT JOIN BUS_AGENT_NUMBER a ON r.ROLE_ID = a.CORPLEVEL
			AND a.ISDELETE = '0'
			AND a.USER_ID = #{USER_ID}
		where
			r.PARENT_ID = '0'
		ORDER BY r.SORT
	</select>

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

	<!-- 删除某代理所有剩余创建企业数-->
	<delete id="deleteByUser" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1'
		where
		USER_ID = #{USER_ID}
	</delete>

	<!-- 剩余数量及删除状态 -->
	<update id="editNum" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		NUMBER = #{NUMBER}
		<if test="ISDELETE != null and ISDELETE != ''">
			,ISDELETE = #{ISDELETE}
		</if>
		where
		AGENT_NUMBER_ID = #{AGENT_NUMBER_ID}
	</update>

</mapper>
