using PadApplication.DB;
|
using PadApplication.Entites.DbModels;
|
using PadApplication.Entites.Dto;
|
using SqlSugar;
|
|
namespace PadApplication.Services;
|
|
/// <summary>
|
/// 设备点检管理服务类
|
/// 负责处理设备点检记录的查询和保存操作
|
/// </summary>
|
public class EquipmentInspectionManager : Repository<MesEquipmentInspection>
|
{
|
// 日常点检项目配置(固定顺序)
|
private readonly string[] _dailyInspectionItems =
|
[
|
"机芯是否清洁",
|
"设备开关",
|
"改善运行",
|
"清理清洁或调试是否有异常",
|
"工艺参数",
|
"机油运行是否有异常"
|
];
|
|
// 月度点检项目配置(固定顺序)
|
private readonly string[] _monthlyInspectionItems =
|
[
|
"电表油面是否正常是否有渗漏",
|
"万向接头复查并加油"
|
];
|
|
/// <summary>
|
/// 查询设备点检记录
|
/// </summary>
|
/// <param name="query">查询参数</param>
|
/// <returns>点检记录响应DTO</returns>
|
public EquipmentInspectionResponseDto QueryInspectionRecord(EquipmentInspectionQueryDto query)
|
{
|
// 参数验证
|
if (string.IsNullOrWhiteSpace(query.MachineNo))
|
throw new Exception("机台编号不能为空");
|
|
if (string.IsNullOrWhiteSpace(query.Date))
|
throw new Exception("日期不能为空");
|
|
// 解析年月
|
if (!DateTime.TryParseExact(query.Date, "yyyy-MM", null,
|
System.Globalization.DateTimeStyles.None, out var date))
|
throw new Exception("日期格式不正确,应为 yyyy-MM");
|
|
var year = date.Year;
|
var month = date.Month;
|
|
// 查询主表记录
|
var inspection = Db.Queryable<MesEquipmentInspection>()
|
.Where(x => x.MachineNo == query.MachineNo
|
&& x.InspectionYear == year
|
&& x.InspectionMonth == month
|
&& !x.IsDeleted)
|
.First();
|
|
// 初始化返回数据(全部为false)
|
var dailyChecks = InitializeBoolArray(6, 31);
|
var monthlyChecks = InitializeBoolArray(2, 31);
|
string? lastUpdateTime = null;
|
string? operatorName = null;
|
|
// 如果存在记录,查询明细数据
|
if (inspection != null)
|
{
|
var details = Db.Queryable<MesEquipmentInspectionDetail>()
|
.Where(x => x.InspectionId == inspection.InspectionId)
|
.ToList();
|
|
// 填充日常点检数据
|
foreach (var detail in details.Where(d => d.ItemType == "daily"))
|
{
|
if (detail.ItemIndex >= 0 && detail.ItemIndex < 6
|
&& detail.DayOfMonth >= 1 && detail.DayOfMonth <= 31)
|
{
|
dailyChecks[detail.ItemIndex][detail.DayOfMonth - 1] = detail.IsChecked;
|
}
|
}
|
|
// 填充月度点检数据
|
foreach (var detail in details.Where(d => d.ItemType == "monthly"))
|
{
|
if (detail.ItemIndex >= 0 && detail.ItemIndex < 2
|
&& detail.DayOfMonth >= 1 && detail.DayOfMonth <= 31)
|
{
|
monthlyChecks[detail.ItemIndex][detail.DayOfMonth - 1] = detail.IsChecked;
|
}
|
}
|
|
lastUpdateTime = inspection.UpdatedTime?.ToString("yyyy-MM-dd HH:mm:ss")
|
?? inspection.CreatedTime.ToString("yyyy-MM-dd HH:mm:ss");
|
operatorName = inspection.UpdatedByName ?? inspection.CreatedByName;
|
}
|
|
return new EquipmentInspectionResponseDto
|
{
|
MachineNo = query.MachineNo,
|
Date = query.Date,
|
DailyChecks = dailyChecks,
|
MonthlyChecks = monthlyChecks,
|
LastUpdateTime = lastUpdateTime,
|
Operator = operatorName
|
};
|
}
|
|
/// <summary>
|
/// 保存设备点检记录
|
/// </summary>
|
/// <param name="saveDto">保存参数</param>
|
/// <returns>保存结果响应DTO</returns>
|
public EquipmentInspectionSaveResponseDto SaveInspectionRecord(EquipmentInspectionSaveDto saveDto)
|
{
|
// 参数验证
|
ValidateSaveData(saveDto);
|
|
// 解析年月
|
var date = DateTime.ParseExact(saveDto.Date, "yyyy-MM", null);
|
var year = date.Year;
|
var month = date.Month;
|
|
var savedTime = DateTime.Now;
|
long inspectionId;
|
|
// 使用事务保存数据
|
UseTransaction(db =>
|
{
|
// 查询是否已存在记录
|
var existingInspection = db.Queryable<MesEquipmentInspection>()
|
.Where(x => x.MachineNo == saveDto.MachineNo
|
&& x.InspectionYear == year
|
&& x.InspectionMonth == month
|
&& !x.IsDeleted)
|
.First();
|
|
if (existingInspection != null)
|
{
|
// 更新现有记录
|
inspectionId = existingInspection.InspectionId;
|
|
db.Updateable<MesEquipmentInspection>()
|
.SetColumns(x => x.UpdatedBy == saveDto.Operator)
|
.SetColumns(x => x.UpdatedByName == saveDto.OperatorName)
|
.SetColumns(x => x.UpdatedTime == savedTime)
|
.Where(x => x.InspectionId == inspectionId)
|
.ExecuteCommand();
|
|
// 删除旧的明细记录
|
db.Deleteable<MesEquipmentInspectionDetail>()
|
.Where(x => x.InspectionId == inspectionId)
|
.ExecuteCommand();
|
}
|
else
|
{
|
// 创建新记录
|
var newInspection = new MesEquipmentInspection
|
{
|
MachineNo = saveDto.MachineNo,
|
InspectionYear = year,
|
InspectionMonth = month,
|
InspectionDate = saveDto.Date,
|
CreatedBy = saveDto.Operator,
|
CreatedByName = saveDto.OperatorName,
|
CreatedTime = savedTime,
|
Status = 0,
|
IsDeleted = false
|
};
|
|
// 插入主表记录(使用Oracle序列自动生成InspectionId)
|
inspectionId = db.Insertable(newInspection)
|
.ExecuteReturnBigIdentity();
|
}
|
|
// 插入明细记录
|
var details = new List<MesEquipmentInspectionDetail>();
|
|
// 添加日常点检明细
|
for (int itemIndex = 0; itemIndex < 6; itemIndex++)
|
{
|
for (int day = 1; day <= 31; day++)
|
{
|
var isChecked = saveDto.DailyChecks[itemIndex][day - 1];
|
|
details.Add(new MesEquipmentInspectionDetail
|
{
|
InspectionId = inspectionId,
|
ItemType = "daily",
|
ItemIndex = itemIndex,
|
ItemName = _dailyInspectionItems[itemIndex],
|
DayOfMonth = day,
|
IsChecked = isChecked,
|
CheckedBy = isChecked ? saveDto.Operator : null,
|
CheckedByName = isChecked ? saveDto.OperatorName : null,
|
CheckedTime = isChecked ? savedTime : null,
|
CreatedTime = savedTime,
|
IsReviewed = false
|
});
|
}
|
}
|
|
// 添加月度点检明细
|
for (int itemIndex = 0; itemIndex < 2; itemIndex++)
|
{
|
for (int day = 1; day <= 31; day++)
|
{
|
var isChecked = saveDto.MonthlyChecks[itemIndex][day - 1];
|
|
details.Add(new MesEquipmentInspectionDetail
|
{
|
InspectionId = inspectionId,
|
ItemType = "monthly",
|
ItemIndex = itemIndex,
|
ItemName = _monthlyInspectionItems[itemIndex],
|
DayOfMonth = day,
|
IsChecked = isChecked,
|
CheckedBy = isChecked ? saveDto.Operator : null,
|
CheckedByName = isChecked ? saveDto.OperatorName : null,
|
CheckedTime = isChecked ? savedTime : null,
|
CreatedTime = savedTime,
|
IsReviewed = false
|
});
|
}
|
}
|
|
// 批量插入明细记录(使用Oracle序列自动生成DetailId)
|
var insertCount = db.Insertable(details)
|
.ExecuteCommand();
|
|
if (insertCount <= 0)
|
throw new Exception("保存点检明细失败");
|
|
return insertCount;
|
});
|
|
return new EquipmentInspectionSaveResponseDto
|
{
|
Success = true,
|
RecordId = $"EI{saveDto.Date.Replace("-", "")}{saveDto.MachineNo}",
|
SavedTime = savedTime.ToString("yyyy-MM-dd HH:mm:ss")
|
};
|
}
|
|
/// <summary>
|
/// 验证保存数据的有效性
|
/// </summary>
|
private void ValidateSaveData(EquipmentInspectionSaveDto saveDto)
|
{
|
if (string.IsNullOrWhiteSpace(saveDto.MachineNo))
|
throw new Exception("参数错误:机台编号不能为空");
|
|
if (string.IsNullOrWhiteSpace(saveDto.Date))
|
throw new Exception("参数错误:日期不能为空");
|
|
if (!DateTime.TryParseExact(saveDto.Date, "yyyy-MM", null,
|
System.Globalization.DateTimeStyles.None, out _))
|
throw new Exception("参数错误:日期格式不正确,应为 yyyy-MM");
|
|
// 验证日常点检数据
|
if (saveDto.DailyChecks == null || saveDto.DailyChecks.Length != 6)
|
throw new Exception("数据验证失败:dailyChecks 必须是 6 行的二维数组");
|
|
for (int i = 0; i < 6; i++)
|
{
|
if (saveDto.DailyChecks[i] == null || saveDto.DailyChecks[i].Length != 31)
|
throw new Exception($"数据验证失败:dailyChecks[{i}] 必须包含 31 个元素");
|
}
|
|
// 验证月度点检数据
|
if (saveDto.MonthlyChecks == null || saveDto.MonthlyChecks.Length != 2)
|
throw new Exception("数据验证失败:monthlyChecks 必须是 2 行的二维数组");
|
|
for (int i = 0; i < 2; i++)
|
{
|
if (saveDto.MonthlyChecks[i] == null || saveDto.MonthlyChecks[i].Length != 31)
|
throw new Exception($"数据验证失败:monthlyChecks[{i}] 必须包含 31 个元素");
|
}
|
}
|
|
/// <summary>
|
/// 初始化布尔二维数组
|
/// </summary>
|
private bool[][] InitializeBoolArray(int rows, int cols)
|
{
|
var array = new bool[rows][];
|
for (int i = 0; i < rows; i++)
|
{
|
array[i] = new bool[cols];
|
}
|
return array;
|
}
|
}
|