using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kivii.DataAnnotations;
using Kivii.Linq;

namespace Kivii.Domain.Contents.Entities
{
    public enum ReportStatusType
    {
        Unsupported = 0,

        /// <summary>
        /// 委托受理
        /// </summary>
        CommissionAccept = 100,

        /// <summary>
        /// 合同评审
        /// </summary>
        ContractReview = 200,

        /// <summary>
        /// 任务分派
        /// </summary>
        TaskAssign = 300,

        /// <summary>
        /// 数据录入
        /// </summary>
        DataEntry = 400,

        /// <summary>
        /// 报告编制
        /// </summary>
        ReportPreparation = 500,

        /// <summary>
        /// 报告审核
        /// </summary>
        ReportReview = 600,

        /// <summary>
        /// 报告签发
        /// </summary>
        ReportIssue = 700,

        /// <summary>
        /// 报告打印
        /// </summary>
        ReportPrint = 800,

        /// <summary>
        /// 报告归档
        /// </summary>
        ReportCollected = int.MaxValue
    }
    public abstract class ReportBase : EntityWithMetadata
    {
        #region 报告编号组成要素
        [DefaultEmptyGuid]
        public Guid BizKvid { get; set; }

        [ApiMember(Description = "业务流水号")]
        [StringLength(200)]
        [Default("")]
        public string BizId { get; set; }

        [ApiMember(Description = "业务类型")]
        [StringLength(100)]
        [Default("")]
        public string BizType { get; set; }

        /// <summary>
        /// 报告编号,全系统唯一
        /// </summary>
        [ApiMember(Description = "报告编号")]
        //[Unique, IgnoreUpdate]//流水号本身就是支持Unique的，不需要重复写，并且报告号是允许更新的，万一他们写错了
        [StringLength(50), Default(""), SerialNumber(NumberField = nameof(ReportIdNumber))]
        public string ReportId { get; set; }

        /// <summary>
        /// 报告编号的流水号数字部分
        /// </summary>
        [ApiMember(Description = "流水号")]
        [Default(0)]
        public int ReportIdNumber { get; set; }

        /// <summary>
        /// 报告类型
        /// 纺织品报告(F),子部门报告(A、B、C、D)
        /// </summary>
        [ApiMember(Description = "报告类型")]
        [StringLength(20), Default("")]
        public string Type { get; set; }

        /// <summary>
        /// 报告类型后缀
        /// 委托报告(W),监督报告(J)
        /// </summary>
        [ApiMember(Description = "报告类型扩展")]
        [StringLength(20), Default("")]
        public string TypeEx { get; set; }

        /// <summary>
        /// 报告类型名称中文显示
        /// 委托送样，委托抽样
        /// </summary>
        [ApiMember(Description = "报告类型名称")]
        [StringLength(20), Default("")]
        public string TypeName { get; set; }

        /// <summary>
        /// 报告分类，现记录客户编码（报告后缀）UR......
        /// </summary>
        [ApiMember(Description = "报告分类")]
        [StringLength(20), Default("")]
        public string Category { get; set; }
        #endregion

        #region 供需双方信息
        //供方可以是组织，也可以是部门

        [ApiMember(Description = "供方Kvid")]
        [DefaultEmptyGuid]
        public Guid SupplierKvid { get; set; }

        [ApiMember(Description = "供方名称,即检测单位")]
        [StringLength(100), Required]
        public string SupplierName { get; set; }

        [ApiMember(Description = "供方联系人Kvid")]
        [DefaultEmptyGuid]
        public Guid SupplierContactKvid { get; set; }

        [ApiMember(Description = "供方联系人")]
        [StringLength(100), Default("")]
        public string SupplierContactName { get; set; }

        [ApiMember(Description = "供方联系电话")]
        [StringLength(50), Default("")]
        public string SupplierContactNumber { get; set; }

        [ApiMember(Description = "供方联系地址")]
        public string SupplierContactAddress { get; set; }

        [ApiMember(Description = "需方Kvid")]
        [DefaultEmptyGuid]
        public Guid DemanderKvid { get; set; }

        [ApiMember(Description = "需方名称,即委托单位")]
        [StringLength(100), Required]
        public string DemanderName { get; set; }

        [DefaultEmptyGuid]
        public Guid DemanderContactKvid { get; set; }

        [ApiMember(Description = "需方联系人")]
        [StringLength(100), Default("")]
        public string DemanderContactName { get; set; }

        [ApiMember(Description = "需方联系电话")]
        [StringLength(50), Default("")]
        public string DemanderContactNumber { get; set; }

        [ApiMember(Description = "需方联系地址")]
        public string DemanderContactAddress { get; set; }

        #endregion

        #region 收付双方 Payer付款单位,贷方  Payee收款单位,借方
        [DefaultEmptyGuid]
        public Guid PayerKvid { get; set; }

