快乐的昕的电脑
2025-12-04 4f33ac39577a3a20860e3895f748d6f10bd39b23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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;
    }
}