<?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.hzya.frame.sys.templateFields.dao.impl.TemplateFieldsMapperImpl">
	<resultMap id="get-entity-result" type="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		<!--唯一标识码 -->
		<result property="id" column="id" />
		<!--模版id -->
		<result property="templateId" column="template_id" />
		<!--Service服务名 -->
		<result property="serviceName" column="service_name" />
		<!--Table名称（英文名称） -->
		<result property="tableName" column="table_name" />
		<!--字段表id -->
		<result property="fieldsId" column="fields_id" />
		<!--数据库表id -->
		<result property="databaseId" column="database_id" />
		<!--字段名称 -->
		<result property="fieldsName" column="fields_name" />
		<!--字段备注 -->
		<result property="fieldsRemark" column="fields_remark" />
		<!--类型 Vachar,number,decimal,datetime,array(如为array字段需设置明细数据) -->
		<result property="fieldsType" column="fields_type" />
		<!--长度 -->
		<result property="fieldsLength" column="fields_length" />
		<!--是否必填 -->
		<result property="fieldsRequired" column="fields_required" />
		<!--是否唯一性 -->
		<result property="fieldsSole" column="fields_sole" />
		<!--字段属性 设置字段的渲染属性，存json -->
		<result property="fieldsOption" column="fields_option" />
		<!--明细字段 只有数据结构为array可设置 -->
		<result property="detailFields" column="detail_fields" />
		<!--明细service名称 -->
		<result property="detailService" column="detail_service" />
		<!--明细表名 -->
		<result property="detailTable" column="detail_table" />
		<!--排序 -->
		<result property="sorts" column="sorts" />
		<!--创建人id -->
		<result property="createUserId" column="create_user_id" />
		<!--创建时间 -->
		<result property="createTime" column="create_time" />
		<!--修改人id -->
		<result property="modifyUserId" column="modify_user_id" />
		<!--修改时间 -->
		<result property="modifyTime" column="modify_time" />
		<!--状态(Y正常N删除) -->
		<result property="sts" column="sts" />
		<!--组织机构ID -->
		<result property="orgId" column="org_id" />
	</resultMap>

	<sql id="Base_Column_List">
		id,
		template_id,
		service_name,
		table_name,
		fields_id,
		database_id,
		fields_name,
		fields_remark,
		fields_type,
		fields_length,
		fields_required,
		fields_sole,
		fields_option,
		detail_fields,
		detail_service,
		detail_table,
		sorts,
		create_user_id,
		create_time,
		modify_user_id,
		modify_time,
		sts,
		org_id
	</sql>

	<!--通过ID获取数据 -->
	<select id="entity_get" resultMap="get-entity-result">
		select
		<include refid="Base_Column_List" />
		from sys_template_fields where id = #{ id } and sts='Y'
	</select>

	<!-- 查询 采用==查询 -->
	<select id="entity_list_base" resultMap="get-entity-result" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		select
		<include refid="Base_Column_List" />
		from sys_template_fields
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null and id !='' ">id = #{id} </if>
			<if test="templateId != null and templateId !='' "> and template_id = #{templateId}</if>
			<if test="serviceName != null and serviceName !='' "> and service_name = #{serviceName}</if>
			<if test="tableName != null and tableName !='' "> and table_name = #{tableName}</if>
			<if test="fieldsId != null and fieldsId !='' "> and fields_id = #{fieldsId}</if>
			<if test="databaseId != null and databaseId !='' "> and database_id = #{databaseId}</if>
			<if test="fieldsName != null and fieldsName !='' "> and fields_name = #{fieldsName}</if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> and fields_remark = #{fieldsRemark}</if>
			<if test="fieldsType != null and fieldsType !='' "> and fields_type = #{fieldsType}</if>
			<if test="fieldsLength != null and fieldsLength !='' "> and fields_length = #{fieldsLength}</if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> and fields_required = #{fieldsRequired}</if>
			<if test="fieldsSole != null and fieldsSole !='' "> and fields_sole = #{fieldsSole}</if>
			<if test="fieldsOption != null and fieldsOption !='' "> and fields_option = #{fieldsOption}</if>
			<if test="detailFields != null and detailFields !='' "> and detail_fields = #{detailFields}</if>
			<if test="detailService != null and detailService !='' "> and detail_service = #{detailService}</if>
			<if test="detailTable != null and detailTable !='' "> and detail_table = #{detailTable}</if>
			<if test="sorts != null "> and sorts = #{sorts}</if>
			<if test="createUserId != null and createUserId !='' "> and create_user_id = #{createUserId}</if>
			<if test="createTime != null "> and create_time = #{createTime}</if>
			<if test="modifyUserId != null and modifyUserId !='' "> and modify_user_id = #{modifyUserId}</if>
			<if test="modifyTime != null "> and modify_time = #{modifyTime}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="orgId != null and orgId !='' "> and org_id = #{orgId}</if>
		</trim>
		<if test=" sort =='' "> order by sorts asc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
	</select>

	<!-- 查询符合条件的数量 -->
	<select id="entity_count" resultType="Integer" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		select count(1) from sys_template_fields
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null and id !='' ">id = #{id} </if>
			<if test="templateId != null and templateId !='' "> and template_id = #{templateId}</if>
			<if test="serviceName != null and serviceName !='' "> and service_name = #{serviceName}</if>
			<if test="tableName != null and tableName !='' "> and table_name = #{tableName}</if>
			<if test="fieldsId != null and fieldsId !='' "> and fields_id = #{fieldsId}</if>
			<if test="databaseId != null and databaseId !='' "> and database_id = #{databaseId}</if>
			<if test="fieldsName != null and fieldsName !='' "> and fields_name = #{fieldsName}</if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> and fields_remark = #{fieldsRemark}</if>
			<if test="fieldsType != null and fieldsType !='' "> and fields_type = #{fieldsType}</if>
			<if test="fieldsLength != null and fieldsLength !='' "> and fields_length = #{fieldsLength}</if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> and fields_required = #{fieldsRequired}</if>
			<if test="fieldsSole != null and fieldsSole !='' "> and fields_sole = #{fieldsSole}</if>
			<if test="fieldsOption != null and fieldsOption !='' "> and fields_option = #{fieldsOption}</if>
			<if test="detailFields != null and detailFields !='' "> and detail_fields = #{detailFields}</if>
			<if test="detailService != null and detailService !='' "> and detail_service = #{detailService}</if>
			<if test="detailTable != null and detailTable !='' "> and detail_table = #{detailTable}</if>
			<if test="sorts != null "> and sorts = #{sorts}</if>
			<if test="createUserId != null and createUserId !='' "> and create_user_id = #{createUserId}</if>
			<if test="createTime != null "> and create_time = #{createTime}</if>
			<if test="modifyUserId != null and modifyUserId !='' "> and modify_user_id = #{modifyUserId}</if>
			<if test="modifyTime != null "> and modify_time = #{modifyTime}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="orgId != null and orgId !='' "> and org_id = #{orgId}</if>
		</trim>
	</select>

	<!-- 分页查询列表 采用like格式 -->
	<select id="entity_list_like" resultMap="get-entity-result" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		select
		<include refid="Base_Column_List" />
		from sys_template_fields
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null and id !='' "> id like concat('%',#{id},'%') </if>
			<if test="templateId != null and templateId !='' "> and template_id like concat('%',#{templateId},'%') </if>
			<if test="serviceName != null and serviceName !='' "> and service_name like concat('%',#{serviceName},'%') </if>
			<if test="tableName != null and tableName !='' "> and table_name like concat('%',#{tableName},'%') </if>
			<if test="fieldsId != null and fieldsId !='' "> and fields_id like concat('%',#{fieldsId},'%') </if>
			<if test="databaseId != null and databaseId !='' "> and database_id like concat('%',#{databaseId},'%') </if>
			<if test="fieldsName != null and fieldsName !='' "> and fields_name like concat('%',#{fieldsName},'%') </if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> and fields_remark like concat('%',#{fieldsRemark},'%') </if>
			<if test="fieldsType != null and fieldsType !='' "> and fields_type like concat('%',#{fieldsType},'%') </if>
			<if test="fieldsLength != null and fieldsLength !='' "> and fields_length like concat('%',#{fieldsLength},'%') </if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> and fields_required like concat('%',#{fieldsRequired},'%') </if>
			<if test="fieldsSole != null and fieldsSole !='' "> and fields_sole like concat('%',#{fieldsSole},'%') </if>
			<if test="fieldsOption != null and fieldsOption !='' "> and fields_option like concat('%',#{fieldsOption},'%') </if>
			<if test="detailFields != null and detailFields !='' "> and detail_fields like concat('%',#{detailFields},'%') </if>
			<if test="detailService != null and detailService !='' "> and detail_service like concat('%',#{detailService},'%') </if>
			<if test="detailTable != null and detailTable !='' "> and detail_table like concat('%',#{detailTable},'%') </if>
			<if test="sorts != null "> and sorts = #{sorts}</if>
			<if test="createUserId != null and createUserId !='' "> and create_user_id like concat('%',#{createUserId},'%') </if>
			<if test="createTime != null and createTime !='' "> and create_time like concat('%',#{createTime},'%') </if>
			<if test="modifyUserId != null and modifyUserId !='' "> and modify_user_id like concat('%',#{modifyUserId},'%') </if>
			<if test="modifyTime != null and modifyTime !='' "> and modify_time like concat('%',#{modifyTime},'%') </if>
			<if test="sts != null and sts !='' "> and sts like concat('%',#{sts},'%') </if>
			<if test="orgId != null and orgId !='' "> and org_id like concat('%',#{orgId},'%') </if>
		</trim>
		<if test=" sort =='' "> order by sorts asc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by  ${sort} ${order}</if>
	</select>

	<!-- 查询列表 字段采用or格式 -->
	<select id="entity_list_or" resultMap="get-entity-result" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		select
		<include refid="Base_Column_List" />
		from sys_template_fields
		<trim prefix="where" prefixOverrides="or">
			<if test="id != null and id !='' ">id = #{id} </if>
			<if test="templateId != null and templateId !='' "> or template_id = #{templateId} </if>
			<if test="serviceName != null and serviceName !='' "> or service_name = #{serviceName} </if>
			<if test="tableName != null and tableName !='' "> or table_name = #{tableName} </if>
			<if test="fieldsId != null and fieldsId !='' "> or fields_id = #{fieldsId} </if>
			<if test="databaseId != null and databaseId !='' "> or database_id = #{databaseId} </if>
			<if test="fieldsName != null and fieldsName !='' "> or fields_name = #{fieldsName} </if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> or fields_remark = #{fieldsRemark} </if>
			<if test="fieldsType != null and fieldsType !='' "> or fields_type = #{fieldsType} </if>
			<if test="fieldsLength != null and fieldsLength !='' "> or fields_length = #{fieldsLength} </if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> or fields_required = #{fieldsRequired} </if>
			<if test="fieldsSole != null and fieldsSole !='' "> or fields_sole = #{fieldsSole} </if>
			<if test="fieldsOption != null and fieldsOption !='' "> or fields_option = #{fieldsOption} </if>
			<if test="detailFields != null and detailFields !='' "> or detail_fields = #{detailFields} </if>
			<if test="detailService != null and detailService !='' "> or detail_service = #{detailService} </if>
			<if test="detailTable != null and detailTable !='' "> or detail_table = #{detailTable} </if>
			<if test="sorts != null "> or sorts = #{sorts}</if>
			<if test="createUserId != null and createUserId !='' "> or create_user_id = #{createUserId} </if>
			<if test="createTime != null and createTime !='' "> or create_time = #{createTime} </if>
			<if test="modifyUserId != null and modifyUserId !='' "> or modify_user_id = #{modifyUserId} </if>
			<if test="modifyTime != null and modifyTime !='' "> or modify_time = #{modifyTime} </if>
			<if test="sts != null and sts !='' "> or sts = #{sts} </if>
			<if test="orgId != null and orgId !='' "> or org_id = #{orgId} </if>
		</trim>
		<if test=" sort =='' "> order by sorts asc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by  ${sort} ${order}</if>
	</select>
	<!-- 新增 -->
	<insert id="entity_insert" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		insert into sys_template_fields(
		<trim suffix="" suffixOverrides=",">
			<if test="id != null and id !='' "> id, </if>
			<if test="templateId != null and templateId !='' "> template_id, </if>
			<if test="serviceName != null and serviceName !='' "> service_name, </if>
			<if test="tableName != null and tableName !='' "> table_name, </if>
			<if test="fieldsId != null and fieldsId !='' "> fields_id, </if>
			<if test="databaseId != null and databaseId !='' "> database_id, </if>
			<if test="fieldsName != null and fieldsName !='' "> fields_name, </if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> fields_remark, </if>
			<if test="fieldsType != null and fieldsType !='' "> fields_type, </if>
			<if test="fieldsLength != null and fieldsLength !='' "> fields_length, </if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> fields_required, </if>
			<if test="fieldsSole != null and fieldsSole !='' "> fields_sole, </if>
			<if test="fieldsOption != null and fieldsOption !='' "> fields_option, </if>
			<if test="detailFields != null and detailFields !='' "> detail_fields, </if>
			<if test="detailService != null and detailService !='' "> detail_service, </if>
			<if test="detailTable != null and detailTable !='' "> detail_table, </if>
			<if test="sorts != null ">sorts,</if>
			<if test="createUserId != null and createUserId !='' "> create_user_id, </if>
			<if test="createTime != null ">create_time,</if>
			<if test="modifyUserId != null and modifyUserId !='' "> modify_user_id, </if>
			<if test="modifyTime != null ">modify_time,</if>
			<if test="sts != null and sts !='' "> sts, </if>
			<if test="orgId != null and orgId !='' "> org_id, </if>
		</trim>
		)values
		(
		<trim suffix="" suffixOverrides=",">
			<if test="id != null and id !='' "> #{id}, </if>
			<if test="templateId != null and templateId !='' "> #{templateId}, </if>
			<if test="serviceName != null and serviceName !='' "> #{serviceName}, </if>
			<if test="tableName != null and tableName !='' "> #{tableName}, </if>
			<if test="fieldsId != null and fieldsId !='' "> #{fieldsId}, </if>
			<if test="databaseId != null and databaseId !='' "> #{databaseId}, </if>
			<if test="fieldsName != null and fieldsName !='' "> #{fieldsName}, </if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> #{fieldsRemark}, </if>
			<if test="fieldsType != null and fieldsType !='' "> #{fieldsType}, </if>
			<if test="fieldsLength != null and fieldsLength !='' "> #{fieldsLength}, </if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> #{fieldsRequired}, </if>
			<if test="fieldsSole != null and fieldsSole !='' "> #{fieldsSole}, </if>
			<if test="fieldsOption != null and fieldsOption !='' "> #{fieldsOption}, </if>
			<if test="detailFields != null and detailFields !='' "> #{detailFields}, </if>
			<if test="detailService != null and detailService !='' "> #{detailService}, </if>
			<if test="detailTable != null and detailTable !='' "> #{detailTable}, </if>
			<if test="sorts != null ">#{sorts},</if>
			<if test="createUserId != null and createUserId !='' "> #{createUserId}, </if>
			<if test="createTime != null ">#{createTime},</if>
			<if test="modifyUserId != null and modifyUserId !='' "> #{modifyUserId}, </if>
			<if test="modifyTime != null ">#{modifyTime},</if>
			<if test="sts != null and sts !='' "> #{sts}, </if>
			<if test="orgId != null and orgId !='' "> #{orgId}, </if>
		</trim>
		)
	</insert>

	<!-- 修改 -->
	<update id="entity_update" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		update sys_template_fields set
		<trim suffix="" suffixOverrides=",">
				<if test="templateId != null and templateId !='' "> template_id = #{templateId},</if>
				<if test="serviceName != null and serviceName !='' "> service_name = #{serviceName},</if>
				<if test="tableName != null and tableName !='' "> table_name = #{tableName},</if>
				<if test="fieldsId != null and fieldsId !='' "> fields_id = #{fieldsId},</if>
				<if test="databaseId != null and databaseId !='' "> database_id = #{databaseId},</if>
				<if test="fieldsName != null and fieldsName !='' "> fields_name = #{fieldsName},</if>
				<if test="fieldsRemark != null and fieldsRemark !='' "> fields_remark = #{fieldsRemark},</if>
				<if test="fieldsType != null and fieldsType !='' "> fields_type = #{fieldsType},</if>
				<if test="fieldsLength != null and fieldsLength !='' "> fields_length = #{fieldsLength},</if>
				<if test="fieldsRequired != null and fieldsRequired !='' "> fields_required = #{fieldsRequired},</if>
				<if test="fieldsSole != null and fieldsSole !='' "> fields_sole = #{fieldsSole},</if>
				<if test="fieldsOption != null and fieldsOption !='' "> fields_option = #{fieldsOption},</if>
				<if test="detailFields != null and detailFields !='' "> detail_fields = #{detailFields},</if>
				<if test="detailService != null and detailService !='' "> detail_service = #{detailService},</if>
				<if test="detailTable != null and detailTable !='' "> detail_table = #{detailTable},</if>
				<if test="sorts != null ">sorts = #{sorts},</if>
				<if test="createUserId != null and createUserId !='' "> create_user_id = #{createUserId},</if>
				<if test="createTime != null ">create_time = #{createTime},</if>
				<if test="modifyUserId != null and modifyUserId !='' "> modify_user_id = #{modifyUserId},</if>
				<if test="modifyTime != null ">modify_time = #{modifyTime},</if>
				<if test="sts != null and sts !='' "> sts = #{sts},</if>
				<if test="orgId != null and orgId !='' "> org_id = #{orgId},</if>
		</trim>
		where id = #{id}
	</update>

	<!-- 逻辑删除 -->
	<update id="entity_logicDelete" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		update sys_template_fields set
		sts='N',modify_time = now(),modify_user_id = #{modifyUserId} where
		id = #{id}
	</update>

	<!-- 逻辑删除多条件 -->
	<update id="entity_logicDelete_Multi_Condition" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		update sys_template_fields set
		sts='N',modify_time = now(),modify_user_id = #{modifyUserId}
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null and id !='' ">id = #{id} </if>
			<if test="templateId != null and templateId !='' "> and template_id = #{templateId}</if>
			<if test="serviceName != null and serviceName !='' "> and service_name = #{serviceName}</if>
			<if test="tableName != null and tableName !='' "> and table_name = #{tableName}</if>
			<if test="fieldsId != null and fieldsId !='' "> and fields_id = #{fieldsId}</if>
			<if test="databaseId != null and databaseId !='' "> and database_id = #{databaseId}</if>
			<if test="fieldsName != null and fieldsName !='' "> and fields_name = #{fieldsName}</if>
			<if test="fieldsRemark != null and fieldsRemark !='' "> and fields_remark = #{fieldsRemark}</if>
			<if test="fieldsType != null and fieldsType !='' "> and fields_type = #{fieldsType}</if>
			<if test="fieldsLength != null and fieldsLength !='' "> and fields_length = #{fieldsLength}</if>
			<if test="fieldsRequired != null and fieldsRequired !='' "> and fields_required = #{fieldsRequired}</if>
			<if test="fieldsSole != null and fieldsSole !='' "> and fields_sole = #{fieldsSole}</if>
			<if test="fieldsOption != null and fieldsOption !='' "> and fields_option = #{fieldsOption}</if>
			<if test="detailFields != null and detailFields !='' "> and detail_fields = #{detailFields}</if>
			<if test="detailService != null and detailService !='' "> and detail_service = #{detailService}</if>
			<if test="detailTable != null and detailTable !='' "> and detail_table = #{detailTable}</if>
			<if test="sorts != null "> and sorts = #{sorts}</if>
			<if test="createUserId != null and createUserId !='' "> and create_user_id = #{createUserId}</if>
			<if test="createTime != null and createTime !='' "> and create_time = #{createTime}</if>
			<if test="modifyUserId != null and modifyUserId !='' "> and modify_user_id = #{modifyUserId}</if>
			<if test="modifyTime != null and modifyTime !='' "> and modify_time = #{modifyTime}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="orgId != null and orgId !='' "> and org_id = #{orgId}</if>
		</trim>
	</update>

	<!-- 物理删除 -->
	<delete id="entity_delete" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateFields">
		delete from sys_template_fields where id =#{id}
	</delete>

	<sql id="Base_Column_List_User">
		a.id,
		a.template_id,
		a.service_name,
		a.table_name,
		a.fields_id,
		a.database_id,
		a.fields_name,
		a.fields_remark,
		a.fields_type,
		a.fields_length,
		a.fields_required,
		a.fields_sole,
		a.fields_option,
		a.detail_fields,
		a.detail_service,
		a.detail_table,
		a.sorts,
		a.create_user_id,
		a.create_time,
		a.modify_user_id,
		a.modify_time,
		a.sts,
		a.org_id
	</sql>

	<!-- 查询 采用==查询 -->
	<select id="queryUserTemplateFields" resultMap="get-entity-result" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateDto">
		SELECT
		<include refid="Base_Column_List_User" />
		FROM
		sys_template_fields a
		left join sys_user_template_view b on a.id = b.template_fields_id and b.sts = 'Y'
		left join sys_template_view c on a.id = c.template_fields_id and c.sts = 'Y'
		where a.sts = 'Y' and b.sts = 'Y' and c.sts = 'Y'
		and b.user_id = #{userId}
		and b.template_view_type = #{templateViewType}
		and c.template_view_type = #{templateViewType}
		and a.service_name = #{serviceName}
		and b.service_name = #{serviceName}
		and c.service_name = #{serviceName}
		order by b.sorts asc
	</select>

	<!-- 查询 采用==查询 -->
	<select id="queryTemplateFields" resultMap="get-entity-result" parameterType="com.hzya.frame.sys.templateFields.entity.TemplateDto">
		SELECT
		<include refid="Base_Column_List_User" />
		FROM
		sys_template_fields a
		left join sys_template_view c on a.id = c.template_fields_id and c.sts = 'Y'
		where a.sts = 'Y'  and c.sts = 'Y'
		and c.template_view_type = #{templateViewType}
		and a.service_name = #{serviceName}
		and c.service_name = #{serviceName}
		order by c.sorts asc
	</select>
</mapper>
