# coding=utf-8
from django.core.management import BaseCommand
from openpyxl import load_workbook, Workbook
from openpyxl.writer.excel import ExcelWriter

from api.models import SpecialSeckillService, Service


class Command(BaseCommand):
    """
    批量修改秒杀美购的更美价和秒杀价 python manage.py set_service_price_discount update(大促专用减)
    """

    def add_arguments(self, parser):
        parser.add_argument('param', nargs='+', type=str)

    def handle(self, *args, **options):
        print options
        for item in options['param']:
            if item == 'update':
                Process.save_data()
            elif item == 'test':
                Process.test_data()


class Process():
    @staticmethod
    def save_data():
        file_log = open('dachuservice/4.txt', 'r')
        result_log = open('0815_jian.txt', 'a')
        lines = file_log.readlines(100000)
        for line in lines:
            try:
                service_id = int(line.strip())
                print service_id
                service = Service.objects.get(id=service_id)
                service.gengmei_price -= 11
                service.discount -= 11
                service.pre_payment_price -= 11
                service.save()
                message = str(service.id) + u',' + str(service.gengmei_price) + u',' + str(service.discount) + u',' + \
                          str(service.pre_payment_price)
                print message
                result_log.writelines(message)
            except Exception as e:
                print e.message
                continue
        print (u"更新成功!")
        file_log.close()
        result_log.close()

    @staticmethod
    def test_data():
        file_log = open('dachuservice/4.txt', 'r')
        lines = file_log.readlines(100000)
        for line in lines:
            try:
                service_id = int(line.strip())
                print service_id
                service = Service.objects.get(id=service_id)
                service.gengmei_price -= 11
                service.discount -= 11
                service.pre_payment_price -= 11
                message = str(service.id) + u',' + str(service.gengmei_price) + u',' + str(service.discount) + u',' + \
                          str(service.pre_payment_price)
                print message
            except Exception as e:
                print e.message
                continue
        print (u"测试一下没问题!请继续执行python manage.py set_service_price_discount update")
        file_log.close()
