public DPMViewModel GetDPM() { DayOfWeek weekStart = DayOfWeek.Monday; // or Sunday, or whenever DateTime startingDate = DateTime.Today; while (startingDate.DayOfWeek != weekStart) { startingDate = startingDate.AddDays(-1); } DateTime previousFiveWeekStart = startingDate.AddDays(-35); //tổng số sản phẩm được kiểm tra rồi var list = _appDbContext.Inspections.Where(t => t.InspectDate.Date >= previousFiveWeekStart.Date && t.InspectDate.Date < startingDate.Date && t.InspectStatus == true).ToList(); var checkedItems = list.Select(b => b.NumberChecked).Sum(); var mistakeItems = list.Select(b => b.ProductQuantityChecked).Sum(); double dpmValue = checkedItems == 0 ? 0 : Math.Round(((double)mistakeItems / (double)checkedItems) * 1000000, 0); DPMViewModel result = new DPMViewModel() { CheckedItems = checkedItems, MistakeItems = mistakeItems, DPMValue = dpmValue }; return(result); }
public DPMListViewModel GetDPMByFactory() { DayOfWeek weekStart = DayOfWeek.Sunday; // or Sunday, or whenever DateTime startingDate = DateTime.Today; while (startingDate.DayOfWeek != weekStart) { startingDate = startingDate.AddDays(-1); } //lấy ngày thứ 2 cách đó 5 tuần DateTime previousFiveWeekStart = startingDate.AddDays(-35); //tổng số sản phẩm được kiểm tra rồi var list = _appDbContext.Inspections.Where(t => t.InspectDate.Date >= previousFiveWeekStart.Date && t.InspectDate.Date < startingDate.Date && t.InspectStatus == true).ToList(); var manufacturerList = (from so in list//_appDbContext.Inspections : avoid retrieving all inspections to improve performance; btw maybe not list all manufacturer unless list does not collect all group so by so.FactoryName into TotaledFactories select new { FactoryName = TotaledFactories.Key, }); List <DPMViewModel> dpmList = new List <DPMViewModel>(); foreach (var item in manufacturerList) { var checkedItems = list.Where(a => a.FactoryName == item.FactoryName).Select(b => b.NumberChecked).Sum(); var mistakeItems = list.Where(x => x.FactoryName == item.FactoryName).Select(b => b.ProductQuantityChecked).Sum(); double dpmValue = checkedItems == 0 ? 0 : Math.Round(((double)mistakeItems / (double)checkedItems) * 1000000, 0); DPMViewModel dpm = new DPMViewModel() { CheckedItems = checkedItems, MistakeItems = mistakeItems, FactoryName = item.FactoryName, DPMValue = dpmValue //FactoryId = factoryId }; dpmList.Add(dpm); } DPMListViewModel result = new DPMListViewModel() { DPMList = dpmList, StartDate = previousFiveWeekStart.AddDays(1).ToString("d"), EndDate = startingDate.AddDays(-1).ToString("d") }; return(result); }