        [ApiMember(Description = "付款单位,贷方")]
        [StringLength(100)]
        public string PayerName { get; set; }


        [IgnoreUpdate]
        [DefaultEmptyGuid]
        public Guid PayeeKvid { get; set; }

        [ApiMember(Description = "收款单位,借方")]
        [IgnoreUpdate]
        [StringLength(100), Required]
        public string PayeeName { get; set; }
        #endregion

        #region 样品相关，和sample冗余

        [ApiMember(Description = "样品Kvid")]
        [IgnoreUpdate]
        [Required]
        public Guid SampleKvid { get; set; }
        /// <summary>
        /// 样品名称
        /// </summary>
        [ApiMember(Description = "样品名称")]
        [StringLength(200), Required]
        public string SampleName { get; set; }

        /// <summary>
        /// 样品名称
        /// </summary>
        [ApiMember(Description = "样品编号")]
        //[SerialNumber]
        [StringLength(50), Default("")]
        public string SampleSerialNumber { get; set; }

        /// <summary>
        /// 样品批次号
        /// </summary>
        [ApiMember(Description = "样品批次号")]
        [StringLength(200), Default("")]
        public string SampleBatchNumber { get; set; }

        /// <summary>
        /// 样品品牌
        /// </summary>
        [ApiMember(Description = "样品品牌")]
        [StringLength(200), Default("")]
        public string SampleBrand { get; set; }

        /// <summary>
        /// 样品规格型号
        /// </summary>
        [ApiMember(Description = "样品规格型号")]
        [StringLength(200), Default("")]
        public string SampleModel { get; set; }

        /// <summary>
        /// 样品数量
        /// </summary>
        [ApiMember(Description = "样品数量")]
        [DecimalLength(10, 2), Default(0)]
        public decimal SampleQuantity { get; set; }

        /// <summary>
        /// 数量单位，老数据的样品数量和单位记到metadata中了
        /// </summary>
        [ApiMember(Description = "样品数量单位")]
        [StringLength(50), Default("")]
        public string SampleQuantityUnit { get; set; }

        /// <summary>
        /// 样品品级
        /// </summary>
        [ApiMember(Description = "样品品级")]
        [StringLength(200), Default("")]
        public string SampleGrade { get; set; }

        /// <summary>
        /// 安全类别
        /// </summary>
        [ApiMember(Description = "安全类别")]
        [StringLength(200), Default("")]
        public string SampleGradeSafety { get; set; }

        [ApiMember(Description = "生产单位Kvid")]
        [DefaultEmptyGuid]
        public Guid SampleManufacturerKvid { get; set; }

        [ApiMember(Description = "生产单位")]
        [StringLength(100), Default("")]
        public string SampleManufacturerName { get; set; }

        [ApiMember(Description = "生产单位联系人Kvid")]
        [DefaultEmptyGuid]
        [IgnoreUpdate]
        public Guid SampleManufacturerContactKvid { get; set; }

        [ApiMember(Description = "生产单位联系人姓名")]
        [StringLength(50), Default("")]
        public string SampleManufacturerContactName { get; set; }

        [ApiMember(Description = "生产单位联系人电话")]
        [StringLength(50), Default("")]
        //正则表达式验证：(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})
        public string SampleManufacturerContactNumber { get; set; }

        [ApiMember(Description = "生产单位地址")]
        [StringLength(200), Default("")]
        public string SampleManufacturerAddress { get; set; }
        #endregion

        #region 样品来源相关，和SampleSource冗余
        [DefaultEmptyGuid]
        public Guid SampleSourceKvid { get; set; }

        /// <summary>
        /// 样品来源编号，计划编号，快递号等
        /// </summary>
        [ApiMember(Description = "来源编号")]
        [StringLength(200), Default("")]
        public string SampleSourceBatchNumber { get; set; }
        #endregion

        #region 检验相关
        /// <summary>
        /// 检验依据
        /// </summary>
        [ApiMember(Description = "检验依据")]
        [StringLength(1000), Default("")]
        public string TestStandards { get; set; }

        /// <summary>
        /// 检测项目
        /// </summary>
        [ApiMember(Description = "检测项目")]
        [StringLength(3000), Default("")]
        public string TestDetections { get; set; }

        /// <summary>
        /// 检验要求：判定、数据
        /// </summary>
        [ApiMember(Description = "检验要求，是否需要判定")]
        [Required]
        public bool TestNeedJudge { get; set; }

        /// <summary>
        /// 产品质量综合判定
        /// </summary>
        [ApiMember(Description = "产品质量综合判定")]
        [StringLength(200), Default("")]
        public string TestJudgement { get; set; }


        #endregion

