from django.db import models
from django.db.models.base import Model
import django.utils.timezone as timezone
from django.db.models.query import QuerySet
from apps.system.models import CommonAModel, CommonBModel, Organization, User, Dict, File
from utils.model import SoftModel, BaseModel
from simple_history.models import HistoricalRecords
from apps.system.models import Organization
from apps.em.models import Equipment

class Material(CommonAModel):
    """
    物料
    """
    type_choices=(
        (1, '成品'),
        (2, '半成品'),
        (3, '原材料')
    )
    unit_choices =(
        ('块', '块'),
        ('套', '套')
    )
    name = models.CharField('物料名称', max_length=100, unique=True)
    number = models.CharField('编号', max_length=100, unique=True)
    type = models.CharField('物料类型', choices= type_choices, max_length=20, default=1)
    sort_str = models.CharField('排序字符', max_length=100, null=True, blank=True)
    processes = models.JSONField('工艺流程', default=list, blank=True)
    unit = models.CharField('基准计量单位', choices=unit_choices, default='块', max_length=10)

    class Meta:
        verbose_name = '物料表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Process(CommonAModel):
    """
    工序
    """
    name = models.CharField('工序名称', max_length=100, unique=True)
    number = models.CharField('编号', max_length=100, unique=True)
    instruction = models.ForeignKey(File, verbose_name='指导书', on_delete=models.SET_NULL, null=True, blank=True)
    instruction_content = models.TextField('指导书内容', null=True, blank=True)
    workshop = models.ForeignKey(Organization, verbose_name='生产车间', on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        verbose_name = '工序'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Step(CommonAModel):
    """
    工序步骤
    """
    process = models.ForeignKey(Process, on_delete=models.CASCADE, verbose_name='所属工序')
    name = models.CharField('工序步骤名称', max_length=100)
    number = models.CharField('步骤编号', max_length=100, null=True, blank=True)
    instruction_content = models.TextField('相应操作指导', null=True, blank=True)
    sort = models.IntegerField('排序号', default=1)
    equipments = models.ManyToManyField(Equipment, verbose_name='使用设备')

    class Meta:
        verbose_name = '工序步骤'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class RecordForm(CommonAModel):
    """
    记录表格
    """
    type_choices=(
        (1, '生产记录'),
    )
    name = models.CharField('表格名称', max_length=100, unique=True)
    type = models.IntegerField('表格类型', choices=type_choices, default=1)
    step = models.ForeignKey(Step, verbose_name='关联子工序', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '记录表格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class RecordFormField(CommonAModel):
    """
    记录字段表
    """
    field_type_choices = (
        ('string', '字符串'),
        ('int', '整型'),
        ('float', '浮点'),
        ('boolean', '布尔'),
        ('date', '日期'),
        ('datetime', '日期时间'),
        ('radio', '单选'),
        ('checkbox', '多选'),
        ('select', '单选下拉'),
        ('selects', '多选下拉'),
        ('textarea', '文本域'),
    )
    form = models.ForeignKey(RecordForm, on_delete=models.CASCADE, verbose_name='关联表格')
    field_type = models.CharField('类型', max_length=50, choices=field_type_choices)
    field_key = models.CharField('字段标识', max_length=50, help_text='字段类型请尽量特殊，避免与系统中关键字冲突')
    field_name = models.CharField('字段名称', max_length=50)
    boolean_field_display = models.JSONField('布尔类型显示名', default=dict, blank=True, null=True,
                                             help_text='当为布尔类型时候，可以支持自定义显示形式。{"1":"是","0":"否"}或{"1":"需要","0":"不需要"}，注意数字也需要引号')
    field_choice = models.JSONField('radio、checkbox、select的选项', default=dict, blank=True, null=True,
                                    help_text='radio,checkbox,select,multiselect类型可供选择的选项，格式为json如:{"1":"中国", "2":"美国"},注意数字也需要引号')
    sort = models.IntegerField('排序号', default=1)
    class Meta:
        verbose_name = '记录表格字段'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.field_key + '-' + self.field_name
 
class ProductProcess(CommonAModel):
    """
    产品生产工艺
    """
    product = models.ForeignKey(Material, verbose_name='产品', on_delete=models.CASCADE)
    process = models.ForeignKey(Process, verbose_name='工序', on_delete=models.CASCADE)
    sort = models.IntegerField('排序号', default=1)

    class Meta:
        verbose_name = '产品生产工序'
        verbose_name_plural = verbose_name

class InputMaterial(CommonAModel):
    """
    输入物料
    """
    material = models.ForeignKey(Material, verbose_name='输入物料', on_delete=models.CASCADE, related_name='inputmaterial')
    count = models.FloatField('消耗量', default=1)
    product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE, related_name='inputmaterial_product')
    process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE)
    sort = models.IntegerField('排序号', default=1)

    class Meta:
        verbose_name = '输入物料'
        verbose_name_plural = verbose_name



class OutputMaterial(CommonAModel):
    """
    输出物料
    """
    material = models.ForeignKey(Material, verbose_name='输出物料', on_delete=models.CASCADE, related_name='outputmaterial')
    count = models.FloatField('产出量', default=1)
    product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE, related_name='outputmaterial_product')
    process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE)
    sort = models.IntegerField('排序号', default=1)

    class Meta:
        verbose_name = '输出物料'
        verbose_name_plural = verbose_name
    
class UsedStep(CommonAModel):
    """
    产品生产子工序
    """
    step = models.ForeignKey(Step, verbose_name='子工序', on_delete=models.CASCADE, related_name='usedsteps')
    product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE)
    process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE)
    
    class Meta:
        verbose_name = '产品生产子工序'
        verbose_name_plural = verbose_name


class TechDoc(CommonAModel):
    """
    技术文件
    """
    name = models.CharField('名称', max_length=50)
    file = models.ForeignKey(File, verbose_name='技术文件', on_delete=models.CASCADE)
    product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE)
    process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE)
    content = models.TextField('内容', null=True, blank=True)

    class Meta:
        verbose_name = '技术文件'
        verbose_name_plural = verbose_name
