diff --git a/DOAN.Admin.WebApi/Controllers/MES/report/ReportController.cs b/DOAN.Admin.WebApi/Controllers/MES/report/ReportController.cs index 16391dc..bcb8d97 100644 --- a/DOAN.Admin.WebApi/Controllers/MES/report/ReportController.cs +++ b/DOAN.Admin.WebApi/Controllers/MES/report/ReportController.cs @@ -1,10 +1,13 @@ using DOAN.Admin.WebApi.Filters; +using DOAN.Model; using DOAN.Model.MES; using DOAN.Model.MES.product.Dto; +using DOAN.Service.MES.product; using DOAN.Service.MES.product.IService; using DOAN.Service.MES.report.IService; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using NPOI.HPSF; namespace DOAN.WebApi.Controllers { @@ -19,7 +22,7 @@ namespace DOAN.WebApi.Controllers public ReportController(IProPlanAchievementrateVersion2Service ProPlanAchievementrateService, IReportService ReportService) { _ProPlanAchievementrateVersion2Service = ProPlanAchievementrateService; - _ReportService=ReportService; + _ReportService = ReportService; } /// /// 月生产数据统计列表 @@ -27,9 +30,9 @@ namespace DOAN.WebApi.Controllers /// /// [HttpGet("listMonth")] - public IActionResult QueryMonthProuct([FromQuery] MonthProuctDto parm) + public IActionResult QueryMonthProduct([FromQuery] MonthProductDto parm) { - var response = _ProPlanAchievementrateVersion2Service.GetQueryMonthProucth(parm); + var response = _ProPlanAchievementrateVersion2Service.GetQueryMonthProduct(parm); return SUCCESS(response); } @@ -65,5 +68,17 @@ namespace DOAN.WebApi.Controllers var response = _ReportService.ProductionCompletionRate(DateTime.Today); return SUCCESS(response); } + + [HttpGet("exportMonthProduct")] + [Log(Title = "工单导出", BusinessType = BusinessType.EXPORT)] + [AllowAnonymous] + public IActionResult ExportMonthProduct([FromQuery] MonthProductDto parm) + { + var (fileBytes, fileName) = _ProPlanAchievementrateVersion2Service.ExportMonthProduct(parm); + + string contnetType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + return File(fileBytes,contnetType,fileName); + + } } } diff --git a/DOAN.Admin.WebApi/appsettings.Development.json b/DOAN.Admin.WebApi/appsettings.Development.json index f9986e9..ca52625 100644 --- a/DOAN.Admin.WebApi/appsettings.Development.json +++ b/DOAN.Admin.WebApi/appsettings.Development.json @@ -9,7 +9,8 @@ "dbConfigs": [ { // "Conn": "Data Source=139.224.232.211;User ID=root;Password=doantech123;Initial Catalog=GXAssembly;Port=3308", - //"Conn": "Data Source=118.25.48.201;User ID=root;Password=123456;Initial Catalog=gxassembly;Port=3306;AllowLoadLocalInfile=true", + //阿里云 + //"Conn": "Data Source=47.101.40.214;User ID=root;Password=Rizo123456@;Initial Catalog=gxassembly;Port=3306;AllowLoadLocalInfile=true", "Conn": "Data Source=192.168.1.48;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306;AllowLoadLocalInfile=true", // "Conn": "Data Source=127.0.0.1;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306", //"Conn": "Data Source=192.168.50.163;User ID=root;Password=123456;Initial Catalog=GXAssembly;Port=3306", diff --git a/DOAN.Admin.WebApi/wwwroot/ImportTemplate/MonthProductData.xlsx b/DOAN.Admin.WebApi/wwwroot/ImportTemplate/MonthProductData.xlsx new file mode 100644 index 0000000..586457f Binary files /dev/null and b/DOAN.Admin.WebApi/wwwroot/ImportTemplate/MonthProductData.xlsx differ diff --git a/DOAN.Model/MES/report/Dto/ReportDto.cs b/DOAN.Model/MES/report/Dto/ReportDto.cs index d23aa02..d803baf 100644 --- a/DOAN.Model/MES/report/Dto/ReportDto.cs +++ b/DOAN.Model/MES/report/Dto/ReportDto.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace DOAN.Model.MES { - public class MonthProuctDto //: PagerInfo + public class MonthProductDto //: PagerInfo { public DateTime SearchYearMonth { get; set; } } diff --git a/DOAN.Service/MES/product/IService/IProPlanAchievementrateVersion2Service.cs b/DOAN.Service/MES/product/IService/IProPlanAchievementrateVersion2Service.cs index 2895e90..78c099f 100644 --- a/DOAN.Service/MES/product/IService/IProPlanAchievementrateVersion2Service.cs +++ b/DOAN.Service/MES/product/IService/IProPlanAchievementrateVersion2Service.cs @@ -47,6 +47,8 @@ namespace DOAN.Service.MES.product.IService byte[] ExportData(DateTime exportDate); - List GetQueryMonthProucth(MonthProuctDto parm); + List GetQueryMonthProduct(MonthProductDto parm); + + (byte[] fileBytes, string fileName) ExportMonthProduct(MonthProductDto parm); } } diff --git a/DOAN.Service/MES/product/ProPlanAchievementrateVersion2Service.cs b/DOAN.Service/MES/product/ProPlanAchievementrateVersion2Service.cs index ccbbbcb..6fa0a30 100644 --- a/DOAN.Service/MES/product/ProPlanAchievementrateVersion2Service.cs +++ b/DOAN.Service/MES/product/ProPlanAchievementrateVersion2Service.cs @@ -22,6 +22,7 @@ using NPOI.HSSF.Record; using JinianNet.JNTemplate; using DOAN.Model.MES; using DOAN.Model.MES.report; +using NPOI.POIFS.Crypt.Dsig; namespace DOAN.Service.MES.product { /// @@ -71,7 +72,7 @@ namespace DOAN.Service.MES.product SqlFunc.AggregateSum(w.DeliveryNum) == 0 ? 0 : SqlFunc.Round( - (decimal)SqlFunc.AggregateSum(r.FinishedNum??0) / (decimal)SqlFunc.AggregateSum(w.DeliveryNum) * 100, + (decimal)SqlFunc.AggregateSum(r.FinishedNum ?? 0) / (decimal)SqlFunc.AggregateSum(w.DeliveryNum) * 100, 0 ), RecordDate = parm.GenarateDate, @@ -572,7 +573,7 @@ namespace DOAN.Service.MES.product /// /// /// - public List GetQueryMonthProucth(MonthProuctDto parm) + public List GetQueryMonthProduct(MonthProductDto parm) { (DateTime FirstDay, DateTime LastDay) Handlemonth = GetFirstAndLastDayOfMonth(parm.SearchYearMonth); var predicate = Expressionable.Create() @@ -580,7 +581,7 @@ namespace DOAN.Service.MES.product var response = Context.Queryable() .Where(predicate.ToExpression()) - .GroupBy(it => new { it.Project,it.RecordDate }) + .GroupBy(it => new { it.Project, it.RecordDate }) .Select(it => new ProPlanAchievementrateVersion2() { Project = it.Project, @@ -588,6 +589,7 @@ namespace DOAN.Service.MES.product SummaryPlanNum = SqlFunc.AggregateSum(it.SummaryPlanNum ?? 0), RecordDate = it.RecordDate }) + .OrderBy(it => it.Project) .ToList(); var monthProuctModelList = response.GroupBy(it => it.Project) @@ -599,9 +601,9 @@ namespace DOAN.Service.MES.product DayIndex = it2.RecordDate?.Day ?? 0, SummaryActualNum = it2.SummaryActualNum, SummaryPlanNum = it2.SummaryPlanNum, - }).OrderBy(t=>t.DayIndex).ToList(), + }).OrderBy(t => t.DayIndex).ToList(), - }).OrderBy(t=>t.Project).ToList(); + }).OrderBy(t => t.Project).ToList(); monthProuctModelList.ForEach(t => { @@ -611,5 +613,67 @@ namespace DOAN.Service.MES.product }); return monthProuctModelList; } + + public (byte[] fileBytes, string fileName) ExportMonthProduct(MonthProductDto parm) + { + string templatePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "ImportTemplate", "MonthProductData.xlsx"); + if (!File.Exists(templatePath)) + { + throw new CustomException("Excel 模板文件不存在"); + } + List list = GetQueryMonthProduct(parm); + if(list.Count==0) + { + throw new CustomException("没有数据可以导出"); + } + + int lastDay = DateTime.DaysInMonth(parm.SearchYearMonth.Year, parm.SearchYearMonth.Month); + + using (FileStream fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = new XSSFWorkbook(fileStream); + ISheet sheet = workbook.GetSheet("Sheet1"); + if (sheet == null) + { + throw new CustomException("未找到 Sheet1,请检查 Excel 模板"); + } + int dataRowIndex = 3; + if (sheet.GetRow(1) != null) + { + sheet.GetRow(1).CreateCell(0).SetCellValue(parm.SearchYearMonth.ToString("yyyy-MM")); + } + IRow headerRow = sheet.GetRow(dataRowIndex-1) ?? sheet.CreateRow(dataRowIndex); + for(int i = 1; i <= lastDay; i++) + { + //headerRow.CreateCell(i).SetCellValue(i.ToString()); + headerRow.CopyCell(0,i).SetCellValue(i.ToString()); + } + headerRow.CopyCell(0, lastDay + 1).SetCellValue("计划数"); + headerRow.CopyCell(0, lastDay + 2).SetCellValue("实际数"); + headerRow.CopyCell(0, lastDay + 3).SetCellValue("完成率%"); + + + + foreach (var item in list) + { + IRow dataRow = sheet.GetRow(dataRowIndex) ?? sheet.CreateRow(dataRowIndex); + dataRow.CreateCell(0).SetCellValue(item.Project); // A列 + for (int i = 1; i <= lastDay; i++) + { + var dayProduct=item.DayProuctModels.FirstOrDefault(t=>t.DayIndex==i); + dataRow.CreateCell(i).SetCellValue(dayProduct?.SummaryActualNum ?? 0); + } + + dataRow.CreateCell(lastDay+1).SetCellValue(item.SummaryPlanNum ?? 0); // B列 + dataRow.CreateCell(lastDay+2).SetCellValue(item.SummaryActualNum ?? 0); // C列 + dataRow.CreateCell(lastDay+3).SetCellValue((double)item.SummaryPlanAchievementRate); // D列 + dataRowIndex++; + } + + var memoryStream = new MemoryStream(); + workbook.Write(memoryStream); + return (memoryStream.ToArray(), "月生产数据统计.xlsx"); + } + } } } \ No newline at end of file