public void Execute(IJobExecutionContext context) { try { Console.WriteLine("RemoveMoneyJob is starting....." + DateTime.Now.ToString()); Log.Info("RemoveMoneyJob запущен!"); List<GetBillsecForClientInfo> BillsecForClientInfoList_Curent = new List<GetBillsecForClientInfo>(); List<GetPhonesCountForClientInfo> PhonesCountForClientInfoList_Curent = new List<GetPhonesCountForClientInfo>(); ////выбираем дату последнего успешного запуска Job-а ( Начало ) DateTime lastactiondate = new DateTime(); JobsInfo JobsInfoQuery = _jobsinfoRep.GetLastActionDateForJob(Constants.RemoveMoneyJob); if (JobsInfoQuery != null) lastactiondate = JobsInfoQuery.lastactiondate; // выбираем дату последнего успешного запуска Job-а ( Конец ) else lastactiondate = DateTime.Parse("2013-03-15"); double numbercostforday = 0; DateTime FinishDate = /*GetDateFromFile()[1];*/DateTime.Now.AddHours(-1).ToUniversalTime(); DateTime StartDate = /*GetDateFromFile()[0];*/ lastactiondate; DateTime CurentDate = StartDate; MinutesCost lastminutecostdate = null; NumberCost lastnumbercostdate = null; //StartDate = DateTime.Parse("29.04.2013 23:00:00"); //CurentDate = StartDate; //FinishDate = DateTime.Parse("30.04.2013 00:00:00").AddHours(-1); while (CurentDate <= FinishDate)//условие <= приводит к двойному выполнению Job-а для одного и того же клиента в сутки { string clients = GetClientsForRemoveMoney_(CurentDate/*DateTime.Parse("30.04.2013 0:00:00")*/);//получаем всех клиентов, для которых нужно списать деньги if (!String.IsNullOrEmpty(clients)) { Console.WriteLine("Диапазон между startdate и finishdate: " + StartDate.ToString() + " - " + FinishDate.ToString()); Console.WriteLine("Грабеж!!!!!!!!! Время грабежа: " + CurentDate.ToString() + " Клиенты: " + clients); List<BillSecForClientsInfo> billsecforclientsinfo = Call_GetBillSecForClients(CurentDate, clients); List<ActivedClientsNumbersCountInfo> activedclientsnumberscountinfo = Call_GetActivedClientsNumbers(CurentDate, clients); lastminutecostdate = _minutescostRep.GetAll().Where(t => t.lastupdate <= CurentDate).OrderByDescending(t => t.lastupdate).FirstOrDefault(); lastnumbercostdate = _numbercostRep.GetAll().Where(t => t.lastupdate <= CurentDate).OrderByDescending(t => t.lastupdate).FirstOrDefault(); List<InfoForPaymants> infoforpaymants = new List<InfoForPaymants>(); var commondata = from b in billsecforclientsinfo from a in activedclientsnumberscountinfo where b.date == a.date && b.ClientId == a.ClientId select new { ClientId = b.ClientId, date = b.date, count = a.count, codes_billsec = b.codes_billsec }; var differentdata = from a in activedclientsnumberscountinfo where !billsecforclientsinfo.Any(t => t.ClientId == a.ClientId && t.date == a.date) select a; if (commondata.Count() > 0) foreach (var item in commondata) infoforpaymants.Add(new InfoForPaymants() { codes_billsec = item.codes_billsec, ClientId = item.ClientId, count = item.count, date = item.date }); if (differentdata.Count() > 0) foreach (var item in differentdata) infoforpaymants.Add(new InfoForPaymants() { codes_billsec = null, count = item.count, date = item.date, ClientId = item.ClientId }); int allbillsec = 0;//общее колическво секунд разговоров для клиента для всех кодов if (infoforpaymants.Count() > 0) { foreach (InfoForPaymants item in infoforpaymants) { numbercostforday = _numbercostRep.GetLastNumberCostToThisDate(item.date);//последняя стоимость номера за день if (item.codes_billsec != null) item.sum = Math.Round(GetCommonBillsecCostForAllCodes(item.codes_billsec) + item.count * numbercostforday, 2); else item.sum = Math.Round(item.count * numbercostforday, 2); if (item.codes_billsec != null) allbillsec = item.codes_billsec.Sum(t => t.billsec); Paymant new_paymant = new Paymant() { ClientId = item.ClientId, date = item.date, Type = false, sum = -1 * item.sum, coment = "Снятие денег со счета. Время разговоров: " + allbillsec + " сек; Количество арендуемых номеров: " + item.count + " Время: " + DateTime.Now.ToString() }; _paymantsRep.Add(new_paymant); _paymantsRep.Save(); Client curent_client = _clientRep.Find(item.ClientId); if (curent_client != null) { string message_ = "Уважаемый " + curent_client.Name + ", " + item.date.ToShortDateString() + " с вашего счета было снято " + item.sum + " грн за " + allbillsec + " секунд разговоров и за аренду телефонов в количестве " + item.count + "."; _sendemailjobinfoRep.Add(new SendEmailJobInfo() { attemptcount = 0, date = DateTime.Now.ToUniversalTime(), message = message_, status = false, subject = "CallTracking Info", To = curent_client.Email }); _sendemailjobinfoRep.Save(); } allbillsec = 0; } } } CurentDate = CurentDate.AddHours(1); } // AddOneHourToDatesFromFile(); JobsInfo new_jobs_info = new JobsInfo() { status = true, lastactiondate = DateTime.Now.ToUniversalTime(), jobId = Constants.RemoveMoneyJob };//поставить еще jobId! ( также в классе JobsInfo ) _jobsinfoRep.Add(new_jobs_info); _jobsinfoRep.Save(); Log.Info("RemoveMoneyJob успешно выполнен!"); Console.WriteLine("RemoveMoneyJob is finished....." + DateTime.Now.ToString()); } catch (Exception ex) { while (ex.InnerException != null) ex = ex.InnerException; /* JobsInfo new_jobs_info = new JobsInfo() { status = false, lastactiondate = DateTime.Now.ToUniversalTime(), jobId = Constants.RemoveMoneyJob, ExceptionText = String.Format(@"Message: {0}, Source: {1}, TargetSite: {2}, Trace: {3} ", ex.Message, ex.Source, ex.TargetSite, ex.StackTrace) };//поставить еще jobId! ( также в классе JobsInfo ) _jobsinfoRep.Add(new_jobs_info); _jobsinfoRep.Save(); Log.Error(String.Format(@"Message: {0}, Source: {1}, TargetSite: {2}, Trace: {3} ", ex.Message, ex.Source, ex.TargetSite, ex.StackTrace)); Console.WriteLine("RemoveMoneyJob is running with error....." + DateTime.Now.ToString()); Console.WriteLine(String.Format(@"Message: {0}, Source: {1}, TargetSite: {2}, Trace: {3} ", ex.Message, ex.Source, ex.TargetSite, ex.StackTrace));*/ throw ex; } }
public void Execute(IJobExecutionContext context) { try { Console.WriteLine("RemoveMoneyJob is starting....." + DateTime.Now.ToString()); Log.Info("RemoveMoneyJob запущен!"); List <GetBillsecForClientInfo> BillsecForClientInfoList_Curent = new List <GetBillsecForClientInfo>(); List <GetPhonesCountForClientInfo> PhonesCountForClientInfoList_Curent = new List <GetPhonesCountForClientInfo>(); ////выбираем дату последнего успешного запуска Job-а ( Начало ) DateTime lastactiondate = new DateTime(); JobsInfo JobsInfoQuery = _jobsinfoRep.GetLastActionDateForJob(Constants.RemoveMoneyJob); if (JobsInfoQuery != null) { lastactiondate = JobsInfoQuery.lastactiondate; } // выбираем дату последнего успешного запуска Job-а ( Конец ) else { lastactiondate = DateTime.Parse("2013-03-15"); } double numbercostforday = 0; DateTime FinishDate = /*GetDateFromFile()[1];*/ DateTime.Now.AddHours(-1).ToUniversalTime(); DateTime StartDate = /*GetDateFromFile()[0];*/ lastactiondate; DateTime CurentDate = StartDate; MinutesCost lastminutecostdate = null; NumberCost lastnumbercostdate = null; //StartDate = DateTime.Parse("29.04.2013 23:00:00"); //CurentDate = StartDate; //FinishDate = DateTime.Parse("30.04.2013 00:00:00").AddHours(-1); while (CurentDate <= FinishDate) //условие <= приводит к двойному выполнению Job-а для одного и того же клиента в сутки { string clients = GetClientsForRemoveMoney_(CurentDate /*DateTime.Parse("30.04.2013 0:00:00")*/); //получаем всех клиентов, для которых нужно списать деньги if (!String.IsNullOrEmpty(clients)) { Console.WriteLine("Диапазон между startdate и finishdate: " + StartDate.ToString() + " - " + FinishDate.ToString()); Console.WriteLine("Грабеж!!!!!!!!! Время грабежа: " + CurentDate.ToString() + " Клиенты: " + clients); List <BillSecForClientsInfo> billsecforclientsinfo = Call_GetBillSecForClients(CurentDate, clients); List <ActivedClientsNumbersCountInfo> activedclientsnumberscountinfo = Call_GetActivedClientsNumbers(CurentDate, clients); lastminutecostdate = _minutescostRep.GetAll().Where(t => t.lastupdate <= CurentDate).OrderByDescending(t => t.lastupdate).FirstOrDefault(); lastnumbercostdate = _numbercostRep.GetAll().Where(t => t.lastupdate <= CurentDate).OrderByDescending(t => t.lastupdate).FirstOrDefault(); List <InfoForPaymants> infoforpaymants = new List <InfoForPaymants>(); var commondata = from b in billsecforclientsinfo from a in activedclientsnumberscountinfo where b.date == a.date && b.ClientId == a.ClientId select new { ClientId = b.ClientId, date = b.date, count = a.count, codes_billsec = b.codes_billsec }; var differentdata = from a in activedclientsnumberscountinfo where !billsecforclientsinfo.Any(t => t.ClientId == a.ClientId && t.date == a.date) select a; if (commondata.Count() > 0) { foreach (var item in commondata) { infoforpaymants.Add(new InfoForPaymants() { codes_billsec = item.codes_billsec, ClientId = item.ClientId, count = item.count, date = item.date }); } } if (differentdata.Count() > 0) { foreach (var item in differentdata) { infoforpaymants.Add(new InfoForPaymants() { codes_billsec = null, count = item.count, date = item.date, ClientId = item.ClientId }); } } int allbillsec = 0; //общее колическво секунд разговоров для клиента для всех кодов if (infoforpaymants.Count() > 0) { foreach (InfoForPaymants item in infoforpaymants) { numbercostforday = _numbercostRep.GetLastNumberCostToThisDate(item.date); //последняя стоимость номера за день if (item.codes_billsec != null) { item.sum = Math.Round(GetCommonBillsecCostForAllCodes(item.codes_billsec) + item.count * numbercostforday, 2); } else { item.sum = Math.Round(item.count * numbercostforday, 2); } if (item.codes_billsec != null) { allbillsec = item.codes_billsec.Sum(t => t.billsec); } Paymant new_paymant = new Paymant() { ClientId = item.ClientId, date = item.date, Type = false, sum = -1 * item.sum, coment = "Снятие денег со счета. Время разговоров: " + allbillsec + " сек; Количество арендуемых номеров: " + item.count + " Время: " + DateTime.Now.ToString() }; _paymantsRep.Add(new_paymant); _paymantsRep.Save(); Client curent_client = _clientRep.Find(item.ClientId); if (curent_client != null) { string message_ = "Уважаемый " + curent_client.Name + ", " + item.date.ToShortDateString() + " с вашего счета было снято " + item.sum + " грн за " + allbillsec + " секунд разговоров и за аренду телефонов в количестве " + item.count + "."; _sendemailjobinfoRep.Add(new SendEmailJobInfo() { attemptcount = 0, date = DateTime.Now.ToUniversalTime(), message = message_, status = false, subject = "CallTracking Info", To = curent_client.Email }); _sendemailjobinfoRep.Save(); } allbillsec = 0; } } } CurentDate = CurentDate.AddHours(1); } // AddOneHourToDatesFromFile(); JobsInfo new_jobs_info = new JobsInfo() { status = true, lastactiondate = DateTime.Now.ToUniversalTime(), jobId = Constants.RemoveMoneyJob }; //поставить еще jobId! ( также в классе JobsInfo ) _jobsinfoRep.Add(new_jobs_info); _jobsinfoRep.Save(); Log.Info("RemoveMoneyJob успешно выполнен!"); Console.WriteLine("RemoveMoneyJob is finished....." + DateTime.Now.ToString()); } catch (Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } /* JobsInfo new_jobs_info = new JobsInfo() { status = false, lastactiondate = DateTime.Now.ToUniversalTime(), jobId = Constants.RemoveMoneyJob, ExceptionText = String.Format(@"Message: {0}, Source: {1}, TargetSite: {2}, Trace: {3} ", ex.Message, ex.Source, ex.TargetSite, ex.StackTrace) };//поставить еще jobId! ( также в классе JobsInfo ) * _jobsinfoRep.Add(new_jobs_info); * _jobsinfoRep.Save(); * * Log.Error(String.Format(@"Message: {0}, Source: {1}, TargetSite: {2}, Trace: {3} ", ex.Message, ex.Source, ex.TargetSite, ex.StackTrace)); * Console.WriteLine("RemoveMoneyJob is running with error....." + DateTime.Now.ToString()); * Console.WriteLine(String.Format(@"Message: {0}, Source: {1}, TargetSite: {2}, Trace: {3} ", ex.Message, ex.Source, ex.TargetSite, ex.StackTrace));*/ throw ex; } }