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
    if not isImports:
        isImports = 0
    storeTypeId = select_data['storeTypeId']
    industryId = select_data['industryId']
    secondIndustry = select_data['secondIndustry'][0]['id']
    platform_code = select_data['platformCode']

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

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

    #用户未选择的资质
    no_choice = []
    data = select_data['data']
    for bussiness_type in data:

        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'],
                "describe":product_item['product_describe'],
                "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'],
                "describe":product_item['product_describe'],
                "price":product_item['product_price']
            }
            product_result.append(data)
        result_data['recommend'] = product_result
    return result_data

















