from django.db import models
from django.db import models, transaction
from apps.utils.models import CommonADModel, BaseModel, CommonBDModel
from apps.system.models import User
from django.core.validators import RegexValidator
from datetime import datetime
from rest_framework.exceptions import ParseError

# Create your models here.
class PatentInfo(CommonBDModel):
    """TN: 专利申密审批表单样式"""
    PATENT_TYPE_CHOICES = (
    ('invention', '发明专利'),
    ('utility', '实用新型专利'),
    ('design', '外观设计专利'),
    )
    APPLY_AREAS = (
        ('Domestic', '国内申请'),
        ('Foreign', '国外申请'),
        ('PCT', 'PCT申请'),
    )

    name = models.CharField('拟申请专利名称', max_length=100)
    organization = models.CharField(max_length=100,blank=True, null=True, verbose_name="申请部门")
    author = models.CharField('发明人（设计人）', max_length=100)
    type = models.CharField('专利类型', max_length=50, choices=PATENT_TYPE_CHOICES, default='invention')
    is_public = models.BooleanField('是否公开', default=False)
    area = models.CharField('拟申请地域', max_length=50, choices=APPLY_AREAS, default='Domestic')
    other_area = models.CharField('其它申请地域', max_length=50, blank=True, null=True)
    tech_status = models.JSONField('技术状态', default=list, blank=True, help_text='技术状态信息列表，每个条目包含name(名称)、status(状态)、file(文件)字段')
    tech_file = models.JSONField('技术文件', default=list, help_text='技术文件信息列表，每个条目包含name(名称)page(页数)字段')
    ticket = models.ForeignKey('wf.ticket', verbose_name='关联工单',
                               on_delete=models.SET_NULL, related_name='patentInfo_ticket', null=True, blank=True, db_constraint=False)
    
class Papersecret(CommonBDModel):
    """TN: 论文申密审批表单"""
    paper_name = models.CharField('拟发表论文名称', max_length=100)
    publication_name = models.CharField('拟投期刊名称', max_length=100)
    author = models.CharField('作者', max_length=100)
    organization = models.CharField(max_length=100, verbose_name="申请部门")
    paper_type = models.CharField('拟发表文章类型', max_length=100)
    is_chinese_core = models.BooleanField('是否为中文核心', default=False)
    is_sci = models.BooleanField('是否被SCI/EI收录', default=False)
    tech_status = models.JSONField('技术状态', default=list, blank=True, help_text='技术状态信息列表，每个条目包含name(名称)、status(状态)、file(文件)字段')
    tech_file = models.JSONField('技术文件', default=list, help_text='技术文件信息列表，每个条目包含name(名称)page(页数)字段')
    ticket = models.ForeignKey('wf.ticket', verbose_name='关联工单',
                               on_delete=models.SET_NULL, related_name='paperse_ticket', null=True, blank=True, db_constraint=False)

class PatentRecord(CommonADModel):
    """TN: 专利台账登记"""
    PATENT_TYPE_CHOICES = (
    ('invention', '发明专利'),
    ('utility', '实用新型专利'),
    ('design', '外观设计专利'),
    )
    volume_number = models.CharField(max_length=50, null=True, blank=True, verbose_name="卷号")
    application_number = models.CharField(max_length=50, null=True, blank=True, verbose_name="申请号（交局后补登）")
    patent = models.ForeignKey('PatentInfo', verbose_name="专利名称", on_delete=models.CASCADE, related_name='patent_record')
    pc_type = models.CharField('专利类型', max_length=50, choices=PATENT_TYPE_CHOICES, default='invention')
    organization = models.CharField(max_length=100, verbose_name="申请部门")
    inventors = models.CharField(max_length=255, verbose_name="发明人")
    agent = models.CharField(max_length=255, null=True, blank=True, verbose_name="代理人")
    affiliated_platforms = models.CharField(max_length=255, blank=True, verbose_name="归属平台")
    affiliated_projects = models.CharField(max_length=255, blank=True, verbose_name="归属项目")
    application_date = models.DateField(null=True, blank=True, verbose_name="申请日")
    authorization_date = models.DateField(null=True, blank=True, verbose_name="授权日")
    validity_years = models.IntegerField(null=True, blank=True, verbose_name="有效年限（年）")
    annuity_paid = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,verbose_name="年费缴纳")

    status = models.CharField(
        max_length=20,
        choices=[
            ("not_disclosed", "未公开"),
            ("under_examination", "实审中"),
            ("first_office_action", "一通"),
            ("second_office_action", "二通"),
            ("rejected", "驳回"),
            ("reexamination", "复审"),
            ("authorized", "授权")
        ],
        null=True,
        blank=True,
        verbose_name="状态"
    )
    award_info = models.TextField(null=True, blank=True, verbose_name="报奖情况")
    bonus_amount = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,verbose_name="奖金金额（元）")

