679 lines
34 KiB
C#
679 lines
34 KiB
C#
using System;
|
||
using SqlSugar;
|
||
using Infrastructure.Attribute;
|
||
using Infrastructure.Extensions;
|
||
using DOAN.Model;
|
||
using DOAN.Model.Dto;
|
||
using DOAN.Model.MES.product;
|
||
using DOAN.Model.MES.product.Dto;
|
||
using DOAN.Repository;
|
||
using DOAN.Service.MES.product.IService;
|
||
using System.Linq;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Infrastructure;
|
||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||
using Mapster;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using NPOI.SS.Formula.Functions;
|
||
using NPOI.SS.UserModel;
|
||
using NPOI.XSSF.UserModel;
|
||
using System.Globalization;
|
||
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
|
||
{
|
||
/// <summary>
|
||
/// 日计划达成率Service业务层处理 版本2
|
||
/// </summary>
|
||
[AppService(ServiceType = typeof(IProPlanAchievementrateVersion2Service), ServiceLifetime = LifeTime.Transient)]
|
||
public class ProPlanAchievementrateVersion2Service : BaseService<ProPlanAchievementrateVersion2>, IProPlanAchievementrateVersion2Service
|
||
{
|
||
/// <summary>
|
||
/// 查询日计划达成率列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public PagedInfo<ProPlanAchievementrateDto> GetList(ProPlanAchievementrateQueryDto parm)
|
||
{
|
||
var predicate = Expressionable.Create<ProPlanAchievementrateVersion2>()
|
||
.AndIF(!string.IsNullOrEmpty(parm.Project), it => it.Project.Contains(parm.Project))
|
||
.AndIF(parm.RecordDate > DateTime.MinValue, it => it.RecordDate == parm.RecordDate)
|
||
;
|
||
|
||
var response = Queryable()
|
||
.Where(predicate.ToExpression())
|
||
.ToPage<ProPlanAchievementrateVersion2, ProPlanAchievementrateDto>(parm);
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成每日的计划达成率
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
|
||
public bool DayofplanAchievementRate([FromBody] ProPlanAchievementrateQueryDto3 parm)
|
||
{
|
||
int result = 0;
|
||
List<ProPlanAchievementrateVersion2> AproPlanAchievementrates = Context.Queryable<ProWorkorder>()
|
||
.LeftJoin<ProReportwork>((w, r) => w.Workorder == r.FkWorkorder)
|
||
.Where((w, r) => w.WorkorderDate == parm.GenarateDate)
|
||
.GroupBy((w, r) => new { w.Project })
|
||
.Select((w, r) => new ProPlanAchievementrateVersion2()
|
||
{
|
||
Project = w.Project,
|
||
SummaryActualNum = SqlFunc.IsNull(SqlFunc.AggregateSum(r.FinishedNum), 0),
|
||
SummaryPlanNum = SqlFunc.IsNull(SqlFunc.AggregateSum(w.DeliveryNum), 0),
|
||
SummaryPlanAchievementRate =
|
||
SqlFunc.AggregateSum(w.DeliveryNum) == 0
|
||
? 0
|
||
: SqlFunc.Round<decimal>(
|
||
(decimal)SqlFunc.AggregateSum(r.FinishedNum ?? 0) / (decimal)SqlFunc.AggregateSum(w.DeliveryNum) * 100,
|
||
0
|
||
),
|
||
RecordDate = parm.GenarateDate,
|
||
UpdatedTime = DateTime.Now
|
||
}).ToList();
|
||
|
||
if (AproPlanAchievementrates.Count > 0)
|
||
{
|
||
UseTran2(() =>
|
||
{
|
||
int index = Context.Deleteable<ProPlanAchievementrateVersion2>().Where(it => it.RecordDate == parm.GenarateDate).ExecuteCommand();
|
||
result = Context.Insertable(AproPlanAchievementrates).ExecuteCommand();
|
||
});
|
||
}
|
||
|
||
return result > 0;
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 查询月计划达成率列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public PagedInfo<ProPlanAchievementrateDto> GetListByMonth(ProPlanAchievementrateQueryDto2 parm)
|
||
{
|
||
(DateTime FirstDay, DateTime LastDay) Handlemonth = GetFirstAndLastDayOfMonth(parm.SearchYearMonth);
|
||
var predicate = Expressionable.Create<ProPlanAchievementrateVersion2>()
|
||
.AndIF(!string.IsNullOrEmpty(parm.Project), it => it.Project.Contains(parm.Project))
|
||
.And(it => it.RecordDate >= Handlemonth.FirstDay && it.RecordDate <= Handlemonth.LastDay)
|
||
;
|
||
|
||
var response = Context.Queryable<ProPlanAchievementrateVersion2>()
|
||
.Where(predicate.ToExpression())
|
||
.GroupBy(it => it.Project)
|
||
.Select(it => new ProPlanAchievementrateVersion2()
|
||
{
|
||
Project = it.Project,
|
||
SummaryActualNum = SqlFunc.AggregateSum(it.SummaryActualNum ?? 0),
|
||
SummaryPlanNum = SqlFunc.AggregateSum(it.SummaryPlanNum ?? 0),
|
||
SummaryPlanAchievementRate = SqlFunc.Round<decimal>((decimal)(SqlFunc.AggregateSum(it.SummaryActualNum ?? 0) / SqlFunc.AggregateSum(it.SummaryPlanNum)), 0) * 100,
|
||
DownQuality = SqlFunc.AggregateSum(it.DownQuality ?? 0),
|
||
DownSuppler = SqlFunc.AggregateSum(it.DownSuppler ?? 0),
|
||
DownDeviceFailure = SqlFunc.AggregateSum(it.DownDeviceFailure ?? 0),
|
||
DownDeviceDebug = SqlFunc.AggregateSum(it.DownDeviceDebug ?? 0),
|
||
DownLogisticsWaitMaterial = SqlFunc.AggregateSum(it.DownLogisticsWaitMaterial ?? 0),
|
||
DownLackMaterial = SqlFunc.AggregateSum(it.DownLackMaterial ?? 0),
|
||
DownInjection = SqlFunc.AggregateSum(it.DownInjection ?? 0),
|
||
DownAssembly = SqlFunc.AggregateSum(it.DownAssembly ?? 0),
|
||
AllLineStopTime = SqlFunc.AggregateSum(it.AllLineStopTime ?? 0),
|
||
RecordDate = Handlemonth.FirstDay
|
||
})
|
||
.ToPage<ProPlanAchievementrateVersion2, ProPlanAchievementrateDto>(parm);
|
||
|
||
return response;
|
||
}
|
||
|
||
private (DateTime FirstDay, DateTime LastDay) GetFirstAndLastDayOfMonth(DateTime date)
|
||
{
|
||
DateTime firstDay = new DateTime(date.Year, date.Month, 1);
|
||
int lastDay = DateTime.DaysInMonth(date.Year, date.Month);
|
||
DateTime lastDayDate = new DateTime(date.Year, date.Month, lastDay);
|
||
|
||
return (firstDay, lastDayDate);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取详情
|
||
/// </summary>
|
||
/// <param name="Id"></param>
|
||
/// <returns></returns>
|
||
public ProPlanAchievementrateVersion2 GetInfo(int Id)
|
||
{
|
||
var response = Context.Queryable<ProPlanAchievementrateVersion2>()
|
||
.Where(x => x.Id == Id)
|
||
.First();
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加日计划达成率
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public ProPlanAchievementrateVersion2 AddProPlanAchievementrate(ProPlanAchievementrateVersion2 model)
|
||
{
|
||
|
||
return Context.Insertable(model).IgnoreColumns(it => new { it.Id }).ExecuteReturnEntity();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改日计划达成率
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public int UpdateProPlanAchievementrate(ProPlanAchievementrateVersion2 model)
|
||
{
|
||
//var response = Update(w => w.Id == model.Id, it => new ProPlanAchievementrate()
|
||
//{
|
||
// Project = model.Project,
|
||
// AgroupMonitor = model.AgroupMonitor,
|
||
// AgroupProductNum = model.AgroupProductNum,
|
||
// AgroupStandardYield = model.AgroupStandardYield,
|
||
// AgroupPlanNum = model.AgroupPlanNum,
|
||
// AgroupCompletionRate = model.AgroupCompletionRate,
|
||
// BgroupMonitor = model.BgroupMonitor,
|
||
// BgroupProductNum = model.BgroupProductNum,
|
||
// BgroupStandardYield = model.BgroupStandardYield,
|
||
// BgroupPlanNum = model.BgroupPlanNum,
|
||
// BgroupCompletionRate = model.BgroupCompletionRate,
|
||
// SummaryActualNum = model.SummaryActualNum,
|
||
// SummaryPlanNum = model.SummaryPlanNum,
|
||
// SummaryPlanAchievementRate = model.SummaryPlanAchievementRate,
|
||
// DownQuality = model.DownQuality,
|
||
// DownSuppler = model.DownSuppler,
|
||
// DownDeviceFailure = model.DownDeviceFailure,
|
||
// DownDeviceDebug = model.DownDeviceDebug,
|
||
// DownLogisticsWaitMaterial = model.DownLogisticsWaitMaterial,
|
||
// DownLackMaterial = model.DownLackMaterial,
|
||
// DownProject = model.DownProject,
|
||
// DownInjection = model.DownInjection,
|
||
// DownAssembly = model.DownAssembly,
|
||
// AllLineStopTime = model.AllLineStopTime,
|
||
// MainProblemDescription = model.MainProblemDescription,
|
||
// Strategy = model.Strategy,
|
||
// ResponsiblePerson = model.ResponsiblePerson,
|
||
// RecordDate = model.RecordDate,
|
||
// CreatedBy = model.CreatedBy,
|
||
// CreatedTime = model.CreatedTime,
|
||
// UpdatedBy = model.UpdatedBy,
|
||
// UpdatedTime = model.UpdatedTime,
|
||
//});
|
||
//return response;
|
||
return Update(model, true);
|
||
}
|
||
|
||
|
||
|
||
public List<ProPlanAchievementrateDto> GetTodayList()
|
||
{
|
||
DateTime today = DateTime.Today;
|
||
var response = Queryable()
|
||
.Where(it => it.RecordDate == today)
|
||
.ToList()
|
||
.Adapt<List<ProPlanAchievementrateVersion2>, List<ProPlanAchievementrateDto>>()
|
||
;
|
||
return response;
|
||
}
|
||
public List<ProPlanAchievementrateDto> GetTodayRealTimeList()
|
||
{
|
||
ProPlanAchievementrateQueryDto3 parm = new ProPlanAchievementrateQueryDto3();
|
||
parm.GenarateDate = DateTime.Today;
|
||
bool result = DayofplanAchievementRate(parm);
|
||
var response = Queryable()
|
||
.Where(it => it.RecordDate == parm.GenarateDate)
|
||
.ToList()
|
||
.Adapt<List<ProPlanAchievementrateVersion2>, List<ProPlanAchievementrateDto>>()
|
||
;
|
||
return response;
|
||
}
|
||
public List<ProPlanAchievementrateDto> GetYesterdayList()
|
||
{
|
||
DateTime yesterday = DateTime.Today.AddDays(-1);
|
||
var response = Queryable()
|
||
.Where(it => it.RecordDate == yesterday)
|
||
.ToList()
|
||
.Adapt<List<ProPlanAchievementrateVersion2>, List<ProPlanAchievementrateDto>>()
|
||
;
|
||
return response;
|
||
}
|
||
|
||
public List<ProPlanAchievementrateDto> GetthisweekList()
|
||
{
|
||
(DateTime FirstDay, DateTime LastDay) Handlemonth = GetWeekStartAndEnd(DateTime.Today);
|
||
var predicate = Expressionable.Create<ProPlanAchievementrateVersion2>()
|
||
|
||
.And(it => it.RecordDate >= Handlemonth.FirstDay && it.RecordDate <= Handlemonth.LastDay)
|
||
;
|
||
|
||
var response = Queryable()
|
||
|
||
.Where(predicate.ToExpression())
|
||
.GroupBy(it => it.Project)
|
||
.Select(it => new ProPlanAchievementrate()
|
||
{
|
||
Project = it.Project,
|
||
//AgroupPlanNum = SqlFunc.AggregateSum(it.AgroupPlanNum ?? 0),
|
||
//AgroupProductNum = SqlFunc.AggregateSum(it.AgroupProductNum ?? 0),
|
||
//AgroupCompletionRate = SqlFunc.AggregateAvg(it.AgroupCompletionRate),
|
||
//BgroupPlanNum = SqlFunc.AggregateSum(it.BgroupPlanNum ?? 0),
|
||
//BgroupProductNum = SqlFunc.AggregateSum(it.BgroupProductNum ?? 0),
|
||
//BgroupCompletionRate = SqlFunc.AggregateAvg(it.BgroupCompletionRate),
|
||
SummaryActualNum = SqlFunc.AggregateSum(it.SummaryActualNum ?? 0),
|
||
SummaryPlanNum = SqlFunc.AggregateSum(it.SummaryPlanNum ?? 0),
|
||
SummaryPlanAchievementRate = SqlFunc.Round<decimal>(SqlFunc.AggregateAvg(it.SummaryPlanAchievementRate), 0),
|
||
DownQuality = SqlFunc.AggregateSum(it.DownQuality ?? 0),
|
||
DownSuppler = SqlFunc.AggregateSum(it.DownSuppler ?? 0),
|
||
DownDeviceFailure = SqlFunc.AggregateSum(it.DownDeviceFailure ?? 0),
|
||
DownDeviceDebug = SqlFunc.AggregateSum(it.DownDeviceDebug ?? 0),
|
||
DownLogisticsWaitMaterial = SqlFunc.AggregateSum(it.DownLogisticsWaitMaterial ?? 0),
|
||
DownLackMaterial = SqlFunc.AggregateSum(it.DownLackMaterial ?? 0),
|
||
|
||
DownInjection = SqlFunc.AggregateSum(it.DownInjection ?? 0),
|
||
DownAssembly = SqlFunc.AggregateSum(it.DownAssembly ?? 0),
|
||
AllLineStopTime = SqlFunc.AggregateSum(it.AllLineStopTime ?? 0),
|
||
|
||
RecordDate = Handlemonth.FirstDay
|
||
})
|
||
.ToList()
|
||
.Adapt<List<ProPlanAchievementrate>, List<ProPlanAchievementrateDto>>();
|
||
|
||
return response;
|
||
}
|
||
|
||
public List<ProPlanAchievementrateDto> GetListByMonth()
|
||
{
|
||
(DateTime FirstDay, DateTime LastDay) Handlemonth = GetFirstAndLastDayOfMonth(DateTime.Today);
|
||
var predicate = Expressionable.Create<ProPlanAchievementrateVersion2>()
|
||
|
||
.And(it => it.RecordDate >= Handlemonth.FirstDay && it.RecordDate <= Handlemonth.LastDay)
|
||
;
|
||
|
||
var response = Queryable()
|
||
|
||
.Where(predicate.ToExpression())
|
||
.GroupBy(it => it.Project)
|
||
.Select(it => new ProPlanAchievementrate()
|
||
{
|
||
Project = it.Project,
|
||
//AgroupPlanNum = SqlFunc.AggregateSum(it.AgroupPlanNum ?? 0),
|
||
//AgroupProductNum = SqlFunc.AggregateSum(it.AgroupProductNum ?? 0),
|
||
//AgroupCompletionRate = SqlFunc.AggregateAvg(it.AgroupCompletionRate),
|
||
//BgroupPlanNum = SqlFunc.AggregateSum(it.BgroupPlanNum ?? 0),
|
||
//BgroupProductNum = SqlFunc.AggregateSum(it.BgroupProductNum ?? 0),
|
||
//BgroupCompletionRate = SqlFunc.AggregateAvg(it.BgroupCompletionRate),
|
||
SummaryActualNum = SqlFunc.AggregateSum(it.SummaryActualNum ?? 0),
|
||
SummaryPlanNum = SqlFunc.AggregateSum(it.SummaryPlanNum ?? 0),
|
||
SummaryPlanAchievementRate = SqlFunc.Round<decimal>(SqlFunc.AggregateAvg(it.SummaryPlanAchievementRate), 0),
|
||
DownQuality = SqlFunc.AggregateSum(it.DownQuality ?? 0),
|
||
DownSuppler = SqlFunc.AggregateSum(it.DownSuppler ?? 0),
|
||
DownDeviceFailure = SqlFunc.AggregateSum(it.DownDeviceFailure ?? 0),
|
||
DownDeviceDebug = SqlFunc.AggregateSum(it.DownDeviceDebug ?? 0),
|
||
DownLogisticsWaitMaterial = SqlFunc.AggregateSum(it.DownLogisticsWaitMaterial ?? 0),
|
||
DownLackMaterial = SqlFunc.AggregateSum(it.DownLackMaterial ?? 0),
|
||
|
||
DownInjection = SqlFunc.AggregateSum(it.DownInjection ?? 0),
|
||
DownAssembly = SqlFunc.AggregateSum(it.DownAssembly ?? 0),
|
||
AllLineStopTime = SqlFunc.AggregateSum(it.AllLineStopTime ?? 0),
|
||
|
||
RecordDate = Handlemonth.FirstDay
|
||
})
|
||
.ToList()
|
||
.Adapt<List<ProPlanAchievementrate>, List<ProPlanAchievementrateDto>>();
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
public int ImportData(IFormFile formFile, string name, DateTime RecordDate)
|
||
{
|
||
using (var stream = formFile.OpenReadStream())
|
||
{
|
||
try
|
||
{
|
||
IWorkbook workbook = new XSSFWorkbook(stream);
|
||
ISheet sheet = workbook.GetSheetAt(0);
|
||
|
||
|
||
List<ProPlanAchievementrateVersion2> AchievementrateList = new List<ProPlanAchievementrateVersion2>();
|
||
// 遍历每一行
|
||
for (int row = 4; row <= sheet.LastRowNum; row++)
|
||
{
|
||
IRow currentRow = sheet.GetRow(row);
|
||
if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空
|
||
{
|
||
ProPlanAchievementrateVersion2 achievementrate = new ProPlanAchievementrateVersion2();
|
||
|
||
//获取项目号
|
||
if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String)
|
||
achievementrate.Project = currentRow.GetCell(0).ToString();
|
||
|
||
////A班班长
|
||
//if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
|
||
// achievementrate.AgroupMonitor = currentRow.GetCell(1).ToString();
|
||
////A班生产数量
|
||
//if (currentRow.GetCell(2) != null && currentRow.GetCell(2).CellType == CellType.Numeric)
|
||
// achievementrate.AgroupProductNum = (int)currentRow.GetCell(2).NumericCellValue;
|
||
////A班标准班产
|
||
//if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.Numeric)
|
||
// achievementrate.AgroupStandardYield = (int)currentRow.GetCell(3).NumericCellValue;
|
||
|
||
////A班计划数量
|
||
//if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.Numeric)
|
||
// achievementrate.AgroupPlanNum = (int)currentRow.GetCell(4).NumericCellValue;
|
||
////A班完成率
|
||
//if (currentRow.GetCell(5) != null && currentRow.GetCell(5).CellType == CellType.Numeric)
|
||
// achievementrate.AgroupCompletionRate = (decimal)currentRow.GetCell(5).NumericCellValue;
|
||
////B班班长
|
||
//if (currentRow.GetCell(6) != null && currentRow.GetCell(6).CellType == CellType.String)
|
||
// achievementrate.BgroupMonitor = currentRow.GetCell(6).ToString();
|
||
////B班生产数量
|
||
//if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.Numeric)
|
||
// achievementrate.BgroupProductNum = (int)currentRow.GetCell(7).NumericCellValue;
|
||
////B班标准班产
|
||
//if (currentRow.GetCell(8) != null && currentRow.GetCell(8).CellType == CellType.Numeric)
|
||
// achievementrate.BgroupStandardYield = (int)currentRow.GetCell(8).NumericCellValue;
|
||
////B班计划数量
|
||
//if (currentRow.GetCell(9) != null && currentRow.GetCell(9).CellType == CellType.Numeric)
|
||
// achievementrate.BgroupPlanNum = (int)currentRow.GetCell(9).NumericCellValue;
|
||
////B班完成率
|
||
//if (currentRow.GetCell(10) != null && currentRow.GetCell(10).CellType == CellType.Numeric)
|
||
// achievementrate.BgroupCompletionRate = (decimal)currentRow.GetCell(10).NumericCellValue;
|
||
//汇总实际数量
|
||
if (currentRow.GetCell(11) != null && currentRow.GetCell(11).CellType == CellType.Numeric)
|
||
achievementrate.SummaryActualNum = (int)currentRow.GetCell(11).NumericCellValue;
|
||
//汇总计划数量
|
||
if (currentRow.GetCell(12) != null && currentRow.GetCell(12).CellType == CellType.Numeric)
|
||
achievementrate.SummaryPlanNum = (int)currentRow.GetCell(12).NumericCellValue;
|
||
//汇总计划达成率
|
||
if (currentRow.GetCell(13) != null && currentRow.GetCell(13).CellType == CellType.Numeric)
|
||
achievementrate.SummaryPlanAchievementRate = (decimal)currentRow.GetCell(13).NumericCellValue;
|
||
//记录日期
|
||
//if (currentRow.GetCell(14) != null && currentRow.GetCell(14).CellType == CellType.String)
|
||
// achievementrate.RecordDate = DateTime.ParseExact(currentRow.GetCell(14).ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture);
|
||
//质量问题
|
||
if (currentRow.GetCell(14) != null && currentRow.GetCell(14).CellType == CellType.Numeric)
|
||
achievementrate.DownQuality = (int)currentRow.GetCell(14).NumericCellValue;
|
||
//供应商问题
|
||
if (currentRow.GetCell(15) != null && currentRow.GetCell(15).CellType == CellType.Numeric)
|
||
achievementrate.DownSuppler = (int)currentRow.GetCell(15).NumericCellValue;
|
||
//设备故障
|
||
if (currentRow.GetCell(16) != null && currentRow.GetCell(16).CellType == CellType.Numeric)
|
||
achievementrate.DownDeviceFailure = (int)currentRow.GetCell(16).NumericCellValue;
|
||
//设备调试
|
||
if (currentRow.GetCell(17) != null && currentRow.GetCell(17).CellType == CellType.Numeric)
|
||
achievementrate.DownDeviceDebug = (int)currentRow.GetCell(17).NumericCellValue;
|
||
//物流等待物料
|
||
if (currentRow.GetCell(18) != null && currentRow.GetCell(18).CellType == CellType.Numeric)
|
||
achievementrate.DownLogisticsWaitMaterial = (int)currentRow.GetCell(18).NumericCellValue;
|
||
//物料短缺
|
||
if (currentRow.GetCell(19) != null && currentRow.GetCell(19).CellType == CellType.Numeric)
|
||
achievementrate.DownLackMaterial = (int)currentRow.GetCell(19).NumericCellValue;
|
||
//注塑异常
|
||
if (currentRow.GetCell(20) != null && currentRow.GetCell(20).CellType == CellType.Numeric)
|
||
achievementrate.DownInjection = (int)currentRow.GetCell(20).NumericCellValue;
|
||
//装配异常
|
||
if (currentRow.GetCell(21) != null && currentRow.GetCell(21).CellType == CellType.Numeric)
|
||
achievementrate.DownAssembly = (int)currentRow.GetCell(21).NumericCellValue;
|
||
//全线停机时间
|
||
if (currentRow.GetCell(22) != null && currentRow.GetCell(22).CellType == CellType.Numeric)
|
||
achievementrate.AllLineStopTime = (int)currentRow.GetCell(22).NumericCellValue;
|
||
|
||
//主要问题描述
|
||
if (currentRow.GetCell(23) != null && currentRow.GetCell(23).CellType == CellType.String)
|
||
achievementrate.MainProblemDescription = currentRow.GetCell(23).ToString();
|
||
//改善对策
|
||
if (currentRow.GetCell(24) != null && currentRow.GetCell(24).CellType == CellType.String)
|
||
achievementrate.Strategy = currentRow.GetCell(24).ToString();
|
||
//责任人
|
||
if (currentRow.GetCell(25) != null && currentRow.GetCell(25).CellType == CellType.String)
|
||
achievementrate.ResponsiblePerson = currentRow.GetCell(25).ToString();
|
||
|
||
|
||
achievementrate.RecordDate = RecordDate.Date;
|
||
|
||
AchievementrateList.Add(achievementrate);
|
||
}
|
||
}
|
||
|
||
if (AchievementrateList.Count == 0)
|
||
{
|
||
return 0;
|
||
}
|
||
UseTran2(() =>
|
||
{
|
||
Context.Deleteable<ProPlanAchievementrateVersion2>().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand();
|
||
Context.Insertable(AchievementrateList).ExecuteCommand();
|
||
});
|
||
return AchievementrateList.Count;
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new CustomException("excel文件异常,请详细核对");
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public byte[] ExportData(DateTime exportDate)
|
||
{
|
||
string templatePath = Path.Combine(
|
||
Directory.GetCurrentDirectory(),
|
||
"wwwroot",
|
||
"ImportTemplate",
|
||
"PlanAchievementRate.xlsx"
|
||
);
|
||
|
||
if (!global::System.IO.File.Exists(templatePath))
|
||
{
|
||
throw new CustomException("Excel 模板文件不存在");
|
||
}
|
||
|
||
List<ProPlanAchievementrateVersion2> dataList = Context.Queryable<ProPlanAchievementrateVersion2>()
|
||
.Where(it => it.RecordDate.HasValue && it.RecordDate.Value.Date == exportDate.Date)
|
||
.ToList();
|
||
|
||
using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read))
|
||
{
|
||
IWorkbook workbook = WorkbookFactory.Create(fileStream);
|
||
ISheet sheet = workbook.GetSheetAt(0);
|
||
|
||
int startRowIndex = 4;
|
||
// 🆕 创建一个数字格式的 CellStyle(非百分比)
|
||
ICellStyle numberCellStyle = workbook.CreateCellStyle();
|
||
// 设置数据格式为常规数字("0.00" 是保留两位小数,也可以用 "0" 或 "General")
|
||
numberCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.0"); // 或者使用 "General" 表示常规
|
||
|
||
for (int i = 0; i < dataList.Count; i++)
|
||
{
|
||
var item = dataList[i];
|
||
IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i);
|
||
|
||
// 为每个需要数字的单元格设置值,并应用 numberCellStyle
|
||
row.CreateCell(1).SetCellValue(item.Project); // 项目名称,一般为字符串,无需数字格式
|
||
SetCellValueWithStyle(row, 12, (double)(item.SummaryActualNum ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 13, (double)(item.SummaryPlanNum ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 14, (double)item.SummaryPlanAchievementRate, numberCellStyle);
|
||
SetCellValueWithStyle(row, 15, (double)(item.DownQuality ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 16, (double)(item.DownSuppler ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 17, (double)(item.DownDeviceFailure ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 18, (double)(item.DownDeviceDebug ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 19, (double)(item.DownLogisticsWaitMaterial ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 20, (double)(item.DownLackMaterial ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 21, (double)(item.DownInjection ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 22, (double)(item.DownAssembly ?? 0), numberCellStyle);
|
||
SetCellValueWithStyle(row, 23, (double)(item.AllLineStopTime ?? 0), numberCellStyle);
|
||
|
||
// 以下为字符串类型字段,不需要数字格式
|
||
row.CreateCell(24).SetCellValue(item.MainProblemDescription);
|
||
row.CreateCell(25).SetCellValue(item.Strategy);
|
||
row.CreateCell(26).SetCellValue(item.ResponsiblePerson);
|
||
}
|
||
|
||
// 改为写入 MemoryStream 然后转成 byte[]
|
||
var memoryStream = new MemoryStream();
|
||
workbook.Write(memoryStream);
|
||
// memoryStream.Position = 0;
|
||
return memoryStream.ToArray(); // ✅ 直接返回 byte 数组
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
// 🔧 辅助方法:为单元格设置值并应用数字样式
|
||
private static void SetCellValueWithStyle(IRow row, int columnIndex, double value, ICellStyle style)
|
||
{
|
||
ICell cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex);
|
||
cell.SetCellValue(value);
|
||
cell.CellStyle = style; // 应用数字格式样式
|
||
}
|
||
|
||
// 🔧 如果有 int 或可空数字,可以重载该方法,例如:
|
||
private static void SetCellValueWithStyle(IRow row, int columnIndex, int value, ICellStyle style)
|
||
{
|
||
ICell cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex);
|
||
cell.SetCellValue(value);
|
||
cell.CellStyle = style;
|
||
}
|
||
|
||
static (DateTime Monday, DateTime Sunday) GetWeekStartAndEnd(DateTime date)
|
||
{
|
||
// 获取该日期是星期几(DayOfWeek 枚举:Sunday=0, Monday=1, ..., Saturday=6)
|
||
DayOfWeek dayOfWeek = date.DayOfWeek;
|
||
|
||
// 计算本周一的日期
|
||
int diffToMonday = (int)DayOfWeek.Monday - (int)dayOfWeek;
|
||
DateTime monday = date.AddDays(diffToMonday);
|
||
|
||
// 计算本周日的日期(周一 + 6 天)
|
||
DateTime sunday = monday.AddDays(6);
|
||
|
||
return (monday, sunday);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 月生产数据统计列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public List<MonthProuctModel> GetQueryMonthProduct(MonthProductDto parm)
|
||
{
|
||
(DateTime FirstDay, DateTime LastDay) Handlemonth = GetFirstAndLastDayOfMonth(parm.SearchYearMonth);
|
||
var predicate = Expressionable.Create<ProPlanAchievementrateVersion2>()
|
||
.And(it => it.RecordDate >= Handlemonth.FirstDay && it.RecordDate <= Handlemonth.LastDay);
|
||
|
||
var response = Context.Queryable<ProPlanAchievementrateVersion2>()
|
||
.Where(predicate.ToExpression())
|
||
.GroupBy(it => new { it.Project, it.RecordDate })
|
||
.Select(it => new ProPlanAchievementrateVersion2()
|
||
{
|
||
Project = it.Project,
|
||
SummaryActualNum = SqlFunc.AggregateSum(it.SummaryActualNum ?? 0),
|
||
SummaryPlanNum = SqlFunc.AggregateSum(it.SummaryPlanNum ?? 0),
|
||
RecordDate = it.RecordDate
|
||
})
|
||
.OrderBy(it => it.Project)
|
||
.ToList();
|
||
|
||
var monthProuctModelList = response.GroupBy(it => it.Project)
|
||
.Select(group => new MonthProuctModel()
|
||
{
|
||
Project = group.Key,
|
||
DayProuctModels = group.Select(it2 => new DayProuctModel()
|
||
{
|
||
DayIndex = it2.RecordDate?.Day ?? 0,
|
||
SummaryActualNum = it2.SummaryActualNum,
|
||
SummaryPlanNum = it2.SummaryPlanNum,
|
||
}).OrderBy(t => t.DayIndex).ToList(),
|
||
|
||
}).OrderBy(t => t.Project).ToList();
|
||
|
||
monthProuctModelList.ForEach(t =>
|
||
{
|
||
t.SummaryActualNum = t.DayProuctModels.Sum(it => it.SummaryActualNum ?? 0);
|
||
t.SummaryPlanNum = t.DayProuctModels.Sum(it => it.SummaryPlanNum ?? 0);
|
||
t.SummaryPlanAchievementRate = Math.Round(t.SummaryActualNum * 100.0M / t.SummaryPlanNum ?? 1, 2);
|
||
});
|
||
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<MonthProuctModel> 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");
|
||
}
|
||
}
|
||
}
|
||
} |