<?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.nbport.zgwl.manifest.mapper.ContractTemplateMapper">

    <select id="selectTemplateSeqNextVal" resultType="java.lang.Long">
        select SEQ_CONTRACT_TEMPLATE_MANAGE.NEXTVAL from dual
    </select>

    <resultMap id="ContractTemplateResult" type="com.nbport.zgwl.manifest.dto.ContractTemplateDto">
        <id property="id" column="id"/>
        <result property="templateCode" column="template_code"/>
        <result property="templateName" column="template_name"/>
        <result property="source" column="source"/>
        <result property="contractType" column="contract_type"/>
        <result property="version" column="version"/>
        <result property="status" column="status"/>
        <result property="variableCount" column="variable_count"/>
        <result property="description" column="description"/>
        <result property="usageTip" column="usage_tip"/>
        <result property="templateFileName" column="template_file_name"/>
        <result property="templateFilePath" column="template_file_path"/>
        <result property="publishOrgIdsJson" column="publish_org_ids_json"/>
        <result property="publishOrgNamesJson" column="publish_org_names_json"/>
        <result property="variableMappingsJson" column="variable_mappings_json"/>
        <result property="createBy" column="create_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="selectTemplateColumns">
        select id, template_code, template_name, source, contract_type,
               version, status, variable_count, description, usage_tip,
               template_file_name, template_file_path,
               publish_org_ids_json, publish_org_names_json, variable_mappings_json,
               create_by, created_time, updated_by, updated_time, del_flag
        from contract_template_manage
    </sql>

    <select id="selectTemplatePage" resultMap="ContractTemplateResult">
        <include refid="selectTemplateColumns"/>
        <where>
            del_flag = '0'
            <if test="query.templateName != null and query.templateName != ''">
                and template_name like CONCAT(CONCAT('%', #{query.templateName}), '%')
            </if>
            <if test="query.contractType != null and query.contractType != ''">
                and contract_type = #{query.contractType}
            </if>
            <if test="query.source != null and query.source != ''">
                and source = #{query.source}
            </if>
            <if test="query.status != null and query.status != ''">
                and status = #{query.status}
            </if>
        </where>
        order by updated_time desc, id desc
    </select>

    <select id="selectPublishedTemplates" resultMap="ContractTemplateResult">
        <include refid="selectTemplateColumns"/>
        where del_flag = '0'
          and status = 'published'
        order by updated_time desc, id desc
    </select>

    <select id="selectTemplateById" resultMap="ContractTemplateResult">
        <include refid="selectTemplateColumns"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertTemplate" parameterType="com.nbport.zgwl.manifest.dto.ContractTemplateDto">
        insert into contract_template_manage
        (
            id, template_code, template_name, source, contract_type,
            version, status, variable_count, description, usage_tip,
            template_file_name, template_file_path,
            publish_org_ids_json, publish_org_names_json, variable_mappings_json,
            create_by, created_time, updated_by, updated_time, del_flag
        )
        values
        (
            #{id}, #{templateCode}, #{templateName}, #{source}, #{contractType},
            #{version}, #{status}, #{variableCount}, #{description}, #{usageTip},
            #{templateFileName}, #{templateFilePath},
            #{publishOrgIdsJson}, #{publishOrgNamesJson}, #{variableMappingsJson},
            #{createBy}, #{createdTime}, #{updatedBy}, #{updatedTime}, #{delFlag}
        )
    </insert>

    <update id="updateTemplate" parameterType="com.nbport.zgwl.manifest.dto.ContractTemplateDto">
        update contract_template_manage
        <trim prefix="set" suffixOverrides=",">
            template_name = #{templateName},
            source = #{source},
            contract_type = #{contractType},
            version = #{version},
            status = #{status},
            variable_count = #{variableCount},
            description = #{description},
            usage_tip = #{usageTip},
            template_file_name = #{templateFileName},
            template_file_path = #{templateFilePath},
            publish_org_ids_json = #{publishOrgIdsJson},
            publish_org_names_json = #{publishOrgNamesJson},
            variable_mappings_json = #{variableMappingsJson},
            updated_by = #{updatedBy},
            updated_time = #{updatedTime},
        </trim>
        where id = #{id} and del_flag = '0'
    </update>

    <update id="updateTemplateStatus">
        update contract_template_manage
        set status = #{status},
            updated_by = #{updateBy},
            updated_time = #{updateTime}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="deleteTemplateById">
        update contract_template_manage
        set del_flag = #{delFlag},
            updated_by = #{updateBy},
            updated_time = #{updateTime}
        where id = #{id} and del_flag = '0'
    </update>
</mapper>
