ocr_process.py 137 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
import os
import time
import json
import shutil
import base64
import signal
import difflib
import requests
import traceback
from collections import Counter
from datetime import datetime, date
from django import db
from django.utils import timezone
from django.core.management import BaseCommand
from multiprocessing import Process, Queue, Manager, Lock

from settings import conf
from common.mixins import LoggerMixin
from common.tools.file_tools import get_pwd_list_from_str, extract_zip_or_rar, get_file_paths
from common.tools.pdf_to_img import PDFHandler
from common.electronic_afc_contract.afc_contract_ocr import predict as afc_predict
from common.electronic_hil_contract.hil_contract_ocr import predict as hil_predict
from apps.doc import consts
# from apps.doc.ocr.edms import EDMS, rh
from apps.doc.ocr.ecm import ECM, rh
from apps.doc.named_enum import KeywordsType, FailureReason, WorkflowName, ProcessName, RequestTeam, RequestTrigger, BSCheckResult
from apps.doc.exceptions import EDMSException, OCR1Exception, OCR2Exception, OCR4Exception
from apps.doc.ocr.wb import BSWorkbook
from apps.doc.models import (
    DocStatus,
    HILDoc,
    AFCDoc,
    Keywords,
    HILOCRResult,
    AFCOCRResult,
    AFCSEOCRResult,
    HILOCRReport,
    HILSEOCRResult,
    AFCOCRReport,
    DDARecords,
    IDBCRecords,
    Configs,
)
from celery_compare.tasks import compare


