唐召明
authored
|
1
|
using Hh.Mes.POJO.Entity;
|
唐召明
authored
|
2
|
using Hh.Mes.POJO.EnumEntitys;
|
唐召明
authored
|
3
|
using Hh.Mes.POJO.WebEntity.equipment;
|
唐召明
authored
|
4
|
using SqlSugar;
|
唐召明
authored
|
5
6
7
8
9
10
|
using System;
using System.Linq;
namespace Quartz.Job.Jobs
{
/// <summary>
|
唐召明
authored
|
11
|
/// 保养记录生成
|
唐召明
authored
|
12
13
14
15
16
17
18
|
/// </summary>
public class MaintainJob : JobBase
{
public override void ExecuteJob(IJobExecutionContext context)
{
var equipments = _dbContext.Queryable<base_equipment>().Where(x => x.isEnable).ToList();
//保养规则
|
唐召明
authored
|
19
20
|
var busEquipmentMaintainRuleHeads = _dbContext.Queryable<bus_equipment_maintain_rule_head>().Where(x => SqlFunc.Subqueryable<base_equipment>().Where(s => s.isEnable && s.equipmentMaintainRuleCode == x.equipmentRuleCode).Any()).ToList();
|
唐召明
authored
|
21
|
var ruleHeadKeys = busEquipmentMaintainRuleHeads.Select(x => x.keys).ToList();
|
唐召明
authored
|
22
|
|
唐召明
authored
|
23
24
|
var busEquipmentMaintainRuleDetails = _dbContext.Queryable<bus_equipment_maintain_rule_detail>().Where(x => ruleHeadKeys.Contains(x
.headKeys)).ToList();
|
唐召明
authored
|
25
|
|
唐召明
authored
|
26
27
28
|
//设备部件
var equipmentTypeCodes = equipments.Select(x => x.equipmentTypeCode).Distinct().ToList();
var equipmentParts = _dbContext.Queryable<base_equipment_part>().Where(x => equipmentTypeCodes.Contains(x.equipmentTypeCode)).ToList();
|
唐召明
authored
|
29
30
|
var busEquipmentMaintainRuleDetailIndicatorRels = _dbContext.Queryable<bus_equipment_maintain_rule_detail_indicator_rel>().ToList();
var busEquipmentMaintainRecordTops = _dbContext.Queryable<bus_equipment_maintain_record_top>().Where(x => SqlFunc.Subqueryable<base_equipment>().Where(s => s.equipmentCode == x.equipmentCode).Any()).ToList();
|
唐召明
authored
|
31
32
33
|
foreach (var equipment in equipments)
{
|
唐召明
authored
|
34
|
try
|
唐召明
authored
|
35
|
{
|
唐召明
authored
|
36
37
38
39
40
41
|
var ruleHead = busEquipmentMaintainRuleHeads.Where(x => x.equipmentRuleCode == equipment.equipmentMaintainRuleCode).First();
//未匹配到维护规则,则跳过
if (ruleHead == null)
{
continue;
}
|
唐召明
authored
|
42
|
|
唐召明
authored
|
43
44
45
|
//无明细数据,则跳过
var ruleDetails = busEquipmentMaintainRuleDetails.Where(x => x.headKeys == ruleHead.keys).ToList();
if (ruleDetails.Count == 0)
|
唐召明
authored
|
46
|
{
|
唐召明
authored
|
47
48
|
continue;
}
|
唐召明
authored
|
49
|
|
唐召明
authored
|
50
51
52
53
54
55
56
|
var currentEquipmentParts = equipmentParts.Where(x => x.equipmentTypeCode == equipment.equipmentTypeCode).ToList();
var busEquipmentMaintainRecordTop = busEquipmentMaintainRecordTops.Where(x => x.equipmentCode == equipment.equipmentCode).FirstOrDefault();
//开启事务
_dbContext.Ado.BeginTran();
if (busEquipmentMaintainRecordTop == null)
|
唐召明
authored
|
57
|
{
|
唐召明
authored
|
58
|
busEquipmentMaintainRecordTop = new bus_equipment_maintain_record_top
|
唐召明
authored
|
59
|
{
|
唐召明
authored
|
60
|
keys = Guid.NewGuid(),
|
唐召明
authored
|
61
|
equipmentCode = equipment.equipmentCode,
|
唐召明
authored
|
62
|
equipmentName = equipment.equipmentName,
|
唐召明
authored
|
63
64
65
66
|
equipmentTypeCode = equipment.equipmentTypeCode,
factoryCode = equipment.factoryCode,
projectKeys = equipment.projectKeys,
createTime = DateTime.Now,
|
唐召明
authored
|
67
|
createBy = nameof(MaintainJob),
|
唐召明
authored
|
68
|
};
|
唐召明
authored
|
69
|
busEquipmentMaintainRecordTop.id = _dbContext.Insertable(busEquipmentMaintainRecordTop).ExecuteReturnIdentity();
|
唐召明
authored
|
70
71
|
}
|
唐召明
authored
|
72
73
74
|
var busEquipmentMaintainRecordHeads = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys).ToList();
var busEquipmentMaintainRecordDetails = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.status != (int)EnumMaintainStatus.Maintained).ToList();
foreach (var equipmentPart in currentEquipmentParts)
|
唐召明
authored
|
75
|
{
|
唐召明
authored
|
76
77
78
79
80
81
82
|
//当前部件的保养规则
var currentRuleDetail = ruleDetails.Where(x => x.equipmentPartCode == equipmentPart.partCode).FirstOrDefault();
if (currentRuleDetail == null)
{
//当前部件未配置保养规则,则跳过
continue;
}
|
唐召明
authored
|
83
|
|
唐召明
authored
|
84
|
var indicatorTypes = busEquipmentMaintainRuleDetailIndicatorRels.Where(x => x.ruleDetailKeys == currentRuleDetail.detailKeys).ToList();
|
唐召明
authored
|
85
|
|
唐召明
authored
|
86
87
88
89
90
|
//保养周期类型未设定,则跳过
if (indicatorTypes.Count == 0)
{
continue;
}
|
唐召明
authored
|
91
|
|
唐召明
authored
|
92
93
|
var busEquipmentMaintainRecordHead = busEquipmentMaintainRecordHeads.Where(x => x.partCode == equipmentPart.partCode).FirstOrDefault();
if (busEquipmentMaintainRecordHead == null)
|
唐召明
authored
|
94
|
{
|
唐召明
authored
|
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
busEquipmentMaintainRecordHead = new bus_equipment_maintain_record_head
{
topKeys = busEquipmentMaintainRecordTop.keys,
headKeys = Guid.NewGuid(),
equipmentCode = equipment.equipmentCode,
equipmentTypeCode = equipment.equipmentTypeCode,
factoryCode = equipment.factoryCode,
projectKeys = equipment.projectKeys,
partCode = equipmentPart.partCode,
partName = equipmentPart.partName,
lastMaintainTime = DateTime.Now,
nextMaintainTime = DateTime.Now,
createTime = DateTime.Now,
createBy = nameof(MaintainJob),
};
busEquipmentMaintainRecordHead.id = _dbContext.Insertable(busEquipmentMaintainRecordHead).ExecuteReturnIdentity();
}
|
唐召明
authored
|
112
|
|
唐召明
authored
|
113
114
115
116
|
//当前部件上次保养时间
var lastMaintainTime = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.status == (int)EnumMaintainStatus.Maintained).Max(x => x.maintainTime) ?? DateTime.Now;
//下次保养时间
var nextMaintainTime = indicatorTypes.Select(indicatorType =>
|
唐召明
authored
|
117
|
{
|
唐召明
authored
|
118
119
120
121
122
123
124
125
|
if (indicatorType.indicatorTypeName == EnumIndicatorType.天.ToString())
{
return lastMaintainTime.AddDays(indicatorType.indicatorTypeCode);
}
else if (indicatorType.indicatorTypeName == EnumIndicatorType.月.ToString())
{
return lastMaintainTime.AddMonths(indicatorType.indicatorTypeCode);
}
|
唐召明
authored
|
126
127
128
129
130
131
132
133
134
|
//else if (indicatorType.indicatorTypeName == EnumIndicatorType.季度.ToString())
//{
// return lastMaintainTime.AddMonths(indicatorType.indicatorTypeCode * 3);
//}
//else if (indicatorType.indicatorTypeName == EnumIndicatorType.年.ToString())
//{
// return lastMaintainTime.AddYears(indicatorType.indicatorTypeCode);
//}
else if (indicatorType.indicatorTypeName.Contains(EnumIndicatorType.公里数.ToString()))
|
唐召明
authored
|
135
136
137
|
{
//待定
}
|
唐召明
authored
|
138
139
140
141
142
143
144
145
|
//else if (indicatorType.indicatorTypeName == EnumIndicatorType.圈数.ToString())
//{
// //待定
//}
//else if (indicatorType.indicatorTypeName == EnumIndicatorType.次数.ToString())
//{
// //待定
//}
|
唐召明
authored
|
146
147
148
149
150
151
152
|
//不支持
return DateTime.MaxValue;
}).Min();
if (nextMaintainTime == DateTime.MaxValue)
{
continue;
|
唐召明
authored
|
153
154
|
}
|
唐召明
authored
|
155
156
157
158
159
|
//解析保养规则,生成保养记录,待完善
var busEquipmentMaintainRecordDetail = busEquipmentMaintainRecordDetails.Where(x => x.headKeys == busEquipmentMaintainRecordHead.headKeys && x.partCode == equipmentPart.partCode).FirstOrDefault();
var nextDurationTime = (nextMaintainTime - DateTime.Now).Days;
//无记录,则需要新增
if (busEquipmentMaintainRecordDetail == null)
|
唐召明
authored
|
160
|
{
|
唐召明
authored
|
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
//基础数据
busEquipmentMaintainRecordDetail = new bus_equipment_maintain_record_detail
{
topKeys = busEquipmentMaintainRecordTop.keys,
headKeys = busEquipmentMaintainRecordHead.headKeys,
equipmentCode = equipment.equipmentCode,
equipmentTypeCode = equipment.equipmentTypeCode,
projectKeys = equipment.projectKeys,
factoryCode = equipment.factoryCode,
partCode = equipmentPart.partCode,
partName = equipmentPart.partName,
lastMaintainTime = lastMaintainTime,
nextMaintainTime = nextMaintainTime,
nextDurationTime = nextDurationTime,
};
|
唐召明
authored
|
176
177
|
_dbContext.Insertable(busEquipmentMaintainRecordDetail).ExecuteCommand();
}
|
唐召明
authored
|
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
|
else
{
busEquipmentMaintainRecordDetail.nextMaintainTime = nextMaintainTime;
busEquipmentMaintainRecordDetail.nextDurationTime = (nextMaintainTime - DateTime.Now).Days;
_dbContext.Updateable(busEquipmentMaintainRecordDetail).UpdateColumns(x => new
{
x.nextMaintainTime,
x.nextDurationTime,
}).ExecuteCommand();
}
//更新Head表
var headSumPrice = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.headKeys == busEquipmentMaintainRecordHead.headKeys && x.maintainTime != null).Sum(x => x.maintainPrice);
busEquipmentMaintainRecordHead.lastMaintainTime = busEquipmentMaintainRecordDetail.lastMaintainTime;
busEquipmentMaintainRecordHead.nextMaintainTime = nextMaintainTime;
busEquipmentMaintainRecordHead.nextMaintianDate = nextDurationTime;
busEquipmentMaintainRecordHead.sumPrice = headSumPrice;
busEquipmentMaintainRecordHead.updateTime = DateTime.Now;
busEquipmentMaintainRecordHead.updateBy = nameof(MaintainJob);
_dbContext.Updateable(busEquipmentMaintainRecordHead).UpdateColumns(x => new
{
x.lastMaintainTime,
x.nextMaintainTime,
x.nextMaintianDate,
x.sumPrice,
x.updateTime,
x.updateBy,
}).ExecuteCommand();
|
唐召明
authored
|
207
208
|
}
|
唐召明
authored
|
209
210
211
212
213
214
|
//更新概述表
var topMaintainNumber = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.status == (int)EnumMaintainStatus.Maintained).Count();
var topLastMaintainTime = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys).Max(x => x.lastMaintainTime);
var topToBeMaintainedNumber = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.status != (int)EnumMaintainStatus.Maintained).Count();
|
唐召明
authored
|
215
|
|
唐召明
authored
|
216
|
var topSumAmount = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys).Sum(x => x.sumPrice);
|
唐召明
authored
|
217
|
|
唐召明
authored
|
218
219
220
221
222
223
224
|
busEquipmentMaintainRecordTop.maintainNumber = topMaintainNumber;
busEquipmentMaintainRecordTop.lastMaintainTime = topLastMaintainTime;
busEquipmentMaintainRecordTop.toBeMaintainedNumber = topToBeMaintainedNumber;
busEquipmentMaintainRecordTop.sumAmount = topSumAmount;
busEquipmentMaintainRecordTop.updateTime = DateTime.Now;
busEquipmentMaintainRecordTop.updateBy = nameof(MaintainJob);
_dbContext.Updateable(busEquipmentMaintainRecordTop).UpdateColumns(x => new
|
唐召明
authored
|
225
|
{
|
唐召明
authored
|
226
|
x.maintainNumber,
|
唐召明
authored
|
227
|
x.lastMaintainTime,
|
唐召明
authored
|
228
229
230
231
|
x.toBeMaintainedNumber,
x.sumAmount,
x.updateTime,
x.updateBy
|
唐召明
authored
|
232
233
|
}).ExecuteCommand();
|
唐召明
authored
|
234
235
236
237
|
//提交
_dbContext.Ado.CommitTran();
}
catch (Exception ex)
|
唐召明
authored
|
238
|
{
|
唐召明
authored
|
239
240
241
242
|
//异常回滚
_dbContext.Ado.RollbackTran();
throw ex;
}
|
唐召明
authored
|
243
244
245
246
|
}
}
}
}
|