#coding: utf8
import xlrd
from django.core.management import BaseCommand
from api.models import UserReceive

class Command(BaseCommand):

    def handle(self, *args, **options):
        file1="eye-shadow_0521.xlsx"
        file2="wash_0521.xls"
        file3="rouge_0521.xlsx"
        print('eye-shadow_0521.xlsx 开始处理')
        self.update_tracking_number_(file1)
        print ('eye-shadow_0521.xlsx 处理结束')
        print ('wash_0521.xls 开始处理')
        self.update_tracking_number_(file2)
        print ('wash_0521.xls 处理结束')
        print ('rouge_0521.xlsx 开始处理')
        self.update_tracking_number_(file3)
        print ('rouge_0521.xlsx 处理结束')

    def update_tracking_number_(self,file):
        data = xlrd.open_workbook(file.decode('utf-8'))
        sheet1 = data.sheets()[0]
        sheet2 = data.sheets()[1]
        self.put_table(sheet1)
        print ('表1处理结束')
        self.put_table(sheet2)
        print ('表2处理结束')

    def put_table(self,sheet):
        for index in range(1, sheet.nrows):
            row = sheet.row_values(index)
            if row:
                express_no, user_phone = str(row[0]), str(row[1])
                obj=UserReceive.objects.filter(user_phone=user_phone,express_no='').first()
                if obj:
                    print express_no, user_phone, obj.id
                    obj.express_no=express_no
                    obj.save()
                else:
                    print "数据不存在"
