#coding=utf-8
import json
import datetime

from django.db import models
from django.utils import timezone
from django.conf import settings

from apps.exceptions import CustomError
from libs.utils import strftime, strfdate
#from managers import OptionManager

# class Option(models.Model):
#     PAYMENT_MODE = 0
#     MATERIAL_MODE = 1
#     CONSUMABLE_MODE = 2
#     GOODS_MODE = 3
#     QUALITY_REQUEST = 4
#     PAYMENT_ACCOUNT = 5
#     SALE_MESSAGE = 6
#
#     TYPE_CHOICES = (
#         (PAYMENT_MODE, u'付款方式'),
#         (MATERIAL_MODE, u'原料类别'),
#         (CONSUMABLE_MODE, u'耗材类别'),
#         (GOODS_MODE, u'成品类别'),
#         (QUALITY_REQUEST, u'质量要求'),
#         (PAYMENT_ACCOUNT, u'银行账户'),
#         (SALE_MESSAGE, u'发货信息'),
#     )
#
#     type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类别")
#     name = models.CharField(max_length=100, verbose_name=u"名称")
#     notes = models.CharField(max_length=500, verbose_name=u"备注",blank=True,null=True)
#     enabled = models.BooleanField(verbose_name=u"在用", default=True)
#
#     #objects = OptionManager()
#
#     @staticmethod
#     def getTypeText(type):
#         return Option.TYPE_CHOICES[type][1]
#
#     @staticmethod
#     def getById(id):
#         try:
#             id = int(id)
#         except:
#             raise CustomError(u'无效的自定义项ID')
#
#         instance = Option.objects.filter(pk=id).first()
#         if not instance:
#             raise CustomError(u'未找到相应的自定义项')
#         return instance
#
#     @staticmethod
#     def getByName(name, type):
#         option = Option.objects.filter(name=name,type=type).first()
#         if not option:
#             raise CustomError(u'未找到[%s]为[%s]的自定义项' % (Option.getTypeText(type),name))
#         return option
#
#     class Meta:
#         db_table = "system_option"
#         verbose_name = u"自定义项管理"
#         ordering = ('-id', )
#         index_together = (
#             'name',
#         )

class BizLogManager(models.Manager):
    def addnew(self, user, type, description, data=None):
        def default(o):
            if isinstance(o, datetime.datetime):
                return strftime(o)
            elif isinstance(o, datetime.date):
                return strfdate(o)

        row = self.model(user=user, type=type, description=description, create_time=timezone.now())
        if data:
            row.data = json.dumps(data, default=default)
        row.save()
        return row

class BizLog(models.Model):
    INSERT = 1
    UPDATE = 2
    DELETE = 3
    CHECK = 4
    EXPORT = 5
    PRINT = 6
    IMPORT = 7
    TYPE_CHOICES = (
        (INSERT, u'添加'),
        (UPDATE, u'修改'),
        (DELETE, u'删除'),
        (CHECK, u'审核'),
        (EXPORT, u'导出'),
        (PRINT, u'打印'),
        (IMPORT, u'导入'),
    )

    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True, blank=True)
    type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, verbose_name=u"类别")
    description = models.CharField(max_length=1000, verbose_name=u"内容")
    data = models.TextField(verbose_name=u"数据", null=True, blank=True)
    create_time = models.DateTimeField(verbose_name=u"添加时间", auto_now_add=True, editable=False)

    objects = BizLogManager()

    class Meta:
        db_table = "system_log"
        ordering = ['-id']
        verbose_name = u"日志管理"
