private void SaveAdvertisement(HttpContext context) { try { string Id = context.Request.Form["ctl00$cphMain$hId"].Trim(); string title = context.Request.Form["ctl00$cphMain$txtTitle"].Trim(); string sSiteFunId = context.Request.Form["siteFunId"].Trim(); string sLayoutPositionId = context.Request.Form["layoutPositionId"].Trim(); string sTimeout = context.Request.Form["ctl00$cphMain$txtTimeout"].Trim(); string sAdLinkJson = context.Request.Form["adLinkJson"].Trim(); sAdLinkJson = sAdLinkJson.Trim(','); int timeout = 0; if (!string.IsNullOrWhiteSpace(sTimeout)) { int.TryParse(sTimeout, out timeout); } Guid siteFunId = Guid.Empty; Guid.TryParse(sSiteFunId, out siteFunId); Guid layoutPositionId = Guid.Empty; Guid.TryParse(sLayoutPositionId, out layoutPositionId); string sDescr = context.Request.Form["ctl00$cphMain$txtaDescr"].Trim(); string content = context.Request.Form["content"].Trim(); content = HttpUtility.HtmlDecode(content); Guid gId = Guid.Empty; if (!string.IsNullOrWhiteSpace(Id)) { Guid.TryParse(Id, out gId); } AdvertisementInfo model = new AdvertisementInfo(); model.Id = gId; model.LastUpdatedDate = DateTime.Now; model.Title = title; model.Timeout = timeout; model.SiteFunId = siteFunId; model.LayoutPositionId = layoutPositionId; AdvertisementItemInfo adiModel = null; if (gId.Equals(Guid.Empty)) { if (!(string.IsNullOrWhiteSpace(sDescr) && string.IsNullOrWhiteSpace(content))) { adiModel = new AdvertisementItemInfo(); adiModel.Descr = sDescr; adiModel.ContentText = content; } } else { adiModel = new AdvertisementItemInfo(); adiModel.Descr = sDescr; adiModel.ContentText = content; adiModel.AdvertisementId = gId; } IList <AdvertisementLinkInfo> listAdLink = null; if (!string.IsNullOrWhiteSpace(sAdLinkJson)) { DataTable dtPostData = JsonConvert.DeserializeObject <DataTable>(sAdLinkJson); if (dtPostData != null && dtPostData.Rows.Count > 0) { listAdLink = new List <AdvertisementLinkInfo>(); foreach (DataRow dr in dtPostData.Rows) { AdvertisementLinkInfo postAdlModel = new AdvertisementLinkInfo(); postAdlModel.Id = dr["AdLinkId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["AdLinkId"].ToString()); postAdlModel.AdvertisementId = gId; postAdlModel.ContentPictureId = dr["ContentPictureId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ContentPictureId"].ToString()); postAdlModel.ActionTypeId = dr["ActionTypeId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ActionTypeId"].ToString()); postAdlModel.Url = dr["Url"] == DBNull.Value ? "" : dr["Url"].ToString(); postAdlModel.Sort = dr["Sort"] == DBNull.Value ? 0 : int.Parse(dr["Sort"].ToString()); postAdlModel.IsDisable = dr["IsDisable"] == DBNull.Value ? false : bool.Parse(dr["IsDisable"].ToString()); listAdLink.Add(postAdlModel); } } } Advertisement bll = new Advertisement(); AdvertisementItem aiBll = new AdvertisementItem(); AdvertisementLink adlBll = new AdvertisementLink(); int effect = -1; if (!gId.Equals(Guid.Empty)) { using (TransactionScope scope = new TransactionScope()) { bll.Update(model); aiBll.Update(adiModel); if (listAdLink != null) { foreach (var adlModel in listAdLink) { adlBll.Update(adlModel); } } scope.Complete(); effect = 1; } } else { using (TransactionScope scope = new TransactionScope()) { Guid adId = bll.InsertByOutput(model); if (listAdLink != null && listAdLink.Count > 0) { foreach (var adlModel in listAdLink) { adlModel.AdvertisementId = adId; adlBll.Insert(adlModel); } } if (adiModel != null) { adiModel.AdvertisementId = adId; aiBll.Insert(adiModel); } scope.Complete(); effect = 1; } } if (effect < 1) { context.Response.Write("{\"success\": false,\"message\": \"操作失败,请正确输入\"}"); return; } context.Response.Write("{\"success\": true,\"message\": \"操作成功\"}"); } catch (Exception ex) { context.Response.Write("{\"success\": false,\"message\": \"异常:" + ex.Message + "\"}"); } }
private void SaveAdvertisement(HttpContext context) { try { string Id = context.Request.Form["ctl00$cphMain$hId"].Trim(); string title = context.Request.Form["ctl00$cphMain$txtTitle"].Trim(); string sSiteFunId = context.Request.Form["siteFunId"].Trim(); string sLayoutPositionId = context.Request.Form["layoutPositionId"].Trim(); string sTimeout = context.Request.Form["ctl00$cphMain$txtTimeout"].Trim(); string sSort = context.Request.Form["ctl00$cphMain$txtSort"].Trim(); string sStartTime = context.Request.Form["ctl00$cphMain$txtStartTime"].Trim(); string sEndTime = context.Request.Form["ctl00$cphMain$txtEndTime"].Trim(); string sVirtualViewCount = context.Request.Form["ctl00$cphMain$txtVirtualViewCount"].Trim(); string sIsDisable = context.Request.Form["isDisable"].Trim(); string sAdLinkJson = context.Request.Form["adLinkJson"].Trim(); sAdLinkJson = sAdLinkJson.Trim(','); int timeout = 0; if (!string.IsNullOrWhiteSpace(sTimeout)) { int.TryParse(sTimeout, out timeout); } Guid siteFunId = Guid.Empty; Guid.TryParse(sSiteFunId, out siteFunId); Guid layoutPositionId = Guid.Empty; Guid.TryParse(sLayoutPositionId, out layoutPositionId); int sort = 0; if (!string.IsNullOrWhiteSpace(sSort)) { int.TryParse(sSort, out sort); } int virtualViewCount = 0; if (!string.IsNullOrWhiteSpace(sVirtualViewCount)) { int.TryParse(sVirtualViewCount, out virtualViewCount); } DateTime startTime = DateTime.MinValue; if (!string.IsNullOrWhiteSpace(sStartTime)) { DateTime.TryParse(sStartTime, out startTime); } DateTime endTime = DateTime.MinValue; if (!string.IsNullOrWhiteSpace(sEndTime)) { DateTime.TryParse(sEndTime, out endTime); } string sDescr = context.Request.Form["ctl00$cphMain$txtaDescr"].Trim(); string content = context.Request.Form["content"].Trim(); content = HttpUtility.HtmlDecode(content); Guid gId = Guid.Empty; if (!string.IsNullOrWhiteSpace(Id)) { Guid.TryParse(Id, out gId); } AdvertisementInfo model = new AdvertisementInfo(); model.Id = gId; model.LastUpdatedDate = DateTime.Now; model.Title = title; model.Timeout = timeout; model.Sort = sort; model.StartTime = startTime; model.EndTime = endTime; model.VirtualViewCount = virtualViewCount; model.SiteFunId = siteFunId; model.LayoutPositionId = layoutPositionId; model.IsDisable = sIsDisable == "1" ? true : false; AdvertisementItemInfo adiModel = null; if (gId.Equals(Guid.Empty)) { if (!(string.IsNullOrWhiteSpace(sDescr) && string.IsNullOrWhiteSpace(content))) { adiModel = new AdvertisementItemInfo(); adiModel.Descr = sDescr; adiModel.ContentText = content; } } else { adiModel = new AdvertisementItemInfo(); adiModel.Descr = sDescr; adiModel.ContentText = content; adiModel.AdvertisementId = gId; } IList <AdvertisementLinkInfo> listAdLink = null; if (!string.IsNullOrWhiteSpace(sAdLinkJson)) { DataTable dtPostData = JsonConvert.DeserializeObject <DataTable>(sAdLinkJson); if (dtPostData != null && dtPostData.Rows.Count > 0) { listAdLink = new List <AdvertisementLinkInfo>(); foreach (DataRow dr in dtPostData.Rows) { AdvertisementLinkInfo postAdlModel = new AdvertisementLinkInfo(); postAdlModel.Id = dr["AdLinkId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["AdLinkId"].ToString()); postAdlModel.AdvertisementId = gId; postAdlModel.ContentPictureId = dr["ContentPictureId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ContentPictureId"].ToString()); postAdlModel.ActionTypeId = dr["ActionTypeId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ActionTypeId"].ToString()); postAdlModel.Url = dr["Url"] == DBNull.Value ? "" : dr["Url"].ToString(); postAdlModel.Sort = dr["Sort"] == DBNull.Value ? 0 : int.Parse(dr["Sort"].ToString()); postAdlModel.IsDisable = dr["IsDisable"] == DBNull.Value ? false : bool.Parse(dr["IsDisable"].ToString()); listAdLink.Add(postAdlModel); } } } Advertisement bll = new Advertisement(); AdvertisementItem aiBll = new AdvertisementItem(); AdvertisementLink adlBll = new AdvertisementLink(); int effect = -1; List <AdvertisementLinkInfo> oldListAdLink = null; if (!gId.Equals(Guid.Empty)) { oldListAdLink = adlBll.GetListByAdId(gId).ToList <AdvertisementLinkInfo>(); } using (TransactionScope scope = new TransactionScope()) { if (!gId.Equals(Guid.Empty)) { bll.Update(model); aiBll.Update(adiModel); if (listAdLink != null) { foreach (var adlModel in listAdLink) { if (oldListAdLink != null && oldListAdLink.Count > 0) { var delModel = oldListAdLink.Find(m => m.Id.ToString() == adlModel.Id.ToString()); if (delModel != null) { oldListAdLink.Remove(delModel); adlBll.Update(adlModel); } else { adlBll.Insert(adlModel); } } else { adlBll.Insert(adlModel); } } } if (oldListAdLink != null && oldListAdLink.Count > 0) { IList <object> adlIdList = new List <object>(); foreach (var oldModel in oldListAdLink) { adlIdList.Add(oldModel.Id); } adlBll.DeleteBatch(adlIdList); } effect = 1; } else { Guid adId = Guid.NewGuid(); model.Id = adId; effect = bll.Insert(model); if (listAdLink != null && listAdLink.Count > 0) { foreach (var adlModel in listAdLink) { adlModel.AdvertisementId = adId; adlBll.Insert(adlModel); } } if (adiModel != null) { adiModel.AdvertisementId = adId; aiBll.Insert(adiModel); } } scope.Complete(); } if (effect == 110) { context.Response.Write("{\"success\": false,\"message\": \"" + MessageContent.Submit_Exist + "\"}"); return; } if (effect < 1) { context.Response.Write("{\"success\": false,\"message\": \"操作失败,请正确输入\"}"); return; } context.Response.Write("{\"success\": true,\"message\": \"操作成功\"}"); } catch (Exception ex) { context.Response.Write("{\"success\": false,\"message\": \"异常:" + ex.Message + "\"}"); } }