MaintainJob.cs
9.71 KB
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
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.WebEntity.equipment;
using System;
using System.Linq;
namespace Quartz.Job.Jobs
{
/// <summary>
/// 保养规则生成
/// </summary>
/// <remarks>待完善</remarks>
public class MaintainJob : JobBase
{
public override void ExecuteJob(IJobExecutionContext context)
{
var equipments = _dbContext.Queryable<base_equipment>().Where(x => x.isEnable).ToList();
//保养规则
var maintainRuleCodes = equipments.Select(x => x.equipmentMaintainRuleCode).Distinct().ToList();
var busEquipmentMaintainRuleHeads = _dbContext.Queryable<bus_equipment_maintain_rule_head>().Where(x => maintainRuleCodes.Contains(x.equipmentRuleCode)).ToList();
var ruleHeadKeys = busEquipmentMaintainRuleHeads.Select(x => x.keys).ToList();
var busEquipmentMaintainRuleDetails = _dbContext.Queryable<bus_equipment_maintain_rule_detail>().Where(x => ruleHeadKeys.Contains(x
.headKeys)).ToList();
//设备部件
var equipmentTypeCodes = equipments.Select(x => x.equipmentTypeCode).Distinct().ToList();
var equipmentParts = _dbContext.Queryable<base_equipment_part>().Where(x => equipmentTypeCodes.Contains(x.equipmentTypeCode)).ToList();
foreach (var equipment in equipments)
{
var ruleHead = busEquipmentMaintainRuleHeads.Where(x => x.equipmentRuleCode == equipment.equipmentMaintainRuleCode).First();
//未匹配到维护规则,则跳过
if (ruleHead == null)
{
continue;
}
var ruleDetails = busEquipmentMaintainRuleDetails.Where(x => x.headKeys == ruleHead.keys).ToList();
if (ruleDetails.Count == 0)
{
continue;
}
var currentEquipmentParts = equipmentParts.Where(x => x.equipmentTypeCode == equipment.equipmentTypeCode).ToList();
var busEquipmentMaintainRecordTop = _dbContext.Queryable<bus_equipment_maintain_record_top>().Where(x => x.equipmentCode == equipment.equipmentCode).First();
if (busEquipmentMaintainRecordTop == null)
{
busEquipmentMaintainRecordTop = new bus_equipment_maintain_record_top
{
keys = Guid.NewGuid(),
equipmentCode = equipment.equipmentCode,
equipmentName = equipment.equipmentName,
equipmentTypeCode = equipment.equipmentTypeCode,
factoryCode = equipment.factoryCode,
projectKeys = equipment.projectKeys,
createTime = DateTime.Now
};
busEquipmentMaintainRecordTop.id = _dbContext.Insertable(busEquipmentMaintainRecordTop).ExecuteReturnIdentity();
}
foreach (var equipmentPart in currentEquipmentParts)
{
var busEquipmentMaintainRecordHead = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.partCode == equipmentPart.partCode).First();
if (busEquipmentMaintainRecordHead == null)
{
busEquipmentMaintainRecordHead = new bus_equipment_maintain_record_head
{
keys = Guid.NewGuid(),
topKeys = busEquipmentMaintainRecordTop.keys,
equipmentCode = equipment.equipmentCode,
equipmentTypeCode = equipment.equipmentTypeCode,
factoryCode = equipment.factoryCode,
projectKeys = equipment.projectKeys,
partCode = equipmentPart.partCode,
partName = equipmentPart.partName,
lastMaintainTime = DateTime.Now,
createTime = DateTime.Now,
};
busEquipmentMaintainRecordHead.id = _dbContext.Insertable(busEquipmentMaintainRecordHead).ExecuteReturnIdentity();
}
//当前部件的保养规则
var currentRuleDetail = ruleDetails.Where(x => x.equipmentPartCode == equipmentPart.partCode).FirstOrDefault();
if (currentRuleDetail == null)
{
//当前部件未配置保养规则,则跳过
continue;
}
//保养周期类型未设定,则跳过
if (string.IsNullOrWhiteSpace(currentRuleDetail.indicatorType))
{
continue;
}
var indicatorTypes = currentRuleDetail.indicatorType.Split(',');
var indicator = currentRuleDetail.indicator;
//解析保养规则,生成保养记录,待完善
var busEquipmentMaintainRecordDetail = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.headKeys == busEquipmentMaintainRecordHead.keys && x.equipmentCode == equipment.equipmentCode && x.partCode == equipmentPart.partCode && x.maintainTime == null).First();
//无记录,则需要新增
if (busEquipmentMaintainRecordDetail == null)
{
busEquipmentMaintainRecordDetail = new bus_equipment_maintain_record_detail
{
topKeys = busEquipmentMaintainRecordTop.keys,
headKeys = busEquipmentMaintainRecordHead.keys,
equipmentCode = equipment.equipmentCode,
equipmentTypeCode = equipment.equipmentTypeCode,
projectKeys = equipment.projectKeys,
factoryCode = equipment.factoryCode,
partCode = equipmentPart.partCode,
partName = equipmentPart.partName,
maintainPrice = currentRuleDetail.price,
};
}
//根据类型来生成记录
foreach (var indicatorType in indicatorTypes)
{
if (indicatorType == "类型1")
{
_dbContext.Insertable(busEquipmentMaintainRecordDetail).ExecuteCommand();
break;
}
if (indicatorType == "类型2")
{
_dbContext.Insertable(busEquipmentMaintainRecordDetail).ExecuteCommand();
break;
}
}
//更新Head表
busEquipmentMaintainRecordHead.sumPrice = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.headKeys == busEquipmentMaintainRecordHead.keys && x.maintainTime != null).Sum(x => x.maintainPrice);
var lastMaintainTime = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.headKeys == busEquipmentMaintainRecordHead.keys && x.maintainTime != null).Max(x => x.maintainTime) ?? DateTime.Now;
var nextMaintainTime = DateTime.Now;//待完善
busEquipmentMaintainRecordHead.lastMaintainTime = lastMaintainTime;
busEquipmentMaintainRecordHead.nextMaintainTime = nextMaintainTime;
busEquipmentMaintainRecordHead.nextMaintianDate = (nextMaintainTime - busEquipmentMaintainRecordHead.lastMaintainTime).Days;
busEquipmentMaintainRecordHead.updateTime = DateTime.Now;
_dbContext.Updateable(busEquipmentMaintainRecordHead).UpdateColumns(x => new
{
x.sumPrice,
x.nextMaintianDate,
x.lastMaintainTime,
x.nextMaintainTime,
x.status,
x.updateTime
}).ExecuteCommand();
}
//更新概述表
busEquipmentMaintainRecordTop.maintainNumber = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.maintainTime != null).Count();
busEquipmentMaintainRecordTop.toBeMaintainedNumber = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.maintainTime == null).Count();
busEquipmentMaintainRecordTop.lastMaintainTime = _dbContext.Queryable<bus_equipment_maintain_record_head>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys).Max(x => x.lastMaintainTime);
busEquipmentMaintainRecordTop.sumAmount = _dbContext.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.topKeys == busEquipmentMaintainRecordTop.keys && x.maintainTime != null).Sum(x => x.maintainPrice);
busEquipmentMaintainRecordTop.updateTime = DateTime.Now;
_dbContext.Updateable(busEquipmentMaintainRecordTop).UpdateColumns(x => new
{
x.maintainNumber,
x.toBeMaintainedNumber,
x.lastMaintainTime,
x.sumAmount,
x.updateTime
}).ExecuteCommand();
}
}
}
}