
import json
from dao.tj_purchase_original_dao import OriginalDao
from dao.tj_lable_depth_dao import LableDao
from dao.tj_product_depth_dao import ProductDao
from dao.tj_provide_company import ProvideDao
from common.time_format import now_time
from common.link_task import ProductService
class Recom:
    #用户历史购买
    def company_behavior(self,company_name):
        company_purchase = []
        original =  OriginalDao()
        behavior_result = original.select(company_name)
        print(behavior_result,11)
        num = 1
        if behavior_result:
            for item in behavior_result:
                # print(item)
                behavior_json = {}
                behavior_json['num'] = num
                if item['merge_product_name']:
                    behavior_json['goods_name'] = item['merge_product_name']
                else:
                    behavior_json['goods_name'] = item['product_name']
                behavior_json['purchase_date'] = str(item['order_add_time'])
                behavior_json['contract_amount'] = int(int(item['order_pay'])/100)
                behavior_json['source'] = item['order_channel']
                behavior_json['company_name'] = company_name
                item['status'] = "已完成"
                company_purchase.append(behavior_json)
                num = num+1
        return company_purchase

    def lable_type(self,label_type,item,company_lable_list):
        label_change_dict ={"base_label":"基本标签","management_label":"经营标签","asset_label":"资产标签","preference_label":"偏好标签","product_label":"产品标签","risk_label":"风险标签","intellectual_property_right":"知识产权标签","business_label":"业务标签","key_people":"关键人标签",}
        dict = {}
        if item != None:
            dict['lable_type'] =label_change_dict[label_type]
            lable_data = []
            base_lable = json.loads(item)
            if len(base_lable) > 0:
                for data in base_lable:
                    for type in data:
                        lable_data.append(data[type])
            dict['lable_data'] = lable_data
            if len(dict['lable_data']) != 0:
                company_lable_list.append(dict)
        return company_lable_list

    def lable_type_list(self,lable_type,item,company_lable_list):
        label_change_dict = {"base_label": "基本标签", "management_label": "经营标签", "asset_label": "资产标签",
                             "preference_label": "偏好标签", "product_label": "产品标签", "risk_label": "风险标签",
                             "intellectual_property_right": "知识产权标签", "business_label": "业务标签", "key_people": "关键人标签", }

        dict = {}
        if item:
            dict['lable_type'] = label_change_dict[lable_type]
            dict['lable_data'] = json.loads(item.replace("'",'"'))
            if len(dict['lable_data']) != 0:
                company_lable_list.append(dict)
        return company_lable_list

    #企业画像
    def company_label(self,company_name):

        # json_info = {}
        # json_info['insert_status'] = 1
        # json_info['pageNum'] = 1
        # json_info['pageSize'] = 2
        # json_info['companyName'] = company_name
        # json_info['currdate'] = str(now_time())
        # ProductService(json_info)

        company_lable_list = []
        lable = LableDao()
        lable_result = lable.select(company_name)
        if lable_result:
            for item in lable_result:

                #基本标签
                # if item['base_label'] != None:
                #     base_lable_dict['lable_type'] = "基本标签"
                #     base_lable_data = []
                #     base_lable = json.loads(item['base_label'])
                #     for data in base_lable:
                #         for type in data:
                #             base_lable_data.append(data[type])
                #     base_lable_dict['lable_data'] = base_lable_data
                #     company_lable_list.append(base_lable_dict)
                #基本标签
                company_lable_list = self.lable_type("base_label",item['base_label'],company_lable_list)
                print(company_lable_list,"结果")
                #经营标签
                company_lable_list = self.lable_type("management_label",item['management_label'], company_lable_list)
                # #资产标签
                company_lable_list = self.lable_type_list("asset_label",item['asset_label'], company_lable_list)
                #偏好标签
                company_lable_list = self.lable_type("preference_label",item['preference_label'], company_lable_list)
                # #产品标签
                company_lable_list = self.lable_type_list("product_label",item['product_label'], company_lable_list)
                # #风险标签
                company_lable_list = self.lable_type_list("risk_label",item['risk_label'], company_lable_list)
                #知识产权标签
                company_lable_list = self.lable_type("intellectual_property_right",item['intellectual_property_right'], company_lable_list)
                #业务标签
                company_lable_list = self.lable_type("business_label",item['business_label'], company_lable_list)
                #关键人标签
                company_lable_list = self.lable_type("key_people",item['key_people'], company_lable_list)


                # #经营标签
                # if item['management_label'] != None:
                #     management_lable_dict['lable_type'] = "经营标签"
                #     management_lable_data = []
                #     management_label = json.loads(item['management_label'])
                #     for data in management_label:
                #         for type in data:
                #             management_lable_data.append(data[type])
                #     management_lable_dict['lable_data'] = management_lable_data
                #     company_lable_list.append(management_lable_dict)
                # #资产标签
                # if item['asset_label'] != None:
                #     asset_lable_dict['lable_type'] = "资产标签"
                #     asset_lable_data = []
                #     asset_label = json.loads(item['asset_label'])
                #     for data in asset_label:
                #         for type in data:
                #             asset_lable_data.append(data[type])
                #     asset_lable_dict['lable_data'] = asset_lable_data
                #     company_lable_list.append(asset_lable_dict)
                #偏好标签
                # if item['preference_label'] != None:
                #     preference_lable_dict['lable_type'] = "偏好标签"
                #     preference_lable_data = []
                #     preference_lable = json.loads(item['preference_label'])
                #     for data in preference_lable:
                #         for type in data:
                #             preference_lable_data.append(data[type])
                #     preference_lable_dict['lable_data'] = preference_lable_data
                #     company_lable_list.append(preference_lable_dict)
                #产品标签
                # if item['product_label'] != None:
                #     product_lable_dict['lable_type'] = "产品标签"
                #     product_lable_data = item['product_label'].replace("[","").replace("]","").replace("'","").replace(" ", "").split(',')
                #     product_lable_dict['lable_data'] = product_lable_data
                #     company_lable_list.append(product_lable_dict)

                #风险标签
                # if item['risk_label'] != None:
                #     risk_lable_dict['lable_type'] = "偏好标签"
                #     preference_lable_data = []
                #     preference_lable = json.loads(item['risk_label'])
                #     for data in preference_lable:
                #         for type in data:
                #             preference_lable_data.append(data[type])
                #     risk_lable_dict['lable_data'] = preference_lable_data
                #     company_lable_list.append(risk_lable_dict)





        # else:
        #     lable = LableDao()
        #     lable_result = lable.select_label_slave(company_name)
        #     product_lable_data = []
        #     product_lable_dict={}
        #     for item in lable_result:
        #         credit_code = item['credit_code']
        #         establish_time_span = item['establish_time_span'] + '年'
        #         gsb_company_cate_1 = item['gsb_company_cate_1']
        #         address = item['address']
        #         product_lable_dict['lable_type'] = '基本标签'
        #         product_lable_data.append(credit_code)
        #         product_lable_data.append(establish_time_span)
        #         product_lable_data.append(gsb_company_cate_1)
        #         product_lable_data.append(address)
        #         product_lable_dict['lable_data'] = product_lable_data
        #         company_lable_list.append(product_lable_dict)
        return company_lable_list

    #企业推荐产品
    def recommend_product(self):
        recommend_product_list = []
        product = ProductDao()
        provide = ProvideDao()
        company_list = []
        company_result = provide.select()
        for item in company_result:
            bussiness_dict = {}
            business_data_dict = {}
            company_name = item['company_name']
            company_list.append(item['id'])
            company_id = item['company_id']
            bussinessid = item['serial_number']
            create_time = str(item['create_time'])[:10]
            business_data_dict['companyId'] = company_id
            business_data_dict['companyName'] = company_name
            business_data_dict['businessId'] = bussinessid
            business_data_dict['createTime'] = create_time
            bussiness_dict['businessData'] = business_data_dict

            product_result = product.select(company_name)
            for data in product_result:
                bussiness_dict['productList'] = []

                if data['relational_product']:
                    relational = eval(data['relational_product'])
                    for item in relational:
                        item['typeCate'] = item['typeName']
                        item['typeName'] = "关联推荐"
                        bussiness_dict['productList'].append(item)
                if data['similar_product']:
                    similar = eval(data['similar_product'])
                    for item in similar:
                        item['typeCate'] = item['typeName']
                        item['typeName'] = "同类推荐"
                        bussiness_dict['productList'].append(item)
                if data['cross_product']:
                    cross = eval(data['cross_product'])
                    for item in cross:
                        item['typeCate'] = item['typeName']
                        item['typeName'] = "交叉推荐"
                        bussiness_dict['productList'].append(item)
            recommend_product_list.append(bussiness_dict)
        if len(company_list) > 0:
            provide.update_status(company_list)
        return recommend_product_list

    # 商机
    def business(self):
        business_list = []
        company_list = []
        provide = ProvideDao()
        company_result = provide.select()
        for item in company_result:
            print(item)
            company_list.append(item['id'])
            business_data_dict = {}
            crm_company_name = item['crm_company_name']
            standard_company_name = item['standard_company_name']
            company_id = item['credit_code']
            bussinessid = item['serial_number']
            create_time = str(item['create_time'])[:10]
            business_data_dict['companyId'] = company_id
            business_data_dict['crm_company_name'] = crm_company_name
            business_data_dict['standard_company_name'] = standard_company_name
            business_data_dict['businessId'] = bussinessid
            business_data_dict['createTime'] = create_time
            print(business_data_dict)
            business_list.append(business_data_dict)
        if len(company_list) > 0:
            provide.update_status(company_list)
        return business_list

        # 公司名推荐产品方法

    def product_fun(self, company_name):
        product_list = []
        product = ProductDao()
        product_result = product.select(company_name)
        for data in product_result:
            print(data,"======推荐产品")
            if data['relational_product_old']:
                relational = eval(data['relational_product_old'])
                for item in relational:
                    print(item,"====业务产品item")
                    item['productName'] = item['commodityName']
                    del item['commodityName']
                    item['typeCate'] = item['typeName']
                    item['typeName'] = "关联推荐"
                    product_list.append(item)
                    print(product_list,"===========业务产品list")
            if data['similar_product_old']:
                similar = eval(data['similar_product_old'])
                for similaritem in similar:
                    similaritem['typeCate'] = similaritem['typeName']
                    similaritem['productName'] = similaritem['commodityName']
                    similaritem['typeName'] = "同类推荐"
                    product_list.append(similaritem)
            if data['cross_product_old']:
                cross = eval(data['cross_product_old'])
                for crossitem in cross:
                    crossitem['typeCate'] = crossitem['typeName']
                    crossitem['productName'] = crossitem['commodityName']
                    crossitem['typeName'] = "交叉推荐"
                    product_list.append(crossitem)
            if data['survey_product_old']:
                survey = eval(data['survey_product_old'])
                for surveyitem in survey:
                    surveyitem['typeCate'] = surveyitem['typeName']
                    surveyitem['productName'] = surveyitem['commodityName']
                    surveyitem['typeName'] = "监测推荐"
                    product_list.append(surveyitem)

            if data['potential_product']:
                potential = eval(data['potential_product'])
                for potentialitem in potential:
                    potentialitem['typeCate'] = potentialitem['typeName']
                    potentialitem['productName'] = potentialitem['commodityName']
                    potentialitem['typeName'] = "潜在商机"
                    product_list.append(potentialitem)
        print(product_list, "===========业务产品list1")
        return product_list

        # 商机推荐产品

    def business_product(self, business_id,product_select ):
        print("0000")
        provide = ProvideDao()
        product_list = []
        resaon = []
        product_result = provide.select_id(business_id)
        for data in product_result:
            if product_select == '复购算法':
                if data['similar_product']:
                    similar = eval(data['similar_product'])
                    for similaritem in similar:
                        similaritem['typeCate'] = similaritem['typeName']
                        similaritem['productName'] = similaritem['commodityName']
                        similaritem['typeName'] = "同类推荐"
                        product_list.append(similaritem)
                # if data['cross_product']:
                #     cross = eval(data['cross_product'])
                #     for crossitem in cross:
                #         crossitem['typeCate'] = crossitem['typeName']
                #         crossitem['productName'] = crossitem['commodityName']
                #         crossitem['typeName'] = "交叉推荐"
                #         product_list.append(crossitem)
            if product_select == "关联推荐":
                if data['relational_product']:
                    relational = eval(data['relational_product'])
                    for item in relational:
                        item['productName'] = item['commodityName']
                        del item['commodityName']
                        item['typeCate'] = item['typeName']
                        item['typeName'] = "关联推荐"
                        product_list.append(item)
                        print(product_list, "===========业务产品list")

            if product_select == "智能监测":
                if data['survey_product']:
                    if data['survey_product_reason']:
                        print("data['survey_product_reason']",data['survey_product_reason'])
                        resaon = json.loads(data['survey_product_reason'])
                    survey = eval(data['survey_product'])
                    for surveyitem in survey:
                        surveyitem['typeCate'] = surveyitem['typeName']
                        surveyitem['productName'] = surveyitem['commodityName']
                        surveyitem['typeName'] = "监测推荐"
                        product_list.append(surveyitem)

            if product_select == "潜在商机":
                if data['potential_product']:
                    potential = eval(data['potential_product'])
                    for potentialitem in potential:
                        potentialitem['typeCate'] = potentialitem['typeName']
                        potentialitem['productName'] = potentialitem['commodityName']
                        potentialitem['typeName'] = "潜在商机"
                        product_list.append(potentialitem)

        print(product_list, "===========业务产品list1")
        return product_list,resaon

        # 公司名推荐产品
    def company_product(self, company_name):
        product_list = self.product_fun(company_name)
        return product_list

# if __name__ == '__main__':
#     aa = Recom()
#
#     behave_result = aa.company_behavior("深圳市海果达科技有限公司")
#     print(behave_result)
# #
    # result = aa.company_label("厦门秋鸣文化传媒有限公司")
    # print(result)

    # product_result = aa.recommend_product("厦门秋鸣文化传媒有限公司")
    # print(product_result)

    # behavior("重庆饱不了餐椅管理有限公司")