private Negotiator UpdateCampaign(ReqCampaignUpdate req) { try { var info = req.info; var camp = req.campaign_id > 0 ? this.CampaignService.GetCampaignById(req.campaign_id, this.GetCurrentUser().AccountCompanyID) : null; var stepInfoList = this.CampaignStepInfoService.GetCampaignStepInfoList(); var sms = this.SMSService.GetSMSByCampaignID(camp.CampaignID); TaInfo ta = null; if (info != null) { try { if (info.date_start > info.date_end || camp == null) { this.Log.Error("info:campaign_id在campaign_info查無資料"); return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.不正確的參數, string.Empty, "info:campaign_id在campaign_info查無資料")); } if (!(DateTime.Now > camp.DateStart)) { camp.DateStart = info.date_start == DateTime.MinValue ? camp.DateStart : info.date_start; } camp.Name = string.IsNullOrEmpty(info.campaign_name) ? camp.Name : info.campaign_name; camp.Description = string.IsNullOrEmpty(info.description) ? camp.Description : info.description; camp.DateEnd = info.date_end == DateTime.MinValue ? camp.DateEnd : info.date_end; camp.TypeID = info.type_id; camp.CampaignTag.ToList().ForEach(q => { this.CampaignTagMappingService.DeleteCampaignTagMapping(q.CampaignTagID, this.GetCurrentUser().AccountCompanyID); }); info.tag_info.ToList().ForEach(q => { var mapping = new Yoda.Core.Model.EF.CampaignTagMapping { }; mapping.CampaignID = camp.CampaignID; if (q.tag_id > 0) { var item = this.TagInfoService.FindById(q.tag_id, this.GetCurrentUser().AccountCompanyID); // 應該不會有不存在的時候...除非前端爆掉... if (item == null || this.CampaignTagMappingService.CampaignMappingTagExist(camp.CampaignID, q.tag_id, this.GetCurrentUser().AccountCompanyID)) { return; } mapping.TagID = item.TagID; } else { var item = new TagInfo { TagName = q.tag_name, CompanyId = this.GetCurrentUser().AccountCompanyID, DateCreated = DateTime.Now }; try { this.TagInfoService.AddTagInfo(item); mapping.TagID = item.TagID; } catch (Exception ex) { this.Log.Fatal(ex.ToString()); } } if (mapping.TagID > 0) { this.CampaignTagMappingService.AddCampaignTag(mapping); } }); if (req.config != null) { camp.CampStatus = req.config.camp_status; } } catch (Exception) { } } else if (req.ta != null) { ta = this.TaInfoService.GetTaInfoById(req.ta.ta_id, this.GetCurrentUser().AccountCompanyID); if (ta != null) { if (!camp.TaID.HasValue) { if (stepInfoList.FirstOrDefault(q => q.Id == camp.StepID).Sequence < stepInfoList.FirstOrDefault(q => q.Id == "ta").Sequence) { camp.StepID = "ta"; } } else { if (stepInfoList.FirstOrDefault(q => q.Id == camp.StepID).Sequence >= stepInfoList.FirstOrDefault(q => q.Id == "schedule").Sequence) { var campTa = this.TaInfoService.GetTaInfoById(camp.TaID.Value, this.GetCurrentUser().AccountCompanyID); if (campTa.SendPeriod != ta.SendPeriod && camp.TypeID == 2) { camp.StepID = "channel"; var tempSms = sms.FirstOrDefault(); tempSms.PeriodType = null; tempSms.PeriodDay = null; tempSms.PeriodTime = null; tempSms.Rate = null; tempSms.RateUnit = 0; tempSms.RepeatMode = 0; } } } camp.TaID = ta.TaID; } } else if (req.channel != null && req.channel.Any()) { var checkReturn = false; req.channel.ForEach(channel => { if (checkReturn) { return; } if (channel.channel_id != 1) { this.Log.Error("channel:channel_id<>1"); } if (!string.IsNullOrEmpty(channel.source_content)) { var channel_temp = this.ChannelInfoService.FindByID(channel.channel_id); if (channel_temp != null) { var tempSms = sms.FirstOrDefault(q => q.SMSID == channel.sms_id); tempSms.SourceName = channel.source_name; tempSms.SourceContent = channel.source_content; tempSms.DateModified = DateTime.Now; tempSms.OTO = channel.oto ?? 1; tempSms.Tested = false; int code = 0; string debug = string.Empty; this.SMSService.SetSMS(tempSms, out code, out debug, this.GetCurrentUser().AccountCompanyID); if (stepInfoList.FirstOrDefault(q => q.Id == camp.StepID).Sequence < stepInfoList.FirstOrDefault(q => q.Id == "channel").Sequence) { camp.StepID = "channel"; } } else { checkReturn = true; return; } } else { this.Log.Info("Source Content 不能為 Null"); } }); if (checkReturn) { return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.不正確的參數, string.Empty)); } } else if (req.schedule != null && req.schedule.Any()) { req.schedule.ForEach(schedule => { if (!(schedule.date_start.HasValue && schedule.date_start.Value >= camp.DateStart)) { throw new Exception("schedule:date_start必須大於等於campaign.date_start"); } if (schedule.date_end.HasValue && schedule.date_end.Value <= camp.DateEnd) { var tempSms = sms.FirstOrDefault(q => q.SMSID == schedule.sms_id); tempSms.DateStart = schedule.date_start; tempSms.DateEnd = schedule.date_end; tempSms.PeriodType = schedule.period_type; tempSms.PeriodDay = schedule.period_day; tempSms.PeriodTime = schedule.period_time; tempSms.VendorID = schedule.vendor_id; tempSms.Rate = schedule.rate; tempSms.RateUnit = schedule.rate_unit; tempSms.RepeatMode = schedule.repeatmode; tempSms.DateModified = DateTime.Now; int code = 0; string debug = string.Empty; this.SMSService.SetSMS(tempSms, out code, out debug, this.GetCurrentUser().AccountCompanyID); if (stepInfoList.FirstOrDefault(q => q.Id == camp.StepID).Sequence < stepInfoList.FirstOrDefault(q => q.Id == "schedule").Sequence) { camp.StepID = "schedule"; } } else { throw new Exception("schedule:date_end必須小於等於campaign.date_end"); } }); } if (req.channel != null && req.channel.Any(q => q.test.HasValue && q.test.Value)) { req.channel.Where(q => q.channel_id == 1).ToList().ForEach(channel => { if (stepInfoList.FirstOrDefault(q => q.Id == camp.StepID).Sequence < stepInfoList.FirstOrDefault(q => q.Id == "test").Sequence) { camp.StepID = "test"; } }); } sms.ForEach(s => { this.SMSService.UpdateSms(s, this.GetCurrentUser().AccountCompanyID); }); this.CampaignService.UpdateCampaign(camp, this.GetCurrentUser().AccountCompanyID); if (ta != null && ta.TaCategoryID == TaCategory.DefTaCategoryId.Member) { // create ta job var job = new SystemJobInfo(); job.CompanyID = this.GetCurrentUser().AccountCompanyID; job.CampaignID = req.campaign_id; job.Type = SystemJobInfo.DefJobType.CreateTa; job.Status = SystemJobInfo.DefJobStatus.Waiting; job.Entity1 = camp.CampaignID.ToString(); this.SystemJobInfoService.CreateJob(job); } } catch (Exception) { return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.不正確的參數)); } return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.OK)); }
private Negotiator AddCampaign(ReqAddCampaign req) { if (req.date_start.Date >= req.date_end.Date || req.date_start.Date < DateTime.Now.Date) { return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.不正確的參數)); } var newCamp = new Campaign { TypeID = req.type_id, Name = req.campaign_name, Description = req.description, DateStart = req.date_start, DateEnd = req.date_end, AcctCompanyID = this.GetCurrentUser().AccountCompanyID, StepID = "info", CampStatus = CampStatus.設定中 }; try { //Updated by Action_Chen 20150805 //EventHandling & Logger框架程式範例 int result = this.CampaignService.AddCampaign(newCamp, this.GetCurrentUser()); var createTagInfoList = new List<TagInfo>(); req.tag_info.ToList().ForEach(q => { var tag = new TagInfo { TagID = q.tag_id, TagName = q.tag_name, CompanyId = this.GetCurrentUser().AccountCompanyID }; if (q.tag_id == 0) { try { this.TagInfoService.AddTagInfo(tag); } catch (Exception) { } } if (!this.CampaignTagMappingService.CampaignMappingTagExist(newCamp.CampaignID, tag.TagID, this.GetCurrentUser().AccountCompanyID)) { if (tag.TagID == 0) { return; } var temp = new Yoda.Core.Model.EF.CampaignTagMapping(); temp.CampaignID = newCamp.CampaignID; temp.TagID = tag.TagID; this.CampaignTagMappingService.AddCampaignTag(temp); } }); this.CampaignKeywordSettingService.AddCampaignWithSetting(newCamp.CampaignID, this.GetCurrentUser().AccountCompanyID); if (result > 0) { return Negotiate.WithOnlyJson(new RspCampaignAdd(CustomStatusCode.OK, result)); } else { return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.DB發生錯誤)); } } //Updated by Action_Chen 20150805 //EventHandling & Logger框架程式範例 //catch (MigoDbUpdateException ex) catch (MigoCommitFailedException ex) { //在/Helper/BaseWebModule.cs宣告物件變數 baseLogger.Error(ex); //回傳code, message訊息給前端 return Negotiate.WithOnlyJson(new RspFrame(CustomStatusCode.不正確的參數)); } }