
import sys
import os
sys.path.append(os.path.dirname(__file__)+"/../..")

from BaseDataMaintenance.maintenance.dataflow import Dataflow_dumplicate,log

import time


flow = Dataflow_dumplicate(start_delete_listener=False)

from BaseDataMaintenance.common.Utils import uniform_package_name
import json
import re
import traceback


def merge_document_interface(item,b_log=False):
    '''
    实时项目合并
    :param item:
    :param dup_docid:重复的公告集合
    :param status_to:
    :return:
    '''
    try:
        if isinstance(item,dict):
            log("merge_document_interface %s"%json.dumps(item,ensure_ascii=False))
            _proj = {
                "page_time":item.get("page_time",""),
                "project_codes":item.get("project_codes",""),
                "project_name":item.get("project_name",""),
                "tenderee":item.get("tenderee",""),
                "agency":item.get("agency",""),
                "product":item.get("product",""),
                "sub_project_name":item.get("sub_project_name",""),
                "bidding_budget":item.get("bidding_budget","0"),
                "win_tenderer":item.get("win_tenderer",""),
                "win_bid_price":item.get("win_bid_price","0"),
                "province":item.get("province",""),
                "city":item.get("city",""),
                "district":item.get("district",""),
                "zhao_biao_page_time":item.get("zhao_biao_page_time",""),
                "zhong_biao_page_time":item.get("zhong_biao_page_time",""),
                "enterprise":item.get("enterprise",""),
                "detail_link":item.get("detail_link",""),
                "doctitle":item.get("doctitle",""),

            }

            if _proj.get("page_time")=="":
                raise RuntimeError("page_time参数不能为空")

            if _proj.get("province"):
                _proj["province"] = re.sub("省","",str(_proj["province"]))
            if _proj.get("city"):
                if len(str(_proj["city"]))>2:
                    _proj["city"] = re.sub("市","",str(_proj["city"]))
            if _proj.get("district"):
                if len(str(_proj["district"]))>2:
                    _proj["district"] = re.sub("区|县|镇","",str(_proj["district"]))

            if _proj.get("bidding_budget"):
                _proj["bidding_budget"] = float(_proj["bidding_budget"])

            if _proj.get("win_bid_price"):
                _proj["win_bid_price"] = float(_proj["win_bid_price"])

            _proj["sub_project_name"] = uniform_package_name(_proj["sub_project_name"])

            enterprise = _proj.get("enterprise","")
            list_enterprise = enterprise.split(",") if enterprise else []
            enterprise = {"nlp_enterprise":list_enterprise}
            _proj["enterprise"] = json.dumps(enterprise,ensure_ascii= False)

            list_projects = flow.merge_projects([_proj],b_log=b_log)
            if len(list_projects)>0:
                uuids = list_projects[0].get("uuid","")
                if uuids:
                    l_uuid = uuids.split(",")
                    if l_uuid:
                        return l_uuid[0]
        else:
            raise RuntimeError("参数应该以字典方式传递")
    except Exception as e:
        traceback.print_exc()
        raise RuntimeError("error: %s"%(str(e)))


import os

os.environ["CUDA_VISIBLE_DEVICES"] = "1"

from flask import Flask,request,jsonify
app = Flask(__name__)


@app.route("/project_merge",methods=["POST"])
def embedding():
    _r = {"success": True}
    try:
        item = request.json
        project_uuid = merge_document_interface(item)
        if project_uuid is None:
            project_uuid = ""
        _r["project_uuid"] = project_uuid
    except Exception as e:
        traceback.print_exc()
        _r["success"] = False
        _r["msg"] = str(e)

    return jsonify(_r)

def start_project_merge_server():
    app.run(host="0.0.0.0",port="15010",debug=False)

def test():
    _proj = {
        "page_time":"2025-01-14",
        "project_codes":"SHX-ZB-2024-01013-07",
        "project_name":"泗洪县2025年财政资金定期（含大额存单）存储金融机构采购项目（一期）",
        "tenderee":"泗洪县财政局",
        "agency":"中天志远咨询有限公司",
        "product":"存储金融机构,财政资金定期（含大额存单）存储金融机构",
        "sub_project_name":"8",
        "bidding_budget":0,
        "win_tenderer":"中国农业银行股份有限公司泗洪县支行",
        "win_bid_price":50000000,
        "province":"江苏",
        "city":"宿迁",
        "district":"泗洪",
        "zhao_biao_page_time":"",
        "zhong_biao_page_time":"2025-01-14",
        "enterprise":"中国银行股份有限公司泗洪支行,南京银行股份有限公司泗洪支行",
        "detail_link":"",
        "doctitle":"泗洪县2025年财政资金定期（含大额存单）存储金融机构采购项目（一期）",

    }
    import requests
    resp = requests.post("http://localhost:15010/project_merge",json=_proj)
    print(resp.content.decode("utf-8"))

    #
    # print(merge_document_interface(_proj,b_log=True))


if __name__ == '__main__':

    # start_project_merge_server()
    test()



