public async Task <IActionResult> GetDailyScrapByShift( [FromQuery] DailyScrapByShiftResourceParameter resourceParameter ) { return(resourceParameter == null ? BadRequest() : (IActionResult)Ok( await _scrapService .GetDailyScrapByShift(resourceParameter) .ConfigureAwait(false) )); }
private async Task <List <DailyScrapByShiftDto> > GetDailyScrapByShiftList( DailyScrapByShiftResourceParameter resourceParams ) { if (resourceParams == null) { throw new ArgumentNullException(nameof(resourceParams)); } //get scrap data from repo var scrapQry = GetScrap2Queryable( new ScrapResourceParameter { StartDate = resourceParams.Start, EndDate = resourceParams.End, IsPurchasedScrap = resourceParams.IsPurchasedScrap, ScrapCode = resourceParams.ScrapCode, Department = resourceParams.Department, Line = resourceParams.Line, Program = resourceParams.Program } ); var scrap = await scrapQry .Select(x => new { x.ShiftDate, x.Shift, x.ScrapCode, x.ScrapDesc, x.Qty }) .ToListAsync() .ConfigureAwait(false); return(scrap.Count > 0 ? scrap .GroupBy(x => new { x.ShiftDate, x.Shift, x.ScrapCode, x.ScrapDesc }) .Select( x => new DailyScrapByShiftDto { ShiftDate = x.Key.ShiftDate.ToDateTime(), Shift = x.Key.Shift, ShiftOrder = _utilityService.MapShiftToShiftOrder(x.Key.Shift), ScrapCode = x.Key.ScrapCode, ScrapDesc = x.Key.ScrapDesc, Qty = x.Sum(s => s.Qty).ToInt() } ) .OrderBy(x => x.ShiftDate) .ThenBy(x => x.ShiftOrder) .ToList() : new List <DailyScrapByShiftDto>()); }
public async Task <dynamic> GetDailyScrapByShift( DailyScrapByShiftResourceParameter resourceParams ) { var summary = await GetDailyScrapByShiftList(resourceParams).ConfigureAwait(false); var distinctShift = summary .Select(x => new { x.Shift, x.ShiftOrder }) .Distinct() .ToList(); //get scrap data each day by filtered date range var result = new List <dynamic>(); var tempStart = resourceParams.Start; while (tempStart <= resourceParams.End) { foreach (var shift in distinctShift.OrderBy(x => x.ShiftOrder)) { var isExist = summary.Any( x => x.ShiftDate == tempStart && x.Shift == shift.Shift ); if (isExist) { var data = summary.First( x => x.ShiftDate == tempStart && x.Shift == shift.Shift ); result.Add( new { data.ShiftDate, data.Shift, data.ShiftOrder, data.ScrapCode, data.ScrapDesc, data.Qty } ); } else { result.Add( new { ShiftDate = tempStart, shift.Shift, shift.ShiftOrder, resourceParams.ScrapCode, ScrapDesc = "", Qty = 0 } ); } } tempStart = tempStart.AddDays(1); } //group by shift var shifts = distinctShift.Select( x => new { shift = x.Shift, shiftOrder = x.ShiftOrder, dailyScrap = new { data = result.Where(s => s.Shift == x.Shift), trendline = TrendLine.GetTrendLine( result.Where(s => s.Shift == x.Shift), "Qty" ) } } ); return(new { AllShifts = new { trendline = TrendLine.GetTrendLine(result, "Qty"), data = result }, Shift = shifts }); }