from dao.ent_qualification import qualificationsDao
from dao.recommend_product_dao import qualificationsProductDao

qualificationsdao = qualificationsDao()
qualificationsproductdao = qualificationsProductDao()
def recommend_function(select_data):
    isImports = select_data['isImports']
    if isImports:
        isImports = (1,3)
    if not isImports:
        isImports = (0,3)
    storeTypeId = select_data['storeTypeId']
    industryId = select_data['industryId']
    secondIndustry = []
    for item in select_data['secondIndustry']:
        if item['isChoice'] == True:
            secondIndustry.append(item['id'])
    platform_code = select_data['platformCode']

    #办理资质必须的业务员
    qualifications = qualificationsdao.selectQualificationsUse(platform_code,isImports,storeTypeId,industryId,secondIndustry)
    must_use = []
    for item in qualifications:
        if item['qualification_comments'] not in must_use:
            must_use.append(item['qualification_comments'])

    print("用户必选=============",must_use)

    #用户未选择的资质
    no_choice = []
    data = select_data['data']
    for bussiness_type in data:
        if data[bussiness_type] != None:
            for choice_type in data[bussiness_type]:
                print("choice_type",choice_type)
                if str(choice_type['isChoice']) == 'False':
                    no_choice.append(choice_type['name'])
    print("用户未选择的资质=============",no_choice)


    #不通过时需要办理的业务
    have_use = []
    for nust_use_item in must_use:
        if nust_use_item in no_choice:
            have_use.append(nust_use_item)

    print("have_use============",have_use)
    product_result = []
    result_data = {"isPass": False, "recommend": []}
    #检测不通过
    if len(have_use) != 0:
        result_data['isPass'] = False
        have_on_use = list(set(have_use))
        product_result_data = qualificationsproductdao.get_recommend(have_on_use)
        for product_item in product_result_data:
            data = {
                "id":product_item['product_id'],
                "name":product_item['product_name'],
                "describe":product_item['product_description'],
                "price":product_item['product_price']
            }
            product_result.append(data)
        result_data['recommend'] = product_result
    #检测通过
    if len(have_use) == 0:
        result_data['isPass'] = True
        product_result_data = qualificationsproductdao.random_recommend()
        for product_item in product_result_data:
            data = {
                "id":product_item['product_id'],
                "name":product_item['product_name'],
                "describe":product_item['product_description'],
                "price":product_item['product_price']
            }
            product_result.append(data)
        result_data['recommend'] = product_result
    return result_data

