        #region 日期相关
        /// <summary>
        /// 委托日期:默认为当天日期
        /// </summary>
        [ApiMember(Description = "委托日期")]
        [DateOnly]
        [NonClusteredIndex]
        public DateTime? DealDate { get; set; }

        /// <summary>
        /// 完成日期(前台选择)
        /// </summary>
        [ApiMember(Description = "要求完成日期")]
        [DateOnly]
        [NonClusteredIndex]
        public DateTime? DeadDate { get; set; }

        /// <summary>
        /// 评审日期
        /// </summary>
        [ApiMember(Description = "评审日期")]
        [DateOnly]
        public DateTime? ContractReviewDate { get; set; }

        /// <summary>
        /// 分派日期
        /// </summary>
        [ApiMember(Description = "分派日期")]
        [DateOnly]
        public DateTime? TaskAssignDate { get; set; }

        /// <summary>
        /// 检验开始日期(为委托的下达日期)
        /// </summary>
        [ApiMember(Description = "检验开始日期")]
        public DateTime? TestStartDate { get; set; }

        /// <summary>
        /// 检验结束日期
        /// 默认为编制时的当天日期
        /// </summary>
        [ApiMember(Description = "检验结束日期")]
        public DateTime? TestFinishDate { get; set; }

        /// <summary>
        /// 编制日期
        /// </summary>
        [ApiMember(Description = "编制日期")]
        public DateTime? PreparationDate { get; set; }

        /// <summary>
        /// 审核日期
        /// </summary>
        [ApiMember(Description = "审核日期")]
        [DateOnly]
        public DateTime? ReviewDate { get; set; }

        /// <summary>
        /// 签发日期
        /// </summary>
        [ApiMember(Description = "签发日期")]
        [InternalSetter]
        [DateOnly]
        [NonClusteredIndex]
        public DateTime? IssueDate { get; set; }
        #endregion

        #region 报告受理，编制，审核，签发人员信息
        /// <summary>
        /// 受理人Kvid
        /// </summary>
        [DefaultEmptyGuid]
        public Guid OperatorKvid { get; set; }

        /// <summary>
        /// 受理人姓名，不一定是Creator（从第三方系统进来的报告）
        /// </summary>
        [ApiMember(Description = "受理人")]
        [StringLength(20), Default("")]
        public string OperatorName { get; set; }
        /// <summary>
        /// 主检人
        /// </summary>
        [ApiMember(Description = "主检人")]
        [StringLength(50), Default("")]
        public string ManagerName { get; set; }

        /// <summary>
        /// 主检人Kvid
        /// </summary>
        [DefaultEmptyGuid]
        public Guid ManagerKvid { get; set; }

        /// <summary>
        /// 编制人Kvid
        /// </summary>
        [DefaultEmptyGuid]
        public Guid PreparerKvid { get; set; }

        /// <summary>
        /// 编制人姓名
        /// </summary>
        [ApiMember(Description = "编制人")]
        [StringLength(50), Default("")]
        public string PreparerName { get; set; }

        /// <summary>
        /// 审核人Kvid
        /// </summary>
        [DefaultEmptyGuid]
        public Guid ReviewerKvid { get; set; }

        /// <summary>
        /// 审核人姓名
        /// </summary>
        [ApiMember(Description = "审核人")]
        [StringLength(50), Default("")]
        public string ReviewerName { get; set; }

        /// <summary>
        /// 签发人Kvid
        /// </summary>
        [DefaultEmptyGuid]
        public Guid IssuerKvid { get; set; }

        /// <summary>
        /// 签发人姓名
        /// </summary>
        [ApiMember(Description = "签发人")]
        [StringLength(50), Default("")]
        public string IssuerName { get; set; }

        #endregion

        #region 报告模板及格式份数
        [DefaultEmptyGuid]
        public Guid TemplateKvid { get; set; }
        /// <summary>
        /// 报告格式
        /// </summary>
        [ApiMember(Description = "报告格式")]
        [StringLength(200), Default("")]
        public string TemplateName { get; set; }

        /// <summary>
        /// 报告份数
        /// </summary>
        [ApiMember(Description = "报告份数")]
        [DecimalLength(4, 2), Default(0)]
        public decimal Quantity { get; set; }

        [ApiMember(Description = "数量单位")]
        [Ignore]
        public string QuantityUnit
        {
            get
            {
                return "份";
            }
        }

        /// <summary>
        /// 加急系数
        /// </summary>
        [ApiMember(Description = "加急系数")]
        [DecimalLength(4, 2), Default(1)]
        public decimal UrgentRate { get; set; } = 1;

        /// <summary>
        /// 报告是否显示生产单位
        /// </summary>
        //[ApiMember(Description = "报告是否显示生产单位")]
        //public bool ShowProductOrg { get; set; }

        #endregion

