
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.now_time 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)
        num = 1
        for item in behavior_result:
            # print(item)
            behavior_json = {}
            behavior_json['num'] = num
            behavior_json['goods_name'] = item['product_name']
            behavior_json['purchase_date'] = item['order_add_time']
            behavior_json['contract_amount'] = item['order_pay']
            behavior_json['source'] = item['source']
            behavior_json['company_name'] = company_name
            item['status'] = "已完成"
            company_purchase.append(behavior_json)
            num = num+1
        return company_purchase
    #企业画像
    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:
                base_lable_dict = {}
                management_lable_dict = {}
                asset_lable_dict = {}
                preference_lable_dict = {}
                product_lable_dict = {}
                #基本标签
                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)
                #经营标签
                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)
        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 = {}
            company_name = item['company_name']
            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
            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:

            if data['relational_product']:
                relational = eval(data['relational_product'])
                for item in relational:
                    item['typeCate'] = item['typeName']
                    item['typeName'] = "关联推荐"
                    product_list.append(item)
            if data['similar_product']:
                similar = eval(data['similar_product'])
                for item in similar:
                    item['typeCate'] = item['typeName']
                    item['typeName'] = "同类推荐"
                    product_list.append(item)
            if data['cross_product']:
                cross = eval(data['cross_product'])
                for item in cross:
                    item['typeCate'] = item['typeName']
                    item['typeName'] = "交叉推荐"
                product_list.append(item)
        return product_list

        # 商机推荐产品

    def business_product(self, business_id):
        provide = ProvideDao()

        company_result = provide.select_id(business_id)
        if company_result:
            company_name = company_result[0]['company_name']
            product_list = self.product_fun(company_name)
        return product_list

        # 公司名推荐产品

    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("重庆饱不了餐椅管理有限公司")