<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                BEGIN
                 UPDATE con_contract_ele_signer cs
                    SET cs.ec_sign_status   = 'Y',
                        cs.sign_time = to_date(${@sign_time},'yyyy-mm-dd HH24:MI:SS'),
                        cs.sign_result = ${@sign_result},
                        cs.last_update_date = sysdate
                  WHERE cs.contract_id = ${@contract_id}
                    AND cs.id_no = ${@id_no}
                    AND cs.esign_type = ${@esign_type};

                   update con_contract cc
                      set cc.ec_sign_type     = 'SIGNING',
                          cc.last_update_date = sysdate
                    where cc.contract_id = ${@contract_id}
                      and cc.ec_sign_type != 'SIGNED';
                END;
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
                BEGIN
                  update con_contract_ele_flow cf
                     set cf.flow_status      = ${@flow_status},
                         cf.end_time         = to_date(${@end_time},'yyyy-mm-dd HH24:MI:SS'),
                         cf.last_update_date = sysdate
                   where cf.ele_flow_id = ${@flow_id};

                   update con_contract_ele_signer cs
                      set cs.ec_sign_status = 'Y',
                          cs.last_update_date = sysdate
                    where cs.contract_id = ${@contract_id};

                   update con_contract cc
                      set cc.ec_sign_type     = 'SIGNED',
                          cc.sign_contract_status = 'SIGN',
                          cc.last_update_date = sysdate
                    where cc.contract_id = ${@contract_id}
                      and cc.sign_type = 'ELE_SIGN'
                      and cc.ec_sign_type != 'NEW'
                      and not exists
                    (select 1
                             from con_contract_ele_flow ce
                            where (ce.flow_status != '2' or ce.flow_status is null)
                              and ce.contract_id = cc.contract_id);

                   wx_ele_contract_pkg.downloadEleFile(p_contract_id => ${@contract_id},
                                                       p_ele_flow_id => ${@flow_id},
                                                       p_user_id     => 1,
                                                       p_company_id  => 1);
                  COMMIT;
                END;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
</bm:model>