class Command(BaseCommand, LoggerMixin):

    def __init__(self):
        super().__init__()
        self.log_base = '[doc ocr process]'
        self.e_log_base = '[e-contract ocr process]'
        # 处理文件开关
        self.switch = True
        # 睡眠时间
        self.sleep_time_doc_get = float(conf.SLEEP_SECOND_DOC_GET)
        self.sleep_time_img_put = float(conf.SLEEP_SECOND_IMG_PUT)
        self.sleep_time_img_get = float(conf.SLEEP_SECOND_IMG_GET)
        self.sleep_time_task_get = float(conf.SLEEP_SECOND_TASK_GET)
        # 队列长度
        self.img_queue_size = int(conf.IMG_QUEUE_SIZE)
        # 数据目录
        self.data_dir = conf.DATA_DIR
        # DDA目录
        self.dda_dir = os.path.join(self.data_dir, 'HIL', 'SF5-CL-S-1', 'DDA')
        self.dda_complete_dir = os.path.join(self.dda_dir, 'complete')
        self.dda_wanting_dir = os.path.join(self.dda_dir, 'wanting')
        # ocr相关
        self.ocr_1_urls = conf.get_namespace('OCR_URL_1_')
        self.ocr_url_2 = conf.OCR_URL_2
        self.ocr_url_2_bc = conf.OCR_URL_2_BC
        self.ocr_url_3 = conf.BC_URL
        self.ocr_url_4 = conf.IC_URL
        # EDMS web_service_api
        # self.edms = EDMS()
        self.edms = ECM()
        # 优雅退出信号:15
        signal.signal(signal.SIGTERM, self.signal_handler)

    def signal_handler(self, sig, frame):
        self.switch = False  # 停止处理文件

    # def get_doc_object(self, task_str):
    #     business_type, doc_id_str = task_str.split(consts.SPLIT_STR)
    #     doc_id = int(doc_id_str)
    #     doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
    #     # doc_info = doc_class.objects.filter(id=doc_id, status=DocStatus.INIT.value).values(
    #     #     'id', 'metadata_version_id', 'application_id', 'document_name', 'document_scheme').first()
    #     doc = doc_class.objects.filter(id=doc_id).first()
    #     return doc, business_type

    def get_zip_doc_info(self, task_str):
        try:
            info_tuple = task_str.split(consts.SPLIT_STR)
            if len(info_tuple) == 2:
                business_type, doc_id_str = info_tuple
            else:
                business_type, doc_id_str, classify_1_str = info_tuple
            doc_id = int(doc_id_str)
            doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
            zip_doc = doc_class.objects.filter(id=doc_id).first()

            if zip_doc is None:
                self.online_log.warn('{0} [zip_2_pdfs] [doc not exist] [task_str={1}]'.format(
                    self.log_base, task_str))
                return None, business_type
            elif zip_doc.status != DocStatus.INIT.value:
                self.online_log.warn('{0} [zip_2_pdfs] [doc status error] [task_str={1}] [doc_status={2}]'.format(
                    self.log_base, task_str, zip_doc.status))
                return None, business_type

            zip_doc.status = DocStatus.PROCESSING.value
            zip_doc.start_time = timezone.now()
            zip_doc.save()
        except Exception as e:
            self.online_log.error('{0} [process error (zip_2_pdfs)] [error={1}]'.format(
                self.log_base, traceback.format_exc()))
            return None, None
        else:
            self.online_log.info('{0} [zip_2_pdfs] [db save end] [task_str={1}]'.format(
                self.log_base, task_str))
            return zip_doc, business_type

    def get_doc_info(self, task_str, is_priority=False):
        try:
            # doc, business_type = self.get_doc_object(task_str)
            info_tuple = task_str.split(consts.SPLIT_STR)
            if len(info_tuple) == 2:
                business_type, doc_id_str = info_tuple
                classify_1_str = '0'
                rebuild_task_str = task_str
            else:
                business_type, doc_id_str, classify_1_str = info_tuple
                rebuild_task_str = '{0}{1}{2}'.format(business_type, consts.SPLIT_STR, doc_id_str)
            doc_id = int(doc_id_str)
            doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
            doc = doc_class.objects.filter(id=doc_id).first()

            if doc is None:
                self.online_log.warn('{0} [get_doc_info] [doc not exist] [task_str={1}] [is_priority={2}]'.format(
                    self.log_base, task_str, is_priority))
                return None, None, None, None
            elif doc.status != DocStatus.INIT.value:
                self.online_log.warn('{0} [get_doc_info] [doc status error] [task_str={1}] [is_priority={2}] '
                                     '[doc_status={3}]'.format(self.log_base, task_str, is_priority, doc.status))
                return None, None, None, None
            doc.status = DocStatus.PROCESSING.value
            doc.start_time = timezone.now()
            doc.save()
        except Exception as e:
            rh.enqueue([task_str], is_priority)
            self.online_log.error('{0} [process error (get doc info in)] [error={1}]'.format(
                self.log_base, traceback.format_exc()))
            raise e
        else:
            self.online_log.info('{0} [get_doc_info] [db save end] [task_str={1}] [is_priority={2}]'.format(
                self.log_base, task_str, is_priority))
            return doc, business_type, rebuild_task_str, classify_1_str

    # def pdf_download(self, doc, pdf_path):
    #     if not doc.application_id.startswith(consts.FIXED_APPLICATION_ID_PREFIX):
    #         for times in range(consts.RETRY_TIMES):
    #             try:
    #                 self.edms.download(pdf_path, doc.metadata_version_id)
    #             except Exception as e:
    #                 self.online_log.warn('{0} [edms download failed] [times={1}] [pdf_path={2}] '
    #                                       '[error={3}]'.format(self.log_base, times, pdf_path, traceback.format_exc()))
    #                 edms_exc = str(e)
    #             else:
    #                 break
    #         else:
    #             raise EDMSException(edms_exc)
    #     self.online_log.info('{0} [edms download success] [pdf_path={1}]'.format(self.log_base, pdf_path))

    def bs_process(self, wb, ocr_data, bs_summary, unknown_summary, classify, res_list, pno, ino, part_idx):
        sheets = ocr_data.get('data', [])
        if not sheets:
            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY))
            return
        # confidence = ocr_data.get('confidence', 1)
        img_name = 'page_{0}_img_{1}_{2}'.format(pno, ino, part_idx)
        cells_exists = False
        for i, sheet in enumerate(sheets):
            cells = sheet.get('cells')
            if not cells:
                continue
            cells_exists = True
            sheet_name = '{0}_{1}'.format(img_name, i)
            ws = wb.create_sheet(sheet_name)
            for cell in cells:
                c1 = cell.get('start_column')
                r1 = cell.get('start_row')
                words = cell.get('words')
                ws.cell(row=r1 + 1, column=c1 + 1, value=words)

            # 真伪
            verify_info = []
            verify_dict = sheet.get('verify', {})
            if verify_dict.get('verify_res') == 'fake':
                verify_info.extend(verify_dict.get('verify_info', []))

            # ['户名', '卡号', '页码', '回单验证码', '打印时间', '起始时间', '终止时间']
            summary = sheet.get('summary')
            card = summary[1]
            if card is None:
                classify_dict = unknown_summary.setdefault(classify, {})
                role = consts.UNKNOWN_ROLE if summary[0] is None else summary[0]
                role_dict = classify_dict.setdefault(role, {})
                role_dict['classify'] = classify
                role_dict['role'] = role
                role_dict.setdefault('sheet', []).append(sheet_name)
                # role_dict.setdefault('confidence', []).append(confidence)
                code_list = role_dict.setdefault('code', [])
                pt_list = role_dict.setdefault('print_time', [])
                sd_list = role_dict.setdefault('start_date', [])
                ed_list = role_dict.setdefault('end_date', [])
                verify_list = role_dict.setdefault('verify', [])
                if summary[3] is not None:
                    code_list.append((summary[2], summary[3]))
                if summary[4] is not None:
                    pt_list.append(summary[4])
                if summary[5] is not None:
                    sd_list.append(summary[5])
                if summary[6] is not None:
                    ed_list.append(summary[6])
                if len(verify_info) > 0:
                    verify_list.append(
                        (pno, ino, '、'.join(verify_info))
                    )
            else:
                card_dict = bs_summary.setdefault(card, {})
                card_dict['count'] = card_dict.get('count', 0) + 1
                card_dict.setdefault('classify', []).append(classify)
                # card_dict.setdefault('confidence', []).append(confidence)
                card_dict.setdefault('sheet', []).append(sheet_name)
                role_list = card_dict.setdefault('role', [])
                role_set = card_dict.setdefault('role_set', set())
                code_list = card_dict.setdefault('code', [])
                pt_list = card_dict.setdefault('print_time', [])
                sd_list = card_dict.setdefault('start_date', [])
                ed_list = card_dict.setdefault('end_date', [])
                verify_list = card_dict.setdefault('verify', [])
                if summary[0] is not None:
                    role_list.append(summary[0])
                    role_set.add(summary[0])
                if summary[3] is not None:
                    code_list.append((summary[2], summary[3]))
                if summary[4] is not None:
                    pt_list.append(summary[4])
                if summary[5] is not None:
                    sd_list.append(summary[5])
                if summary[6] is not None:
                    ed_list.append(summary[6])
                if len(verify_info) > 0:
                    verify_list.append(
                        (pno, ino, '、'.join(verify_info))
                    )

        if cells_exists:
            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS))
        else:
            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY))

    def contract_process(self, classify, ocr_data, contract_result, res_list, pno, ino, part_idx,
                         img_path, contract_result_compare):
        contract_dict = ocr_data.get('data')
        if not contract_dict or contract_dict.get('page_num') is None or contract_dict.get('page_info') is None:
            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY))
            return
        res_list.append((pno, ino, part_idx, consts.RES_SUCCESS))
        page_num = contract_dict.get('page_num')
        if page_num.startswith('page_'):
            page_num_only = page_num.split('_')[-1]
        else:
            page_num_only = page_num
        rebuild_page_info = []
        text_key = 'words'
        position_key = 'position'
        for key, value in contract_dict.get('page_info', {}).items():
            if value is None:
                rebuild_page_info.append((key, ))
            elif text_key in value:
                if value[text_key] is None:
                    rebuild_page_info.append((key,))
                elif isinstance(value[text_key], str):
                    rebuild_page_info.append((key, value[text_key]))
                elif isinstance(value[text_key], list):
                    rebuild_page_info.append((key,))
                    for row_list in value[text_key]:
                        rebuild_page_info.append(row_list)
            else:
                rebuild_page_info.append((key,))
                for sub_key, sub_value in value.items():
                    if sub_value is None:
                        rebuild_page_info.append((sub_key,))
                    elif text_key in sub_value:
                        if sub_value[text_key] is None:
                            rebuild_page_info.append((sub_key,))
                        elif isinstance(sub_value[text_key], str):
                            rebuild_page_info.append((sub_key, sub_value[text_key]))
                        elif isinstance(sub_value[text_key], list):
                            rebuild_page_info.append((sub_key,))
                            for row_list in sub_value[text_key]:
                                rebuild_page_info.append(row_list)

        contract_result.setdefault(classify, dict()).setdefault(page_num_only, []).append(rebuild_page_info)

        page_compare_dict = {
            consts.IMG_PATH_KEY: img_path,
            consts.ALL_POSITION_KEY: {},
        }
        for key, value in contract_dict.get('page_info', {}).items():
            if not isinstance(value, dict):
                continue
            elif text_key in value:
                position_list = value.get(position_key, [])
                page_compare_dict[consts.ALL_POSITION_KEY][key] = position_list if isinstance(position_list, list) else []

                if value[text_key] is None:
                    page_compare_dict[key] = ''
                elif isinstance(value[text_key], str):
                    page_compare_dict[key] = value[text_key]
                elif isinstance(value[text_key], list):
                    page_compare_dict[key] = value[text_key]
            else:
                page_compare_dict[key] = {}
                page_compare_dict[consts.ALL_POSITION_KEY][key] = {}
                for sub_key, sub_value in value.items():
                    position_list = sub_value.get(position_key, [])
                    page_compare_dict[consts.ALL_POSITION_KEY][key][sub_key] = position_list if isinstance(
                        position_list, list) else []

                    if sub_value[text_key] is None:
                        page_compare_dict[key][sub_key] = ''
                    elif isinstance(sub_value[text_key], str):
                        page_compare_dict[key][sub_key] = sub_value[text_key]

        contract_result_compare.setdefault(classify, dict())[consts.ASP_KEY] = contract_dict.get(consts.ASP_KEY, False)
        # "position" = [xmin, ymin, xmax, ymax]
        contract_result_compare.setdefault(classify, dict())[page_num_only] = page_compare_dict

    @staticmethod
    def rebuild_position(src_position):
        # 'position': {'left': 470, 'top': 671, 'right': 542, 'bottom': 694}
        #                                        'width'='right-left', 'height'='bottom-top'
        # 'position': {'left': 470, 'top': 671, 'width': 542, 'height': 694}
        try:
            left = src_position.get('left', 0)
            top = src_position.get('top', 0)
            right = src_position.get('right', 0)
            bottom = src_position.get('bottom', 0)
            width = right - left
            height = bottom - top
            return {
                'left': left,
                'top': top,
                'width': width,
                'height': height,
            }
        except Exception as e:
            return {
                'left': 0,
                'top': 0,
                'width': 0,
                'height': 0,
            }

    def license1_process(self, ocr_data, license_summary, classify, res_list, pno, ino, part_idx, img_path, do_dda,
                         dda_id_bc_mapping):
        # 类别:'0'身份证, '1'居住证
        license_data = ocr_data.get('data')
        if not license_data:
            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY))
            return
        if isinstance(license_data, dict):
            pre, suf = os.path.splitext(img_path)
            base64_img = license_data.pop('base64_img', '')
            is_save = True if len(base64_img) > 0 else False
            section_img_path = '{0}_{1}{2}'.format(pre, part_idx, suf) if is_save else img_path
            if is_save:
                try:
                    with open(section_img_path, "wb") as fh:
                        fh.write(base64.b64decode(base64_img.encode()))
                except Exception as e:
                    self.online_log.warn(
                        '{0} [section img save failed] [img_path={1}]'
                        ' [part_idx={2}]'.format(self.log_base, img_path, part_idx))
        else:
            is_save = False
            section_img_path = img_path

        # 保单
        if classify == consts.INSURANCE_CLASSIFY:
            product_result = ['', '', '']
            product_result_position = [dict(), dict(), dict()]
            min_char_count_1 = 1000
            min_char_count_2 = 1000
            for product in license_data.get('result', {}).get('productList', []):
                name = product.get('name', {}).get('words', '')
                if name.find('机动车损失') != -1 or name.find('汽车损失') != -1 or name.find('车损险') != -1 or \
                        name.find('车损失险') != -1 or name.find('车损失保险') != -1:
                    if len(name) < min_char_count_1:
                        min_char_count_1 = len(name)
                        product_result[0] = product.get('coverage', {}).get('words', '')
                        product_result[2] = product.get('deductible_franchise', {}).get('words', '')
                        product_result_position[0] = self.rebuild_position(product.get('coverage', {}).get(
                            'position', {}))
                        product_result_position[2] = self.rebuild_position(product.get('deductible_franchise', {}).get(
                            'position', {}))
                elif name.find('第三者责任') != -1:
                    if len(name) < min_char_count_2:
                        min_char_count_2 = len(name)
                        product_result[1] = product.get('coverage', {}).get('words', '')
                        product_result_position[1] = self.rebuild_position(product.get('coverage', {}).get(
                            'position', {}))

            special_str = license_data.get('result', {}).get('1stBeneficiary', {}).get('words', '')
            special = '无'
            if special_str.find('宝马') != -1 or special_str.find('先锋国际融资租赁有限公司') != -1:
                special = '有'
            insurance_ocr_result = {
                '被保险人姓名': license_data.get('result', {}).get('insured', {}).get('name', {}).get('words', ''),
                '被保险人证件号码': license_data.get('result', {}).get('insured', {}).get('certiCode', {}).get('words', ''),
                '车架号': license_data.get('result', {}).get('vehicle', {}).get('VIN', {}).get('words', ''),
                '机动车损失保险金额': product_result[0],
                '机动车第三者责任保险金额': product_result[1],
                '机动车损失保险绝对免赔率/绝对免赔额': product_result[2],
                '保险费合计': license_data.get('result', {}).get('premiumSum', {}).get('words', ''),
                '保险起始日期': license_data.get('result', {}).get('startDate', {}).get('words', ''),
                '保险截止日期': license_data.get('result', {}).get('endDate', {}).get('words', ''),
                '保单章': license_data.get('result', {}).get('seal', {}).get('words', ''),
                '特别约定第一受益人': special,
                consts.IMG_PATH_KEY: img_path,
                consts.SECTION_IMG_PATH_KEY: section_img_path,
            }

            position_dict = {
                '被保险人姓名': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'insured', {}).get('name', {}).get('position', {}))},
                '被保险人证件号码': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'insured', {}).get('certiCode', {}).get('position', {}))},
                '车架号': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'vehicle', {}).get('VIN', {}).get('position', {}))},
                '机动车损失保险金额': {consts.FIELD_POSITION_KEY: product_result_position[0]},
                '机动车第三者责任保险金额': {consts.FIELD_POSITION_KEY: product_result_position[1]},
                '机动车损失保险绝对免赔率/绝对免赔额': {consts.FIELD_POSITION_KEY: product_result_position[2]},
                '保险费合计': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'premiumSum', {}).get('position', {}))},
                '保险起始日期': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'startDate', {}).get('position', {}))},
                '保险截止日期': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'endDate', {}).get('position', {}))},
                '保单章': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    'seal', {}).get('position', {}))},
                '特别约定第一受益人': {consts.FIELD_POSITION_KEY: self.rebuild_position(license_data.get('result', {}).get(
                    '1stBeneficiary', {}).get('position', {}))},
            }
            insurance_ocr_result[consts.ALL_POSITION_KEY] = position_dict
            license_summary.setdefault(classify, []).append(insurance_ocr_result)
        # DDA
        elif classify == consts.DDA_CLASSIFY:
            pro = ocr_data.get('confidence', 0)
            if pro < consts.DDA_PRO_MIN:
                res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_EMPTY))
                return
            dda_ocr_result = {}
            position_dict = {}
            for key, value in license_data.get('result', {}).items():
                dda_ocr_result[key] = value.get('words', '')
                position_dict[key] = {
                    consts.FIELD_POSITION_KEY: value.get('position', {})
                }
            dda_ocr_result[consts.DDA_IMG_PATH] = img_path
            dda_ocr_result[consts.DDA_PRO] = pro
            dda_ocr_result[consts.IMG_PATH_KEY] = img_path
            dda_ocr_result[consts.SECTION_IMG_PATH_KEY] = section_img_path
            dda_ocr_result[consts.ALL_POSITION_KEY] = position_dict
            license_summary.setdefault(classify, []).append(dda_ocr_result)
        # 抵押登记豁免函
        elif classify == consts.HMH_CLASSIFY:
            hmh_ocr_result = {}
            position_dict = {}
            for key, value in license_data.get('words_result', {}).items():
                hmh_ocr_result[key] = value.get('words', '')
                location_list = value.get('location', [-1, -1, -1, -1])
                if len(location_list) == 4:
                    position_dict[key] = {
                        consts.FIELD_POSITION_KEY: {
                            'top': location_list[1],
                            'left': location_list[0],
                            'height': location_list[-1] - location_list[1],
                            'width': location_list[2] - location_list[0]
                        }
                    }
            hmh_ocr_result[consts.IMG_PATH_KEY] = img_path
            hmh_ocr_result[consts.SECTION_IMG_PATH_KEY] = section_img_path
            hmh_ocr_result[consts.ALL_POSITION_KEY] = position_dict
            license_summary.setdefault(classify, []).append(hmh_ocr_result)
        # 二手车交易凭证
        elif classify == consts.JYPZ_CLASSIFY:
            jypz_ocr_result = {}
            position_dict = {}
            for key, value in license_data.get('result', {}).items():
                jypz_ocr_result[key] = value.get('words', '')
                position_dict[key] = {
                    consts.FIELD_POSITION_KEY: value.get('position', {})
                }
            jypz_ocr_result[consts.IMG_PATH_KEY] = img_path
            jypz_ocr_result[consts.SECTION_IMG_PATH_KEY] = section_img_path
            jypz_ocr_result[consts.ALL_POSITION_KEY] = position_dict
            license_summary.setdefault(classify, []).append(jypz_ocr_result)
        # 车辆登记证 3/4页结果整合
        elif classify == consts.MVC_CLASSIFY:
            rebuild_data_dict = {}
            position_dict = {}
            mvc_page = license_data.pop('page', 'VehicleRCI')
            mvc_res = license_data.pop('results', {})
            if mvc_page == 'VehicleRegArea':
                rebuild_data_dict['机动车登记证书编号'] = mvc_res.get('机动车登记证书编号', {}).get('words', '')
                code_position_list = mvc_res.get('机动车登记证书编号', {}).get('position', [0, 0, 0, 0])
                if len(code_position_list) == 4:
                    position_dict['机动车登记证书编号'] = {
                        consts.FIELD_POSITION_KEY: {
                            'top': code_position_list[1],
                            'left': code_position_list[0],
                            'height': code_position_list[-1],
                            'width': code_position_list[2],
                        }
                    }
                for register_info in mvc_res.get('登记信息', []):
                    register_info.pop('register_type', None)
                    register_info.pop('register_type_name', None)
                    for cn_key, detail_dict in register_info.items():
                        rebuild_data_dict.setdefault(cn_key, []).append(
                            detail_dict.get('words', ''))
                        tmp_position_list = detail_dict.get('position', [0, 0, 0, 0])
                        if len(tmp_position_list) == 4:
                            position_dict.setdefault(cn_key, []).append(
                                {
                                    consts.FIELD_POSITION_KEY: {
                                        'top': tmp_position_list[1],
                                        'left': tmp_position_list[0],
                                        'height': tmp_position_list[-1],
                                        'width': tmp_position_list[2],
                                    }
                                }
                            )
                            
                rebuild_data_dict[consts.ALL_POSITION_KEY_2] = position_dict
                rebuild_data_dict[consts.IMG_PATH_KEY_2] = img_path
                rebuild_data_dict[consts.SECTION_IMG_PATH_KEY_2] = section_img_path
            else:
                for cn_key, detail_dict in mvc_res.items():
                    rebuild_data_dict[cn_key] = detail_dict.get('words', '')
                    position_list = detail_dict.get('position', [0, 0, 0, 0])
                    if len(position_list) == 4:
                        position_dict[cn_key] = {
                            consts.FIELD_POSITION_KEY: {
                                'top': position_list[1],
                                'left': position_list[0],
                                'height': position_list[-1],
                                'width': position_list[2],
                            }
                        }
                rebuild_data_dict[consts.ALL_POSITION_KEY] = position_dict
                rebuild_data_dict[consts.IMG_PATH_KEY] = img_path
                rebuild_data_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path
            del mvc_res
            license_summary.setdefault(classify, []).append(rebuild_data_dict)


            # for mvc_dict in license_data:
            #     mvc_dict[consts.IMG_PATH_KEY] = img_path
            #     try:
            #         mvc_page = mvc_dict.pop('page')
            #     except Exception as e:
            #         pass
            #     else:
            #         if mvc_page == 'VehicleRegArea':
            #             mvc_res = mvc_dict.pop('results', {})
            #             mvc_dict['机动车登记证书编号'] = mvc_res.get('register_no', {}).get('words', '')
            #             for register_info in mvc_res.get('register_info', []):
            #                 for detail_dict in register_info.get('details', {}).values():
            #                     mvc_dict.setdefault(detail_dict.get('chinese_key', '未知'), []).append(
            #                         detail_dict.get('words', ''))
            #             del mvc_res
            # license_summary.setdefault(classify, []).extend(license_data)

        # 身份证真伪
        elif classify == consts.IC_CLASSIFY:
            id_card_dict = {}
            position_dict = {}
            card_type = license_data.get('type', '')
            is_ic = card_type.startswith('身份证')
            is_info_side = card_type.endswith('信息面')
            id_card_dict['类别'] = '0' if is_ic else '1'
            if is_ic:
                field_map = consts.IC_MAP_0 if is_info_side else consts.IC_MAP_1
            else:
                field_map = consts.RP_MAP_0 if is_info_side else consts.RP_MAP_1
            for write_field, search_field in field_map:
                id_card_dict[write_field] = license_data.get('words_result', {}).get(search_field, {}).get('words', '')
                location_list = license_data.get('words_result', {}).get(search_field, {}).get(
                    'location', [-1, -1, -1, -1])
                if len(location_list) == 4:
                    position_dict[write_field] = {
                        consts.FIELD_POSITION_KEY: {
                            'top': location_list[1],
                            'left': location_list[0],
                            'height': location_list[-1] - location_list[1],
                            'width': location_list[2] - location_list[0]
                        }
                    }
            if not is_info_side:
                start_time = license_data.get('words_result', {}).get('签发日期', {}).get('words', '')
                end_time = license_data.get('words_result', {}).get('失效日期', {}).get('words', '')
                id_card_dict['有效期限'] = '{0}-{1}'.format(start_time, end_time)
                end_time_location_list = license_data.get('words_result', {}).get('失效日期', {}).get(
                    'location', [-1, -1, -1, -1])
                if len(end_time_location_list) == 4:
                    position_dict['有效期限'] = {
                        consts.FIELD_POSITION_KEY: {
                            'top': end_time_location_list[1],
                            'left': end_time_location_list[0],
                            'height': end_time_location_list[-1] - end_time_location_list[1],
                            'width': end_time_location_list[2] - end_time_location_list[0]
                        }
                    }


            if not is_info_side:
                id_card_dict[consts.IMG_PATH_KEY_2] = img_path
                id_card_dict[consts.ALL_POSITION_KEY_2] = position_dict
                id_card_dict[consts.SECTION_IMG_PATH_KEY_2] = section_img_path

            else:
                id_card_dict[consts.ALL_POSITION_KEY] = position_dict
                id_card_dict[consts.IMG_PATH_KEY] = img_path
                id_card_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path

            if is_ic and is_save:
                card_type = -1
                json_data_4 = {
                    'mode': 1,
                    'user_info': {
                        'image_content': base64_img,
                    },
                    'options': {
                        'distinguish_type': 1,
                        'auto_rotate': True,
                    },
                }
                for times in range(consts.RETRY_TIMES):
                    try:
                        start_time = time.time()
                        ocr_4_response = requests.post(self.ocr_url_4, json=json_data_4)
                        if ocr_4_response.status_code != 200:
                            raise OCR4Exception('ocr_4 status code: {0}'.format(ocr_4_response.status_code))
                    except Exception as e:
                        self.online_log.warn(
                            '{0} [ocr_4 failed] [times={1}] [img_path={2}] [error={3}]'.format(
                                self.log_base, times, img_path, traceback.format_exc()))
                    else:
                        ocr_4_res = ocr_4_response.json()
                        end_time = time.time()
                        speed_time = int(end_time - start_time)

                        if ocr_4_res.get('code') == 0 and ocr_4_res.get('result', {}).get('rtn') == 0:
                            card_type = ocr_4_res.get('result', {}).get(
                                'idcard_distinguish_result', {}).get('result', -1)

                        self.online_log.info(
                            '{0} [ocr_4 success] [img_path={1}] [speed_time={2}]'.format(
                                self.log_base, img_path, speed_time))
                        break
                else:
                    self.online_log.warn(
                        '{0} [ocr_4 failed] [img_path={1}]'.format(self.log_base, img_path))

                id_card_dict[consts.IC_TURE_OR_FALSE] = consts.IC_RES_MAPPING.get(card_type)

                if do_dda and isinstance(id_card_dict.get(consts.IC_KEY_FIELD[0]), str) and \
                        isinstance(id_card_dict.get(consts.IC_KEY_FIELD[1]), str):
                    ic_name = id_card_dict.get(consts.IC_KEY_FIELD[0], '').strip()
                    ic_id = id_card_dict.get(consts.IC_KEY_FIELD[1], '').strip()
                    if len(ic_name) > 0 and len(ic_id) > 0:
                        dda_id_bc_mapping.setdefault(consts.IC_FIELD, []).append((ic_name, ic_id, img_path))
            license_summary.setdefault(classify, []).append(id_card_dict)
        # 购车发票 & 二手车发票
        elif classify == consts.MVI_CLASSIFY or classify == consts.UCI_CLASSIFY:
            rebuild_data_dict = {}
            position_dict = {}
            mvi_res = license_data.pop('result', {})
            for en_key, detail_dict in mvi_res.items():
                rebuild_data_dict[detail_dict.get('chinese_key', '')] = detail_dict.get('words', '')
                position_dict[detail_dict.get('chinese_key', '')] = {
                    consts.FIELD_POSITION_KEY: detail_dict.get('position', {})
                }
            rebuild_data_dict['新旧版式'] = license_data.get('layout', '')
            rebuild_data_dict[consts.IMG_PATH_KEY] = img_path
            rebuild_data_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path
            rebuild_data_dict[consts.ALL_POSITION_KEY] = position_dict
            license_summary.setdefault(classify, []).append(rebuild_data_dict)
        # 其他
        else:
            for res_dict in license_data:
                res_dict[consts.IMG_PATH_KEY] = img_path
                res_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path
            license_summary.setdefault(classify, []).extend(license_data)
        res_list.append((pno, ino, part_idx, consts.RES_SUCCESS))

    def license2_process(self, ocr_res_2, license_summary, pid, classify, res_list, pno, ino, part_idx, img_path, do_dda, dda_id_bc_mapping, file_data):
        if ocr_res_2.get('ErrorCode') in consts.SUCCESS_CODE_SET:
            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS))
            if pid == consts.BC_PID:
                # 银行卡
                # res_dict = {}
                # for en_key, chn_key in consts.BC_FIELD:
                #     res_dict[chn_key] = ocr_res_2.get(en_key, '')
                ocr_res_2[consts.IMG_PATH_KEY] = img_path
                license_summary.setdefault(classify, []).append(ocr_res_2)
                if do_dda and isinstance(ocr_res_2.get(consts.BC_KEY_FIELD), str):
                    bc_no = ocr_res_2[consts.BC_KEY_FIELD].strip()
                    if len(bc_no) > 0:
                        dda_id_bc_mapping.setdefault(consts.BC_FIELD, []).append((bc_no, img_path))
            else:
                # 营业执照等
                pre, suf = os.path.splitext(img_path)
                src_section_img_path = img_path if file_data is None else '{0}_{1}{2}'.format(pre, part_idx, suf)

                is_save = False
                for res_idx, result_dict in enumerate(ocr_res_2.get('ResultList', [])):
                    image_data = result_dict.get('image_data', '')
                    if len(image_data) > 0:
                        position = {}
                        angle = 0
                        section_img_path = '{0}_{1}_{2}{3}'.format(pre, part_idx, res_idx, suf)
                        try:
                            with open(section_img_path, "wb") as fh:
                                fh.write(base64.b64decode(image_data.encode()))
                        except Exception as e:
                            self.online_log.warn(
                                '{0} [section img save failed] [img_path={1}]'
                                ' [part_idx={2}] [res_idx={3}]'.format(self.log_base, img_path, part_idx, res_idx))
                    else:
                        is_save = True
                        section_img_path = src_section_img_path
                        position = result_dict.get('position', {})
                        angle = result_dict.get('angle', 0)
                    res_dict = {}
                    position_dict = {}
                    for field_dict in result_dict.get('FieldList', []):
                        res_dict[field_dict.get('chn_key', '')] = field_dict.get('value', '')
                        position_dict[field_dict.get('chn_key', '')] = {
                            consts.FIELD_POSITION_KEY: field_dict.get('position', {}),
                            consts.FIELD_QUAD_KEY: field_dict.get('quad', []),
                        }
                    position_dict[consts.POSITION_KEY] = position
                    position_dict[consts.ANGLE_KEY] = angle
                    res_dict[consts.IMG_PATH_KEY] = img_path
                    res_dict[consts.SECTION_IMG_PATH_KEY] = section_img_path
                    res_dict[consts.ALL_POSITION_KEY] = position_dict
                    license_summary.setdefault(classify, []).append(res_dict)

                if is_save and file_data is not None:
                    try:
                        with open(src_section_img_path, "wb") as fh:
                            fh.write(base64.b64decode(file_data.encode()))
                    except Exception as e:
                        self.online_log.warn(
                            '{0} [section img save failed] [img_path={1}]'
                            ' [part_idx={2}]'.format(self.log_base, img_path, part_idx))
        else:
            res_list.append((pno, ino, part_idx, consts.RES_FAILED_2))

    @staticmethod
    def license_rebuild(license_summary):
        ic_merge = False
        rp_merge = False

        for classify in (consts.IC_CLASSIFY, consts.MVI_CLASSIFY, consts.MVC_CLASSIFY):

            license_list = license_summary.get(classify)

            if not license_list:
                continue

            if classify == consts.IC_CLASSIFY:  # 身份证、居住证分开,先正面,后反面
                key, _, _ = consts.FIELD_ORDER_MAP.get(classify)
                ic_side1_list = []
                ic_side2_list = []
                rp_side1_list = []
                rp_side2_list = []
                for license_dict in license_list:
                    is_rp = license_dict.pop('类别', '0')
                    if key in license_dict:
                        if is_rp == '1':
                            rp_side2_list.append(license_dict)
                        else:
                            ic_side2_list.append(license_dict)
                    elif is_rp == '1':
                        rp_side1_list.append(license_dict)
                    else:
                        ic_side1_list.append(license_dict)

                ic_merge = len(ic_side1_list) == len(ic_side2_list) == 1
                rp_merge = len(rp_side1_list) == len(rp_side2_list) == 1

                ic_side1_list.extend(ic_side2_list)
                rp_side1_list.extend(rp_side2_list)

                if ic_side1_list:
                    # license_list = ic_side1_list
                    license_summary[classify] = ic_side1_list
                else:
                    license_summary.pop(classify, None)

                if rp_side1_list:
                    license_summary[consts.RP_CLASSIFY] = rp_side1_list

                ic_side1_list = ic_side2_list = rp_side1_list = rp_side2_list = None

            if classify == consts.MVI_CLASSIFY:  # 机动车销售统一发票, 增加不含税价(逻辑计算)
                for license_dict in license_list:
                    price = ''
                    rate_str = license_dict.get('增值税税率')
                    price_total_str = license_dict.get('价税合计小写')
                    if rate_str is not None and price_total_str is not None:
                        try:
                            rate = int(rate_str.rstrip('%'))
                            price_total = float(price_total_str)
                        except Exception as e:
                            pass
                        else:
                            price = round(price_total * 100 / (rate + 100), 2)
                    license_dict['不含税价(逻辑计算)'] = price

            if classify == consts.MVC_CLASSIFY:  # 机动车登记证先1/2页,后3/4页
                key, _, _ = consts.FIELD_ORDER_MAP.get(classify)
                page_1_2 = []
                page_3_4 = []
                for license_dict in license_list:
                    if key in license_dict:
                        page_3_4.append(license_dict)
                    else:
                        page_1_2.append(license_dict)
                page_1_2.extend(page_3_4)
                license_summary[classify] = page_1_2
                page_1_2 = page_3_4 = None

        return ic_merge, rp_merge

    def parse_img_path(self, img_path):
        img_name, _ = os.path.splitext(os.path.basename(img_path))
        part_list = img_name.split('_')
        # page_7_img_11_0
        return int(part_list[1])+1, int(part_list[3])+1

    def get_most(self, value_list):
        if value_list:
            most_common = Counter(value_list).most_common(1)
            return most_common[0][0] if most_common else None

    def date_format(self, date_str, format_str):
        try:
            date_res = datetime.strptime(date_str, format_str).date()
        except Exception as e:
            return
        else:
            return date_res

    def get_validate_date(self, date_list):
        for date_str in date_list:
            for format_str in consts.DATE_FORMAT:
                date_res = self.date_format(date_str, format_str)
                if isinstance(date_res, date):
                    return date_res

    def merge_card(self, bs_summary):
        classify_info = {}
        merged_bs_summary = {}
        sorted_card = sorted(bs_summary.keys(), key=lambda x: bs_summary[x]['count'], reverse=True)
        for main_card in sorted_card:
            if bs_summary.get(main_card) is None:
                continue
            merged_bs_summary[main_card] = bs_summary.pop(main_card)
            del merged_bs_summary[main_card]['count']
            merge_cards = []
            for card in bs_summary.keys():
                if difflib.SequenceMatcher(None, main_card, card).quick_ratio() > consts.CARD_RATIO:
                    merged_bs_summary[main_card]['classify'].extend(bs_summary[card]['classify'])
                    # merged_bs_summary[main_card]['confidence'].extend(bs_summary[card]['confidence'])
                    merged_bs_summary[main_card]['sheet'].extend(bs_summary[card]['sheet'])
                    merged_bs_summary[main_card]['role'].extend(bs_summary[card]['role'])
                    merged_bs_summary[main_card]['role_set'].update(bs_summary[card]['role_set'])
                    merged_bs_summary[main_card]['code'].extend(bs_summary[card]['code'])
                    merged_bs_summary[main_card]['verify'].extend(bs_summary[card]['verify'])
                    merged_bs_summary[main_card]['print_time'].extend(bs_summary[card]['print_time'])
                    merged_bs_summary[main_card]['start_date'].extend(bs_summary[card]['start_date'])
                    merged_bs_summary[main_card]['end_date'].extend(bs_summary[card]['end_date'])
                    merge_cards.append(card)
            for card in merge_cards:
                del bs_summary[card]
            most_classify = self.get_most(merged_bs_summary[main_card]['classify'])
            classify_count = classify_info.get(most_classify, 0)
            classify_info[most_classify] = classify_count + 1
            merged_bs_summary[main_card]['classify'] = most_classify
            merged_bs_summary[main_card]['role'] = self.get_most(merged_bs_summary[main_card]['role'])
        del bs_summary
        return merged_bs_summary, classify_info

    def prune_bs_summary(self, bs_summary):
        for summary in bs_summary.values():
            del summary['count']
            summary['classify'] = self.get_most(summary['classify'])
            summary['role'] = self.get_most(summary['role'])
        return bs_summary

    def rebuild_bs(self, bs_summary):
        # bs_summary = {
        #     '卡号': {
        #         'classify': 0,
        #         'confidence': 0.9,
        #         'role': '柳雪',
        #         'code': [('page', 'code'), ],
        #         'verify': [(pno, ino, reason_str), ]
        #         'print_time': 'datetime',
        #         'start_date': 'datetime',
        #         'end_date': 'datetime',
        #         'sheet': ['sheet_name']
        #     }
        # }
        res = []
        for bs_info in bs_summary.values():
            try:
                print_date = bs_info.get('print_time', '').strftime("%Y-%m-%d")
            except Exception as e:
                try:
                    print_date = bs_info.get('end_date', '').strftime("%Y-%m-%d")
                except Exception as e:
                    print_date = ''
            res.append(
                {
                    'role': bs_info.get('role', ''),
                    'print_time': print_date,
                    'timedelta': bs_info.get('timedelta', ''),
                    'verify': bs_info.get('verify_res_ebank', True)
                }
            )
        return res

    def rebuild_contract(self, license_summary, contract_result_compare):
        for classify, page_info_dict in contract_result_compare.items():
            if classify == consts.CONTRACT_CLASSIFY:
                res = {}
                is_asp = page_info_dict.get(consts.ASP_KEY, False)
                # is_asp = True
                for key, (pno_not_asp, pno_asp, key1, key2) in consts.SE_AFC_CON_MAP.items():
                    pno = pno_asp if is_asp else pno_not_asp
                    if pno is None:
                        if isinstance(pno_asp, int):
                            continue
                        end_idx = 9
                        # end_idx = 9 if is_asp else 8
                        for i in range(1, end_idx):
                            res.setdefault(key, list()).append(page_info_dict.get(str(i), {}).get(key1, ''))
                    elif key2 is None:
                        res[key] = page_info_dict.get(str(pno), {}).get(key1, '')
                        res.setdefault(consts.IMG_PATH_KEY, dict())[key] = page_info_dict.get(str(pno), {}).get(
                            consts.IMG_PATH_KEY, '')
                        res.setdefault(consts.ALL_POSITION_KEY, dict())[key] = page_info_dict.get(str(pno), {}).get(
                            consts.ALL_POSITION_KEY, {}).get(key1, [])
                    else:
                        res[key] = page_info_dict.get(str(pno), {}).get(key1, {}).get(key2, '')
                        res.setdefault(consts.IMG_PATH_KEY, dict())[key] = page_info_dict.get(str(pno), {}).get(
                            consts.IMG_PATH_KEY, '')
                        res.setdefault(consts.ALL_POSITION_KEY, dict())[key] = page_info_dict.get(str(pno), {}).get(
                            consts.ALL_POSITION_KEY, {}).get(key1, {}).get(key2, [])

                # res = {
                #     'key': 'list or str',
                #     'uniq_img_path_key': {
                #         'key': 'str',
                #     },
                #     'uniq_all_position_key': {
                #         'key': 'list'
                #     }
                # }
                license_summary[classify] = [res]
            else:
                res = {}
                for key, (pno1, pno2, end_idx, key1, key2) in consts.SE_HIL_CON_MAP[classify].items():
                    if pno1 is None:
                        for i in range(1, end_idx):
                            res.setdefault(key, list()).append(page_info_dict.get(str(i), {}).get(key1, ''))
                    elif key2 is None:
                        tmp_res = page_info_dict.get(str(pno1), {}).get(key1)
                        img_pno = pno1
                        if tmp_res is None and isinstance(pno2, int):
                            tmp_res = page_info_dict.get(str(pno2), {}).get(key1, '')
                            img_pno = pno1
                        res[key] = tmp_res
                        res.setdefault(consts.IMG_PATH_KEY, dict())[key] = page_info_dict.get(str(img_pno), {}).get(
                            consts.IMG_PATH_KEY, '')
                    else:
                        tmp_res = page_info_dict.get(str(pno1), {}).get(key1, {}).get(key2)
                        img_pno = pno1
                        if tmp_res is None and isinstance(pno2, int):
                            tmp_res = page_info_dict.get(str(pno2), {}).get(key1, {}).get(key2, '')
                            img_pno = pno1
                        res[key] = tmp_res
                        res.setdefault(consts.IMG_PATH_KEY, dict())[key] = page_info_dict.get(str(img_pno), {}).get(
                            consts.IMG_PATH_KEY, '')

                license_summary[classify] = [res]

    def rebuild_bs_summary(self, bs_summary, unknown_summary):
        # bs_summary = {
        #     '卡号': {
        #         'count': 100,
        #         'classify': [],
        #         'confidence': [],
        #         'role': [],
        #         'code': [('page', 'code'), ],
        #         'verify': [(pno, ino, reason_str), ]
        #         'print_time': [],
        #         'start_date': [],
        #         'end_date': [],
        #         'sheet': ['sheet_name']
        #     }
        # }
        #
        # unknown_summary = {
        #     0: {
        #         '户名': {
        #             'classify': 0,
        #             'confidence': [],
        #             'role': '户名',
        #             'code': [('page', 'code'), ],
        #             'verify': [(pno, ino, reason_str), ]
        #             'print_time': [],
        #             'start_date': [],
        #             'end_date': [],
        #             'sheet': ['sheet_name']
        #         }
        #     }
        # }
        # 归为同一份流水的逻辑
        # 所有图片均无卡号:同一分类同一户名归为同一份流水(如果同一分类下只有一个已知户名,则此分类下其他未知户名归为此户名)
        # 所有图片只已知1卡号:其他未知卡号流水归为此卡号
        # 所有图片已知多卡号: 1.根据相似度和图片数目合并相似已知卡号,并整理多数分类和户名集合
        #                  2.遍历所有未知卡号,进行过滤:当未知卡号分类与某已知卡号一致,且此未知卡号户名在此已知卡号户名集合中时,将未知卡号归为已知卡号。剩余未知卡号同一分类同一户名归为同一流水
        # 无卡号
        if len(bs_summary) == 0:
            del bs_summary
            merged_bs_summary = {}
            card_num = 1
            for role_dict in unknown_summary.values():
                if len(role_dict) == 2 and consts.UNKNOWN_ROLE in role_dict:
                    summary_dict = role_dict.pop(consts.UNKNOWN_ROLE, {})
                    for summary in role_dict.values():
                        # summary_dict['confidence'].extend(summary['confidence'])
                        summary_dict['role'] = summary['role']
                        summary_dict['code'].extend(summary['code'])
                        summary_dict['verify'].extend(summary['verify'])
                        summary_dict['print_time'].extend(summary['print_time'])
                        summary_dict['start_date'].extend(summary['start_date'])
                        summary_dict['end_date'].extend(summary['end_date'])
                        summary_dict['sheet'].extend(summary['sheet'])
                    card = '{0}_{1}'.format(consts.UNKNOWN_CARD, card_num)
                    merged_bs_summary[card] = summary_dict
                else:
                    for summary in role_dict.values():
                        card = '{0}_{1}'.format(consts.UNKNOWN_CARD, card_num)
                        card_num += 1
                        merged_bs_summary[card] = summary
        else:
            # 1卡号
            one_card = False
            if len(bs_summary) == 1:
                merged_bs_summary = self.prune_bs_summary(bs_summary)
                one_card = True
                classify_info = {}
            # 多卡号
            else:
                merged_bs_summary, classify_info = self.merge_card(bs_summary)

            for card_summary in merged_bs_summary.values():
                merge_role = []
                classify_summary = unknown_summary.get(card_summary['classify'], {})
                for role, summary in classify_summary.items():
                    if one_card or classify_info.get(card_summary['classify'], 0) == 1 or role in card_summary['role_set']:
                        merge_role.append(role)
                        # card_summary['confidence'].extend(summary['confidence'])
                        card_summary['sheet'].extend(summary['sheet'])
                        card_summary['code'].extend(summary['code'])
                        card_summary['verify'].extend(summary['verify'])
                        card_summary['print_time'].extend(summary['print_time'])
                        card_summary['start_date'].extend(summary['start_date'])
                        card_summary['end_date'].extend(summary['end_date'])

                for role in merge_role:
                    del classify_summary[role]

            card_num = 1
            for role_dict in unknown_summary.values():
                for summary in role_dict.values():
                    card = '{0}_{1}'.format(consts.UNKNOWN_CARD, card_num)
                    card_num += 1
                    merged_bs_summary[card] = summary

        del unknown_summary
        for summary in merged_bs_summary.values():
            if summary.get('role_set') is not None:
                del summary['role_set']
            summary['print_time'] = self.get_validate_date(summary['print_time'])
            summary['start_date'] = self.get_validate_date(summary['start_date'])
            summary['end_date'] = self.get_validate_date(summary['end_date'])
            # summary['confidence'] = max(summary['confidence'])
        return merged_bs_summary

    def zip_2_pdfs(self, zip_task_queue, error_list):
        while len(error_list) == 0:
            # 1. 从redis队列中读取任务: AFC_111_0
            task_str = rh.dequeue_zip()
            if task_str is None:
                self.online_log.info('{0} [zip_2_pdfs] [zip queue empty]'.format(self.log_base))
                time.sleep(self.sleep_time_doc_get)
                continue

            self.online_log.info('{0} [zip_2_pdfs] [task={1}]'.format(self.log_base, task_str))

            # 2. 修改doc状态: 识别中
            zip_doc, business_type = self.get_zip_doc_info(task_str)
            if zip_doc is None:
                time.sleep(self.sleep_time_doc_get)
                continue

            # 3. 从ECM下载压缩包
            doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, str(zip_doc.id))
            os.makedirs(doc_data_path, exist_ok=True)
            zip_path = os.path.join(doc_data_path, zip_doc.document_name)
            for times in range(consts.RETRY_TIMES):
                try:
                    self.edms.download(zip_path, zip_doc.metadata_version_id, zip_doc.document_scheme, business_type)
                except Exception as e:
                    self.online_log.warn('{0} [zip_2_pdfs] [ecm download failed] [task={1}] [times={2}] '
                                         '[error={3}]'.format(self.log_base, task_str, times,
                                                              traceback.format_exc()))
                else:
                    self.online_log.info('{0} [zip_2_pdfs] [ecm download success] [task={1}] [times={2}] '
                                         '[zip_path={3}]'.format(self.log_base, task_str, times, zip_path))
                    break
            else:
                try:
                    zip_doc.status = DocStatus.PROCESS_FAILED.value
                    zip_doc.save()
                except Exception as e:
                    self.online_log.error('{0} [zip_2_pdfs] [process error (db save)] [task={1}] [error={2}]'.format(
                        self.log_base, task_str, traceback.format_exc()))
                time.sleep(self.sleep_time_doc_get)
                continue

            # 4. 解压
            extract_path = os.path.join(doc_data_path, 'extract_content')
            os.makedirs(extract_path, exist_ok=True)
            try:
                pwd_list = get_pwd_list_from_str(zip_doc.document_name, zip_doc.password)
                is_success = extract_zip_or_rar(zip_path, extract_path, pwd_list)
            except Exception as e:
                is_success = False

            if not is_success:
                self.online_log.warn('{0} [zip_2_pdfs] [extract failed] [task={1}] [error={2}]'.format(
                    self.log_base, task_str, traceback.format_exc()))
                try:
                    zip_doc.status = DocStatus.PROCESS_FAILED.value
                    zip_doc.save()
                except Exception as e:
                    self.online_log.error('{0} [zip_2_pdfs] [process error (db save)] [task={1}] [error={2}]'.format(
                        self.log_base, task_str, traceback.format_exc()))
                time.sleep(self.sleep_time_doc_get)
                continue

            self.online_log.info('{0} [zip_2_pdfs] [extract success] [task={1}] [extract_path={2}]'.format(
                self.log_base, task_str, extract_path))

            # 5. 找出PDF文件重命名并移动到目标文件夹中。新建doc记录,新建task_str进入队列
            pdf_paths = get_file_paths(extract_path, ['.pdf', '.PDF'])
            count = 0
            pdf_task_str_list = []
            for pdf_path in pdf_paths:
                if count > 50:
                    self.online_log.info('{0} [zip_2_pdfs] [pdf count > 50, skip] [task={1}]'.format(
                        self.log_base, task_str))
                    break

                count += 1
                try:
                    doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
                    pdf_doc = doc_class.objects.create(
                        metadata_version_id='from: {0}'.format(zip_doc.id),
                        application_id=zip_doc.application_id,
                        # main_applicant=applicant_data.get('mainApplicantName'),
                        # co_applicant=applicant_data.get('coApplicantName'),
                        # guarantor_1=applicant_data.get('guarantor1Name'),
                        # guarantor_2=applicant_data.get('guarantor2Name'),
                        document_name=os.path.basename(pdf_path),
                        document_scheme=zip_doc.document_scheme,
                        data_source=zip_doc.data_source,
                        upload_finish_time=zip_doc.upload_finish_time,
                    )

                    pdf_doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, str(pdf_doc.id))
                    os.makedirs(pdf_doc_data_path, exist_ok=True)
                    target_pdf_path = os.path.join(pdf_doc_data_path, '{0}.pdf'.format(pdf_doc.id))
                    shutil.move(pdf_path, target_pdf_path)

                    pdf_task_str = consts.SPLIT_STR.join([business_type, str(pdf_doc.id), '0'])
                    pdf_task_str_list.append(pdf_task_str)
                except Exception as e:
                    self.online_log.warn('{0} [zip_2_pdfs] [recreate pdf task failed] [task={1}] [pdf_path={2}]'
                                         ' [error={3}]'.format(self.log_base, task_str, pdf_path,
                                                               traceback.format_exc()))
                else:
                    self.online_log.info('{0} [zip_2_pdfs] [recreate pdf task success] [task={1}] '
                                         '[pdf_task={2}]'.format(self.log_base, task_str, pdf_path,
                                                                 traceback.format_exc()))

            if len(pdf_task_str_list) > 0:
                for pdf_task_str in pdf_task_str_list:
                    try:
                        zip_task_queue.put(pdf_task_str)
                    except Exception as e:
                        self.online_log.warn('{0} [zip_2_pdfs] [put pdf task failed] [task={1}] [pdf_task={2}]'
                                             ' [error={3}]'.format(self.log_base, task_str, pdf_task_str,
                                                                   traceback.format_exc()))
            else:
                self.online_log.info('{0} [zip_2_pdfs] [zip task no pdf] [task={1}]'.format(self.log_base, task_str))

            # 6. 完成,修改doc状态:识别完成
            try:
                zip_doc.status = DocStatus.COMPLETE.value
                zip_doc.end_time = timezone.now()
                zip_doc.duration = min((zip_doc.end_time - zip_doc.start_time).seconds, 32760)
                zip_doc.save()
            except Exception as e:
                self.online_log.error('{0} [zip_2_pdfs] [process error (db save)] [task={1}] [error={2}]'.format(
                    self.log_base, task_str, traceback.format_exc()))

    def pdf_2_img_2_queue(self, img_queue, todo_count_dict, lock, error_list, res_dict, finish_queue, zip_task_queue):
        while self.switch:
            try:
                task_str = zip_task_queue.get(block=False)
                is_priority = False
            except Exception as e:
                task_str, is_priority = rh.dequeue()
                if task_str is None:
                    self.online_log.info('{0} [get_doc_info] [queue empty]'.format(self.log_base))
                    time.sleep(self.sleep_time_doc_get)
                    continue

            self.online_log.info('{0} [get_doc_info] [task={1}] [is_priority={2}]'.format(
                self.log_base, task_str, is_priority))

            try:
                # 1. 从队列获取文件信息
                doc, business_type, task_str, classify_1_str = self.get_doc_info(task_str, is_priority)
                # 队列为空时的处理
                if doc is None:
                    time.sleep(self.sleep_time_doc_get)
                    continue
            except Exception as e:
                self.online_log.error('{0} [process error (get doc info out)] [error={1}]'.format(
                    self.log_base, traceback.format_exc()))
                error_list.append(1)
                return
            else:
                doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, str(doc.id))
                os.makedirs(doc_data_path, exist_ok=True)
                img_save_path = os.path.join(doc_data_path, 'img')
                pdf_path = os.path.join(doc_data_path, '{0}.pdf'.format(doc.id))

                pwd_list = get_pwd_list_from_str(doc.document_name, doc.password)
                pdf_handler = PDFHandler(pdf_path, img_save_path, doc.document_name, pwd_list=pwd_list)

                if classify_1_str == '0':
                    try:
                        # 2. 从EDMS获取PDF文件
                        # max_count_obj = Configs.objects.filter(id=2).first()
                        # try:
                        #     max_img_count = int(max_count_obj.value)
                        # except Exception as e:
                        max_img_count = 500

                        for times in range(consts.RETRY_TIMES):
                            try:
                                if doc.application_id.startswith(consts.FIXED_APPLICATION_ID_PREFIX):
                                    self.online_log.info('{0} [mo ni xia dan] [task={1}] [times={2}] '
                                                         '[pdf_path={3}]'.format(self.log_base, task_str,
                                                                                 times, pdf_path))
                                elif os.path.exists(pdf_path):
                                    self.online_log.info('{0} [pdf from zip file] [task={1}] [times={2}] '
                                                         '[pdf_path={3}]'.format(self.log_base, task_str,
                                                                                 times, pdf_path))
                                else:
                                    # self.edms.download(pdf_path, doc.metadata_version_id)
                                    self.edms.download(pdf_path, doc.metadata_version_id, doc.document_scheme,
                                                       business_type)
                                    self.online_log.info('{0} [ecm download success] [task={1}] [times={2}] '
                                                         '[pdf_path={3}]'.format(self.log_base, task_str,
                                                                                 times, pdf_path))

                                # 3.PDF文件提取图片
                                self.online_log.info('{0} [pdf to img start] [task={1}] [times={2}]'.format(
                                    self.log_base, task_str, times))
                                start_time = time.time()
                                pdf_handler.extract_image(max_img_count)
                                end_time = time.time()
                                speed_time = int(end_time - start_time)
                                self.online_log.info('{0} [pdf to img end] [task={1}] [times={2}] [spend_time={3}]'.format(
                                    self.log_base, task_str, times, speed_time))
                            except Exception as e:
                                self.online_log.warn('{0} [download or pdf to img failed] [task={1}] [times={2}] '
                                                      '[error={3}]'.format(self.log_base, task_str, times,
                                                                           traceback.format_exc()))
                            else:
                                break
                        else:
                            raise Exception('download or pdf to img failed')

                        if pdf_handler.img_count == 0:
                            self.online_log.warn('{0} [pdf to img failed (pdf img empty)] [task={1}]'.format(
                                self.log_base, task_str))
                            raise Exception('pdf img empty')
                        elif pdf_handler.img_count >= max_img_count:
                            self.online_log.info('{0} [too many pdf image] [task={1}] [img_count={2}]'.format(
                                self.log_base, task_str, pdf_handler.img_count))

                            try:
                                report_table = HILOCRReport if business_type == consts.HIL_PREFIX else AFCOCRReport
                                report_table.objects.create(
                                    case_number=doc.application_id,
                                    request_team=RequestTeam.get_value(doc.document_scheme, 0),
                                    request_trigger=RequestTrigger.get_value(doc.data_source, 0),
                                    input_file=doc.document_name,
                                    transaction_start=doc.start_time,
                                    transaction_end=doc.start_time,
                                    successful_at_this_level=False,
                                    failure_reason=FailureReason.IMG_LIMIT.value,
                                    process_name=ProcessName.ALL.value,
                                    notes='pdf page count: {0}'.format(str(pdf_handler.img_count))
                                )
                            except Exception as e:
                                self.online_log.error('{0} [process error (report db save)] [error={1}]'.format(
                                    self.log_base, traceback.format_exc()))

                            try:
                                doc.status = DocStatus.PROCESS_FAILED.value
                                doc.page_count = pdf_handler.page_count
                                doc.save()
                            except Exception as e:
                                self.online_log.error('{0} [process error (db save)] [error={1}]'.format(
                                    self.log_base, traceback.format_exc()))
                        else:

                            try:
                                if pdf_handler.is_e_pdf:
                                    doc.metadata = pdf_handler.metadata if pdf_handler.metadata is None else \
                                        json.dumps(pdf_handler.metadata)
                                doc.page_count = pdf_handler.page_count
                                doc.save()
                            except Exception as e:
                                self.online_log.error('{0} [process error (db save)] [error={1}]'.format(
                                    self.log_base, traceback.format_exc()))

                            with lock:
                                todo_count_dict[task_str] = pdf_handler.img_count

                            self.online_log.info('{0} [pdf_2_img_2_queue] [{1}] [is_ebank={2}]'.format(
                                self.log_base, task_str, pdf_handler.is_ebank
                            ))
                            for img_idx, img_path in enumerate(pdf_handler.img_path_list):
                                while img_queue.full():
                                    self.online_log.info('{0} [pdf_2_img_2_queue] [img queue full]'.format(self.log_base))
                                    time.sleep(self.sleep_time_img_put)
                                if pdf_handler.is_ebank:
                                    try:
                                        text_list = pdf_handler.page_text_list[img_idx].pop('rebuild_text')
                                    except Exception as e:
                                        text_list = []
                                else:
                                    text_list = []
                                img_queue.put((business_type, img_path, text_list))
                    # except EDMSException as e:
                    #     try:
                    #         doc.status = DocStatus.PROCESS_FAILED.value
                    #         doc.save()
                    #         self.online_log.warn('{0} [process failed (edms download)] [task={1}] [error={2}]'.format(
                    #             self.log_base, task_str, traceback.format_exc()))
                    #     except Exception as e:
                    #         self.online_log.error('{0} [process error (db save 1)] [error={1}]'.format(
                    #             self.log_base, traceback.format_exc()))
                    #         error_list.append(1)
                    #         return
                    except Exception as e:
                        try:
                            end_time = timezone.now()
                            report_table = HILOCRReport if business_type == consts.HIL_PREFIX else AFCOCRReport
                            report_table.objects.create(
                                case_number=doc.application_id,
                                request_team=RequestTeam.get_value(doc.document_scheme, 0),
                                request_trigger=RequestTrigger.get_value(doc.data_source, 0),
                                input_file=doc.document_name,
                                transaction_start=doc.start_time,
                                transaction_end=end_time,
                                successful_at_this_level=False,
                                failure_reason=FailureReason.PDF.value,
                                process_name=ProcessName.ALL.value,
                            )
                        except Exception as e:
                            self.online_log.error('{0} [process error (report db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))

                        try:
                            doc.status = DocStatus.PROCESS_FAILED.value
                            doc.page_count = pdf_handler.page_count
                            doc.save()
                            self.online_log.warn('{0} [process failed (pdf_2_img_2_queue)] [task={1}] '
                                                  '[error={2}]'.format(self.log_base, task_str, traceback.format_exc()))
                        except Exception as e:
                            self.online_log.error('{0} [process error (db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))
                            error_list.append(1)
                            return
                else:  # e-contract
                    try:
                        # pdf下载 处理 图片存储 识别
                        for times in range(consts.RETRY_TIMES):
                            try:
                                self.edms.download(pdf_path, doc.metadata_version_id, doc.document_scheme, business_type)
                                self.online_log.info('{0} [edms download success] [task={1}] [times={2}] '
                                                     '[pdf_path={3}]'.format(self.e_log_base, task_str, times, pdf_path))

                                self.online_log.info('{0} [pdf to img start] [task={1}] [times={2}]'.format(
                                    self.e_log_base, task_str, times))
                                pdf_handler.e_contract_process()
                                self.online_log.info(
                                    '{0} [pdf to img end] [task={1}] [times={2}]'.format(self.e_log_base, task_str, times))
                            except Exception as e:
                                self.online_log.warn('{0} [download or pdf to img failed] [task={1}] [times={2}] '
                                                     '[error={3}]'.format(self.e_log_base, task_str, times,
                                                                          traceback.format_exc()))
                            else:
                                break
                        else:
                            raise Exception('download or pdf to img failed')

                        try:
                            doc.page_count = pdf_handler.page_count
                            doc.save()
                        except Exception as e:
                            self.online_log.error('{0} [process error (db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))

                        if classify_1_str == str(consts.CONTRACT_CLASSIFY):
                            ocr_result = afc_predict(pdf_handler.pdf_info)
                            page_res = {}
                            for page_num, page_info in ocr_result.get('page_info', {}).items():
                                if isinstance(page_num, str) and page_num.startswith('page_'):
                                    page_res[page_num] = {
                                        'classify': int(classify_1_str),
                                        "is_asp": ocr_result.get('is_asp', False),
                                        'page_num': page_num,
                                        'page_info': page_info
                                    }
                        else:
                            file_type_1 = consts.HIL_CONTRACT_TYPE_MAP.get(classify_1_str)
                            ocr_result_1 = hil_predict(pdf_handler.pdf_info, file_type_1)
                            rebuild_res_1 = {}
                            page_res = {}
                            for field_name, field_info in ocr_result_1.items():
                                page_num = field_info.pop('page', 'page_1')
                                rebuild_res_1.setdefault(page_num, dict())[field_name] = field_info
                            for page_num, page_info in rebuild_res_1.items():
                                if isinstance(page_num, str) and page_num.startswith('page_'):
                                    page_res[page_num] = {
                                        'classify': int(classify_1_str),
                                        'page_num': page_num,
                                        'page_info': page_info
                                    }

                        contract_res = {}
                        for img_path_tmp, page_key in pdf_handler.img_path_pno_list:
                            if page_key in page_res:
                                img_contract_res = {
                                    'code': 1,
                                    'data': [
                                        {
                                            'classify': page_res[page_key].pop('classify', consts.OTHER_CLASSIFY),
                                            'data': page_res[page_key]
                                        }
                                    ]
                                }
                            else:
                                img_contract_res = {
                                    'code': 1,
                                    'data': [
                                        {
                                            'classify': int(classify_1_str),
                                        }
                                    ]
                                }
                            contract_res[img_path_tmp] = img_contract_res

                        with lock:
                            res_dict[task_str] = contract_res
                        finish_queue.put(task_str)
                    except Exception as e:
                        try:
                            doc.status = DocStatus.PROCESS_FAILED.value
                            doc.page_count = pdf_handler.page_count
                            doc.save()
                            self.online_log.warn('{0} [process failed (e-contract)] [task={1}] '
                                                  '[error={2}]'.format(self.e_log_base, task_str, traceback.format_exc()))
                        except Exception as e:
                            self.online_log.error('{0} [process error (db save)] [error={1}]'.format(
                                self.e_log_base, traceback.format_exc()))
                            error_list.append(1)
                            return

    def img_2_ocr_1(self, img_queue, todo_count_dict, res_dict, finish_queue, lock, url, error_list):
        while len(error_list) == 0 or not img_queue.empty():
            try:
                channel, img_path, text_list = img_queue.get(block=False)
            except Exception as e:
                # self.online_log.info('{0} [img_2_ocr_1] [queue empty]'.format(self.log_base))
                time.sleep(self.sleep_time_img_get)
                continue
            else:
                try:
                    self.online_log.info('{0} [img_2_ocr_1] [get img] [img_path={1}]'.format(self.log_base, img_path))

                    for times in range(consts.RETRY_TIMES):
                        try:
                            with open(img_path, 'rb') as f:
                                base64_data = base64.b64encode(f.read())
                                # 获取解码后的base64值
                                file_data = base64_data.decode()
                            json_data_1 = {
                                "file": file_data,
                                "channel": channel,
                            }
                            if len(text_list) > 0:
                                json_data_1['text_list'] = text_list

                            start_time = time.time()
                            ocr_1_response = requests.post(url, json=json_data_1)
                            if ocr_1_response.status_code != 200:
                                raise OCR1Exception('ocr_1 status code: {0}'.format(ocr_1_response.status_code))
                        except Exception as e:
                            self.online_log.warn('{0} [ocr_1 failed] [times={1}] [url={2}] [img_path={3}] '
                                                  '[error={4}]'.format(self.log_base, times, url, img_path,
                                                                       traceback.format_exc()))
                        else:
                            ocr_1_res = ocr_1_response.json()
                            end_time = time.time()
                            speed_time = int(end_time - start_time)
                            self.online_log.info('{0} [ocr_1 success] [img={1}] [url={2}] [speed_time={3}]'.format(
                                self.log_base, img_path, url, speed_time))
                            break
                    else:
                        ocr_1_res = {}
                        self.online_log.warn('{0} [ocr_1 failed] [img_path={1}] [url={2}]'.format(
                            self.log_base, img_path, url))
                        # continue
                except Exception as e:
                    self.online_log.error('{0} [process error (ocr fetch)] [img_path={1}] [error={2}]'.format(
                        self.log_base, img_path, traceback.format_exc()))
                else:
                    try:
                        del json_data_1
                        # /data/bmw-ocr-data/AFC/tmp/6/img/page_0_img_0.jpeg
                        # AFC_2
                        path_split = img_path.split('/')
                        task_str = consts.SPLIT_STR.join((path_split[-5], path_split[-3]))

                        with lock:
                            doc_res_dict = res_dict.setdefault(task_str, {})
                            doc_res_dict[img_path] = ocr_1_res
                            res_dict[task_str] = doc_res_dict
                            todo_count = todo_count_dict.get(task_str)
                            if todo_count == 1:
                                finish_queue.put(task_str)
                                del todo_count_dict[task_str]
                            else:
                                todo_count_dict[task_str] = todo_count - 1
                    except Exception as e:
                        self.online_log.error('{0} [process error (store ocr res)] [img_path={1}] [error={2}]'.format(
                            self.log_base, img_path, traceback.format_exc()))

    def res_2_wb(self, res_dict, img_queue, finish_queue, error_list):
        while len(error_list) == 0 or not img_queue.empty() or not finish_queue.empty():
            try:
                task_str = finish_queue.get(block=False)
            except Exception as e:
                # self.online_log.info('{0} [res_2_wb] [queue empty]'.format(self.log_base))
                time.sleep(self.sleep_time_task_get)
                continue
            else:
                self.online_log.info('{0} [res_2_wb] [get task] [task={1}]'.format(self.log_base, task_str))
                ocr_1_res = res_dict.pop(task_str, {})

                business_type, doc_id_str = task_str.split(consts.SPLIT_STR)
                doc_id = int(doc_id_str)
                doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
                is_hil = True if business_type == consts.HIL_PREFIX else False
                dda_id_bc_mapping = dict()

                doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, doc_id_str)
                excel_path = os.path.join(doc_data_path, '{0}.xlsx'.format(doc_id_str))

                try:
                    doc = doc_class.objects.filter(id=doc_id).first()
                    # report_dict = {
                    #     'process': None or pdf or excel or edms
                    #     'idcard': True or False,
                    #     'bs': None or normal or mobile,
                    # }
                    report_list = [None, False, None, None, None, None]
                    do_dda = is_hil and doc.document_scheme == consts.DOC_SCHEME_LIST[1]
                except Exception as e:
                    self.online_log.error('{0} [process error (db filter)] [task={1}] [error={2}]'.format(
                        self.log_base, task_str, traceback.format_exc()))
                else:
                    try:
                        # 4.OCR结果并且构建excel文件
                        bs_classify_set = set()
                        bs_summary = {}
                        unknown_summary = {}
                        license_summary = {}
                        contract_result = {}
                        contract_result_compare = {}
                        res_list = []
                        interest_keyword = Keywords.objects.filter(
                            type=KeywordsType.INTEREST.value, on_off=True).values_list('keyword', flat=True)
                        salary_keyword = Keywords.objects.filter(
                            type=KeywordsType.SALARY.value, on_off=True).values_list('keyword', flat=True)
                        loan_keyword = Keywords.objects.filter(
                            type=KeywordsType.LOAN.value, on_off=True).values_list('keyword', flat=True)
                        wechat_keyword = Keywords.objects.filter(
                            type=KeywordsType.ALI_WECHART.value, on_off=True).values_list('keyword', flat=True)
                        repayments_keyword = Keywords.objects.filter(
                            type=KeywordsType.REPAYMENTS.value, on_off=True).values_list('keyword', flat=True)
                        wb = BSWorkbook(interest_keyword, salary_keyword, loan_keyword, wechat_keyword, repayments_keyword)
                        for img_path, res in ocr_1_res.items():
                            pno, ino = self.parse_img_path(img_path)
                            part_idx = 1
                            if res.get('code') == 1:
                                ocr_data_list = res.get('data', [])
                                if not isinstance(ocr_data_list, list):
                                    res_list.append((pno, ino, part_idx, consts.RES_FAILED_3))
                                    self.online_log.warn('{0} [ocr_1 res error] [img={1}]'.format(self.log_base, img_path))
                                else:
                                    for part_idx, ocr_data in enumerate(ocr_data_list):
                                        part_idx = part_idx + 1
                                        classify = ocr_data.get('classify')
                                        if classify is None:
                                            res_list.append((pno, ino, part_idx, consts.RES_FAILED_3))
                                            self.online_log.warn('{0} [ocr_1 res error] [img={1}]'.format(
                                                self.log_base, img_path))
                                            continue
                                        elif classify in consts.OTHER_CLASSIFY_SET:  # 其他类
                                            res_list.append((pno, ino, part_idx, consts.RES_SUCCESS_OTHER))
                                            continue
                                        elif classify in consts.LICENSE_CLASSIFY_SET_1:  # 证件1
                                            self.license1_process(ocr_data, license_summary, classify, res_list, pno,
                                                                  ino, part_idx, img_path, do_dda, dda_id_bc_mapping)
                                        elif classify in consts.LICENSE_CLASSIFY_SET_2:  # 证件2
                                            pid, _, _, _, _, _ = consts.LICENSE_CLASSIFY_MAPPING.get(classify)
                                            file_data = ocr_data.get('section_img')
                                            if file_data is None:
                                                with open(img_path, 'rb') as f:
                                                    base64_data = base64.b64encode(f.read())
                                                    # 获取解码后的base64值
                                                    file_data = base64_data.decode()
                                            json_data_2 = {
                                                "pid": str(pid),
                                                "filedata": file_data
                                            }
                                            ocr_url_2 = self.ocr_url_2_bc if classify == consts.BC_CLASSIFY else self.ocr_url_2

                                            for times in range(consts.RETRY_TIMES):
                                                try:
                                                    start_time = time.time()
                                                    ocr_2_response = requests.post(ocr_url_2, data=json_data_2)
                                                    if ocr_2_response.status_code != 200:
                                                        raise OCR2Exception('ocr_2 status code: {0}'.format(ocr_2_response.status_code))
                                                except Exception as e:
                                                    self.online_log.warn(
                                                        '{0} [ocr_2 failed] [times={1}] [img_path={2}] [error={3}]'.format(
                                                            self.log_base, times, img_path, traceback.format_exc()))
                                                else:
                                                    ocr_2_res = json.loads(ocr_2_response.text)
                                                    end_time = time.time()
                                                    speed_time = int(end_time - start_time)
                                                    self.online_log.info(
                                                        '{0} [ocr_2 success] [img={1}] [speed_time={2}]'.format(
                                                            self.log_base, img_path, speed_time))

                                                    if classify == consts.BC_CLASSIFY:
                                                        new_ocr_2_res = {}
                                                        position_dict = {}
                                                        get_info = False
                                                        bc_image_data = ''
                                                        position = {}
                                                        angle = 0
                                                        for page_info in ocr_2_res.get('PageInfo', []):
                                                            if get_info:
                                                                break
                                                            if page_info.get('ErrorCode', 1) in consts.SUCCESS_CODE_SET:
                                                                for bc_res_all in page_info.get('Result', []):
                                                                    if get_info:
                                                                        break
                                                                    if bc_res_all.get('ErrorCode', 1) in consts.SUCCESS_CODE_SET:
                                                                        for bc_res in bc_res_all.get('ResultList', []):
                                                                            if get_info:
                                                                                break
                                                                            get_info = True
                                                                            bc_image_data = bc_res.get('image_data', '')
                                                                            position = bc_res.get('position', {})
                                                                            angle = bc_res.get('angle', 0)
                                                                            for field_info in bc_res.get('FieldList', []):
                                                                                new_ocr_2_res[field_info.get('key', '')] = field_info.get('value', '')
                                                                                position_dict[field_info.get('key', '')] = {
                                                                                    consts.FIELD_POSITION_KEY: field_info.get('position', {}),
                                                                                    consts.FIELD_QUAD_KEY: field_info.get('quad', []),
                                                                                }

                                                        if get_info:
                                                            pre, suf = os.path.splitext(img_path)
                                                            if len(bc_image_data) > 0:
                                                                position = {}
                                                                angle = 0
                                                                section_img_path = '{0}_{1}_0{2}'.format(pre, part_idx, suf)
                                                                try:
                                                                    with open(section_img_path, "wb") as fh:
                                                                        fh.write(base64.b64decode(bc_image_data.encode()))
                                                                except Exception as e:
                                                                    self.online_log.warn(
                                                                        '{0} [bc section img save failed] [img_path={1}]'
                                                                        ' [part_idx={2}]]'.format(self.log_base, img_path, part_idx))
                                                            else:
                                                                section_img_path = img_path if ocr_data.get('section_img') is None else '{0}_{1}{2}'.format(pre, part_idx, suf)
                                                                if ocr_data.get('section_img') is not None:
                                                                    try:
                                                                        with open(section_img_path, "wb") as fh:
                                                                            fh.write(base64.b64decode(ocr_data.get('section_img').encode()))
                                                                    except Exception as e:
                                                                        self.online_log.warn(
                                                                            '{0} [bc section img save failed] [img_path={1}]'
                                                                            ' [part_idx={2}]'.format(self.log_base,
                                                                                                     img_path, part_idx))

                                                            new_ocr_2_res['ErrorCode'] = 0
                                                            position_dict[consts.POSITION_KEY] = position
                                                            position_dict[consts.ANGLE_KEY] = angle
                                                            new_ocr_2_res[consts.SECTION_IMG_PATH_KEY] = section_img_path
                                                            new_ocr_2_res[consts.ALL_POSITION_KEY] = position_dict

                                                            name = '有'
                                                            json_data_3 = {
                                                                "file": file_data,
                                                                'card_res': new_ocr_2_res
                                                            }
                                                            card_name_response = requests.post(self.ocr_url_3, json_data_3)
                                                            if card_name_response.status_code == 200:
                                                                card_name_res = card_name_response.json()
                                                                if isinstance(card_name_res, dict) and \
                                                                        card_name_res.get('data', {}).get('is_exists_name') == 0:
                                                                    name = '无'
                                                            new_ocr_2_res['Name'] = name
                                                    else:
                                                        new_ocr_2_res = ocr_2_res

                                                    self.license2_process(new_ocr_2_res, license_summary, pid, classify,
                                                                          res_list, pno, ino, part_idx, img_path,
                                                                          do_dda, dda_id_bc_mapping, file_data=ocr_data.get('section_img'))
                                                    break
                                            else:
                                                res_list.append((pno, ino, part_idx, consts.RES_FAILED_2))
                                                self.online_log.warn(
                                                    '{0} [ocr_2 failed] [img_path={1}]'.format(self.log_base, img_path))
                                        elif classify in consts.CONTRACT_SET:
                                            self.contract_process(classify, ocr_data, contract_result, res_list, pno,
                                                                  ino, part_idx, img_path, contract_result_compare)
                                        else:  # 流水处理
                                            bs_classify_set.add(classify)
                                            self.bs_process(wb, ocr_data, bs_summary, unknown_summary, classify, res_list, pno, ino, part_idx)
                            else:
                                res_list.append((pno, ino, part_idx, consts.RES_FAILED_1))
                                self.online_log.info('{0} [ocr_1 res error] [img={1}]'.format(self.log_base, img_path))

                        self.online_log.info('{0} [task={1}] [bs_summary={2}] [unknown_summary={3}]'.format(
                            self.log_base, task_str, bs_summary, unknown_summary))

                        # self.license_log.info('[task={0}] [license_summary={1}]'.format(task_str, license_summary))
                        idcard_list = license_summary.get(consts.IC_CLASSIFY)
                        if idcard_list:
                            report_list[1] = True
                            self.idcard_log.info('[task={0}] [idcard={1}]'.format(task_str, idcard_list))

                        if len(bs_classify_set) > 0:
                            if consts.ALI_WECHART_CLASSIFY & bs_classify_set:
                                report_list[2] = WorkflowName.MOBILE.value
                            else:
                                report_list[2] = WorkflowName.NORMAL.value

                            bs_name_list = []
                            for tmp_classify in bs_classify_set:
                                try:
                                    bs_name = consts.CLASSIFY_LIST[tmp_classify][0]
                                except Exception as e:
                                    bs_name = 'Unknown'
                                bs_name_list.append(bs_name)
                            report_list[4] = '、'.join(bs_name_list)

                        merged_bs_summary = self.rebuild_bs_summary(bs_summary, unknown_summary)
                        del unknown_summary

                        ic_merge, rp_merge = self.license_rebuild(license_summary)

                        # self.bs_log.info('[task={0}] [bs_summary={1}]'.format(task_str, merged_bs_summary))

                        self.online_log.info('{0} [task={1}] [merged_bs_summary={2}] [license_summary={3}] [contract={4}] '
                                             '[res_list={5}]'.format(self.log_base, task_str, merged_bs_summary,
                                                                     license_summary, contract_result, res_list))

                    except Exception as e:

                        report_list[0] = FailureReason.EXCEL.value
                        self.online_log.warn('{0} [process failed (res conformity)] [task={1}] [error={2}]'.format(
                            self.log_base, task_str, traceback.format_exc()))

                        try:
                            doc.status = DocStatus.PROCESS_FAILED.value
                            doc.save()
                        except Exception as e:
                            self.online_log.error('{0} [process error (db save)] [task={1}] [error={2}]'.format(
                                self.log_base, task_str, traceback.format_exc()))

                    else:

                        try:
                            # 重构Excel文件
                            # src_excel_path = os.path.join(doc_data_path, 'src.xlsx')
                            # wb.save(src_excel_path)
                            count_list = wb.rebuild(merged_bs_summary, license_summary, res_list, doc.document_scheme, contract_result, doc.metadata)
                            wb.save(excel_path)

                        except Exception as e:

                            report_list[0] = FailureReason.EXCEL.value
                            self.online_log.warn('{0} [process failed (wb rebuild)] [task={1}] [error={2}]'.format(
                                self.log_base, task_str, traceback.format_exc()))

                            try:
                                doc.status = DocStatus.PROCESS_FAILED.value
                                doc.save()
                            except Exception as e:
                                self.online_log.error('{0} [process error (db save)] [task={1}] [error={2}]'.format(
                                    self.log_base, task_str, traceback.format_exc()))

                        else:
                            try:
                                # 上传至EDMS
                                for times in range(consts.RETRY_TIMES):
                                    try:
                                        self.edms.upload(excel_path, doc, business_type)
                                    except Exception as e:
                                        self.online_log.warn(
                                            '{0} [edms upload failed] [times={1}] [task={2}] [error={3}]'.format(
                                                self.log_base, times, task_str, traceback.format_exc()))
                                        edms_exc = str(e)
                                    else:
                                        break
                                else:
                                    raise EDMSException(edms_exc)
                            except Exception as e:
                                report_list[0] = FailureReason.EDMS.value
                                doc.status = DocStatus.UPLOAD_FAILED.value
                                self.online_log.warn('{0} [process failed (edms upload)] [task={1}] [error={2}]'.format(
                                    self.log_base, task_str, traceback.format_exc()))

                            else:

                                doc.status = DocStatus.COMPLETE.value
                                self.online_log.info('{0} [edms upload success] [task={1}]'.format(self.log_base, task_str))

                            finally:
                                try:
                                    doc.end_time = timezone.now()
                                    doc.duration = min((doc.end_time - doc.start_time).seconds, 32760)
                                    for field, count in count_list:
                                        if hasattr(doc, field):
                                            setattr(doc, field, count)
                                    doc.save()
                                except Exception as e:
                                    self.online_log.error('{0} [process error (db save)] [task={1}] [error={2}]'.format(
                                        self.log_base, task_str, traceback.format_exc()))
                                else:
                                    self.online_log.info('{0} [process complete] [task={1}]'.format(self.log_base, task_str))
                                    os.remove(excel_path)

                                # report新增流水真伪
                                try:
                                    check_false_classify = set()
                                    all_check_classify = set()
                                    if isinstance(doc.metadata, str):
                                        verify_field = 'verify_res_ebank'
                                    else:
                                        verify_field = 'verify_res_paper_bank'
                                    for bs_info in merged_bs_summary.values():
                                        all_check_classify.add(bs_info['classify'])
                                        if verify_field not in bs_info:
                                            report_list[5] = BSCheckResult.CHECK_FAILED.value
                                            break
                                        if not bs_info[verify_field]:
                                            check_false_classify.add(bs_info['classify'])
                                    else:
                                        # 电子
                                        if isinstance(doc.metadata, str):
                                            if len(check_false_classify) > 0:
                                                report_list[5] = BSCheckResult.CHECK_FALSE.value
                                            else:
                                                report_list[5] = BSCheckResult.CHECK_TRUE.value
                                        # 纸质
                                        else:
                                            if check_false_classify & consts.BS_VERIFY_CLASSIFY:
                                                report_list[5] = BSCheckResult.CHECK_FALSE.value
                                            elif all_check_classify & consts.BS_VERIFY_CLASSIFY:
                                                report_list[5] = BSCheckResult.CHECK_TRUE.value
                                            else:
                                                report_list[5] = BSCheckResult.NO_CHECK.value
                                except Exception as e:
                                    report_list[5] = BSCheckResult.CHECK_FAILED.value

                        finally:
                            self.rebuild_contract(license_summary, contract_result_compare)

                            bs_rebuild = self.rebuild_bs(merged_bs_summary)
                            if len(bs_rebuild) > 0:
                                license_summary[consts.BS_CLASSIFY] = bs_rebuild

                            # 比对
                            if len(license_summary) > 0 and doc.document_scheme != consts.DOC_SCHEME_LIST[2]:
                                try:
                                    is_ca = True if doc.document_scheme == consts.DOC_SCHEME_LIST[0] else False
                                    # 更新OCR累计识别结果表
                                    if business_type == consts.HIL_PREFIX:
                                        result_class = HILOCRResult if is_ca else HILSEOCRResult
                                    else:
                                        result_class = AFCOCRResult if is_ca else AFCSEOCRResult
                                    res_obj = result_class.objects.filter(application_id=doc.application_id).first()
                                    if res_obj is None:
                                        res_obj = result_class()
                                        res_obj.application_id = doc.application_id
                                    for classify, field in consts.RESULT_MAPPING.items():
                                        if not hasattr(res_obj, field):
                                            continue
                                        license_list = license_summary.get(classify)
                                        if not license_list:
                                            continue
                                        if classify == consts.IC_CLASSIFY and ic_merge:
                                            license_list[0].update(license_list[1])
                                            license_list.pop(1)
                                        elif classify == consts.RP_CLASSIFY and rp_merge:
                                            license_list[0].update(license_list[1])
                                            license_list.pop(1)
                                        old_res_str = getattr(res_obj, field)
                                        if old_res_str is None:
                                            last_res_str = json.dumps(license_list)
                                        else:
                                            old_res_list = json.loads(old_res_str)
                                            old_res_list.extend(license_list)
                                            last_res_str = json.dumps(old_res_list)
                                        setattr(res_obj, field, last_res_str)
                                    res_obj.save()
                                except Exception as e:
                                    self.online_log.error(
                                        '{0} [process error (ocr result save)] [task={1}] [error={2}]'.format(
                                            self.log_base, task_str, traceback.format_exc()))
                                else:
                                    self.online_log.info('{0} [ocr result save success] [task={1}] [res_id={2}]'.format(
                                        self.log_base, task_str, res_obj.id))
                                    # 触发比对
                                    try:
                                        # pass
                                        compare.apply_async((doc.application_id, business_type, None, res_obj.id,
                                                             is_ca, True), queue='queue_compare')
                                    except Exception as e:
                                        self.online_log.error(
                                            '{0} [process error (comparison info send)] [task={1}] [error={2}]'.format(
                                                self.log_base, task_str, traceback.format_exc()))
                                    else:
                                        self.online_log.info('{0} [comparison info send success] [task={1}] '
                                                             '[res_id={2}]'.format(self.log_base, task_str, res_obj.id))

                            # DDA处理
                            if do_dda:
                                # 入库
                                try:
                                    dda_record = DDARecords.objects.filter(
                                        application_id=doc.application_id).first()
                                    if dda_record is None:
                                        dda_record = DDARecords(application_id=doc.application_id)
                                except Exception as e:
                                    report_list[3] = False
                                    self.online_log.error('{0} [process error (dda db get)] [task={1}] '
                                                          '[error={2}]'.format(self.log_base, task_str, traceback.format_exc()))
                                else:
                                    try:
                                        if not dda_record.all_found:
                                            found_time = timezone.now()
                                            move_img_path_dict = dict()
                                            ic_res_list = dda_id_bc_mapping.get(consts.IC_FIELD, [])
                                            bc_res_list = dda_id_bc_mapping.get(consts.BC_FIELD, [])
                                            self.online_log.info('{0} [dda process] [task={1}] [ic={2}] '
                                                                 '[bc={3}]'.format(self.log_base, task_str,ic_res_list,
                                                                                   bc_res_list))

                                            if not dda_record.is_dda_found:
                                                try:
                                                    # DDA过滤,获取有效DDA
                                                    best_dda_res = None
                                                    dda_res_list = license_summary.get(consts.DDA_CLASSIFY, [])
                                                    if len(dda_res_list) > 0:
                                                        dda_res_list.sort(key=lambda x: x.get(consts.DDA_PRO, 0),
                                                                          reverse=True)
                                                        best_dda_res = dda_res_list[0]
                                                        # tmp_best_dda_res = dda_res_list[0]
                                                        # if tmp_best_dda_res.get(consts.DDA_PRO, 0) >= consts.DDA_PRO_MIN:
                                                        #     best_dda_res = tmp_best_dda_res
                                                    self.online_log.info(
                                                        '{0} [dda process] [task={1}] [dda={2}]'.format(
                                                            self.log_base, task_str, dda_res_list))
                                                except Exception as e:
                                                    best_dda_res = None

                                                dda_record.is_dda_found = False if best_dda_res is None else True

                                                if dda_record.is_dda_found:
                                                    dda_path = best_dda_res.get(consts.DDA_IMG_PATH, '')
                                                    customer_name = best_dda_res.get(consts.DDA_IC_NAME, '')
                                                    customer_id = best_dda_res.get(consts.DDA_IC_ID, '')
                                                    account_id = best_dda_res.get(consts.DDA_BC_ID, '')
                                                    dda_record.dda_path = dda_path
                                                    dda_record.dda_found_time = found_time
                                                    dda_record.customer_name = customer_name
                                                    dda_record.customer_id = customer_id
                                                    dda_record.account_id = account_id
                                                    # move
                                                    move_img_path_dict.setdefault(
                                                        consts.DDA_FIELD, set()).add(dda_path)

                                            if dda_record.is_dda_found:

                                                try:
                                                    if not dda_record.is_id_found:
                                                        for ic_name, ic_id, ic_img_path in ic_res_list:
                                                            if ic_id == dda_record.customer_id \
                                                                    or ic_name == dda_record.customer_name:
                                                                dda_record.is_id_found = True
                                                                dda_record.id_path = ic_img_path
                                                                dda_record.id_found_time = found_time
                                                                move_img_path_dict.setdefault(
                                                                    consts.IC_FIELD, set()).add(ic_img_path)
                                                                break
                                                        else:
                                                            id_record = IDBCRecords.objects.filter(
                                                                application_id=doc.application_id,
                                                                target_id=dda_record.customer_id,
                                                                is_id=True).first()

                                                            if id_record is None:
                                                                id_record = IDBCRecords.objects.filter(
                                                                    application_id=doc.application_id,
                                                                    target_name=dda_record.customer_name,
                                                                    is_id=True).first()

                                                            if id_record is not None:
                                                                dda_record.is_id_found = True
                                                                dda_record.id_path = id_record.file_path
                                                                dda_record.id_found_time = id_record.create_time
                                                                move_img_path_dict.setdefault(
                                                                    consts.IC_FIELD, set()).add(id_record.file_path)
                                                except Exception as e:
                                                    report_list[3] = False
                                                    self.online_log.error(
                                                        '{0} [process error (dda id process)] [task={1}] '
                                                        '[error={2}]'.format(self.log_base, task_str,
                                                                             traceback.format_exc()))

                                                try:
                                                    if not dda_record.is_bc_found:
                                                        for bc_no, bc_img_path in bc_res_list:
                                                            if bc_no == dda_record.account_id:
                                                                dda_record.is_bc_found = True
                                                                dda_record.bc_path = bc_img_path
                                                                dda_record.bc_found_time = found_time
                                                                move_img_path_dict.setdefault(
                                                                    consts.BC_FIELD, set()).add(bc_img_path)
                                                                break
                                                        else:
                                                            bc_record = IDBCRecords.objects.filter(
                                                                application_id=doc.application_id,
                                                                target_id=dda_record.account_id,
                                                                is_id=False).first()

                                                            if bc_record is not None:
                                                                dda_record.is_bc_found = True
                                                                dda_record.bc_path = bc_record.file_path
                                                                dda_record.bc_found_time = bc_record.create_time
                                                                move_img_path_dict.setdefault(
                                                                    consts.BC_FIELD, set()).add(bc_record.file_path)
                                                except Exception as e:
                                                    report_list[3] = False
                                                    self.online_log.error(
                                                        '{0} [process error (dda bc process)] [task={1}] '
                                                        '[error={2}]'.format(self.log_base, task_str,
                                                                             traceback.format_exc()))

                                            if dda_record.is_dda_found and dda_record.is_id_found and dda_record.is_bc_found:
                                                dda_record.all_found = True
                                            dda_record.save()

                                            # 图片移动
                                            try:
                                                if len(move_img_path_dict) > 0:
                                                    self.online_log.info(
                                                        '{0} [dda process] [task={1}] [move_img_path={2}]'.format(
                                                            self.log_base, task_str, move_img_path_dict))

                                                    wanting_dir = os.path.join(self.dda_wanting_dir, doc.application_id)
                                                    wanting_dir_exists = os.path.isdir(wanting_dir)
                                                    if dda_record.all_found:
                                                        target_dir = os.path.join(self.dda_complete_dir, doc.application_id)
                                                        if wanting_dir_exists:
                                                            shutil.move(wanting_dir, target_dir)
                                                        else:
                                                            os.makedirs(target_dir, exist_ok=True)
                                                    else:
                                                        target_dir = wanting_dir
                                                        if not wanting_dir_exists:
                                                            os.makedirs(target_dir, exist_ok=True)

                                                    for prefix, path_set in move_img_path_dict.items():
                                                        for idx, path in enumerate(path_set):
                                                            if os.path.isfile(path):
                                                                file_name = '{0}_{1}{2}'.format(
                                                                    prefix, idx, os.path.splitext(path)[-1])
                                                                target_path = os.path.join(target_dir, file_name)
                                                                shutil.copyfile(path, target_path)
                                                            else:
                                                                self.online_log.warn(
                                                                    '{0} [dda process] [img path empty] [task={1}] '
                                                                    '[path={2}]'.format(self.log_base, task_str, path))
                                            except Exception as e:
                                                report_list[3] = False
                                                self.online_log.error(
                                                    '{0} [process error (dda img move)] [task={1}] '
                                                    '[error={2}]'.format(self.log_base, task_str, traceback.format_exc()))

                                            # id & bc 入库
                                            try:
                                                if not dda_record.is_dda_found and not dda_record.is_id_found:
                                                    ic_set = set()
                                                    for ic_name, ic_id, ic_img_path in ic_res_list:
                                                        query_str = '{0}{1}'.format(ic_name, ic_id)
                                                        if query_str in ic_set:
                                                            continue
                                                        ic_set.add(query_str)
                                                        IDBCRecords.objects.create(
                                                            application_id=doc.application_id,
                                                            target_name=ic_name,
                                                            target_id=ic_id,
                                                            is_id=True,
                                                            file_path=ic_img_path)
                                                if not dda_record.is_dda_found and not dda_record.is_bc_found:
                                                    bc_set = set()
                                                    for bc_no, bc_img_path in bc_res_list:
                                                        if bc_no in bc_set:
                                                            continue
                                                        bc_set.add(bc_no)
                                                        IDBCRecords.objects.create(
                                                            application_id=doc.application_id,
                                                            target_id=bc_no,
                                                            is_id=False,
                                                            file_path=bc_img_path)
                                            except Exception as e:
                                                report_list[3] = False
                                                self.online_log.error(
                                                    '{0} [process error (dda id&bc db save)] [task={1}] '
                                                    '[error={2}]'.format(self.log_base, task_str, traceback.format_exc()))

                                    except Exception as e:
                                        report_list[3] = False
                                        self.online_log.error(
                                            '{0} [process error (dda process)] [task={1}] '
                                            '[error={2}]'.format(self.log_base, task_str, traceback.format_exc()))
                                    else:
                                        if report_list[3] is None:
                                            report_list[3] = True

                    finally:
                        # report_dict = {
                        #     'process': None or pdf or excel or edms
                        #     'idcard': True or False,
                        #     'bs': None or normal or mobile,
                        # }

                        end_time = timezone.now()
                        report_table = HILOCRReport if business_type == consts.HIL_PREFIX else AFCOCRReport

                        try:
                            if report_list[0] is None:
                                report_table.objects.create(
                                    case_number=doc.application_id,
                                    request_team=RequestTeam.get_value(doc.document_scheme, 0),
                                    request_trigger=RequestTrigger.get_value(doc.data_source, 0),
                                    input_file=doc.document_name,
                                    transaction_start=doc.start_time,
                                    transaction_end=end_time,
                                    process_name=ProcessName.ALL.value,
                                )
                            else:
                                report_table.objects.create(
                                    case_number=doc.application_id,
                                    request_team=RequestTeam.get_value(doc.document_scheme, 0),
                                    request_trigger=RequestTrigger.get_value(doc.data_source, 0),
                                    input_file=doc.document_name,
                                    transaction_start=doc.start_time,
                                    transaction_end=end_time,
                                    successful_at_this_level=False,
                                    failure_reason=report_list[0],
                                    process_name=ProcessName.ALL.value,
                                )
                        except Exception as e:
                            self.online_log.error('{0} [process error (report db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))

                        try:
                            if report_list[1]:
                                report_table.objects.create(
                                    case_number=doc.application_id,
                                    request_team=RequestTeam.CONTROLLING.value,
                                    request_trigger=RequestTrigger.DOCUPLOAD.value,
                                    input_file=doc.document_name,
                                    transaction_start=doc.start_time,
                                    transaction_end=end_time,
                                    process_name=ProcessName.IDCARD.value,
                                )
                        except Exception as e:
                            self.online_log.error('{0} [process error (report db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))

                        try:
                            if report_list[2] is not None:
                                is_ebank = True if isinstance(doc.metadata, str) else False
                                bank_name = report_list[4] if isinstance(report_list[4], str) else 'Unknown'
                                bs_check_result = report_list[5] if isinstance(report_list[5], int) else BSCheckResult.CHECK_FAILED.value
                                report_table.objects.create(
                                    case_number=doc.application_id,
                                    request_team=RequestTeam.get_value(doc.document_scheme, 0),
                                    request_trigger=RequestTrigger.DOCUPLOAD.value,
                                    input_file=doc.document_name,
                                    transaction_start=doc.start_time,
                                    transaction_end=end_time,
                                    process_name=ProcessName.BS.value,
                                    workflow_name=report_list[2],
                                    bank_name=bank_name,
                                    is_ebank=is_ebank,
                                    bs_check_result=bs_check_result,
                                )
                        except Exception as e:
                            self.online_log.error('{0} [process error (report db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))

                        try:
                            if report_list[3] is not None:
                                report_table.objects.create(
                                    case_number=doc.application_id,
                                    request_team=RequestTeam.get_value(doc.document_scheme, 0),
                                    request_trigger=RequestTrigger.DOCUPLOAD.value,
                                    input_file=doc.document_name,
                                    transaction_start=doc.start_time,
                                    transaction_end=end_time,
                                    successful_at_this_level=report_list[3],
                                    process_name=ProcessName.DDA.value,
                                )
                        except Exception as e:
                            self.online_log.error('{0} [process error (report db save)] [error={1}]'.format(
                                self.log_base, traceback.format_exc()))

                finally:
                    try:
                        # img_save_path = os.path.join(doc_data_path, 'img')
                        # write_zip_file(img_save_path, os.path.join(doc_data_path, '{0}_img.zip'.format(doc_id_str)))
                        # shutil.rmtree(img_save_path, ignore_errors=True)
                        pdf_path = os.path.join(doc_data_path, '{0}.pdf'.format(doc_id_str))
                        os.remove(pdf_path)
                        self.online_log.info('{0} [pdf & img removed] [task={1}]'.format(self.log_base, task_str))
                    except Exception as e:
                        self.online_log.error('{0} [process error (pdf & img remove)] [task={1}] [error={2}]'.format(
                            self.log_base, task_str, traceback.format_exc()))

    def handle(self, *args, **kwargs):
        db.close_old_connections()
        lock = Lock()
        with Manager() as manager:
            error_list = manager.list()
            todo_count_dict = manager.dict()
            res_dict = manager.dict()
            img_queue = Queue(self.img_queue_size)
            finish_queue = Queue()
            zip_task_queue = Queue()

            process_list = []
            zip_process = Process(target=self.zip_2_pdfs,
                                  args=(zip_task_queue, error_list))
            process_list.append(zip_process)

            pdf_process = Process(target=self.pdf_2_img_2_queue,
                                  args=(img_queue, todo_count_dict, lock, error_list, res_dict,
                                        finish_queue, zip_task_queue))
            process_list.append(pdf_process)

            for url in self.ocr_1_urls.values():
                ocr_1_process = Process(target=self.img_2_ocr_1, args=(
                    img_queue, todo_count_dict, res_dict, finish_queue, lock, url, error_list))
                process_list.append(ocr_1_process)

            wb_process = Process(target=self.res_2_wb, args=(res_dict, img_queue, finish_queue, error_list))
            process_list.append(wb_process)

            for p in process_list:
                p.start()
            for p in process_list:
                p.join()

            self.online_log.info('{0} [stop safely]'.format(self.log_base))