def getreturnbgpro(productList, targetbgpro, product_activity_list,
    fullcourt_activity_list, optimal_dis, gabg=None, pabg=None):
    """
    满足执行的但目前单据商品无法执行的赠品活动列表
    :param targetproducts: 执行买赠活动列表
    :param product_activity_list: 可参加商品活动列表
    :param fullcourt_activity_list: 可参加全场活动列表
    :param gabg  参加过商品活动的促销活动明细
    :return:
    """
    bgpro = targetbgpro
    if gabg:
        for row in gabg:
            if 'qtty' not in row.keys():
                continue
            new_dis = {}
            new_dis['compute_list'] = []
            operations = []
            totals = 0
            for dis_i in product_activity_list:
                if row['id'] == dis_i['id']:
                    new_dis['type_one'] = 'GA'
                    new_dis['publish_date'] = dis_i['publish_date']
                    new_dis['type_three'] = dis_i['prom_type_three']
                    new_dis['type_two'] = dis_i['prom_type_two']
                    new_dis['ename'] = dis_i['ename']
                    new_dis['id'] = dis_i['id']
                    new_dis['is_run_other_pro'] = dis_i['is_run_other_pro'
                        ].lower()
                    new_dis['is_run_store_act'] = dis_i['is_run_store_act'
                        ].lower()
                    new_dis['is_run_other'] = 'y'
                    if new_dis['type_two'] == 5:
                        new_dis['prom_type_two_c'] = dis_i['prom_type_two_c']
                    for seat in productList:
                        for product in seat.productSeatList:
                            if dis_i['specific_activities'][0]['target_type'
                                ].lower() == 'amt_receivable':
                                totals += product.amt_receivable
                                if new_dis['type_two'] == 4:
                                    if product.is_taken_off:
                                        continue
                                elif new_dis['type_two'] == 5:
                                    if (product.is_taken_off or product.
                                        is_buy_gifts == 'y'):
                                        continue
                                totals += product.amt_receivable
                            if dis_i['specific_activities'][0]['target_type'
                                ].lower() == 'amt_list':
                                totals += product.amt_list
                                if new_dis['type_two'] == 4:
                                    if product.is_taken_off:
                                        continue
                                elif new_dis['type_two'] == 5:
                                    if (product.is_taken_off or product.
                                        is_buy_gifts == 'y'):
                                        continue
                                totals += product.amt_list
                            if dis_i['specific_activities'][0]['target_type'
                                ].lower() == 'qtty':
                                if new_dis['type_two'] == 4:
                                    if product.is_taken_off:
                                        continue
                                elif new_dis['type_two'] == 5:
                                    if (product.is_taken_off or product.
                                        is_buy_gifts == 'y'):
                                        continue
                                totals += product.qtty
                            if dis_i['specific_activities'][0]['target_type'
                                ].lower() == 'amt_retail':
                                if new_dis['type_two'] == 4:
                                    if product.is_taken_off:
                                        continue
                                elif new_dis['type_two'] == 5:
                                    if (product.is_taken_off or product.
                                        is_buy_gifts == 'y'):
                                        continue
                                totals += product.amt_retail
                    bg = dis_i['specific_activities'][0]['operation_set']
                    for gift in bg:
                        compare = gift['comp_symb_type']
                        if compare == 'G':
                            value_num = gift['value_num'] + 1
                        else:
                            value_num = gift['value_num']
                        if totals >= value_num:
                            operations.append(gift)
                    if new_dis['type_three'] == 'GA1501' or new_dis[
                        'type_three'] == 'GA1502':
                        purchase_way = 'T'
                    elif new_dis['type_three'] == 'GA1503' or new_dis[
                        'type_three'] == 'GA1504':
                        purchase_way = 'D'
                    else:
                        purchase_way = 'J'
                    for operation in operations:
                        if new_dis['type_two'] != 5:
                            try:
                                if new_dis['type_three'] == 'ga1402':
                                    if int(operation['promotion_lineno']
                                        ) <= int(row['promotion_lineno']):
                                        new_dis['compute_list'].append({
                                            'product_list': operation[
                                            'buy_gifts']['product_list'],
                                            'give_value': row['qtty'],
                                            'list_sort': operation[
                                            'promotion_lineno']})
                                else:
                                    new_dis['compute_list'].append({
                                        'product_list': operation[
                                        'buy_gifts']['product_list'],
                                        'give_value': row['qtty'],
                                        'list_sort': 1})
                            except KeyError:
                                new_dis['compute_list'].append({
                                    'product_list': operation['buy_gifts'][
                                    'product_list'], 'give_value': row['qtty']}
                                    )
                        else:
                            try:
                                new_dis['compute_list'].append({'pcond_id':
                                    operation['pcond_id'], 'target_item':
                                    dis_i['target_item'],
                                    'purchase_condition': operation[
                                    'redemption']['purchase_condition'],
                                    'purchase_way': purchase_way,
                                    'product_list': operation['redemption']
                                    ['product_list'], 'times': row['times'],
                                    'single_value': row['bsc_qtty'],
                                    'give_value': row['qtty']})
                            except KeyError:
                                new_dis['compute_list'].append({
                                    'product_list': operation['buy_gifts'][
                                    'product_list'], 'give_value': row['qtty']}
                                    )
                    bgpro.append(new_dis)
    if pabg:
        for row in pabg:
            new_dis = {}
            new_dis['compute_list'] = []
            for dis_i1 in fullcourt_activity_list:
                if row['id'] == dis_i1['id']:
                    new_dis['type_one'] = 'PA'
                    new_dis['publish_date'] = dis_i1['publish_date']
                    new_dis['type_three'] = dis_i1['prom_type_three']
                    new_dis['type_two'] = dis_i1['prom_type_two']
                    new_dis['ename'] = dis_i1['ename']
                    new_dis['id'] = dis_i1['id']
                    new_dis['is_run_other_pro'] = dis_i1['is_run_other_pro'
                        ].lower()
                    new_dis['is_run_store_act'] = dis_i1['is_run_store_act'
                        ].lower()
                    if new_dis['type_two'] == 5:
                        new_dis['prom_type_two_c'] = dis_i1['prom_type_two_c']
                    dis_num = 0
                    if optimal_dis:
                        for dis in optimal_dis:
                            if dis['type_one'] == 'GA':
                                dis_num += 1
                        if dis_num != len(optimal_dis):
                            for dis in optimal_dis:
                                if dis['type_one'] == 'PA':
                                    for dis_i in fullcourt_activity_list:
                                        if dis['id'] == dis_i['id']:
                                            if dis_i['is_run_store_act'] == 'Y':
                                                new_dis['is_run_other'] = 'y'
                                            else:
                                                new_dis['is_run_other'] = 'n'
                                                break
                                    if new_dis['is_run_other'] == 'n':
                                        break
                                else:
                                    for dis_i1 in product_activity_list:
                                        if dis['id'] == dis_i1['id']:
                                            if dis_i1['is_run_store_act'] == 'Y':
                                                new_dis['is_run_other'] = 'y'
                                            else:
                                                new_dis['is_run_other'] = 'n'
                                                break
                                    if new_dis['is_run_other'] == 'n':
                                        break
                        else:
                            new_dis['is_run_other'] = 'y'
                    else:
                        new_dis['is_run_other'] = 'n'
                    if new_dis['type_three'] == 'PA1401':
                        purchase_way = 'T'
                    elif new_dis['type_three'] == 'PA1402':
                        purchase_way = 'D'
                    else:
                        purchase_way = 'J'
                    for operation in reversed(row['operations']):
                        if new_dis['type_two'] != 5:
                            try:
                                new_dis['compute_list'].append({
                                    'product_list': operation['buy_gifts'][
                                    'product_list'], 'give_value': row[
                                    'give_value'], 'list_sort': operation[
                                    'promotion_lineno']})
                            except KeyError:
                                new_dis['compute_list'].append({
                                    'product_list': operation['buy_gifts'][
                                    'product_list'], 'give_value': row[
                                    'give_value']})
                        else:
                            try:
                                new_dis['compute_list'].append({'pcond_id':
                                    operation['pcond_id'], 'target_item':
                                    dis_i1['target_item'],
                                    'purchase_condition': operation[
                                    'redemption']['purchase_condition'],
                                    'purchase_way': purchase_way,
                                    'product_list': operation['redemption']
                                    ['product_list'], 'times': row['times'],
                                    'single_value': row['bsc_qtty'],
                                    'give_value': row['qtty']})
                            except KeyError:
                                new_dis['compute_list'].append({
                                    'product_list': operation['redemption']
                                    ['product_list'], 'give_value': row[
                                    'qtty']})
                    bgpro.append(new_dis)
    return bgpro
