f77b2322 by 周伟奇

e-contract part 1

1 parent cc6c63c8
......@@ -1773,3 +1773,21 @@ APPLICANT_TYPE_MAP = {
}
APPLICANT_TYPE_ORDER = ['Borrower', 'Co-Borrower', 'Guarantor', 'Mortgager']
FILE_NAME_PREFIX_MAP = {
AFC_PREFIX: [
((CONTRACT_CLASSIFY, 0), '{0}_电子签署-汽车抵押贷款合同'),
((HMH_CLASSIFY, 0), '{0}_电子签署-抵押登记豁免函'),
],
HIL_PREFIX: [
((HIL_CONTRACT_1_CLASSIFY, HIL_CONTRACT_3_CLASSIFY), '{0}_电子签署-售后回租合同'),
((HIL_CONTRACT_2_CLASSIFY, 0), '{0}_电子签署-汽车租赁抵押合同'),
((HMH_CLASSIFY, 0), '{0}_电子签署-抵押登记豁免函'),
]
}
HIL_CONTRACT_TYPE_MAP = {
str(HIL_CONTRACT_1_CLASSIFY): 0,
str(HIL_CONTRACT_2_CLASSIFY): 2,
str(HIL_CONTRACT_3_CLASSIFY): 1,
}
\ No newline at end of file
......
......@@ -789,3 +789,24 @@ class HILCACompareResultRecord(models.Model):
db_table = 'hil_ca_compare_result_record'
class HILContract(models.Model):
id = models.AutoField(primary_key=True, verbose_name="id") # 主键
application_id = models.CharField(max_length=64, verbose_name="申请id") # 索引
create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
class Meta:
managed = False
db_table = 'hil_contract'
class AFCContract(models.Model):
id = models.AutoField(primary_key=True, verbose_name="id") # 主键
application_id = models.CharField(max_length=64, verbose_name="申请id") # 索引
create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
class Meta:
managed = False
db_table = 'afc_contract'
situ_db_label = 'afc'
......
import os
import base64
import requests
from common.redis_cache import redis_handler as rh
......@@ -44,7 +45,6 @@ class ECM:
"b_coborrower_id", "b_coborrower_name", "b_guarantor_id", "b_guarantor_name",
"b_frontend_partner", "b_dealer_code", "b_dealer_name", "b_input_date", "b_comment",
"b_contract_no", "b_location"]
self.contract_prefix = '电子'
def update_oauth_token(self):
response = requests.post(self.oauth_url, headers=self.oauth_headers, data=self.oauth_payload, verify=False)
......@@ -69,9 +69,9 @@ class ECM:
def get_headers(self):
return {'Authorization': '{0} {1}'.format(self.token_type, self.get_oauth_token())}
def search(self, application_id, business_type):
def search(self, application_id, business_type, prefix):
sql = "select * from {0} where b_application_no='{1}' and object_name like '{2}%'".format(
self.settlement_type, application_id, self.contract_prefix)
self.settlement_type, application_id, prefix)
search_args = {
"userName": self.username,
"password": self.pwd,
......@@ -96,7 +96,6 @@ class ECM:
result.append((object_name, object_id))
return result
def download(self, save_path, object_id, document_scheme, business_type):
doc_type, _, _ = self.doc_type_map.get(document_scheme)
download_json = {
......
......@@ -36,12 +36,14 @@ from .models import (
AFCSECompareResultRecord,
HILCACompareResultRecord,
HILSECompareResultRecord,
HILContract,
AFCContract,
)
from .named_enum import ErrorType
from .mixins import DocHandler
from . import consts
from apps.account.authentication import OAuth2AuthenticationWithUser
from celery_compare.tasks import compare
from celery_compare.tasks import compare, forwarding_station
class CustomDate(fields.Date):
......@@ -1164,5 +1166,11 @@ class SEContractView(GenericView):
# pos上传e-contract信息接口 SE
@use_args(se_contract_args, location='data')
def post(self, request, args):
self.running_log.info('e-contract in')
contract_info = args.get('content', {})
application_id = contract_info.get('applicationId', '')
entity = contract_info.get('applicationEntity', '')
table_class = HILContract if entity == consts.HIL_PREFIX else AFCContract
table_class.objects.create(application_id=application_id)
forwarding_station.apply_async((application_id, entity), queue='queue_compare', countdown=conf.DELAY_SECONDS)
self.running_log.info('[e-contract] [application_id={0}] [entity={1}]'.format(application_id, entity))
return response.ok()
......
......@@ -27,10 +27,13 @@ from apps.doc.models import (
AFCCACompareResult,
HILSECompareResult,
HILCACompareResult,
AFCDoc,
HILDoc
)
from apps.doc import consts
from apps.doc.ocr.gcap import gcap
from apps.doc.ocr.cms import cms
from apps.doc.ocr.ecm import ECM, rh
from apps.doc.exceptions import GCAPException
from apps.doc.named_enum import RequestTeam, RequestTrigger, ProcessName, ErrorType
from common.tools.comparison import cp
......@@ -38,9 +41,11 @@ from common.tools.des import decode_des
compare_log = logging.getLogger('compare')
log_base = '[Compare]'
e_log_base = '[e-contract]'
empty_str = ''
empty_error_type = 1000
des_key = conf.CMS_DES_KEY
ecm = ECM()
def rotate_bound(image, angle):
......@@ -1867,4 +1872,32 @@ def compare(application_id, application_entity, uniq_seq, ocr_res_id, is_ca=True
se_compare(application_id, application_entity, ocr_res_id, last_obj, ocr_res_dict, is_cms)
@app.task
def forwarding_station(application_id, entity):
compare_log.info('{0} [forward start] [application_id={1}] [entity={2}]'.format(e_log_base, application_id, entity))
doc_class = HILDoc if entity in consts.HIL_SET else AFCDoc
entity_prefix = consts.HIL_PREFIX if entity in consts.HIL_SET else consts.AFC_PREFIX
for (classify_1, classify_2), prefix in consts.FILE_NAME_PREFIX_MAP.get(entity):
try:
file_list = ecm.search(application_id, entity, prefix.format(application_id)) # TODO 获取最新文件
except Exception as e:
compare_log.error('{0} [search failed] [application_id={1}] [entity={2}] [error={3}]'.format(
e_log_base, application_id, entity, traceback.format_exc()))
else:
compare_log.info('{0} [search end] [application_id={1}] [entity={2}] [file_list={3}]'.format(
e_log_base, application_id, entity, file_list))
for object_name, object_id in file_list:
doc = doc_class.objects.create(
metadata_version_id=object_id,
application_id=application_id,
document_name=object_name,
document_scheme='SETTLEMENT',
data_source='POS',
upload_finish_time=datetime.now(),
)
task = consts.SPLIT_STR.join([entity_prefix, str(doc.id), str(classify_1), str(classify_2)])
enqueue_res = rh.enqueue([task], False)
compare_log.info('{0} [upload success] [res={1}] [application_id={2}] [entity={3}] [object_name={4}] '
'[object_id={5}] [doc_id={6}]'.format(e_log_base, enqueue_res, application_id, entity,
object_name, object_id, doc.id))
compare_log.info('{0} [forward end] [application_id={1}] [entity={2}]'.format(e_log_base, application_id, entity))
......
# -*- coding: utf-8 -*-
# @Author : lk
# @Email : 9428.al@gmail.com
# @Created Date : 2021-06-29 17:43:46
# @Last Modified : 2021-09-07 14:11:25
# @Description :
from .get_char import Finder
def predict(pdf_info):
# 输入是整个 PDF 中的信息
f = Finder(pdf_info)
results = f.get_info()
return results
# -*- coding: utf-8 -*-
# @Author : lk
# @Email : 9428.al@gmail.com
# @Created Date : 2021-06-29 17:43:46
# @Last Modified : 2021-11-03 16:07:36
# @Description :
from .get_char import Finder
def predict(pdf_info, file_cls):
"""Summary
Args:
pdf_info (TYPE): Description
file_cls (TYPE): file_cls = 0: 售后回租合同; file_cls = 1: 车辆处置协议; file_cls = 2: 车辆租赁抵押合同
Returns:
TYPE: Description
"""
# 0: 售后回租合同
pdf_info_0 = []
for pno in pdf_info:
for block in pdf_info[f'{pno}']['blocks']:
if block['type'] != 0:
continue
for line in block['lines']:
for span in line['spans']:
bbox, text = span['bbox'], span['text']
if '售后回租合同_' in text:
pdf_info_0.append(pdf_info[pno])
# 1: 车辆处置协议
pdf_info_1 = []
for pno in pdf_info:
for block in pdf_info[f'{pno}']['blocks']:
if block['type'] != 0:
continue
for line in block['lines']:
for span in line['spans']:
bbox, text = span['bbox'], span['text']
if '售后回租合同附件一' in text:
pdf_info_1.append(pdf_info[pno])
# 2: 车辆租赁抵押合同
pdf_info_2 = []
for pno in pdf_info:
for block in pdf_info[f'{pno}']['blocks']:
if block['type'] != 0:
continue
for line in block['lines']:
for span in line['spans']:
bbox, text = span['bbox'], span['text']
if '车辆租赁抵押合同_' in text:
pdf_info_2.append(pdf_info[pno])
is_clczxy = False
# 如果 pdf_info_1 == 4 页,则说明此时输入包含了车辆处置协议
if len(pdf_info_1) == 4 and file_cls == 1 and len(pdf_info_0) != 0:
is_clczxy = True
pdf_info = dict()
for pno, page_info in enumerate(pdf_info_1):
pdf_info[str(pno)] = page_info
f = Finder(pdf_info)
if file_cls == 0:
results = f.get_info()
if file_cls == 1:
# 提取信息 ———— 车辆处置协议
results = f.get_info_1()
if file_cls == 2:
# 提取信息 ———— 车辆租赁抵押合同
results = f.get_info_2()
if is_clczxy == True:
for key in results:
if results[key]['page'] is not None:
results[key]['page'] = str(int(results[key]['page'])+6)
for key in results:
if results[key]['page'] is not None:
results[key]['page'] = 'page_' + str(int(results[key]['page'])+1)
return results
import pyodbc
afc_sql = """
create table afc_contract
(
id bigint identity primary key,
application_id nvarchar(64) not null,
create_time datetime not null
);
create index afc_contract_application_id_index
on afc_contract (application_id);
"""
hil_sql = """
create table hil_contract
(
id bigint identity primary key,
application_id nvarchar(64) not null,
create_time datetime not null
);
create index hil_contract_application_id_index
on hil_contract (application_id);
"""
hil_cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};', autocommit=True)
hil_cursor = hil_cnxn.cursor()
hil_cursor.execute(hil_sql)
hil_cursor.close()
hil_cnxn.close()
afc_cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};', autocommit=True)
afc_cursor = afc_cnxn.cursor()
afc_cursor.execute(afc_sql)
afc_cursor.close()
afc_cnxn.close()
import os
import json
import cv2
import shutil
import fitz
......@@ -35,6 +36,8 @@ class PDFHandler:
self.suffix = self.get_suffix(document_name)
self.is_ebank = False
self.page_text_list = []
self.pdf_info = {}
self.img_path_pno_list = []
def get_suffix(self, file_name):
if file_name is None:
......@@ -296,6 +299,17 @@ class PDFHandler:
self.is_ebank = True
self.page_text_list = page_text_list
def e_contract_process(self):
with fitz.Document(self.path) as pdf:
for pno in range(pdf.pageCount):
page = pdf.loadPage(pno)
self.pdf_info[str(pno)] = json.loads(page.getText('json'))
pix = page.getPixmap()
img_save_path = self.get_img_save_path(page.number)
self.img_path_pno_list.append((img_save_path, 'page_{0}'.format(str(pno+1))))
pix.writePNG(img_save_path)
def extract_image(self, max_img_count=None):
self.img_path_list = []
self.xref_set = set()
......
......@@ -14,3 +14,5 @@ DEALER_CODE = ocr_group
BASE_URL = https://li19dkocruat02vm.bmwgroup.net
DELAY_SECONDS = 60
......
......@@ -12,4 +12,6 @@ EDMS_DOWNLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/Dow
EDMS_UPLOAD_URL = https://edms-test.bmw.com/FH/FileHold/DocumentRepository/UploadHandler.ashx
DEALER_CODE = ocr_situ_group
BASE_URL = https://staging-bmw-ocr.situdata.com
\ No newline at end of file
BASE_URL = https://staging-bmw-ocr.situdata.com
DELAY_SECONDS = 60
\ No newline at end of file
......
......@@ -12,4 +12,6 @@ EDMS_DOWNLOAD_URL = http://sccn0637.bmwgroup.net/FH/FileHold/DocumentRepository/
EDMS_UPLOAD_URL = http://sccn0637.bmwgroup.net/FH/FileHold/DocumentRepository/UploadHandler.ashx
DEALER_CODE = ocr_situ_group
BASE_URL = https://li19dkocruat01vm.bmwgroup.net
\ No newline at end of file
BASE_URL = https://li19dkocruat01vm.bmwgroup.net
DELAY_SECONDS = 60
\ No newline at end of file
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!