public string Export() { var codes = this._layerRepository.GetAll() .Where(r => r.Category == DictionaryCategory.Budget_Account) .OrderBy(r => r.LevelCode).Select(r => new { Code = r.LevelCode, r.Name }).ToList(); var list = this.GetBudgetPerformances(); var newCodes = list.Select(r => r.Code).ToList(); foreach (var item in list) { GetNewCode(item.Code); } void GetNewCode(string code) { if (!newCodes.Contains(code)) { newCodes.Add(code); } if (code.Length > 1) { var code2 = code.Substring(0, code.Length - 1); GetNewCode(code2); } } newCodes.Sort(); var result = new List <BudgetPerformanceListDto> { new BudgetPerformanceListDto { Code = string.Empty, Id = Guid.NewGuid(), Name = string.Empty, Total4 = list.Sum(r => r.Total4), Total10 = list.Sum(r => r.Total10), Total12 = list.Sum(r => r.Total12), Total13 = list.Sum(r => r.Total13), Total14 = list.Sum(r => r.Total14), Total16 = list.Sum(r => r.Total16), Total17 = list.Sum(r => r.Total17), Total18 = list.Sum(r => r.Total18), Total19 = list.Sum(r => r.Total19), Total2 = list.Sum(r => r.Total2), Total22 = list.Sum(r => r.Total22), Total24 = list.Sum(r => r.Total24), Total25 = list.Sum(r => r.Total25), Total3 = list.Sum(r => r.Total3), Total5 = list.Sum(r => r.Total5), Total8 = list.Sum(r => r.Total8), Total9 = list.Sum(r => r.Total9) } }; foreach (var code in newCodes) { var item = list.FirstOrDefault(r => r.Code == code); if (item == null) { var codeName = codes.FirstOrDefault(r => r.Code == code); if (codeName == null) { continue; } item = new BudgetPerformanceListDto { Code = codeName.Code, Id = Guid.NewGuid(), Name = codeName.Name, Total4 = -1234 // 给个特定值,填充行数据时替换成空 }; } result.Add(item); } return(DownloadFileService.Load("budgetOutlay.xls", "预算执行情况.xls", new string[] { }) .ExcuteXls(result, this.ExportToXls)); }
public List <BudgetPerformanceListDto> GetBudgetPerformances() { var currentYearItem = this._sysDictionaryRepository.FirstOrDefault(r => r.Value4 == true.ToString() && r.Category == DictionaryCategory.Budget_Year); if (currentYearItem == null) { throw new UserFriendlyException("预算年度不存在"); } var accounts = this._layerRepository.GetAllList(r => r.Category == DictionaryCategory.Budget_Account); var result = this._budgetReceiptRepository.GetAllList(r => r.Year.ToString() == currentYearItem.Value) .OrderBy(r => r.Code).ToList(); var result0 = this._budgetOutlayRepository .GetAllList(r => r.Year.ToString() == currentYearItem.Value && r.HasRelated).OrderBy(r => r.Code) .ToList(); var codeList = result.Select(r => r.Code).Distinct().ToList(); var list = new List <BudgetPerformanceListDto>(); foreach (var code in codeList) { var receipt = result.FirstOrDefault(r => r.Code == code); var outlay1 = result0.Where(r => r.Code == code && r.Type == BungetType.Year).ToList(); var outlay2 = result0.Where(r => r.Code == code && r.Type == BungetType.Adjust).ToList(); var outlay3 = result0.Where(r => r.Code == code && r.Type == BungetType.Increase).ToList(); var outlay = this._outlayRepository.FirstOrDefault(r => r.Code == code && r.Year.ToString() == currentYearItem.Value); if (receipt == null) { throw new UserFriendlyException($"科目编码为[{code}]的年初预算收入不存在"); } var performance = new BudgetPerformanceListDto { Id = receipt.Id, Code = code, Name = accounts.FirstOrDefault(r => r.LevelCode == code)?.Name, Total2 = result.Where(r => r.Code == code).Sum(r => r.Column1), Total3 = result.Where(r => r.Code == code).Sum(r => r.Column21 + r.Column22), Total4 = result.Where(r => r.Code == code).Sum(r => r.Column31 + r.Column32 + r.Column33 + r.Column34 + r.Column35 + r.Column36 + r.Column37), Total5 = result.Where(r => r.Code == code).Sum(r => r.Column41 + r.Column42 + r.Column43 + r.Column44 + r.Column45 + r.Column46 + r.Column47), Total8 = outlay1.Any() ? outlay1.Sum(r => r.Column1) : 0, Total9 = outlay1.Any() ? outlay1.Sum(r => r.Column2) : 0, Total10 = outlay1.Any() ? outlay1.Sum(r => r.Column3) : 0, Total12 = outlay2.Any() ? outlay2.Sum(r => r.Column1) : 0, Total13 = outlay2.Any() ? outlay2.Sum(r => r.Column2) : 0, Total14 = outlay2.Any() ? outlay2.Sum(r => r.Column3) : 0, Total16 = outlay3.Any() ? outlay3.Sum(r => r.Column1) : 0, Total17 = outlay3.Any() ? outlay3.Sum(r => r.Column2) : 0, Total18 = outlay3.Any() ? outlay3.Sum(r => r.Column3) : 0, Total19 = decimal.Round( ((!outlay1.Any() ? 0 : outlay1.SelectMany(r => r.ActualOutlays).Sum(s => s.Amount)) + (!outlay2.Any() ? 0 : outlay2.SelectMany(r => r.ActualOutlays).Sum(s => s.Amount)) + (!outlay3.Any() ? 0 : outlay3.SelectMany(r => r.ActualOutlays).Sum(s => s.Amount))) / 10000, 2, MidpointRounding.AwayFromZero), Total22 = outlay == null ? 0 : outlay.Total1, Total24 = outlay == null ? 0 : outlay.Total2, Total25 = outlay == null ? 0 : outlay.Total3, Note = outlay?.Note }; list.Add(performance); } return(list); }