private async Task ReloadNotCompleteDataAsync() { //get the list of month and year which those saved in MemberPayroll table. var lstMonthYear = await unitOfWork.MemberPayrollDataService.GetMonthYearListAsync(); //specify the range data for reloading NotCompleteRecord notCompleteRecord = await SetNotCompleteRangeTime( lstMonthYear.Select(x => new YearMonth() { Month = x.Month, Year = x.Year }).ToList()); if (notCompleteRecord.ExistAnyRecord) { //load the members records to mapping data var lstMembers = await unitOfWork.MemberDataService.GetAllAsync(); //call web service var lstEliteData = await ServiceWrapper.Instance.LoadPayrollsAsync(yearMonth.Year , yearMonth.Month , lstMembers); //compare & mapping the exist and just loaded data var lstExistData = await unitOfWork.MemberPayrollDataService.GetAsync(x => x.Year == yearMonth.Year && x.Month == yearMonth.Month); var lstNewData = new List <MemberPayroll>(); //seperation the new data lstEliteData .Where(x => lstMembers.Any(y => y.RefId == x.MemberRefId)) .ToList() .ForEach(x => { if (!lstExistData.Any(y => y.MemberRefId == x.MemberRefId)) { lstNewData.Add(x); } }); //checking the new data with the branch data lstEliteData .Where(x => !lstMembers.Any(y => y.RefId == x.MemberRefId)) .Select(x => x.MemberRefId) .Distinct() .ToList() .ForEach(x => { logger.Warn(serviceName, "there isn't any member with following data : MemberRefId={0}--Year={1}--Month={2}" , x, yearMonth.Year, yearMonth.Month); }); //insert batch data var newDataInserted = await unitOfWork.MemberPayrollDataService.InsertBulkAsync(lstNewData); LogInsertedData(lstEliteData.Count, newDataInserted); //insert/update data sync status await RecordDataSyncStatusAsync(newDataInserted, lstEliteData.Count, notCompleteRecord.DataSyncStatus); } else { LogNotDataForReloading(); } }
private async Task ReloadNotCompleteDataAsync() { var result_lstMonthYear = await unitOfWork.BranchSalesDataService.GetMonthYearListAsync(); //specify the range data for reloading NotCompleteRecord notCompleteRecord = await SetNotCompleteRangeTime( result_lstMonthYear.Select(x => new YearMonth() { Month = x.Month, Year = x.Year }).ToList()); if (notCompleteRecord.ExistAnyRecord) { //load branches var lstBranches = await unitOfWork.BranchDataService.GetAsync(where : null); //load goods var lstGoods = await unitOfWork.GoodsDataService.GetAsync(where : null); //call web service var lstEliteData = await ServiceWrapper.Instance.LoadBranchSalesAsync(yearMonth.Year , yearMonth.Month , lstGoods , lstBranches); //compare & mapping the exist and just loaded data var lstExistData = await unitOfWork.BranchSalesDataService.GetAsync(x => x.Year == yearMonth.Year && x.Month == yearMonth.Month); var lstNewData = new List <BranchSales>(); //seperation the new data lstEliteData .Where(x => lstBranches.Any(y => y.RefId == x.BranchRefId) && lstGoods.Any(y => y.RefId == x.GoodsRefId)) .ToList() .ForEach(x => { if (!lstExistData.Any(y => y.BranchRefId == x.BranchRefId && x.GoodsRefId == y.GoodsRefId)) { lstNewData.Add(x); } }); //checking the new data with the branch data lstEliteData .Where(x => !lstBranches.Any(y => y.RefId == x.BranchRefId)) .Select(x => x.BranchRefId) .Distinct() .ToList() .ForEach(x => { logger.Warn(serviceName, "there isn't any branch with following data : BranchRefId={0}--Year={1}--Month={2}" , x , yearMonth.Year , yearMonth.Month); }); lstEliteData .Where(x => !lstGoods.Any(y => y.RefId == x.GoodsRefId)) .Select(x => x.GoodsRefId) .Distinct() .ToList() .ForEach(x => { logger.Warn(serviceName, "there isn't any goods with following data :GoodsRefId={0}--Year={1}--Month={2}." , x , yearMonth.Year , yearMonth.Month); }); //insert batch data var newDataInserted = await unitOfWork.BranchSalesDataService.InsertBulkAsync(lstNewData); LogInsertedData(lstEliteData.Count, newDataInserted); //insert/update data sync status await RecordDataSyncStatusAsync(newDataInserted, lstEliteData.Count, notCompleteRecord.DataSyncStatus); } else { LogNotDataForReloading(); } }
private async Task ReloadNotCompleteDataAsync(ExternalData externalData) { //get the list of month and year which those saved in BranchReceipt table. var lstMonthYear = await businessService.LoadMonthYearListAsync(); if (!lstMonthYear.ReturnStatus) { logger.exError(serviceName, lstMonthYear.ReturnMessage.ConcatAll()); return; } //specify the range data for reloading NotCompleteRecord notCompleteRecord = await SetNotCompleteRangeTime( lstMonthYear.ResultValue.Select(x => new YearMonth() { Month = x.Month, Year = x.Year }).ToList()); if (notCompleteRecord.ExistAnyRecord) { //DoLogServiceStarted(); //load the members records to mapping data var lstMembers = await memberListLoader.LoadAllAsync(); if (!lstMembers.ReturnStatus) { logger.exError(serviceName, lstMembers.ReturnMessage.ConcatAll()); return; } //call web service var lstEliteData = await ServiceWrapper.Instance.LoadMemberReceiptsAsync(externalData.Year , externalData.Month , lstMembers.ResultValue); //compare & mapping the exist and just loaded data var lstExistData = await businessService.LoadListAsync(externalData.Year, externalData.Month); if (!lstExistData.ReturnStatus) { logger.exError(serviceName, lstExistData.ReturnMessage.ConcatAll()); return; } var lstNewData = new List <MemberReceipt>(); //seperation the new data lstEliteData .Where(x => lstMembers.ResultValue.Any(y => y.RefId == x.MemberRefId)) .ToList() .ForEach(x => { if (!lstExistData.ResultValue.Any(y => y.MemberRefId == x.MemberRefId)) { lstNewData.Add(x); } }); //checking the new data with the branch data lstEliteData .Where(x => !lstMembers.ResultValue.Any(y => y.RefId == x.MemberRefId)) .Select(x => x.MemberRefId) .Distinct() .ToList() .ForEach(x => { logger.exError(serviceName, "there isn't any member with following data : MemberRefId={0}--Year={1}--Month={2}" , x, externalData.Year, externalData.Month); }); //insert batch data var newDataInserted = await businessService.AddBatchAsync(lstNewData); //check result of the inserted data if (newDataInserted.ReturnStatus) { DoLogInsertedData(lstEliteData.Count, newDataInserted.ResultValue); } else { logger.exError(serviceName, newDataInserted.ReturnMessage.ConcatAll()); } //insert/update data sync status await RecordDataSyncStatusAsync(newDataInserted.ResultValue, lstEliteData.Count, notCompleteRecord.DataSyncStatus); //DoLogServiceCompleted(); } else { DoLogNotDataForReloading(); } }
protected async Task <NotCompleteRecord> SetNotCompleteRangeTime(List <YearMonth> lstMonthYear) { NotCompleteRecord notCompleteRecord = new NotCompleteRecord(); notCompleteRecord.ExistAnyRecord = false; if (lstMonthYear.Count != 0) { foreach (var item in lstMonthYear) { var dataSyncStatus = await unitOfWork.DataSyncStatusDataService.FirstOrDefaultAsync(x => x.ServiceName == serviceName.ToString() && x.Year == item.Year && x.Month == item.Month); if (dataSyncStatus == null) { //failing at saving the DataSyncStatus row in the database on AcquireData mode. yearMonth.Month = item.Month; yearMonth.Year = item.Year; notCompleteRecord.ExistAnyRecord = true; break; } else if (dataSyncStatus.IsInsertedAllData == false && dataSyncStatus.DateCreated < DateTime.Now.Date) { if (dataSyncStatus.TryCount >= dataSyncConfiguration.MaxCheckFailCount) { var mailData = new Mail(); mailData.Subject = "Failing in running a service"; mailData.Body = string.Format("service {0} has failed {1} times.", serviceName, dataSyncStatus.TryCount); mailData.To = dataSyncConfiguration.SupporterEmailAddress; await Communicator.Instance.SendGmailAsync(mailData); } yearMonth.Month = dataSyncStatus.Month; yearMonth.Year = dataSyncStatus.Year; notCompleteRecord.ExistAnyRecord = true; notCompleteRecord.DataSyncStatus = dataSyncStatus; break; } } } else { var dataSyncStatus = await unitOfWork.DataSyncStatusDataService.FirstOrDefaultAsync(x => x.ServiceName == serviceName.ToString()); if (dataSyncStatus == null) { notCompleteRecord.ExistAnyRecord = true; } else if (dataSyncStatus.IsInsertedAllData == false && dataSyncStatus.DateCreated < DateTime.Now.Date) { if (dataSyncStatus.TryCount >= dataSyncConfiguration.MaxCheckFailCount) { var mailData = new Mail(); mailData.Subject = "Failing in running a service"; mailData.Body = string.Format("service {0} has failed {1} times.", serviceName, dataSyncStatus.TryCount); mailData.To = dataSyncConfiguration.SupporterEmailAddress; await Communicator.Instance.SendGmailAsync(mailData); } yearMonth.Month = dataSyncStatus.Month; yearMonth.Year = dataSyncStatus.Year; notCompleteRecord.ExistAnyRecord = true; notCompleteRecord.DataSyncStatus = dataSyncStatus; } } return(notCompleteRecord); }