From 39cc81763aa1be8ec0a36c4e278fad12cba2e875 Mon Sep 17 00:00:00 2001
From: fengxuan <fengxuan@situdata.com>
Date: Fri, 28 Mar 2025 17:30:19 +0800
Subject: [PATCH] init 5153-5234

---
 src/apps/doc/internal_urls.py |  2 ++
 src/apps/doc/models.py        | 26 +++++++++++++++++++++++++-
 src/apps/doc/ocr/ecm.py       | 17 +++++++++++++++++
 src/apps/doc/views.py         | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 95 insertions(+), 3 deletions(-)

diff --git a/src/apps/doc/internal_urls.py b/src/apps/doc/internal_urls.py
index 82887eb..3ae8acd 100644
--- a/src/apps/doc/internal_urls.py
+++ b/src/apps/doc/internal_urls.py
@@ -5,5 +5,7 @@ from . import views
 urlpatterns = [
     path(r'', views.DocView.as_view()),
     path(r'query/employee', views.EmployeeView.as_view()),
+    path(r'query/greenBookHistoryFile', views.SearchGBHistoryFileView.as_view()),
+    path(r'query/greenBookHistoryFile', views.DownloadGBHistoryFileView.as_view()),
     path(r'contract/v1', views.SEContractView.as_view()),
 ]
diff --git a/src/apps/doc/models.py b/src/apps/doc/models.py
index cd93aa6..97becfa 100644
--- a/src/apps/doc/models.py
+++ b/src/apps/doc/models.py
@@ -1130,4 +1130,28 @@ class DealerMapping(models.Model):
 
     class Meta:
         managed = False
-        db_table = 'dealer_mapping'
\ No newline at end of file
+        db_table = 'dealer_mapping'
+
+class HILGreenBookHistoryFile(models.Model):
+    id = models.AutoField(primary_key=True, verbose_name="id")  # 主键
+    application_id = models.CharField(max_length=64, verbose_name="申请id")  # 索引
+    download_finish = models.BooleanField(default=True, verbose_name="是否下载完成")
+    update_time = models.DateTimeField(auto_now=True, verbose_name='修改时间')
+    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
+
+    class Meta:
+        managed = False
+        db_table = 'hil_gb_history_file'
+
+
+class AFCGreenBookHistoryFile(models.Model):
+    id = models.AutoField(primary_key=True, verbose_name="id")  # 主键
+    application_id = models.CharField(max_length=64, verbose_name="申请id")  # 索引
+    download_finish = models.BooleanField(default=True, verbose_name="是否下载完成")
+    update_time = models.DateTimeField(auto_now=True, verbose_name='修改时间')
+    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
+
+    class Meta:
+        managed = False
+        db_table = 'afc_gb_history_file'
+        situ_db_label = 'afc'
diff --git a/src/apps/doc/ocr/ecm.py b/src/apps/doc/ocr/ecm.py
index a925154..4b5a4a0 100644
--- a/src/apps/doc/ocr/ecm.py
+++ b/src/apps/doc/ocr/ecm.py
@@ -163,3 +163,20 @@ class ECM(GenericView):
                 response.status_code, response.headers, response.text))
         if 'ns12:createResponse' not in response.json().get('S:Envelope', {}).get('S:Body', {}):
             raise ECMException('ECM upload failed: {0} , with headers: {1}'.format(response.json(), response.headers))
