
from django.views.decorators.csrf import csrf_exempt
from django.db import connection
from django.http import JsonResponse
from company.models import Company

@csrf_exempt
def check_companies(request):
    data = {
        'e':'',
        'companies':[]
    }

    companies = request.POST.get('companies','')
    ancestor_id = request.GET.get('ancestor_id','')
    if companies and ancestor_id:
        companies = companies.split(',')


        companies_in_str = ''
        if len(companies) > 0:
            for n in range(len(companies)):
                companies_in_str+= "'"+ companies[n] +"',"
            companies_in_str = companies_in_str[:-1]
        #data['e'] = companies_in_str
        
        if companies_in_str !='':
            cur=connection.cursor()
            cur.execute(f"""
                select id,name from company_company where name in ({companies_in_str}) and ancestor_id = {ancestor_id}
            
            """)
            rs = cur.fetchall()
            for r in rs:
                data['companies'].append({
                    'id':r[0],
                    'name':r[1]
                })
                for i in range(len(companies)):
                    if r[1] == companies[i]:
                        del companies[i]
                        break

            
            for c in range(len(companies)):
                comp = Company()
                comp.name = companies[c]
                comp.ancestor_id = int(ancestor_id)
                comp.level_id = 2
                comp.hierarchy_id = int(ancestor_id)
                comp.category_id = 1
                comp.parent_id = int(ancestor_id)
                comp.remark = '数据迁移导入'
                try: 
                    comp.save()
                    data['companies'].append({
                        'id':comp.id,
                        'name':comp.name
                    })
                except:
                    pass
                else:
                    pass



        
    else:
        data['e'] = '数据丢失'

    return JsonResponse(data)


@csrf_exempt
def check_products(request):
    data = {
        'e':'',
        'products':[]
    }

    products = request.POST.get('products','')
    ancestor_id = request.GET.get('ancestor_id','')
    if products and ancestor_id:
        products = products.split(',')


        products_in_str = ''
        if len(products) > 0:
            for n in range(len(products)):
                products_in_str+= "'"+ products[n] +"',"
            products_in_str = products_in_str[:-1] 
        
        if products_in_str !='':
            cur=connection.cursor()
            cur.execute("""
                select id,name from product_product where name in ("""+products_in_str+""") and company_id = """+ancestor_id+"""
            
            """)
            rs = cur.fetchall()
            for r in rs:
                data['products'].append({
                    'id':r[0],
                    'name':r[1]
                })
                for i in range(len(products)):
                    if r[1] == products[i]:
                        del products[i]
                        break 
        
    else:
        data['e'] = '数据丢失'

    return JsonResponse(data)