<?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.template.dao.impl.TemplateMapperImpl">
    <resultMap id="get-entity-result" type="com.hzya.frame.sys.template.entity.Template">
        <!--唯一标识码 -->
        <result property="id" column="id"/>
        <!--应用ID -->
        <result property="appId" column="app_id"/>
        <!--应用编码 -->
        <result property="appCode" column="app_code"/>
        <!--应用名称 -->
        <result property="appName" column="app_name"/>
        <!--应用ID -->
        <result property="productId" column="product_id"/>
        <!--应用编码 -->
        <result property="productCode" column="product_code"/>
        <!--应用名称 -->
        <result property="productName" column="product_name"/>
        <!--模版名称 -->
        <result property="templateName" column="template_name"/>
        <!--模板编码 	唯一值 -->
        <result property="templateCode" column="template_code"/>
        <!--类型		单据,档案 -->
        <result property="templateType" column="template_type"/>
        <!--Service服务名 -->
        <result property="serviceName" column="service_name"/>
        <!--表名（英文名称） -->
        <result property="tableName" column="table_name"/>
        <!--树结构属性 -->
        <result property="treeOption" column="tree_option"/>
        <!--排序 -->
        <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,
		app_id,
		app_code,
		app_name,
		product_id,
		product_code,
		product_name,
		template_name,
		template_code,
		template_type,
		service_name,
		table_name,
		tree_option,
		sorts,
		create_user_id,
		create_time,
		modify_user_id,
		modify_time,
		sts,
		org_id
	</sql>
    <!--通过ID获取数据 -->
    <select id="getMaxSorts" resultType="java.lang.Long">
        select MAX(IFNULL(sorts,0))+1 from ${tableName} where sts='Y'
        <if test="serviceName != null and serviceName !='' ">and service_name = #{serviceName}</if>
    </select>

    <!--通过ID获取数据 -->
    <select id="checkTable" resultType="java.lang.Integer">
		SELECT
		COUNT(*)
		FROM
		information_schema.TABLES
		WHERE
		TABLE_SCHEMA = 'businesscenter'
		and TABLE_NAME =  #{tableName}
	</select>

    <!--通过ID获取数据 -->
    <select id="getTableName" resultType="java.lang.String">
		select
		table_name
		from sys_template
		WHERE
		service_name =  #{service} and sts='Y'
	</select>

    <!--通过ID获取数据 -->
    <select id="createTable" resultType="java.lang.Integer">
        CREATE TABLE ${databaseNames.tableName} (
        <foreach collection="databaseFields" item="item" index="index">
            <choose>
                <when test="item.fieldsType  == '1'.toString()">
                    <choose>
                        <when test="item.fieldsName  == 'id'.toString()">
                            id varchar(50) NOT NULL COMMENT '主键ID',
                        </when>
                        <otherwise>
                            ${item.fieldsName} varchar(${item.fieldsLength}) DEFAULT NULL COMMENT #{item.fieldsRemark},
                        </otherwise>
                    </choose>
                </when>
                <when test="item.fieldsType  == '2'.toString()">
                    ${item.fieldsName} bigint(${item.fieldsLength}) DEFAULT NULL COMMENT #{item.fieldsRemark},
                </when>
                <when test="item.fieldsType  == '3'.toString()">
                    ${item.fieldsName} decimal(${item.fieldsLength}) DEFAULT NULL COMMENT #{item.fieldsRemark},
                </when>
                <when test="item.fieldsType  == '4'.toString()">
                    ${item.fieldsName} datetime DEFAULT NULL COMMENT #{item.fieldsRemark},
                </when>
            </choose>
        </foreach>
        PRIMARY KEY (`id`)
        ) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = #{databaseNames.tableRemark}
    </select>


    <!--通过ID获取数据 -->
    <select id="alterTable" resultType="java.lang.Integer">
        ALTER TABLE ${databaseNames.tableName}
        <trim suffix="" suffixOverrides=",">
            <foreach collection="databaseFields" item="item" index="index">
                <choose>
                    <when test="item.fieldsType  == '1'.toString()">
                        <choose>
                            <when test="item.fieldsName  == 'id'.toString()">
                            </when>
                            <otherwise>
                                <choose>
                                    <when test="item.dataType  == '1'.toString()">
                                        MODIFY COLUMN ${item.fieldsName} varchar(${item.fieldsLength}) DEFAULT NULL
                                        COMMENT #{item.fieldsRemark},
                                    </when>
                                    <otherwise>
                                        add COLUMN ${item.fieldsName} varchar(${item.fieldsLength}) DEFAULT NULL COMMENT
                                        #{item.fieldsRemark},
                                    </otherwise>
                                </choose>
                            </otherwise>
                        </choose>
                    </when>
                    <when test="item.fieldsType  == '2'.toString()">
                        <choose>
                            <when test="item.dataType  == '1'.toString()">
                                MODIFY COLUMN ${item.fieldsName} bigint(${item.fieldsLength}) DEFAULT NULL COMMENT
                                #{item.fieldsRemark},
                            </when>
                            <otherwise>
                                add COLUMN ${item.fieldsName} bigint(${item.fieldsLength}) DEFAULT NULL COMMENT
                                #{item.fieldsRemark},
                            </otherwise>
                        </choose>
                    </when>
                    <when test="item.fieldsType  == '3'.toString()">
                        <choose>
                            <when test="item.dataType  == '1'.toString()">
                                MODIFY COLUMN ${item.fieldsName} decimal(${item.fieldsLength}) DEFAULT NULL COMMENT
                                #{item.fieldsRemark},
                            </when>
                            <otherwise>
                                add COLUMN ${item.fieldsName} decimal(${item.fieldsLength}) DEFAULT NULL COMMENT
                                #{item.fieldsRemark},
                            </otherwise>
                        </choose>
                    </when>
                    <when test="item.fieldsType  == '4'.toString()">
                        <choose>
                            <when test="item.dataType  == '1'.toString()">
                                MODIFY COLUMN ${item.fieldsName} datetime DEFAULT NULL COMMENT #{item.fieldsRemark},
                            </when>
                            <otherwise>
                                add COLUMN ${item.fieldsName} datetime DEFAULT NULL COMMENT #{item.fieldsRemark},
                            </otherwise>
                        </choose>
                    </when>
                </choose>
            </foreach>
        </trim>
    </select>


    <!--通过ID获取数据 -->
    <select id="alterTableName" resultType="java.lang.Integer">
			CREATE TABLE ${tableName} COMMENT = #{ tableRemark }
	</select>

    <!--通过ID获取数据 -->
    <select id="dropTable" resultType="java.lang.Integer">
			DROP TABLE IF EXISTS ${tableName}
	</select>
    <!--通过ID获取数据 -->
    <select id="entity_get" resultMap="get-entity-result">
        select
        <include refid="Base_Column_List"/>
        from sys_template where id = #{ id } and sts='Y'
    </select>
    <!--通过ID获取数据 -->
    <select id="entity_get_by_service" resultMap="get-entity-result">
        select
        <include refid="Base_Column_List"/>
        from sys_template where service_name = #{ serviceName } and sts='Y'
    </select>

    <!-- 逻辑删除多条件 -->
    <update id="deleteFormmain" parameterType="hashmap">
		update ${tableName} set
		sts='N',modify_time = now(),modify_user_id = #{loginIdAsString}
		where formmain_id = #{formmain_id}
	</update>

    <!-- 逻辑删除多条件 -->
    <update id="deleteTemplateById" parameterType="com.hzya.frame.sys.entity.FormmainDeleteDto">
        update ${tableName} set
        sts='N',modify_time = now(),modify_user_id = #{loginId}
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">and id = #{id}</if>
            <if test="formmain_id != null and formmain_id !='' ">and formmain_id = #{formmain_id}</if>
            and sts='Y'
        </trim>
    </update>
    <!-- 查询 采用==查询 -->
    <select id="entity_list_base" resultMap="get-entity-result"
            parameterType="com.hzya.frame.sys.template.entity.Template">
        select
        <include refid="Base_Column_List"/>
        from sys_template
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">id = #{id}</if>
            <if test="appId != null and appId !='' ">and app_id = #{appId}</if>
            <if test="appCode != null and appCode !='' ">and app_code = #{appCode}</if>
            <if test="appName != null and appName !='' ">and app_name = #{appName}</if>
            <if test="productId != null and productId !='' ">and product_id = #{productId}</if>
            <if test="productCode != null and productCode !='' ">and product_code = #{productCode}</if>
            <if test="productName != null and productName !='' ">and product_name = #{productName}</if>
            <if test="templateName != null and templateName !='' ">and template_name = #{templateName}</if>
            <if test="templateCode != null and templateCode !='' ">and template_code = #{templateCode}</if>
            <if test="templateType != null and templateType !='' ">and template_type = #{templateType}</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="treeOption != null and treeOption !='' ">and tree_option = #{treeOption}</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>

            <if test="templateViewAccreditList != null and templateViewAccreditList.size>0">
                AND id in
                <foreach collection="templateViewAccreditList" item="item" index="index" open="(" close=")"
                         separator=",">
                    #{item.templateId}
                </foreach>
            </if>
            <if test="templateTypes != null and templateTypes.size>0">
                AND template_type in
                <foreach collection="templateTypes" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </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.template.entity.Template">
        select count(1) from sys_template
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">id = #{id}</if>
            <if test="appId != null and appId !='' ">and app_id = #{appId}</if>
            <if test="appCode != null and appCode !='' ">and app_code = #{appCode}</if>
            <if test="appName != null and appName !='' ">and app_name = #{appName}</if>
            <if test="productId != null and productId !='' ">and product_id = #{productId}</if>
            <if test="productCode != null and productCode !='' ">and product_code = #{productCode}</if>
            <if test="productName != null and productName !='' ">and product_name = #{productName}</if>
            <if test="templateName != null and templateName !='' ">and template_name = #{templateName}</if>
            <if test="templateCode != null and templateCode !='' ">and template_code = #{templateCode}</if>
            <if test="templateType != null and templateType !='' ">and template_type = #{templateType}</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="treeOption != null and treeOption !='' ">and tree_option = #{treeOption}</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.template.entity.Template">
        select
        <include refid="Base_Column_List"/>
        from sys_template
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">id like concat('%',#{id},'%')</if>
            <if test="appId != null and appId !='' ">and app_id like concat('%',#{appId},'%')</if>
            <if test="appCode != null and appCode !='' ">and app_code like concat('%',#{appCode},'%')</if>
            <if test="appName != null and appName !='' ">and app_name like concat('%',#{appName},'%')</if>
            <if test="productId != null and productId !='' ">and product_id like concat('%',#{productId},'%')</if>
            <if test="productCode != null and productCode !='' ">and product_code like concat('%',#{productCode},'%')
            </if>
            <if test="productName != null and productName !='' ">and product_name like concat('%',#{productName},'%')
            </if>
            <if test="templateName != null and templateName !='' ">and template_name like
                concat('%',#{templateName},'%')
            </if>
            <if test="templateCode != null and templateCode !='' ">and template_code like
                concat('%',#{templateCode},'%')
            </if>
            <if test="templateType != null and templateType !='' ">and template_type like
                concat('%',#{templateType},'%')
            </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="treeOption != null and treeOption !='' ">and tree_option like concat('%',#{treeOption},'%')</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>


    <!-- 分页查询列表 采用like格式 -->
    <select id="getServiceTemplateData" resultType="java.util.HashMap"
            parameterType="com.hzya.frame.sys.entity.TemplateQuery">
        select
        <trim suffixOverrides=",">
            <foreach collection="returnField" item="returnField">
                ${returnField} as ${returnField} ,
            </foreach>
        </trim>
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="queryCondition != null and queryCondition.size>0">
                <foreach collection="queryCondition" item="item" index="index">
                    ${item.getValueByType}
                </foreach>
            </if>
            and sts='Y'
        </trim>
        order by sorts asc
    </select>

    <!-- 分页查询列表 采用like格式 -->
    <select id="queryTemplateId" resultType="java.lang.String"
            parameterType="com.hzya.frame.sys.entity.FormmainDeleteDto">
        select
        id
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">and id = #{id}</if>
            <if test="formmain_id != null and formmain_id !='' ">and formmain_id = #{formmain_id}</if>
            and sts='Y'
        </trim>
    </select>
    <!-- 查询符合条件的数量 -->
    <select id="checkData" resultType="Integer" parameterType="com.hzya.frame.sys.entity.FormmainCheckDto">
        SELECT
        count(*)
        from
        ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="stringList != null and stringList.size>0">
                AND ${filedsName} in
                <foreach collection="stringList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>

            <if test="id != null and id !='' ">and id != #{id}</if>
            and sts = 'Y'
        </trim>
    </select>
    <!-- 分页查询列表 采用like格式 -->
    <select id="getServiceTemplateDataById" resultType="java.util.HashMap" parameterType="java.util.HashMap">
        select
        <trim suffixOverrides=",">
            <foreach collection="templateFieldsList" item="item">
                <if test="item.fieldsType != '5'.toString()">
                    ${item.fieldsName} as ${item.fieldsName} ,
                </if>
            </foreach>
        </trim>
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="detailFlag">
                and formmain_id = #{id}
            </if>
            <if test="!detailFlag">
                and id = #{id}
            </if>
            and sts = 'Y'
        </trim>
    </select>

    <!-- 分页查询列表 采用like格式 -->
    <select id="getServiceTemplateDataByFormmainId" resultType="java.util.HashMap" parameterType="java.util.HashMap">
        select
        <trim suffixOverrides=",">
            <foreach collection="templateFieldsList" item="item">
                <if test="item.fieldsType != '5'.toString()">
                    ${item.fieldsName} as ${item.fieldsName} ,
                </if>
            </foreach>
        </trim>
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="detailFlag">
                and formmain_id = #{id}
            </if>
            <if test="!detailFlag">
                and id = #{id}
            </if>
            and sts = 'Y'
        </trim>
        order by sorts asc
    </select>

    <!-- 查询列表 字段采用or格式 -->
    <select id="entity_list_or" resultMap="get-entity-result"
            parameterType="com.hzya.frame.sys.template.entity.Template">
        select
        <include refid="Base_Column_List"/>
        from sys_template
        <trim prefix="where" prefixOverrides="or">
            <if test="id != null and id !='' ">id = #{id}</if>
            <if test="appId != null and appId !='' ">or app_id = #{appId}</if>
            <if test="appCode != null and appCode !='' ">or app_code = #{appCode}</if>
            <if test="appName != null and appName !='' ">or app_name = #{appName}</if>
            <if test="productId != null and productId !='' ">or product_id = #{productId}</if>
            <if test="productCode != null and productCode !='' ">or product_code = #{productCode}</if>
            <if test="productName != null and productName !='' ">or product_name = #{productName}</if>
            <if test="templateName != null and templateName !='' ">or template_name = #{templateName}</if>
            <if test="templateCode != null and templateCode !='' ">or template_code = #{templateCode}</if>
            <if test="templateType != null and templateType !='' ">or template_type = #{templateType}</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="treeOption != null and treeOption !='' ">or tree_option = #{treeOption}</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.template.entity.Template">
        insert into sys_template(
        <trim suffix="" suffixOverrides=",">
            <if test="id != null and id !='' ">id,</if>
            <if test="appId != null and appId !='' ">app_id,</if>
            <if test="appCode != null and appCode !='' ">app_code,</if>
            <if test="appName != null and appName !='' ">app_name,</if>
            <if test="productId != null and productId !='' ">product_id,</if>
            <if test="productCode != null and productCode !='' ">product_code,</if>
            <if test="productName != null and productName !='' ">product_name,</if>
            <if test="templateName != null and templateName !='' ">template_name,</if>
            <if test="templateCode != null and templateCode !='' ">template_code,</if>
            <if test="templateType != null and templateType !='' ">template_type,</if>
            <if test="serviceName != null and serviceName !='' ">service_name,</if>
            <if test="tableName != null and tableName !='' ">table_name,</if>
            <if test="treeOption != null and treeOption !='' ">tree_option,</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>
            <if test="sorts == null ">sorts,</if>

        </trim>
        )values
        (
        <trim suffix="" suffixOverrides=",">
            <if test="id != null and id !='' ">#{id},</if>
            <if test="appId != null and appId !='' ">#{appId},</if>
            <if test="appCode != null and appCode !='' ">#{appCode},</if>
            <if test="appName != null and appName !='' ">#{appName},</if>
            <if test="productId != null and productId !='' ">#{productId},</if>
            <if test="productCode != null and productCode !='' ">#{productCode},</if>
            <if test="productName != null and productName !='' ">#{productName},</if>
            <if test="templateName != null and templateName !='' ">#{templateName},</if>
            <if test="templateCode != null and templateCode !='' ">#{templateCode},</if>
            <if test="templateType != null and templateType !='' ">#{templateType},</if>
            <if test="serviceName != null and serviceName !='' ">#{serviceName},</if>
            <if test="tableName != null and tableName !='' ">#{tableName},</if>
            <if test="treeOption != null and treeOption !='' ">#{treeOption},</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>
            <if test="sorts == null ">(select (max(IFNULL( a.sorts, 0 )) + 1) as sort from sys_template a WHERE a.sts =
                'Y' ),
            </if>

        </trim>
        )
    </insert>

    <!-- 修改 -->
    <update id="entity_update" parameterType="com.hzya.frame.sys.template.entity.Template">
        update sys_template set
        <trim suffix="" suffixOverrides=",">
            <if test="appId != null and appId !='' ">app_id = #{appId},</if>
            <if test="appCode != null and appCode !='' ">app_code = #{appCode},</if>
            <if test="appName != null and appName !='' ">app_name = #{appName},</if>
            <if test="productId != null and productId !='' ">product_id = #{productId},</if>
            <if test="productCode != null and productCode !='' ">product_code = #{productCode},</if>
            <if test="productName != null and productName !='' ">product_name = #{productName},</if>
            <if test="templateName != null and templateName !='' ">template_name = #{templateName},</if>
            <if test="templateCode != null and templateCode !='' ">template_code = #{templateCode},</if>
            <if test="templateType != null and templateType !='' ">template_type = #{templateType},</if>
            <if test="serviceName != null and serviceName !='' ">service_name = #{serviceName},</if>
            <if test="tableName != null and tableName !='' ">table_name = #{tableName},</if>
            <if test="treeOption != null and treeOption !='' ">tree_option = #{treeOption},</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.template.entity.Template">
		update sys_template 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.template.entity.Template">
        update sys_template 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="appId != null and appId !='' ">and app_id = #{appId}</if>
            <if test="appCode != null and appCode !='' ">and app_code = #{appCode}</if>
            <if test="appName != null and appName !='' ">and app_name = #{appName}</if>
            <if test="productId != null and productId !='' ">and product_id = #{productId}</if>
            <if test="productCode != null and productCode !='' ">and product_code = #{productCode}</if>
            <if test="productName != null and productName !='' ">and product_name = #{productName}</if>
            <if test="templateName != null and templateName !='' ">and template_name = #{templateName}</if>
            <if test="templateCode != null and templateCode !='' ">and template_code = #{templateCode}</if>
            <if test="templateType != null and templateType !='' ">and template_type = #{templateType}</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="treeOption != null and treeOption !='' ">and tree_option = #{treeOption}</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.template.entity.Template">
		delete from sys_template where id =#{id}
	</delete>

    <insert id="saveFram" parameterType="com.hzya.frame.sys.entity.FormmainDto">
        insert into ${tableName}(
        <trim suffix="" suffixOverrides=",">
            <if test="filedsDtos != null and filedsDtos.size>0">
                <foreach collection="filedsDtos" item="item" index="index">
                    <if test="item.filedsName != 'logid'.toString() ">
                        ${item.filedsName},
                    </if>
                    <if test="item.filedsName == 'logid'.toString() ">
                        create_user_id,
                        modify_user_id,
                    </if>
                </foreach>
            </if>
            sorts,
            create_time,
            modify_time,
            sts,
        </trim>
        )values
        (
        <trim suffix="" suffixOverrides=",">
            <if test="filedsDtos != null and filedsDtos.size>0">
                <foreach collection="filedsDtos" item="item" index="index">
                    <if test="item.filedsName != 'logid'.toString() ">
                        #{item.filedsValue},
                    </if>
                    <if test="item.filedsName == 'logid'.toString() ">
                        #{item.filedsValue},
                        #{item.filedsValue},
                    </if>
                </foreach>
            </if>
            (select (max(IFNULL( a.sorts, 0 )) + 1) as sort from ${tableName} a WHERE
            a.sts = 'Y' ),
            now(),
            now(),
            'Y',
        </trim>
        )
    </insert>
    <update id="update" parameterType="com.hzya.frame.sys.template.entity.Template">
        update ${tableName} set
        <trim suffix="" suffixOverrides=",">
            <if test="filedsDtos != null and filedsDtos.size>0">
                <foreach collection="filedsDtos" item="item" index="index">
                    <if test="item.filedsName != 'logid'.toString() ">
                        ${item.filedsName} = #{item.filedsValue},
                    </if>
                    <if test="item.filedsName == 'logid'.toString() ">
                        modify_user_id = #{item.filedsValue},
                    </if>
                </foreach>
            </if>
            modify_time = now(),
        </trim>
        where id = #{id}
    </update>


    <!-- 分页查询列表 采用like格式 -->
    <select id="queryTemplateData" resultType="java.util.HashMap" parameterType="com.hzya.frame.sys.entity.ModuleDto">
        select
        ${value},
        ${label}
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">and ${value} = #{id}</if>
            <if test="lableValue != null and lableValue !='' ">and ${label} like concat('%',#{lableValue},'%')</if>
            and sts = 'Y'
        </trim>
        order by sorts asc
    </select>

    <!-- 分页查询列表 采用like格式 -->
    <select id="queryTemplateDataOne" resultType="java.util.HashMap"
            parameterType="com.hzya.frame.sys.entity.ModuleDto">
        select
        ${value},
        ${label},
        ${upId}
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id !='' ">and id = #{id}</if>
            and sts = 'Y'
        </trim>
        order by sorts asc
    </select>


    <!-- 分页查询列表 采用like格式 -->
    <select id="queryTemplateDataUp" resultType="java.util.HashMap" parameterType="com.hzya.frame.sys.entity.ModuleDto">
        select
        id as qsdfg,
        ${value},
        ${label},
        ${upId}
        from ${tableName}
        <trim prefix="where" prefixOverrides="and">
            <if test="!queryUp">
                and ${upId} is null
            </if>
            <if test="queryUp">
                <if test="id != null and id !='' ">and ${upId} = #{id}</if>
            </if>
            and sts = 'Y'
        </trim>
        order by sorts asc
    </select>

    <!-- 分页查询列表 采用like格式 -->
    <select id="queryTemplateDataAll" resultType="java.util.HashMap"
            parameterType="com.hzya.frame.sys.entity.ModuleDto">
		select
		id as qsdfg,
		${value},
		${label},
		${upId}
		from ${tableName}
		where sts = 'Y'
		order by sorts asc
	</select>
</mapper>
