public void ActionSaveNew(FormBaoCaoBanDauSuCoModel model, MInfoMagicModelForm modelInfo, MAppendForm append)
 {
     if (ValidSave(model, modelInfo, append))
     {
         SaveNewRedirect(model.RecordID, entity.ID);
     }
 }
 public void ActionSave(FormBaoCaoBanDauSuCoModel model, MInfoMagicModelForm modelInfo, MAppendForm append)
 {
     if (ValidSave(model, modelInfo, append))
     {
         SaveRedirect();
     }
 }
        private bool ValidSave(FormBaoCaoBanDauSuCoModel model, MInfoMagicModelForm modelInfo, MAppendForm append)
        {
            TryUpdateModel(entity);

            //chong hack
            entity.ID = model.RecordID;

            ViewBag.Data  = entity;
            ViewBag.Model = model;

            CPViewPage.Message.MessageType = Message.MessageTypeEnum.Error;

            //kiem tra quyen han
            if ((model.RecordID < 1 && !CPViewPage.UserPermissions.Add) || (model.RecordID > 0 && !CPViewPage.UserPermissions.Edit))
            {
                CPViewPage.Message.ListMessage.Add("Quyền hạn chế.");
            }

            //kiem tra ten
            if (entity.Name.Trim() == string.Empty)
            {
                CPViewPage.Message.ListMessage.Add("Nhập tên.");
            }

            //kiem tra chuyen muc
            //if (entity.MenuID < 1)
            //    CPViewPage.Message.ListMessage.Add("Chọn chuyên mục.");

            if (CPViewPage.Message.ListMessage.Count == 0)
            {
                //neu khong nhap code -> tu sinh
                if (entity.Code.Trim() == string.Empty)
                {
                    entity.Code = Data.GetCode(entity.Name);
                }

                //cap nhat state
                entity.State = GetState(model.ArrState);

                //save
                int id = ModBaoCaoBanDauSuCoService.Instance.Save(entity);
                if (model.RecordID > 0)
                {
                    UpdateOther(entity, modelInfo, append);
                }
                else
                {
                    SaveOther(id, modelInfo, append);
                }

                return(true);
            }

            return(false);
        }
        public void UpdateOther(ModBaoCaoBanDauSuCoEntity entityBc, MInfoMagicModelForm modelInfo, MAppendForm append)
        {
            int userId = HL.Lib.Global.CPLogin.UserID;
            var entity = ModBaoCaoBanDauSuCoService.Instance.CreateQuery()
                         //.Where(userId > 0, o => o.UserID == userId)
                         .Where(o => o.Code == entityBc.Code)
                         .ToSingle();

            if (entity != null)
            {
                DateTime date             = DateTime.Now;
                string   ngayGioPhatHien  = append.Ngay + " " + append.GioPhut;
                string[] arr              = append.ThoiGian.Split('/');
                string   thoiGianThucHien = "";
                if (arr.Length == 5)
                {
                    thoiGianThucHien = arr[0] + "/" + arr[1] + "/" + arr[2] + " " + arr[3] + ":" + arr[4];
                }
                if (!string.IsNullOrEmpty(ngayGioPhatHien))
                {
                    entityBc.ChiTiet_NgayGioPhatHien = HL.Core.Global.Convert.ToDateTime(ngayGioPhatHien);
                }
                else
                {
                    entityBc.ChiTiet_NgayGioPhatHien = DateTime.MinValue;
                }
                if (!string.IsNullOrEmpty(thoiGianThucHien))
                {
                    entityBc.ThoiGianThucHien = HL.Core.Global.Convert.ToDateTime(thoiGianThucHien);
                }
                else
                {
                    entityBc.ThoiGianThucHien = DateTime.MinValue;
                }

                entityBc.ID         = entity.ID;
                entityBc.UserID     = entity.UserID;
                entityBc.UserID1    = userId;
                entityBc.MenuID     = entity.MenuID;
                entityBc.State      = entity.State;
                entityBc.Name       = entity.Name;
                entityBc.Code       = entity.Code;
                entityBc.Order      = entity.Order;
                entityBc.Published  = entity.Published;
                entityBc.Published1 = date;
                entityBc.Activity   = false;
                ModBaoCaoBanDauSuCoService.Instance.Save(entityBc);

                //Xoa toan bo thong tin InfoMagic cua Bao cao ban dau su co
                var lstInfoMagic = ModInfoMagicService.Instance.CreateQuery()
                                   .Where(o => o.BaoCaoBanDauSuCoID == entity.ID)
                                   .ToList();
                if (lstInfoMagic != null)
                {
                    ModInfoMagicService.Instance.Delete(lstInfoMagic);
                }

                //Cach thuc phat hien
                int num = modelInfo.chkCachThuc != null ? modelInfo.chkCachThuc.Length : 0;
                for (int i = 0; i < num; i++)
                {
                    string[] tmp = modelInfo.chkCachThuc[i].Split('_');
                    if (tmp.Length == 3)
                    {
                        ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                        {
                            BaoCaoBanDauSuCoID = entity.ID,
                            Order     = GetMaxOrder_InfoMagic(),
                            Published = date,
                            Activity  = true
                        };
                        entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                        if (tmp[0] == "1")
                        {
                            int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                            if (modelInfo.txtCachThuc != null && modelInfo.txtCachThuc.Length >= idx)
                            {
                                entityTmp.Name = modelInfo.txtCachThuc[idx];
                                entityTmp.Code = Data.GetCode(modelInfo.txtCachThuc[idx]);
                            }
                        }
                        ModInfoMagicService.Instance.Save(entityTmp);
                    }
                }

                //Da gui thong bao su co
                num = modelInfo.chkThongBao != null ? modelInfo.chkThongBao.Length : 0;
                for (int i = 0; i < num; i++)
                {
                    string[] tmp = modelInfo.chkThongBao[i].Split('_');
                    if (tmp.Length == 3)
                    {
                        ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                        {
                            BaoCaoBanDauSuCoID = entity.ID,
                            Order     = GetMaxOrder_InfoMagic(),
                            Published = date,
                            Activity  = true
                        };
                        entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                        if (tmp[0] == "1")
                        {
                            int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                            if (modelInfo.txtThongBao != null && modelInfo.txtThongBao.Length >= idx)
                            {
                                entityTmp.Name = modelInfo.txtThongBao[idx];
                                entityTmp.Code = Data.GetCode(modelInfo.txtThongBao[idx]);
                            }
                        }
                        ModInfoMagicService.Instance.Save(entityTmp);
                    }
                }

                //Dich vu
                num = modelInfo.chkDichVu != null ? modelInfo.chkDichVu.Length : 0;
                for (int i = 0; i < num; i++)
                {
                    string[] tmp = modelInfo.chkDichVu[i].Split('_');
                    if (tmp.Length == 3)
                    {
                        ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                        {
                            BaoCaoBanDauSuCoID = entity.ID,
                            Order     = GetMaxOrder_InfoMagic(),
                            Published = date,
                            Activity  = true
                        };
                        entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                        if (tmp[0] == "1")
                        {
                            int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                            if (modelInfo.txtDichVu != null && modelInfo.txtDichVu.Length >= idx)
                            {
                                entityTmp.Name = modelInfo.txtDichVu[idx];
                                entityTmp.Code = Data.GetCode(modelInfo.txtDichVu[idx]);
                            }
                        }
                        ModInfoMagicService.Instance.Save(entityTmp);
                    }
                }

                //Bien phap
                num = modelInfo.chkBienPhap != null ? modelInfo.chkBienPhap.Length : 0;
                for (int i = 0; i < num; i++)
                {
                    string[] tmp = modelInfo.chkBienPhap[i].Split('_');
                    if (tmp.Length == 3)
                    {
                        ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                        {
                            BaoCaoBanDauSuCoID = entity.ID,
                            Order     = GetMaxOrder_InfoMagic(),
                            Published = date,
                            Activity  = true
                        };
                        entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                        if (tmp[0] == "1")
                        {
                            int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                            if (modelInfo.txtBienPhap != null && modelInfo.txtBienPhap.Length >= idx)
                            {
                                entityTmp.Name = modelInfo.txtBienPhap[idx];
                                entityTmp.Code = Data.GetCode(modelInfo.txtBienPhap[idx]);
                            }
                        }
                        ModInfoMagicService.Instance.Save(entityTmp);
                    }
                }

                //Thong tin gui kem
                num = modelInfo.chkThongTinGuiKem != null ? modelInfo.chkThongTinGuiKem.Length : 0;
                for (int i = 0; i < num; i++)
                {
                    string[] tmp = modelInfo.chkThongTinGuiKem[i].Split('_');
                    if (tmp.Length == 3)
                    {
                        ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                        {
                            BaoCaoBanDauSuCoID = entity.ID,
                            Order     = GetMaxOrder_InfoMagic(),
                            Published = date,
                            Activity  = true
                        };
                        entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                        if (tmp[0] == "1")
                        {
                            int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                            if (modelInfo.txtThongTinGuiKem != null && modelInfo.txtThongTinGuiKem.Length >= idx)
                            {
                                entityTmp.Name = modelInfo.txtThongTinGuiKem[idx];
                                entityTmp.Code = Data.GetCode(modelInfo.txtThongTinGuiKem[idx]);
                            }
                        }
                        ModInfoMagicService.Instance.Save(entityTmp);
                    }
                }

                ViewBag.BaoCao = entityBc;
            }
        }
        private void SaveOther(int id, MInfoMagicModelForm modelInfo, MAppendForm append)
        {
            DateTime date            = DateTime.Now;
            string   ngayGioPhatHien = append.Ngay + " " + append.GioPhut;

            string[] arr = append.ThoiGian.Split('/');
            string   thoiGianThucHien = "";

            if (arr.Length == 5)
            {
                thoiGianThucHien = arr[0] + "/" + arr[1] + "/" + arr[2] + " " + arr[3] + ":" + arr[4];
            }
            string code = "BCBDSC" + ModBaoCaoBanDauSuCoService.Instance.GetMaxID();

            //Cach thuc phat hien
            int num = modelInfo.chkCachThuc != null ? modelInfo.chkCachThuc.Length : 0;

            for (int i = 0; i < num; i++)
            {
                string[] tmp = modelInfo.chkCachThuc[i].Split('_');
                if (tmp.Length == 3)
                {
                    ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                    {
                        BaoCaoBanDauSuCoID = id,
                        Order     = GetMaxOrder_InfoMagic(),
                        Published = date,
                        Activity  = true
                    };
                    entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                    if (tmp[0] == "1")
                    {
                        int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                        if (modelInfo.txtCachThuc != null && modelInfo.txtCachThuc.Length >= idx)
                        {
                            entityTmp.Name = modelInfo.txtCachThuc[idx];
                            entityTmp.Code = Data.GetCode(modelInfo.txtCachThuc[idx]);
                        }
                    }
                    ModInfoMagicService.Instance.Save(entityTmp);
                }
            }

            //Da gui thong bao su co
            num = modelInfo.chkThongBao != null ? modelInfo.chkThongBao.Length : 0;
            for (int i = 0; i < num; i++)
            {
                string[] tmp = modelInfo.chkThongBao[i].Split('_');
                if (tmp.Length == 3)
                {
                    ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                    {
                        BaoCaoBanDauSuCoID = id,
                        Order     = GetMaxOrder_InfoMagic(),
                        Published = date,
                        Activity  = true
                    };
                    entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                    if (tmp[0] == "1")
                    {
                        int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                        if (modelInfo.txtThongBao != null && modelInfo.txtThongBao.Length >= idx)
                        {
                            entityTmp.Name = modelInfo.txtThongBao[idx];
                            entityTmp.Code = Data.GetCode(modelInfo.txtThongBao[idx]);
                        }
                    }
                    ModInfoMagicService.Instance.Save(entityTmp);
                }
            }

            //Dich vu
            num = modelInfo.chkDichVu != null ? modelInfo.chkDichVu.Length : 0;
            for (int i = 0; i < num; i++)
            {
                string[] tmp = modelInfo.chkDichVu[i].Split('_');
                if (tmp.Length == 3)
                {
                    ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                    {
                        BaoCaoBanDauSuCoID = id,
                        Order     = GetMaxOrder_InfoMagic(),
                        Published = date,
                        Activity  = true
                    };
                    entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                    if (tmp[0] == "1")
                    {
                        int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                        if (modelInfo.txtDichVu != null && modelInfo.txtDichVu.Length >= idx)
                        {
                            entityTmp.Name = modelInfo.txtDichVu[idx];
                            entityTmp.Code = Data.GetCode(modelInfo.txtDichVu[idx]);
                        }
                    }
                    ModInfoMagicService.Instance.Save(entityTmp);
                }
            }

            //Bien phap
            num = modelInfo.chkBienPhap != null ? modelInfo.chkBienPhap.Length : 0;
            for (int i = 0; i < num; i++)
            {
                string[] tmp = modelInfo.chkBienPhap[i].Split('_');
                if (tmp.Length == 3)
                {
                    ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                    {
                        BaoCaoBanDauSuCoID = id,
                        Order     = GetMaxOrder_InfoMagic(),
                        Published = date,
                        Activity  = true
                    };
                    entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                    if (tmp[0] == "1")
                    {
                        int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                        if (modelInfo.txtBienPhap != null && modelInfo.txtBienPhap.Length >= idx)
                        {
                            entityTmp.Name = modelInfo.txtBienPhap[idx];
                            entityTmp.Code = Data.GetCode(modelInfo.txtBienPhap[idx]);
                        }
                    }
                    ModInfoMagicService.Instance.Save(entityTmp);
                }
            }

            //Thong tin gui kem
            num = modelInfo.chkThongTinGuiKem != null ? modelInfo.chkThongTinGuiKem.Length : 0;
            for (int i = 0; i < num; i++)
            {
                string[] tmp = modelInfo.chkThongTinGuiKem[i].Split('_');
                if (tmp.Length == 3)
                {
                    ModInfoMagicEntity entityTmp = new ModInfoMagicEntity()
                    {
                        BaoCaoBanDauSuCoID = id,
                        Order     = GetMaxOrder_InfoMagic(),
                        Published = date,
                        Activity  = true
                    };
                    entityTmp.MenuID = HL.Core.Global.Convert.ToInt(tmp[2]);
                    if (tmp[0] == "1")
                    {
                        int idx = HL.Core.Global.Convert.ToInt(tmp[1]);
                        if (modelInfo.txtThongTinGuiKem != null && modelInfo.txtThongTinGuiKem.Length >= idx)
                        {
                            entityTmp.Name = modelInfo.txtThongTinGuiKem[idx];
                            entityTmp.Code = Data.GetCode(modelInfo.txtThongTinGuiKem[idx]);
                        }
                        ModInfoMagicService.Instance.Save(entityTmp);
                    }
                }
            }

            ViewBag.BaoCao = entity;
        }