using LinqToDB.Mapping;
using MasaBlazorApp3.Pojo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace MasaBlazorApp3.Pojo
{
    [Table("dm_machine_record")]
    public class MachineRecord
    {
        /// <summary>
        /// 主键 
        ///</summary>
        [Column("id", IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        /// <summary>
        /// 设备id 
        ///</summary>
        [Column("machine_id")]
        public string MachineId { get; set; }
        /// <summary>
        /// 药品id 
        ///</summary>
        [Column("drug_id")]
        public string DrugId { get; set; }


        [Association(ThisKey = nameof(DrugId), OtherKey = nameof(DrugInfo.DrugId))]
        public DrugInfo Drug { get; set; }
        /// <summary>
        /// 数量 
        ///</summary>
        [Column("quantity")]
        public int Quantity { get; set; }
        
        /// <summary>
        /// 批号 
        ///</summary>
        [Column("manu_no")]
        public string ManuNo { get; set; }
        /// <summary>
        /// 操作人id 
        ///</summary>
        [Column("operator")]
        public int? Operator { get; set; }


        [Association(ThisKey = nameof(Operator), OtherKey = nameof(User.Id))]
        public User OperatorUser { get; set; }
        /// <summary>
        /// 审核人id 
        ///</summary>
        [Column("reviewer")]
        public int? Reviewer { get; set; }


        [Association(ThisKey = nameof(Reviewer), OtherKey = nameof(User.Id))]
        public User ReviewerUser { get; set; }
        /// <summary>
        /// 操作时间 
        /// 默认值: CURRENT_TIMESTAMP
        ///</summary>
        [Column("operation_time")]
        public DateTime OperationTime { get; set; }
        /// <summary>
        /// 效期 
        ///</summary>
        [Column("eff_date")]
        public DateTime? EffDate { get; set; }
        /// <summary>
        /// 出库入库类型(1入库2出库4盘点31还药32还空瓶,21药盒移出移入，69药盒药品替换，) 
        /// 50添加套餐;51删除套餐；52向套餐中添加药品；53修改套餐中的药品；54删除套餐中的药品;55药盒套餐绑定;56药盒套餐解绑,
        ///</summary>
        [Column("type")]
        public int Type { get; set; }
        /// <summary>
        /// 出入库调拨单id 
        ///</summary>
        [Column("invoice_id")]
        public string InvoiceId { get; set; }
        /// <summary>
        /// 列号 
        ///</summary>
        [Column("col_no")]
        public int ColNo { get; set; }
        /// <summary>
        /// 抽屉号 
        ///</summary>
        [Column("drawer_no")]
        public int DrawerNo { get; set; }
        /// <summary>
        /// 取药科室 
        ///</summary>
        [Column("department_id")]
        public string DepartmentId { get; set; }
        /// <summary>
        /// 是否已经退药(0:没有1:还了部分2:完成) 
        /// 默认值: 0
        ///</summary>
        [Column("status")]
        public int Status { get; set; }
        /// <summary>
        /// 退药量 
        /// 默认值: 0
        ///</summary>
        [Column("return_quantity1")]
        public int ReturnQuantity1 { get; set; }
        /// <summary>
        /// 退空瓶量 
        /// 默认值: 0
        ///</summary>
        [Column("return_quantity2")]
        public int ReturnQuantity2 { get; set; }


        [Column(IsColumn = false)]
        public int CurrentReturnQuantity { get; set; }
        /// <summary>
        /// 取药记录id 
        ///</summary>
        [Column("get_id")]
        public int? GetId { get; set; }
        /// <summary>
        /// 是否已经销毁 
        /// 默认值: 0
        ///</summary>
        [Column("is_destroy")]
        public int? IsDestroy { get; set; }

        [Column(IsColumn = false)]
        public int CanReturnQuantity
        {
            get => Quantity - ReturnQuantity1 - ReturnQuantity2;

        }
        //是否选中
        [Column(IsColumn = false)]
        public bool IsSelected { get; set; }

        [Column(IsColumn =false)]
        public string Location
        {
            get => DrawerNo + "-" + ColNo;
        }
        public static MachineRecord Build(MachineRecord machineRecord,DrugInfo drug,User user)
        {
            if(machineRecord!=null)
            {
                machineRecord.Drug = drug;
                machineRecord.OperatorUser = user;

            }
            return machineRecord;
        }
        /// <summary>
        /// 药盒号
        ///</summary>
        [Column("Box_Drawer")]
        public int? BoxDrawer { get; set; }
        /// <summary>
        /// 药盒号
        ///</summary>
        [Column("Box_ColNo")]
        public int? BoxColNo { get; set; }
    }
}
