Ejemplo n.º 1
0
        public ActionResult Save(CRM_RemiderModel model, List <CRM_Remider_EmailParameter_Mapping> detail, List <CRM_Remider_SMSParameter_Mapping> SMSPara)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        CRMNextDateReminderRepository repo = new CRMNextDateReminderRepository(_context);
                        model.NextDateRemind        = repo.GetNextDateRemind(model);
                        _context.Entry(model).State = System.Data.Entity.EntityState.Added;
                        _context.SaveChanges();

                        if (detail != null)
                        {
                            CRM_Remider_EmailParameter_Mapping RemiderEmailPara;
                            foreach (var item in detail)
                            {
                                RemiderEmailPara = new CRM_Remider_EmailParameter_Mapping()
                                {
                                    RemiderId        = model.RemiderId,
                                    EmailTemplateId  = model.EmailTemplateId.Value,
                                    EmailParameterId = item.EmailParameterId,
                                    ValueType        = item.ValueType,
                                    Value            = item.Value
                                };
                                _context.Entry(RemiderEmailPara).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();
                            }
                        }
                        if (SMSPara != null)
                        {
                            CRM_Remider_SMSParameter_Mapping RemiderSMSPara;
                            foreach (var item in SMSPara)
                            {
                                RemiderSMSPara = new CRM_Remider_SMSParameter_Mapping()
                                {
                                    RemiderId      = model.RemiderId,
                                    SMSTemplateId  = model.SMSTemplateId.Value,
                                    SMSParameterId = item.SMSParameterId,
                                    ValueType      = item.ValueType,
                                    Value          = item.Value
                                };
                                _context.Entry(RemiderSMSPara).State = System.Data.Entity.EntityState.Added;
                                _context.SaveChanges();
                            }
                        }

                        ts.Complete();
                        return(Json("success", JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json("Vui lòng kiểm tra lại thông tin không hợp lệ", JsonRequestBehavior.AllowGet));
                }
            }
            catch
            {
                return(Json(Resources.LanguageResource.AddErrorMessage, JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Update(CRM_RemiderModel model, List <CRM_Remider_EmailParameter_Mapping> detail, List <CRM_Remider_SMSParameter_Mapping> SMSPara)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    CRMNextDateReminderRepository repo = new CRMNextDateReminderRepository(_context);
                    model.NextDateRemind        = repo.GetNextDateRemind(model);
                    _context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    _context.SaveChanges();

                    #region B1 : Xoá các Para cũ
                    var Remidermodel = _context.CRM_RemiderModel
                                       .Include(p => p.CRM_Remider_EmailParameter_Mapping)
                                       .Include(p => p.CRM_Remider_SMSParameter_Mapping)
                                       .Where(p => p.RemiderId == model.RemiderId)
                                       .FirstOrDefault();
                    //Xoá EmailPara cũ
                    if (Remidermodel.CRM_Remider_EmailParameter_Mapping.Count > 0)
                    {
                        while (Remidermodel.CRM_Remider_EmailParameter_Mapping.Count > 0)
                        {
                            _context.Entry(Remidermodel.CRM_Remider_EmailParameter_Mapping.First()).State = System.Data.Entity.EntityState.Deleted;
                            _context.SaveChanges();
                        }
                    }
                    //Xoá SMSPara cũ
                    if (Remidermodel.CRM_Remider_SMSParameter_Mapping.Count > 0)
                    {
                        while (Remidermodel.CRM_Remider_SMSParameter_Mapping.Count > 0)
                        {
                            _context.Entry(Remidermodel.CRM_Remider_SMSParameter_Mapping.First()).State = System.Data.Entity.EntityState.Deleted;
                            _context.SaveChanges();
                        }
                    }

                    //Mapper.CreateMap<CRM_RemiderModel, CRM_RemiderModel>();
                    //Remidermodel = Mapper.Map<CRM_RemiderModel>(model);


                    #endregion

                    #region Insert Para mới
                    if (detail != null)
                    {
                        CRM_Remider_EmailParameter_Mapping RemiderEmailPara;
                        foreach (var item in detail)
                        {
                            RemiderEmailPara = new CRM_Remider_EmailParameter_Mapping()
                            {
                                RemiderId        = model.RemiderId,
                                EmailTemplateId  = model.EmailTemplateId.Value,
                                EmailParameterId = item.EmailParameterId,
                                ValueType        = item.ValueType,
                                Value            = item.Value
                            };
                            _context.Entry(RemiderEmailPara).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                        }
                    }
                    if (SMSPara != null)
                    {
                        CRM_Remider_SMSParameter_Mapping RemiderSMSPara;
                        foreach (var item in SMSPara)
                        {
                            RemiderSMSPara = new CRM_Remider_SMSParameter_Mapping()
                            {
                                RemiderId      = model.RemiderId,
                                SMSTemplateId  = model.SMSTemplateId.Value,
                                SMSParameterId = item.SMSParameterId,
                                ValueType      = item.ValueType,
                                Value          = item.Value
                            };
                            _context.Entry(RemiderSMSPara).State = System.Data.Entity.EntityState.Added;
                            _context.SaveChanges();
                        }
                    }
                    #endregion

                    ts.Complete();
                    return(Json("success", JsonRequestBehavior.AllowGet));
                }
            }
            catch
            {
                return(Json(Resources.LanguageResource.EditErrorMessage, JsonRequestBehavior.AllowGet));
            }
        }