class PaperRecord(CommonADModel):
    """TN: 论文台账登记"""
    PAPER_TYPE_CHOICES = (
    ('research', '研究论文'),
    ('overview', '综述'),
    )
    paper_number = models.CharField(max_length=50, null=True, blank=True, verbose_name="论文编号（投稿后补登）")
    paper = models.ForeignKey('Papersecret', verbose_name="论文名称", on_delete=models.CASCADE, related_name='paper_record')
    pa_type = models.CharField('论文类型', max_length=50, choices=PAPER_TYPE_CHOICES, default='invention')
    organization = models.CharField(max_length=100, verbose_name="申请部门")
    author = models.CharField(max_length=100, verbose_name="作者")
    cor_author = models.CharField(max_length=100, null=True, blank=True, verbose_name="通讯作者")
    affiliated_platforms = models.CharField(max_length=255, blank=True, verbose_name="归属平台")
    affiliated_projects = models.CharField(max_length=255, blank=True, verbose_name="归属项目")
    accept_date = models.DateField(null=True, blank=True, verbose_name="接受日期")
    public_date = models.DateField(null=True, blank=True, verbose_name="发表日期")
    # validity_years = models.IntegerField(null=True, blank=True, verbose_name="有效年限（年）")
    pub_paid = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,verbose_name="版面费缴纳")
    status = models.CharField(
        max_length=20,
        choices=[
            ("审稿中", "审稿中"),
            ("一修", "一修"),
            ("二修", "二修"),
            ("接收", "接收"),
            ("发表", "发表"),
        ],
        verbose_name="状态",
        null=True,
        blank=True
    )
    award_info = models.TextField(null=True, blank=True, verbose_name="报奖情况")
    bonus_amount = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,verbose_name="奖金金额（元）")

class Platform(CommonADModel):
    """TN: 平台信息表"""
    name = models.CharField("名称", max_length=50, null=True, blank=True)
    p_date = models.DateField(null=True, blank=True, verbose_name="日期")
    p_dept = models.CharField("归口部门", max_length=200, null=True, blank=True)
    city_p = models.BooleanField("市级平台", default=False)
    province_p = models.BooleanField("省级平台", default=False)
    const = models.CharField("建设期", max_length=50, null=True, blank=True)
    charge = models.CharField("负责人", max_length=50, null=True, blank=True)
    condition = models.TextField("平台基本情况与目标绩效", max_length=200, null=True, blank=True)
    ticket = models.ForeignKey('wf.ticket', verbose_name='关联工单',
                               on_delete=models.SET_NULL, related_name='plat_ticket', null=True, blank=True, db_constraint=False)
    
class Platstanding(CommonBDModel):
    """TN: 项目信息表
    
    """
    red_head_doc_no = models.CharField("红头发文号/公示页", max_length=100, null=True, blank=True)
    name = models.CharField("名称", max_length=200, null=True, blank=True)
    project_type = models.CharField("项目类型", max_length=100, null=True, blank=True)
    city_p = models.BooleanField("市级平台", default=False)
    province_p = models.BooleanField("省级平台", default=False)
    project_source = models.CharField("项目来源", max_length=100, null=True, blank=True)
    const = models.CharField("建设期", max_length=100, null=True, blank=True)
    platinfo = models.ForeignKey('Platform', verbose_name="平台信息", on_delete=models.CASCADE, related_name='platstanding',null=True, blank=True)
    project_funding = models.DecimalField("项目资金(财政与自筹)", max_digits=15, decimal_places=2, null=True, blank=True)
    support_period = models.CharField("项目支持期", max_length=100, null=True, blank=True)
    undertaking_unit = models.CharField("承担单位", max_length=200, null=True, blank=True)
    responsible_person = models.CharField("负责人", max_length=50, null=True, blank=True)
    project_members = models.TextField("项目人员", null=True, blank=True)
    milestone = models.TextField("里程碑节点", null=True, blank=True)
    mid_term_status = models.TextField("项目中期情况", null=True, blank=True)
    acceptance_status = models.TextField("项目验收情况", null=True, blank=True)
    sci_tech_achievements = models.TextField("科技成果", null=True, blank=True)