public Response UpdateDocumentStatus(int id, string processId, string status)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var doc = ctx.Document.Where(o => o.Id == id).FirstOrDefault();
                    if (doc != null)
                    {
                        doc.ProcessId = processId;
                        doc.Status = status;

                        DocumentProcess docProcess = new DocumentProcess()
                        {
                            CreatedDate = DateTime.Now,
                            DocumentId = doc.Id,
                            ProcessId = processId,
                            Status = status
                        };

                        ctx.DocumentProcess.Add(docProcess);

                        ctx.SaveChanges();
                        resp.Status = true;
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
            }

            return resp;
        }
        public Response CancelSaveDocument(int id)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var doc = ctx.Document.Where(o => o.Id == id).FirstOrDefault();
                    if (doc != null)
                    {
                        foreach (var attachment in doc.DocumentAttachment)
                        {
                            attachment.State = "บันทึก";

                        }

                        foreach (var reference in doc.DocumentReference)
                        {
                            reference.State = "บันทึก";

                        }

                        ctx.SaveChanges();
                        resp.Status = true;
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
                resp.Exception = ex.ToString();
            }

            return resp;
        }
        public Response UpdateMainAttachment(int id, byte[] file)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var doc = ctx.Document.Include("DocumentAttachment").Where(o => o.Id == id).FirstOrDefault();
                    if (doc != null)
                    {

                        doc.MainAttachmentBinary = file;
                        doc.FileSize = Util.ConvertBytesToMegabytes(file.Length).ToString("N5") + " mb";

                        ctx.SaveChanges();
                        resp.Status = true;
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
                resp.Exception = ex.ToString();
            }

            return resp;
        }
        public Response UpdateDocumentAttachment(int id, List<byte[]> files)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var doc = ctx.Document.Include("DocumentAttachment").Where(o => o.Id == id).FirstOrDefault();
                    if (doc != null)
                    {

                        int i = 0;
                        foreach (var attach in doc.DocumentAttachment)
                        {
                            attach.AttachmentBinary = files[i];
                            attach.FileSize = Util.ConvertBytesToMegabytes(files[i].Length).ToString("N5") + " mb";
                            attach.State = "บันทึก";
                            attach.Type = "1";
                            i++;
                        }

                        ctx.SaveChanges();
                        resp.Status = true;
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
                resp.Exception = ex.ToString();
            }

            return resp;
        }
        public Response UpdateDocumentInAcceptId(int id, string acceptId)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var obj = ctx.DocumentIn.Where(o => o.Id == id).FirstOrDefault();

                    if (obj != null)
                    {


                        obj.AcceptId = acceptId;

                        ctx.SaveChanges();

                        resp.ResponseObject = obj.Id;
                        resp.Status = true;



                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
            }

            return resp;
        }
        public Response AddDocumentIn(DocumentIn docIn)
        {

            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var doc = ctx.DocumentIn.Where(o => o.ProcessId == docIn.ProcessId).FirstOrDefault();

                    if (doc != null)
                    {
                        resp.ResponseObject = doc;
                    }
                    else
                    {
                        if (docIn.DocumentReference != null)
                        {
                            foreach (var item in docIn.DocumentReference)
                            {
                                item.State = "บันทึก";
                                item.Type = 2;
                            }
                        };
                        ctx.DocumentIn.Add(docIn);

                        DocumentProcess docProcess = new DocumentProcess()
                        {
                            CreatedDate = DateTime.Now,
                            DocumentInId = docIn.Id,
                            ProcessId = docIn.ProcessId,
                            Status = "รับหนังสือรอส่งหนังสือตอบรับ"
                        };

                        ctx.DocumentProcess.Add(docProcess);

                        ctx.SaveChanges();
                        resp.ResponseObject = docIn;
                    }

                    resp.Status = true;
                    //resp.Description = "1";

                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
            }

            return resp;
        }
        public Response AddDocument(Document doc)
        {

            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    if (doc.DocumentReference != null)
                    {
                        foreach (var item in doc.DocumentReference)
                        {
                            item.State = "บันทึก";
                        }
                    };
                    if (ctx.Document.Any(o => o.No == doc.No && o.SenderOrganizationId == doc.SenderOrganizationId && o.ReceiverOrganizationId == doc.ReceiverOrganizationId))
                    {
                        resp.Status = false;
                        resp.Description = "เลขที่หนังสือซ้ำ";
                    }
                    else
                    {
                        doc.CreatedDate = DateTime.Now;
                        ctx.Document.Add(doc);
                        ctx.SaveChanges();
                        
                        resp.Status = true;
                       
                        doc.DocumentAttachment = null;
                        doc.DocumentReference = null;
                        
                        resp.ResponseObject = doc;
                    }

                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
            }

            return resp;
        }
        public Response UpdateDocumentAttachment(int id, List<string> fileName, List<byte[]> files)
        {

            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {


                    foreach (var file in fileName.Where(o => !o.Contains("mainFile")))
                    {
                        var i = 0;
                        DocumentAttachment att = new DocumentAttachment()
                        {
                            DocumentId = id,
                            AttachmentName = file,
                            AttachmentBinary = files[i],
                            MimeCode = Util.ConvertContentType(System.IO.Path.GetExtension(file)),
                            State = "บันทึก",
                            Type = "1",
                            FileSize = Util.ConvertBytesToMegabytes(files[i].Length).ToString("N5") + " mb",
                        };
                        ctx.DocumentAttachment.Add(att);
                        i++;
                    }


                    ctx.SaveChanges();
                    resp.Status = true;

                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
            }

            return resp;
        }
        public Response AddListOrganization(List<Organization> organizaions)
        {
            Response resp = new Response();
            try
            {

                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {

                    ctx.Organization.AddRange(organizaions);
                    ctx.SaveChanges();
                    resp.Status = true;

                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
                resp.Exception = ex.ToString();
            }

            return resp;
        }
        public Response DeleteReferenceBook(int id)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var reference = ctx.DocumentReference.Where(o => o.Id == id).FirstOrDefault();
                    if (reference != null)
                    {
                        reference.State = "รอลบ";
                        ctx.SaveChanges();
                        resp.Status = true;
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
                resp.Exception = ex.ToString();
            }

            return resp;
        }
        public Response DeleteDocument(int id)
        {
            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var doc = ctx.Document.Where(o => o.Id == id).FirstOrDefault();
                    if (doc != null)
                    {
                        ctx.Document.Remove(doc);
                        ctx.SaveChanges();
                        resp.Status = true;
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
                resp.Exception = ex.ToString();
            }

            return resp;
        }
        public Response UpdateDocument(Document doc)
        {

            try
            {
                using (DGAMilDocEntities ctx = new DGAMilDocEntities())
                {
                    var document = ctx.Document.Include("DocumentReference").Where(o => o.Id == doc.Id).FirstOrDefault();
                    if (document != null)
                    {
                        document.No = doc.No;
                        document.From = doc.From;
                        document.Type = doc.Type;
                        document.Date = doc.Date;
                        document.Subject = doc.Subject;
                        document.Speed = doc.Speed;
                        document.Secret = doc.Secret;
                        document.Description = doc.Description;
                        document.MainAttachmentName = doc.MainAttachmentName;
                        document.MimeCode = doc.MimeCode;

                        document.SenderPosition = doc.SenderPosition;
                        document.SenderName = doc.SenderName;
                        document.SenderSurname = doc.SenderSurname;

                        document.ReceiverPosition = doc.ReceiverPosition;
                        document.ReceiverName = doc.ReceiverName;
                        document.ReceiverSurname = doc.ReceiverSurname;

                        document.ReceiverOrganizationId = doc.ReceiverOrganizationId;

                    }

                    if (doc.DocumentReference != null)
                    {
                        var newRef = doc.DocumentReference.Where(o => o.State == "เพิ่ม").ToList();
                        foreach (var refer in newRef)
                        {
                            refer.State = "บันทึก";
                            document.DocumentReference.Add(refer);
                        }

                        var deleteRef = doc.DocumentReference.Where(o => o.State == "รอลบ").ToList();
                        foreach (var refer in deleteRef)
                        {

                            var obj = ctx.DocumentReference.Where(o => o.Id == refer.Id).FirstOrDefault();
                            if (obj != null)
                            {
                                ctx.DocumentReference.Remove(obj);
                            }

                        }

                    }


                    var attList = ctx.DocumentAttachment.Where(o => o.DocumentId == document.Id).ToList();
                    foreach (var att in attList)
                    {
                        if (att.State == "รอลบ")
                        {
                            ctx.DocumentAttachment.Remove(att);
                        }
                    }




                    ctx.SaveChanges();
                    resp.Status = true;
                    document.DocumentReference = null;
                    document.DocumentAttachment = null;
                    resp.ResponseObject = document;
                    document.MainAttachmentBinary = null;



                }
            }
            catch (Exception ex)
            {
                resp.Status = false;
                resp.Description = ex.Message;
            }

            return resp;
        }