diff --git a/photo_review/__init__.py b/photo_review/__init__.py index 7b6e76f..96b2fdf 100644 --- a/photo_review/__init__.py +++ b/photo_review/__init__.py @@ -46,10 +46,13 @@ ADMISSION_ID = ["住院号"] SETTLEMENT_ID = ["医保结算单号码"] # 年龄 AGE = ["年龄"] +# 大写总额 +UPPERCASE_MEDICAL_EXPENSES = ["大写总额"] SETTLEMENT_LIST_SCHEMA = \ - PATIENT_NAME + ADMISSION_DATE + DISCHARGE_DATE + MEDICAL_EXPENSES + PERSONAL_CASH_PAYMENT \ - + PERSONAL_ACCOUNT_PAYMENT + PERSONAL_FUNDED_AMOUNT + MEDICAL_INSURANCE_TYPE + ADMISSION_ID + SETTLEMENT_ID + (PATIENT_NAME + ADMISSION_DATE + DISCHARGE_DATE + MEDICAL_EXPENSES + PERSONAL_CASH_PAYMENT + + PERSONAL_ACCOUNT_PAYMENT + PERSONAL_FUNDED_AMOUNT + MEDICAL_INSURANCE_TYPE + ADMISSION_ID + SETTLEMENT_ID + + UPPERCASE_MEDICAL_EXPENSES) DISCHARGE_RECORD_SCHEMA = \ HOSPITAL + DEPARTMENT + PATIENT_NAME + ADMISSION_DATE + DISCHARGE_DATE + DOCTOR + ADMISSION_ID + AGE diff --git a/photo_review/photo_review.py b/photo_review/photo_review.py index 724d3b5..a64c8a3 100644 --- a/photo_review/photo_review.py +++ b/photo_review/photo_review.py @@ -13,11 +13,12 @@ from db import MysqlSession from db.mysql import BdYljg, BdYlks, ZxOcr, ZxIeCost, ZxIeDischarge, ZxIeSettlement, ZxPhhd, ZxPhrec from photo_review import PATIENT_NAME, ADMISSION_DATE, DISCHARGE_DATE, MEDICAL_EXPENSES, PERSONAL_CASH_PAYMENT, \ PERSONAL_ACCOUNT_PAYMENT, PERSONAL_FUNDED_AMOUNT, MEDICAL_INSURANCE_TYPE, HOSPITAL, DEPARTMENT, DOCTOR, \ - ADMISSION_ID, SETTLEMENT_ID, AGE, OCR, SETTLEMENT_IE, DISCHARGE_IE, COST_IE, PHHD_BATCH_SIZE, SLEEP_MINUTES + ADMISSION_ID, SETTLEMENT_ID, AGE, OCR, SETTLEMENT_IE, DISCHARGE_IE, COST_IE, PHHD_BATCH_SIZE, SLEEP_MINUTES, \ + UPPERCASE_MEDICAL_EXPENSES from ucloud import ucloud from util import image_util, util from util.data_util import handle_date, handle_decimal, parse_department, handle_name, \ - handle_insurance_type, handle_original_data, handle_hospital, handle_department, handle_id, handle_age + handle_insurance_type, handle_original_data, handle_hospital, handle_department, handle_id, handle_age, parse_money from util.util import get_default_datetime @@ -163,8 +164,6 @@ def settlement_task(pk_phhd, settlement_list): "name": handle_name(get_best_value_in_keys(settlement_list_ie_result, PATIENT_NAME)), "admission_date_str": handle_original_data(get_best_value_in_keys(settlement_list_ie_result, ADMISSION_DATE)), "discharge_date_str": handle_original_data(get_best_value_in_keys(settlement_list_ie_result, DISCHARGE_DATE)), - "medical_expenses_str": handle_original_data( - get_best_value_in_keys(settlement_list_ie_result, MEDICAL_EXPENSES)), "personal_cash_payment_str": handle_original_data( get_best_value_in_keys(settlement_list_ie_result, PERSONAL_CASH_PAYMENT)), "personal_account_payment_str": handle_original_data( @@ -179,11 +178,15 @@ def settlement_task(pk_phhd, settlement_list): settlement_data["admission_date"] = handle_date(settlement_data["admission_date_str"]) settlement_data["admission_date"] = handle_date(settlement_data["admission_date_str"]) settlement_data["discharge_date"] = handle_date(settlement_data["discharge_date_str"]) - settlement_data["medical_expenses"] = handle_decimal(settlement_data["medical_expenses_str"]) settlement_data["personal_cash_payment"] = handle_decimal(settlement_data["personal_cash_payment_str"]) settlement_data["personal_account_payment"] = handle_decimal(settlement_data["personal_account_payment_str"]) settlement_data["personal_funded_amount"] = handle_decimal(settlement_data["personal_funded_amount_str"]) settlement_data["medical_insurance_type"] = handle_insurance_type(settlement_data["medical_insurance_type_str"]) + + parse_money_result = parse_money(get_best_value_in_keys(settlement_list_ie_result, UPPERCASE_MEDICAL_EXPENSES), + get_best_value_in_keys(settlement_list_ie_result, MEDICAL_EXPENSES)) + settlement_data["medical_expenses_str"] = handle_original_data(parse_money_result[0]) + settlement_data["medical_expenses"] = parse_money_result[1] save_or_update_ie(ZxIeSettlement, pk_phhd, settlement_data) diff --git a/util/data_util.py b/util/data_util.py index 4d6b8da..4bdcaa2 100644 --- a/util/data_util.py +++ b/util/data_util.py @@ -1,6 +1,9 @@ +import logging import re from datetime import datetime +from util import util + # 处理金额类数据 def handle_decimal(string): @@ -8,21 +11,28 @@ def handle_decimal(string): if not string: return "" if "." not in string: - front = string - back = "" + if len(string) > 2: + result = string[:-2] + "." + string[-2:] + else: + result = string else: front, back = string.rsplit('.', 1) front = front.replace(".", "") + if back: + back = "." + back[:2] + result = front + back + return result[:16] - front = front[-16:] - if back: - back = "." + back - result = float(front + back) - # 金额较大的暂且交给人工确认 - if result > 100000: - return "" - else: - return front + back + +def parse_money(capital_num, num): + if capital_num: + try: + money = util.chinese_money_to_number(capital_num) + return capital_num, money + except Exception as e: + logging.warning("大写金额解析失败", exc_info=e) + + return num, handle_decimal(num) # 处理日期类数据 @@ -161,7 +171,3 @@ def handle_age(string): string = string.split("岁")[0] num = re.sub(r'\D', '', string) return num[-3:] - - -if __name__ == '__main__': - print(handle_decimal(" ")) \ No newline at end of file diff --git a/util/string_util.py b/util/string_util.py new file mode 100644 index 0000000..985f40d --- /dev/null +++ b/util/string_util.py @@ -0,0 +1,7 @@ +def blank(string): + """ + 判断字符串是否为空或者纯空格 + :param string: 字符串 + :return: 字符串是否为空或者纯空格 + """ + return not string or string.isspace() diff --git a/util/util.py b/util/util.py index 540994e..6921a94 100644 --- a/util/util.py +++ b/util/util.py @@ -2,6 +2,8 @@ import logging import os from datetime import datetime +from util import string_util + # 获取yyyy-MM-dd HH:mm:ss格式的当前时间 def get_default_datetime(): @@ -76,3 +78,130 @@ def zoom_rectangle(rectangle, ratio): x2 = round(x2 + x2 * ratio) y2 = round(y2 + y2 * ratio) return [x1, y1, x2, y2] + + +def chinese_to_money_unit(chinese): + if chinese in ["拾", "十"]: + return 10, False + elif chinese in ["佰", "百"]: + return 100, False + elif chinese in ["仟", "千"]: + return 1000, False + elif chinese == "万": + return 10000, True + elif chinese == "亿": + return 100000000, True + else: + return None, False + + +def chinese_char_to_number(chinese): + if chinese == "零": + return 0 + elif chinese in ["一", "壹"]: + return 1 + elif chinese in ["二", "贰"]: + return 2 + elif chinese in ["三", "叁"]: + return 3 + elif chinese in ["四", "肆"]: + return 4 + elif chinese in ["五", "伍"]: + return 5 + elif chinese in ["六", "陆"]: + return 6 + elif chinese in ["七", "柒"]: + return 7 + elif chinese in ["八", "捌"]: + return 8 + elif chinese in ["九", "玖"]: + return 9 + else: + return -1 + + +def chinese_to_number(chinese): + length = len(chinese) + result = 0 + section = 0 + number = 0 + unit = [None, False] + for i in range(length): + c = chinese[i] + num = chinese_char_to_number(c) + if num >= 0: + if num == 0: + if number > 0 and unit[0] != None: + section += number * (unit[0] / 10) + unit = [None, False] + elif number > 0: + raise ValueError(f"Bad number '{chinese[i - 1]}{c}' at: {i}") + number = num + else: + unit = chinese_to_money_unit(c) + if unit[0] == None: + raise ValueError(f"Unknown unit '{c}' at: {i}") + if unit[1]: + section = (section + number) * unit[0] + result += section + section = 0 + else: + unitNumber = number + if number == 0 and i == 0: + unitNumber = 1 + + section += unitNumber * unit[0] + number = 0 + + if number > 0 and unit[0] != None: + number *= unit[0] / 10 + return result + section + number + + +def chinese_money_to_number(chinese_money_amount): + if string_util.blank(chinese_money_amount): + return None + yi = chinese_money_amount.find("元") + if yi == -1: + yi = chinese_money_amount.find("圆") + ji = chinese_money_amount.find("角") + fi = chinese_money_amount.find("分") + y_str = None + if yi > 0: + y_str = chinese_money_amount[0:yi] + + j_str = None + if ji > 0: + if yi >= 0: + if ji > yi: + j_str = chinese_money_amount[yi + 1:ji] + else: + j_str = chinese_money_amount[0:ji] + + f_str = None + if fi > 0: + if ji >= 0: + if fi > ji: + f_str = chinese_money_amount[ji + 1:fi] + elif yi > 0: + if fi > yi: + f_str = chinese_money_amount[yi + 1:fi] + else: + f_str = chinese_money_amount[0: fi] + + y = 0 + j = 0 + f = 0 + if not string_util.blank(y_str): + y = chinese_to_number(y_str) + + if not string_util.blank(j_str): + j = chinese_to_number(j_str) + + if not string_util.blank(f_str): + f = chinese_to_number(f_str) + + amount = y + amount += j / 10 + amount += f / 100 + return round(amount, 2)