<?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.muchinfo.mtp3interface.mapper.IUseresignrecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.muchinfo.mtp3pojos.entity.Useresignrecord">
        <id column="RECORDID" property="recordid" />
        <result column="USERID" property="userid" />
        <result column="TEMPLATECONFIGID" property="templateconfigid" />
        <result column="TEMPLATENO" property="templateno" />
        <result column="TEMPLATETYPE" property="templatetype" />
        <result column="TEMPLATENAME" property="templatename" />
        <result column="ORDERINDEX" property="orderindex" />
        <result column="RECORDSTATUS" property="recordstatus" />
        <result column="CONTRACTNO" property="contractno" />
        <result column="CONTRACTFILEADDR" property="contractfileaddr" />
        <result column="SIGNREMARK" property="signremark" />
        <result column="CREATORID" property="creatorid" />
        <result column="CREATETIME" property="createtime" />
        <result column="UPDATETIME" property="updatetime" />
        <result column="AUTHINFO" property="authinfo" />
        <result column="SIGNURL" property="signurl" />
        <result column="PARTAKEY" property="partakey" />
        <result column="PARTBKEY" property="partbkey" />
        <result column="TRADEDATE" property="tradedate" />
        <result column="AREAUSERID" property="areauserid" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        RECORDID, USERID, TEMPLATECONFIGID, TEMPLATENO, TEMPLATETYPE, TEMPLATENAME, ORDERINDEX, RECORDSTATUS, CONTRACTNO, CONTRACTFILEADDR, SIGNREMARK, CREATORID, CREATETIME, UPDATETIME, AUTHINFO, SIGNURL, PARTAKEY, PARTBKEY, TRADEDATE, AREAUSERID
    </sql>

    <insert id="insertUserESignRecordFromConfig" parameterType="java.util.Map">
        insert into UserESignRecord
        (recordid, userid, templateconfigid, templateno, templatetype, templatename, orderindex, recordstatus,
         contractno, creatorid, createtime, updatetime, areauserid)
        select seq_useresignrecord.nextval,
               #{userid},
               t.templateconfigid,
               t.templateno,
               t.templatetype,
               t.templatename,
               t.orderindex,
               1,
               null,
               1,
               sysdate,
               sysdate,
               #{areauserid}
        from ESignTemplateConfig t
        where t.templatecertype = 0 or t.templatecertype in (select ui.userinfotype from userinfo ui where ui.userid = #{userid})
    </insert>

    <insert id="insertMD_UserWwapProtocol" parameterType="java.util.Map">
        MERGE INTO md_userswapprotocol t1
            USING (SELECT #{userid} AS userid, #{areauserid} AS areauserid FROM dual) t2
            ON (t1.userid = t2.userid AND t1.areauserid = t2.areauserid)
            WHEN MATCHED THEN
                UPDATE SET t1.protocolstatus = t1.protocolstatus
            WHEN NOT MATCHED THEN
                INSERT (userid, areauserid, protocolstatus, updatetime)
                    VALUES (t2.userid, t2.areauserid,  #{protocolstatus}, SYSDATE)
    </insert>

</mapper>
