254 lines
11 KiB
C#
254 lines
11 KiB
C#
using System.Collections.Generic;
|
|
using DOAN.Model.BZFM;
|
|
using DOAN.Model.MES.SmartScreen.Material;
|
|
using DOAN.Model.MES.SmartScreen.Order.Dto;
|
|
using DOAN.Service.MES.SmartScreen.Order.IService;
|
|
using Infrastructure.Attribute;
|
|
using SqlSugar;
|
|
|
|
namespace DOAN.Service.MES.SmartScreen.Order
|
|
{
|
|
/// <summary>
|
|
/// 采购订单Service业务层处理
|
|
/// </summary>
|
|
[AppService(
|
|
ServiceType = typeof(IMaterialSmartScreenService),
|
|
ServiceLifetime = LifeTime.Transient
|
|
)]
|
|
public class MaterialSmartScreenService : BaseService<MmMaterial>, IMaterialSmartScreenService
|
|
{
|
|
public MaterialScreenDto GetMaterialScreenData()
|
|
{
|
|
MaterialScreenDto materialScreenDto = new MaterialScreenDto();
|
|
MaterialSummaryDto _Summary = new MaterialSummaryDto();
|
|
// 本月开始第一天
|
|
DateTime monthStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
|
|
// 本月结束最后一天
|
|
DateTime monthEnd = monthStart
|
|
.AddMonths(1)
|
|
.AddDays(-1)
|
|
.AddHours(23)
|
|
.AddMinutes(59)
|
|
.AddSeconds(59);
|
|
// 今日开始时间0点
|
|
DateTime todayStart = DateTime.Now.Date;
|
|
// 今日结束时间
|
|
DateTime todayEnd = todayStart.AddHours(23).AddMinutes(59).AddSeconds(59);
|
|
|
|
// 1. 获取汇总数据
|
|
// 本月领料
|
|
_Summary.RawMaterialOutMonth = Math.Abs(
|
|
Convert.ToDecimal(
|
|
Context
|
|
.Queryable<MmRecordOutbound>()
|
|
.Where(it => it.Remarks == null || it.Remarks != "已撤销")
|
|
.Where(it => it.TransactionType == "领料出库")
|
|
.Where(it => it.CreatedTime >= monthStart)
|
|
.Where(it => it.CreatedTime <= monthEnd)
|
|
.Sum(it => it.Quantity)
|
|
)
|
|
);
|
|
// 本月生产入库
|
|
_Summary.ProductInMonth = Math.Abs(
|
|
Convert.ToDecimal(
|
|
Context
|
|
.Queryable<MmRecordInbound>()
|
|
.Where(it => it.Remarks == null || it.Remarks != "已撤销")
|
|
.Where(it => it.TransactionType == "生产入库")
|
|
.Where(it => it.CreatedTime >= monthStart)
|
|
.Where(it => it.CreatedTime <= monthEnd)
|
|
.Sum(it => it.Quantity)
|
|
)
|
|
);
|
|
// 本月出货
|
|
_Summary.ProductOutMonth = Math.Abs(
|
|
Convert.ToDecimal(
|
|
Context
|
|
.Queryable<MmRecordOutbound>()
|
|
.Where(it => it.Remarks == null || it.Remarks != "已撤销")
|
|
.Where(it => it.TransactionType == "出货出库")
|
|
.Where(it => it.CreatedTime >= monthStart)
|
|
.Where(it => it.CreatedTime <= monthEnd)
|
|
.Sum(it => it.Quantity)
|
|
)
|
|
);
|
|
materialScreenDto.Summary = _Summary;
|
|
|
|
// 2. 获取物料清单信息
|
|
List<MaterialDetailDto> materialDetailDtos = Context
|
|
.Queryable<MmMaterial>()
|
|
.Where(it => it.Status == "启用")
|
|
.OrderBy(it => it.Type)
|
|
.Select(it => new MaterialDetailDto
|
|
{
|
|
Id = it.Id,
|
|
MaterialCode = it.MaterialCode,
|
|
MaterialName = it.MaterialName,
|
|
SupplierName = it.SupplierName,
|
|
CategoryCode = it.CategoryCode,
|
|
Type = it.Type,
|
|
Status = it.Status,
|
|
})
|
|
.ToList();
|
|
|
|
if (materialDetailDtos.Count > 0)
|
|
{
|
|
// 3. 批量获取按物料分组的统计数据
|
|
// 本月采购入库(按物料分组)
|
|
var monthProcurement = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_inbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '采购入库'
|
|
AND created_time >= @monthStart
|
|
AND created_time <= @monthEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { monthStart, monthEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
// 本月领料统计(按物料分组)
|
|
var monthMaterialOutData = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_outbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '领料出库'
|
|
AND created_time >= @monthStart
|
|
AND created_time <= @monthEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { monthStart, monthEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
|
|
// 本月生产入库统计(按物料分组)
|
|
var monthProductInboundData = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_inbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '生产入库'
|
|
AND created_time >= @monthStart
|
|
AND created_time <= @monthEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { monthStart, monthEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
|
|
// 本月出货统计(按物料分组)
|
|
var monthShipmentData = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_outbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '出货出库'
|
|
AND created_time >= @monthStart
|
|
AND created_time <= @monthEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { monthStart, monthEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
|
|
// 今日领料统计(按物料分组)
|
|
var todayMaterialOutData = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_outbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '领料出库'
|
|
AND created_time >= @todayStart
|
|
AND created_time <= @todayEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { todayStart, todayEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
|
|
// 今日生产入库统计(按物料分组)
|
|
var todayProductInboundData = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_inbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '生产入库'
|
|
AND created_time >= @todayStart
|
|
AND created_time <= @todayEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { todayStart, todayEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
|
|
// 今日出货统计(按物料分组)
|
|
var todayShipmentData = Context
|
|
.Ado.SqlQuery<dynamic>(
|
|
@"
|
|
SELECT material_code as MaterialCode, SUM(quantity) as Quantity
|
|
FROM mm_record_outbound
|
|
WHERE (remarks IS NULL OR remarks != '已撤销')
|
|
AND transaction_type = '出货出库'
|
|
AND created_time >= @todayStart
|
|
AND created_time <= @todayEnd
|
|
GROUP BY material_code
|
|
",
|
|
new { todayStart, todayEnd }
|
|
)
|
|
.ToDictionary(it => it.MaterialCode, it => Convert.ToDecimal(it.Quantity));
|
|
|
|
// 4. 并行处理物料数据
|
|
Parallel.ForEach(
|
|
materialDetailDtos,
|
|
item =>
|
|
{
|
|
item.MonthProcurement = (decimal)(
|
|
monthProcurement.TryGetValue(item.MaterialCode, out var value)
|
|
? value
|
|
: 0m
|
|
);
|
|
item.MonthMaterialOut = (decimal)(
|
|
monthMaterialOutData.TryGetValue(item.MaterialCode, out value)
|
|
? value
|
|
: 0m
|
|
);
|
|
item.MonthProductInbound = (decimal)(
|
|
monthProductInboundData.TryGetValue(item.MaterialCode, out value)
|
|
? value
|
|
: 0m
|
|
);
|
|
item.MonthShipment = (decimal)(
|
|
monthShipmentData.TryGetValue(item.MaterialCode, out value) ? value : 0m
|
|
);
|
|
item.TodayMaterialOut = (decimal)(
|
|
todayMaterialOutData.TryGetValue(item.MaterialCode, out value)
|
|
? value
|
|
: 0m
|
|
);
|
|
item.TodayProductInbound = (decimal)(
|
|
todayProductInboundData.TryGetValue(item.MaterialCode, out value)
|
|
? value
|
|
: 0m
|
|
);
|
|
item.TodayShipment = (decimal)(
|
|
todayShipmentData.TryGetValue(item.MaterialCode, out value) ? value : 0m
|
|
);
|
|
}
|
|
);
|
|
}
|
|
|
|
materialScreenDto.Materials = materialDetailDtos;
|
|
materialScreenDto.LastUpdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
return materialScreenDto;
|
|
}
|
|
}
|
|
}
|