from django.db import models
from django.utils.translation import gettext_lazy as _
import os
# Create your models here.

# 图像上传路径


def image_upload(instance, filename):
    return os.path.join(instance.company.name, 'product', filename) 

#用户积分明细
class UserPoints_Record(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    member = models.ForeignKey('sales.Member', verbose_name=_(
        '用户'), on_delete=models.DO_NOTHING)
    reason = models.CharField(_('原因'), max_length=200)
    points = models.IntegerField(_('积分数量'), blank=True, null=True)
    tm = models.DateTimeField(_('获取时间'), auto_now_add=True)

# 用户积分
class UserPoints(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    member = models.ForeignKey('sales.Member', verbose_name=_(
        '用户'), on_delete=models.DO_NOTHING)
    num = models.IntegerField(_('积分数量'), blank=True, null=True)
    is_freeze = models.BooleanField(
        _('是否冻结'), blank=False, null=False, default=False)
    tm = models.DateTimeField(_('更新时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('用户积分')
        verbose_name_plural = _('用户积分')

    def update_user_points(self,points,reason):
        p = UserPoints_Record(
            company=self.company,
            member=self.member,
            points=points,
            reason=reason
        )
        p.save()

        self.num = self.num+points
        self.save()

    def __str__(self):
        return self.name


# 积分商城设置
class Settings(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    exchange_rate = models.IntegerField(
        _('汇率'), blank=True, null=True, default=1)
    is_open = models.BooleanField(
        _('是否开放'), blank=False, null=False, default=True)

    class Meta:
        verbose_name = _('积分商城设置')
        verbose_name_plural = _('积分商城设置')

# 商城用户类型
class Grade(models.Model):
    name = models.CharField(_('用户类型名称'), max_length=30)
    class Meta:
        verbose_name = _('用户类型')
        verbose_name_plural = _('用户类型')

# 积分商品分类
class Points_Product_Category(models.Model):
    cat_name = models.CharField(_('分类名称'), max_length=50)
    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    def __str__(self):
        return self.cat_name


class Platform(models.Model):
    name = models.CharField(_('平台商名称'), max_length=50)
    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    def __str__(self):
        return self.name


# 积分商品信息
class Points_Products(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    product_name = models.CharField(_('商品名称'), max_length=300)
    product_image = models.ImageField(
        _('商品图片'), upload_to=image_upload, blank=True, null=True)
    product_cost = models.DecimalField(
        _('总价值'), max_digits=10, decimal_places=2, blank=True, null=True)
    product_desc = models.CharField(_('商品介绍'), max_length=3000, blank=True, null=True)
    product_content = models.CharField(_('商品内容'), max_length=10000, blank=True, null=True)
    face_to = models.SmallIntegerField(_('面向'), blank=True, null=True)

    cat = models.ForeignKey(Points_Product_Category, verbose_name=_(
        '分类ID'), null=True, blank=True, on_delete=models.DO_NOTHING)

    is_active = models.BooleanField(
        _('是否激活'), blank=False, null=False, default=True)
    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)
    
    platform = models.ForeignKey(Platform, verbose_name=_('平台商'), on_delete=models.SET_NULL, blank=True, null=True)
    skuid = models.CharField(_('skuid'), max_length=50, blank=True, null=True)
    brand = models.CharField(_('商品品牌'), max_length=50, blank=True, null=True)
    offShelf = models.BooleanField(_('是否下架'), blank=True, null=True, default=False)
    seq = models.SmallIntegerField(_('排序'), blank=True, null=True, default=0)

    class Meta:
        verbose_name = _('产品管理')
        verbose_name_plural = _('产品管理')

#产品轮播图管理
class Points_Products_Banner(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    product = models.ForeignKey(Points_Products, verbose_name=_(
        '产品'), on_delete=models.CASCADE)
    image = models.ImageField(
        _('轮播图'), upload_to=image_upload, blank=True, null=True)
    sort_id = models.SmallIntegerField(_('排序'), blank=True, null=True, default=0)
    is_active = models.BooleanField(
        _('是否激活'), blank=False, null=False, default=True)
    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)
    class Meta:
        verbose_name = _('产品轮播图管理')
        verbose_name_plural = _('产品轮播图管理')

# 轮播图管理
class Banners(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    image = models.ImageField(
        _('轮播图'), upload_to=image_upload, blank=True, null=True)
    is_active = models.BooleanField(
        _('是否激活'), blank=False, null=False, default=True)
    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('轮播图管理')
        verbose_name_plural = _('轮播图管理')

# 订单状态
class Order_Status(models.Model):
    status_name = models.CharField(_('订单状态'), max_length=50)
    order_type = models.SmallIntegerField(_('订单类型'), blank=True, null=True)
    class Meta:
        verbose_name = _('订单状态')
        verbose_name_plural = _('订单状态')

    def __str__(self):
        return self.status_name

class Delivery_Company(models.Model):
    name = models.CharField(_('快递公司名称'), max_length=50)
    code = models.CharField(_('快递公司编码'), max_length=30)
    abbr = models.CharField(_('快递公司简称'), max_length=30)
    class Meta:
        verbose_name = _('快递公司')
        verbose_name_plural = _('快递公司')
    def __str__(self):
        return self.name

# 订单 
class Orders(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    user = models.ForeignKey('sales.Member', verbose_name=_(
        '用户'), on_delete=models.DO_NOTHING)
    order_number = models.CharField(_('订单号'), max_length=50)
    order_type = models.SmallIntegerField(_('订单类型'), blank=True, null=True)
    status = models.ForeignKey(Order_Status, default=0, blank=True, null=True, on_delete=models.DO_NOTHING)
    total_cost = models.DecimalField(
        _('总价值'), max_digits=10, decimal_places=2, blank=True, null=True)
    remarks = models.CharField(_('备注'), max_length=1000)
    user_name = models.CharField(_('姓名'), max_length=10)
    user_phone = models.CharField(_('电话号码'), max_length=30)
    user_address = models.CharField(_('地址'), max_length=500)
    province = models.CharField(_('省'), max_length=100)
    city = models.CharField(_('市'), max_length=100)
    county = models.CharField(_('区'), max_length=100)
    street = models.CharField(_('街道'), max_length=200)
    deliver_company = models.ForeignKey(Delivery_Company, blank=True, null=True, on_delete=models.DO_NOTHING)
    deliver_company_name = models.CharField(
        _('快递公司'), null=True, blank=True, max_length=50)
    deliver_company_code = models.CharField(
        _('快递单号'), null=True, blank=True, max_length=50)
    deliver_tm = models.DateTimeField(
        _('发货时间'), null=True, blank=True, auto_now_add=False)
    shouhuo_tm = models.DateTimeField(
        _('收货时间'), null=True, blank=True, auto_now_add=False)

    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('订单信息')
        verbose_name_plural = _('订单信息')


# 订单详情 
class Order_Detail(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    order = models.ForeignKey(Orders, verbose_name=_(
        '订单ID'), on_delete=models.DO_NOTHING)
    order_number = models.CharField(_('订单号'), max_length=50)
    product_id = models.IntegerField(_('产品ID'), blank=True, null=True)
    num = models.SmallIntegerField(_('数量'), blank=True, null=True)
    product_name = models.CharField(_('产品名称'), max_length=300)
    product_image = models.CharField(_('产品图片'), max_length=500)
    product_desc = models.CharField(_('产品描述'), max_length=500,blank=True, null=True)
    product_cost = models.DecimalField(
        _('价值'), max_digits=10, decimal_places=2, blank=True, null=True)

    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('订单详情')
        verbose_name_plural = _('订单详情')

#订单评论
class Order_Comment(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    order = models.ForeignKey(Orders, verbose_name=_(
        '订单ID'), on_delete=models.DO_NOTHING)
    user = models.ForeignKey('sales.Member', verbose_name=_(
        '用户'), on_delete=models.DO_NOTHING)
    score = models.SmallIntegerField(_('评分'), blank=True, null=True)
    product = models.ForeignKey(Points_Products, verbose_name=_(
        '商品'),on_delete=models.DO_NOTHING)
    product_name = models.CharField(_('产品名称'), max_length=300)
    product_image = models.CharField(_('产品图片'), max_length=500)
    product_cost = models.DecimalField(
        _('价值'), max_digits=10, decimal_places=2, blank=True, null=True)
    content = models.CharField(_('评价内容'),blank=True, null=True, max_length=1000) 
    is_show = models.BooleanField(
        _('是否显示'), blank=False, null=False, default=True)
    is_delete = models.BooleanField(
        _('是否删除'), blank=False, null=False, default=False)

    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)


# 用户地址 
class User_Address(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    user = models.ForeignKey('sales.Member', verbose_name=_(
        '用户'), on_delete=models.DO_NOTHING)
    name = models.CharField(_('姓名'), max_length=50)
    phone = models.CharField(_('手机号码'), max_length=50)
    address = models.CharField(_('地址信息'), max_length=500)
    province = models.CharField(_('省'), max_length=100)
    city = models.CharField(_('市'), max_length=100)
    county = models.CharField(_('区、县'), max_length=100)
    street = models.CharField(_('街道'), max_length=100)
    address_detail = models.CharField(_('详细地址'), max_length=300)
    area_code = models.CharField(_('地区编码'), max_length=100)
    is_default = models.BooleanField(
        _('是否默认'), blank=False, null=False, default=False)
    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('用户地址')
        verbose_name_plural = _('用户地址')

# 资讯发布


class Boards(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司名称'), on_delete=models.DO_NOTHING)
    subtitle = models.CharField(_('大标题'), max_length=300)
    content = models.CharField(_('内容'), max_length=3000)
    endtm = models.DateTimeField(_('结束时间'))
    is_active = models.BooleanField(
        _('是否激活'), blank=False, null=False, default=True)

    tm = models.DateTimeField(_('添加时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('资讯发布')
        verbose_name_plural = _('资讯发布')