        #region 费用相关信息
        /// <summary>
        /// 单价,仅指报告打印的单价
        /// </summary>
        [ApiMember(Description = "单价")]
        [DecimalLength(10, 2), Default(0)]
        public decimal QuantityUnitPrice { get; set; }

        /// <summary>
        /// 理论单价
        /// </summary>
        [ApiMember(Description = "理论单价")]
        [DecimalLength(10, 2), Default(0)]
        public decimal QuantityUnitPricePlan { get; set; }

        /// <summary>
        /// 检验费，ReportItem中的Amount总和
        /// </summary>
        [ApiMember(Description = "检验费")]
        [InternalSetter]
        [DecimalLength(10, 2), Default(0)]
        public decimal AmountTest { get; set; }

        /// <summary>
        /// 理论检验费，ReportItem中的AmountPlan总和
        /// </summary>
        [ApiMember(Description = "理论检验费")]
        [InternalSetter]
        [DecimalLength(10, 2), Default(0)]
        public decimal AmountTestPlan { get; set; }


        /// <summary>
        /// 报告的费用,N份报告的打印费用
        /// </summary>
        [ApiMember(Description = "报告费")]
        [DecimalLength(10, 2), Default(0)]
        public decimal Amount { get; set; }

        /// <summary>
        /// 理论报告的费用
        /// </summary>
        [ApiMember(Description = "理论报告费")]
        [DecimalLength(10, 2), Default(0)]
        public decimal AmountPlan { get; set; }

        [ApiMember(Description = "货币单位")]
        [IgnoreUpdate]
        [StringLength(50), Required]
        public CurrencyUnit Currency { get; set; }

        /// <summary>
        /// 结算总费用：即实际费用结算的费用
        /// </summary>
        [ApiMember(Description = "结算总费用")]
        [DecimalLength(10, 2), Default(0)]
        [InternalSetter]
        public decimal AmountSettlement { get; set; }

        /// <summary>
        /// 已缴总费用：即费用结算后所缴纳的实际费用
        /// </summary>
        [ApiMember(Description = "已缴总费用")]
        [DecimalLength(10, 2), Default(0)]
        [InternalSetter]
        public decimal AmountPayment { get; set; }

        /// <summary>
        /// 执行的协议Kvid
        /// </summary>
        [ApiMember(Description = "执行协议Kvid")]
        [DefaultEmptyGuid]
        [IgnoreUpdate]
        public Guid QuotationKvid { get; set; }

        /// <summary>
        /// 执行的协议名称
        /// </summary>
        [ApiMember(Description = "执行协议名称")]
        [StringLength(200), Default("")]
        [IgnoreUpdate]
        public string QuotationName { get; set; }

        /// <summary>
        /// 执行的协议的价格，若价格大于0，则以此价格为准
        /// </summary>
        [ApiMember(Description = "执行协议价格")]
        [DecimalLength(10, 2), Default(0)]
        [IgnoreUpdate]
        public decimal QuotationAmount { get; set; }


        /// <summary>
        /// 协议的折扣，0-1之间
        /// </summary>
        [ApiMember(Description = "协议折扣率")]
        [DecimalLength(4, 2), Default(1)]
        [IgnoreUpdate]
        public decimal? QuotationDiscountRate { get; set; }
        #endregion

        /// <summary>
        /// 检验结论，报告的结论
        /// </summary>
        [ApiMember(Description = "检验结论")]
        [StringLength(2000), Default("")]
        public string Conclusion { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [ApiMember(Description = "备注")]
        [StringLength(2000), Default("")]
        public string Remark { get; set; }

        #region 创建/更新人V1.0
        [ApiMember(Description = "创建人Kvid ")]
        [IgnoreUpdate]
        [CurrentMemberKvid]
        public Guid CreatorKvid { get; set; }

        [ApiMember(Description = "创建人")]
        [IgnoreUpdate]
        [StringLength(50), CurrentMemberName]
        public string CreatorName { get; set; }

        [ApiMember(Description = "更新人Kvid ")]
        [CurrentMemberKvid]
        public Guid UpdaterKvid { get; set; }

        [ApiMember(Description = "更新人")]
        [StringLength(50), CurrentMemberName]
        public string UpdaterName { get; set; }
        #endregion

        [Ignore]
        public ReportStatusType StatusType
        {
            get
            {
                return (ReportStatusType)Status;
            }
            set
            {
                Status = (int)value;
            }
        }
    }

    [Alias(Configs.TableNameReport)]
    [Api(Description = "检验报告")]
    public class Report : ReportBase, IEntityHasPayer, IEntityHasPayee, IEntityHasSettlement,
        IEntityHasPayment, IEntityHasDemander, IEntityHasSupplier, IEntityInAssemblyDb
    {

    }
}