+
+    def search_doc_info_list(self, filePath, business_type):
+        args = {
+            "username": self.username,
+            "password": self.pwd,
+            "docbase": self.doc_base_map.get(business_type),
+            "documentType": "green_book",
+            "dql":"select r_object_id, object_name,b_application_no, r_object_type,b_customer_name,r_content_size, owner_name, b_input_date, r_modify_date, b_location from green_book where b_location = '"+ filePath +"' order by r_modify_date desc"
+        }
+        header_info = self.get_headers()
+        self.running_log.info("{0} search header_info:{1}".format(self.log_base, header_info))
+        self.running_log.info("{0} search args_info:{1}".format(self.log_base, args))
+        response = requests.post(self.search_url, headers=header_info, json=args, verify=False)
+        if response.status_code != 200:
+            raise ECMException('ECM search failed with code: {0}'.format(response.status_code))
+        self.running_log.info("{0} search response.json():{1}".format(self.log_base, response.json()))
+        return response.json()
\ No newline at end of file
diff --git a/src/apps/doc/views.py b/src/apps/doc/views.py
index d77f8c6..6635e99 100644
--- a/src/apps/doc/views.py
+++ b/src/apps/doc/views.py
@@ -53,7 +53,9 @@ from .models import (
     AFCOCRResult,
     AFCSEOCRResult,
     HILCmsStatusInfo,
-    AFCCmsStatusInfo
+    AFCCmsStatusInfo,
+    HILGreenBookHistoryFile,
+    AFCGreenBookHistoryFile
 )
 from .named_enum import ErrorType, AutoResult, WholeResult, RPAResult, SystemName, RequestTeam
 from .mixins import DocHandler, MPOSHandler, PreSEHandler
@@ -61,7 +63,7 @@ from . import consts
 from apps.account.authentication import OAuth2AuthenticationWithUser
 from celery_compare.tasks import compare, fsm_compare
 from prese.compare import get_empty_result
-
+from apps.doc.ocr.ecm import ECM
 import time
 
 
@@ -1876,4 +1878,51 @@ class EmployeeView(GenericView):
             self.running_log.info('[query Employee] [application_id={0}] [income_keywords={1}]'.format(application_id, income_keywords))
             if income_keywords is not None and len(income_keywords) > 0:
                 return response.ok(data=True)
+        return response.ok(data=False)
+
+class SearchGBHistoryFileView(GenericView):
+    permission_classes = [IsAuthenticated]
+    authentication_classes = [OAuth2AuthenticationWithUser]
+
+    @use_args(employee_args, location='data')
+    def post(self, request, args): 
+        
+        filePath = args.get('filePath')
+        business_type = args.get('business_type')
+
+        gb_history_file_class = HILGreenBookHistoryFile if business_type in consts.HIL_SET else AFCGreenBookHistoryFile
+        ecm = ECM()
+        response_json = ecm.search_doc_info_list(filePath, business_type)
+        data_objects = response_json['Envelope']['Body']['executeResponse']['return']['dataPackage']['DataObjects']
+        for data_object in data_objects:
+            object_id = data_object['Identity']['ObjectId']['@id']
+            properties_dict = {}
+            properties = data_object['Properties']['Properties']
+            for prop in properties:
+                name = prop['@name']
+                value = prop.get('Value', 'null')  # 如果Value为空,则输出null
+                properties_dict[name] = value
+            self.running_log.info('[SearchGBHistoryFileView] [properties_dict={0}] '.format(properties_dict))
+            gb_history_file_class.objects.create(
+                    application_id=properties_dict['b_application_no'],
+                    download_finish=False
+                )
+        return response.ok(data=False)
+
+class DownloadGBHistoryFileView(GenericView):
+    permission_classes = [IsAuthenticated]
+    authentication_classes = [OAuth2AuthenticationWithUser]
+
+    @use_args(employee_args, location='data')
+    def post(self, request, args): 
+        
+        filePath = args.get('filePath')
+        business_type = args.get('business_type')
+        ecm = ECM()
+        ecm.download(filePath, business_type)
+        gb_history_file_class = HILGreenBookHistoryFile if business_type in consts.HIL_SET else AFCGreenBookHistoryFile
+        gb_history_file_class.objects.create(
+                    application_id=gb_history_file_class.application_id,
+                    download_finish=False
+                )
         return response.ok(data=False)
\ No newline at end of file
--
libgit2 0.24.0