重写模具保养

This commit is contained in:
2024-08-01 10:11:23 +08:00
parent 5df23e01ee
commit f6da16904d
23 changed files with 1532 additions and 50 deletions

View File

@@ -9,33 +9,36 @@ using Tnb.EquipMgr.Entities;
namespace JNPF.TaskScheduler.Listener
{
/// <summary>
/// 生成设备保养计划
/// 生成保养计划
/// </summary>
public class GenerateMaintainPlanTimeWorker : ISpareTimeWorker
{
private ISqlSugarRepository<EqpMaintainTemEquipH> _repository => App.GetService<ISqlSugarRepository<EqpMaintainTemEquipH>>();
// public GenerateSpotInspectionPlanTimeWorker(ISqlSugarRepository<EqpMaintainTemEquipH> repository)
private ISqlSugarRepository<ToolMaintainTemMoldH> _repository => App.GetService<ISqlSugarRepository<ToolMaintainTemMoldH>>();
// public GenerateSpotInspectionPlanTimeWorker(ISqlSugarRepository<ToolMaintainTemMoldH> repository)
// {
// _repository = repository;
// }
private IMessageService _sendMessageService => App.GetService<IMessageService>();
[SpareTime("0 0 0 * * ?", "生成设备保养计划", ExecuteType = SpareTimeExecuteTypes.Serial, StartNow = false)]
[SpareTime("0 0 0 * * ?", "生成保养计划", ExecuteType = SpareTimeExecuteTypes.Serial, StartNow = false)]
public async void GenerateSpotInspectionPlan(SpareTimer timer, long count)
{
Log.Information("----------------------开始生成设备保养计划----------------------");
Log.Information("----------------------开始生成保养计划----------------------");
var db = _repository.CopyNew();
try
{
var db = _repository.CopyNew();
List<EqpMaintainTemEquipH> eqpSpotInsTemEquipHsByOne = await db.Queryable<EqpMaintainTemEquipH>().Where(x => x.is_start == "1" && x.plan_cycle_unit == "1").ToListAsync();
List<EqpMaintainTemEquipH> eqpSpotInsTemEquipHsByCirculate = await db.Queryable<EqpMaintainTemEquipH>().Where(x => x.is_start == "1" && x.plan_cycle_unit == "2").ToListAsync();
//设备保养
List<EqpMaintainTemEquipH> eqpSpotInsTemEquipHsByOne = await db.Queryable<EqpMaintainTemEquipH>()
.Where(x => x.is_start == "1" && x.plan_cycle_unit == "1").ToListAsync();
List<EqpMaintainTemEquipH> eqpSpotInsTemEquipHsByCirculate = await db.Queryable<EqpMaintainTemEquipH>()
.Where(x => x.is_start == "1" && x.plan_cycle_unit == "2").ToListAsync();
List<EqpMaintainRecordH> tobeCreateList = new List<EqpMaintainRecordH>();
List<EqpMaintainTemEquipH> tobeCreateTemplets = new List<EqpMaintainTemEquipH>();
foreach (var item in eqpSpotInsTemEquipHsByOne)
{
if (item.start_time.AddDays((double)item.plan_cycle).ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))
if (item.start_time.AddDays((double)item.plan_cycle).ToString("yyyy-MM-dd") ==
DateTime.Now.ToString("yyyy-MM-dd"))
{
tobeCreateTemplets.Add(item);
}
@@ -55,37 +58,43 @@ namespace JNPF.TaskScheduler.Listener
// tobeCreateTemplets.Add(item);
// }
// }
//按上一个保养记录来生成数据
foreach (var item in eqpSpotInsTemEquipHsByCirculate)
{
EqpMaintainRecordH eqpMaintainRecordH = await db.Queryable<EqpMaintainRecordH>().Where(x=>x.maintain_tem_equip_id==item.id).OrderByDescending(x=>x.create_time).FirstAsync();
EqpMaintainRecordH eqpMaintainRecordH = await db.Queryable<EqpMaintainRecordH>()
.Where(x => x.maintain_tem_equip_id == item.id).OrderByDescending(x => x.create_time)
.FirstAsync();
if (eqpMaintainRecordH == null)
{
TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(item.start_time.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
if (ts3.TotalDays * 10 % (10 * (double)item.plan_cycle) == 0)
{
tobeCreateTemplets.Add(item);
}
}else if (eqpMaintainRecordH.status!="1")
{
TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(eqpMaintainRecordH.execute_time.Value.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts2 =
new TimeSpan(Convert.ToDateTime(item.start_time.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
if (ts3.TotalDays * 10 % (10 * (double)item.plan_cycle) == 0)
{
tobeCreateTemplets.Add(item);
}
}
else if (eqpMaintainRecordH.status != "1")
{
TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts2 = new TimeSpan(Convert
.ToDateTime(eqpMaintainRecordH.execute_time.Value.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
if (ts3.TotalDays * 10 % (10 * (double)item.plan_cycle) == 0)
{
tobeCreateTemplets.Add(item);
}
}
}
}
if (tobeCreateTemplets != null && tobeCreateTemplets.Count > 0)
{
List<EqpEquipment> equipments = await db.Queryable<EqpEquipment>().Where(x => x.life == Tnb.EquipMgr.EquipmentLife.ENABLE).ToListAsync();
List<EqpEquipment> equipments = await db.Queryable<EqpEquipment>()
.Where(x => x.life == Tnb.EquipMgr.EquipmentLife.ENABLE).ToListAsync();
int index = 1;
foreach (var item in tobeCreateTemplets)
{
@@ -111,8 +120,8 @@ namespace JNPF.TaskScheduler.Listener
repeat_user_id = item.repeat_user_id,
execute_user_id = item.duty_user_id,
create_time = DateTime.Now,
status = Tnb.EquipMgr.SpotInsRecordExecutionStatus.TOBEEXECUTED
});
}
@@ -122,9 +131,11 @@ namespace JNPF.TaskScheduler.Listener
if (tobeCreateList != null && tobeCreateList.Count > 0)
{
List<string> templetIDs = tobeCreateList.Select(x => x.maintain_tem_equip_id).ToList();
List<EqpMaintainTemEquipD> spotInsTemEquipDs = await db.Queryable<EqpMaintainTemEquipD>().Where(x => templetIDs.Contains(x.maintain_tem_equip_id)).ToListAsync();
List<EqpMaintainTemEquipD> spotInsTemEquipDs = await db.Queryable<EqpMaintainTemEquipD>()
.Where(x => templetIDs.Contains(x.maintain_tem_equip_id)).ToListAsync();
List<string> spotInsItemIDs = spotInsTemEquipDs.Select(x => x.maintain_item_id).ToList();
List<EqpMaintainItem> spotCheckItems = await db.Queryable<EqpMaintainItem>().Where(x => spotInsItemIDs.Contains(x.id)).ToListAsync();
List<EqpMaintainItem> spotCheckItems = await db.Queryable<EqpMaintainItem>()
.Where(x => spotInsItemIDs.Contains(x.id)).ToListAsync();
List<EqpMaintainRecordD> spotInsRecordDs = new List<EqpMaintainRecordD>();
@@ -133,7 +144,8 @@ namespace JNPF.TaskScheduler.Listener
List<string> spotInsItems = spotInsTemEquipDs
.Where(x => x.maintain_tem_equip_id == tobeCreatePlan.maintain_tem_equip_id)
.Select(x => x.maintain_item_id).ToList();
List<EqpMaintainItem> tobeCreateItems = spotCheckItems.Where(x => spotInsItems.Contains(x.id)).ToList();
List<EqpMaintainItem> tobeCreateItems =
spotCheckItems.Where(x => spotInsItems.Contains(x.id)).ToList();
foreach (var tobeCreateItem in tobeCreateItems)
{
spotInsRecordDs.Add(new EqpMaintainRecordD()
@@ -152,6 +164,7 @@ namespace JNPF.TaskScheduler.Listener
}
}
if (spotInsRecordDs.Count <= 0)
{
return;
@@ -176,7 +189,8 @@ namespace JNPF.TaskScheduler.Listener
}
else
{
List<EqpEquipment> equipments = await db.Queryable<EqpEquipment>().Where(x => x.life == Tnb.EquipMgr.EquipmentLife.ENABLE).ToListAsync();
List<EqpEquipment> equipments = await db.Queryable<EqpEquipment>()
.Where(x => x.life == Tnb.EquipMgr.EquipmentLife.ENABLE).ToListAsync();
foreach (var item in tobeCreateList)
{
try
@@ -187,22 +201,212 @@ namespace JNPF.TaskScheduler.Listener
var title = equipments.FirstOrDefault(x => x.id == item.equip_id).code + "设备保养提醒";
await _sendMessageService.SentMessage(users, title, title);
}
catch (Exception)
catch (Exception e)
{
Log.Error(e.Message);
}
}
}
Log.Information($"---------------生成{tobeCreateList.Count}个计划---------------");
}
//模具保养
List<ToolMaintainTemMoldH> toolMoldByOne = await db.Queryable<ToolMaintainTemMoldH>()
.Where(x => x.is_start == "1" && x.plan_cycle_unit == "1").ToListAsync();
List<ToolMaintainTemMoldH> toolMoldByCirculate = await db.Queryable<ToolMaintainTemMoldH>()
.Where(x => x.is_start == "1" && x.plan_cycle_unit == "2").ToListAsync();
List<ToolMoldMaintainRecordH> toolMoldTobeCreateList = new List<ToolMoldMaintainRecordH>();
List<ToolMaintainTemMoldH> toolMoldTobeCreateTemplets = new List<ToolMaintainTemMoldH>();
foreach (var item in toolMoldByOne)
{
if (item.start_time.AddDays((double)item.plan_cycle).ToString("yyyy-MM-dd") ==
DateTime.Now.ToString("yyyy-MM-dd"))
{
toolMoldTobeCreateTemplets.Add(item);
}
}
if (toolMoldByCirculate != null && toolMoldByCirculate.Count > 0)
{
//整除表示一个周期到了
// foreach (var item in toolMoldByCirculate)
// {
// TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Ticks);
// TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(item.start_time.ToString("yyyy-MM-dd")).Ticks);
// TimeSpan ts3 = ts1.Subtract(ts2).Duration();
// if (ts3.TotalDays * 10 % (10 * (double)item.plan_cycle) == 0)
// {
// toolMoldTobeCreateTemplets.Add(item);
// }
// }
//按上一个保养记录来生成数据
foreach (var item in toolMoldByCirculate)
{
ToolMoldMaintainRecordH eqpMaintainRecordH = await db.Queryable<ToolMoldMaintainRecordH>()
.Where(x => x.maintain_tem_mold_id == item.id).OrderByDescending(x => x.create_time)
.FirstAsync();
if (eqpMaintainRecordH == null)
{
TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts2 =
new TimeSpan(Convert.ToDateTime(item.start_time.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
if (ts3.TotalDays * 10 % (10 * (double)item.plan_cycle) == 0)
{
toolMoldTobeCreateTemplets.Add(item);
}
}
else if (eqpMaintainRecordH.status != "1")
{
TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts2 = new TimeSpan(Convert
.ToDateTime(eqpMaintainRecordH.execute_time.Value.ToString("yyyy-MM-dd")).Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
if (ts3.TotalDays * 10 % (10 * (double)item.plan_cycle) == 0)
{
toolMoldTobeCreateTemplets.Add(item);
}
}
}
}
if (toolMoldTobeCreateTemplets != null && toolMoldTobeCreateTemplets.Count > 0)
{
List<ToolMolds> toolMoldList = await db.Queryable<ToolMolds>().ToListAsync();
int index = 1;
foreach (var item in toolMoldTobeCreateTemplets)
{
//只有启用设备才生成计划
if (toolMoldList.FirstOrDefault(x => x.id == item.mold_id) == null)
continue;
string code = $"{DateTime.Now.ToString("yyyyMMdd") + (index++).ToString().PadLeft(3, '0')}";
toolMoldTobeCreateList.Add(new ToolMoldMaintainRecordH()
{
code = code,
// equip_type_id = item.equip_type_id,
mold_id = item.mold_id,
maintain_tem_mold_id = item.id,
plan_run_notice = item.plan_run_notice,
plan_run_notice_unit = item.plan_run_notice_unit,
plan_delay = item.plan_delay,
plan_delay_unit = item.plan_delay_unit,
send_post_info_user_id = item.send_post_info_user_id,
is_repeat = item.is_repeat,
repeat_post_info_user_id = item.repeat_post_info_user_id,
is_send = item.is_send,
repeat_user_id = item.repeat_user_id,
execute_user_id = item.duty_user_id,
create_time = DateTime.Now,
status = Tnb.EquipMgr.SpotInsRecordExecutionStatus.TOBEEXECUTED
});
}
}
if (toolMoldTobeCreateList != null && toolMoldTobeCreateList.Count > 0)
{
List<string> templetIDs = toolMoldTobeCreateList.Select(x => x.maintain_tem_mold_id).ToList();
List<ToolMaintainTemMoldD> spotInsTemEquipDs = await db.Queryable<ToolMaintainTemMoldD>()
.Where(x => templetIDs.Contains(x.maintain_tem_mold_id)).ToListAsync();
List<string> spotInsItemIDs = spotInsTemEquipDs.Select(x => x.maintain_item_id).ToList();
List<ToolMaintainItem> spotCheckItems = await db.Queryable<ToolMaintainItem>()
.Where(x => spotInsItemIDs.Contains(x.id)).ToListAsync();
List<ToolMoldMaintainRecordD> spotInsRecordDs = new List<ToolMoldMaintainRecordD>();
foreach (var tobeCreatePlan in toolMoldTobeCreateList)
{
List<string> spotInsItems = spotInsTemEquipDs
.Where(x => x.maintain_tem_mold_id == tobeCreatePlan.maintain_tem_mold_id)
.Select(x => x.maintain_item_id).ToList();
List<ToolMaintainItem> tobeCreateItems =
spotCheckItems.Where(x => spotInsItems.Contains(x.id)).ToList();
foreach (var tobeCreateItem in tobeCreateItems)
{
spotInsRecordDs.Add(new ToolMoldMaintainRecordD()
{
id = SnowflakeIdHelper.NextId(),
maintain_record_id = tobeCreatePlan.id,
maintain_tem_mold_id = tobeCreatePlan.maintain_tem_mold_id,
maintain_item_id = tobeCreateItem.id,
code = tobeCreateItem.code,
name = tobeCreateItem.name,
maintain_type = tobeCreateItem.maintain_type,
maintain_content = tobeCreateItem.maintain_content,
descrip = tobeCreateItem.descrip,
remark = tobeCreateItem.remark
});
}
}
if (spotInsRecordDs.Count <= 0)
{
return;
}
var dbResult = db.Ado.UseTran(() =>
{
if (toolMoldTobeCreateList != null && toolMoldTobeCreateList.Count > 0)
{
db.Insertable<ToolMoldMaintainRecordH>(toolMoldTobeCreateList).ExecuteCommand();
}
if (spotInsRecordDs != null && spotInsRecordDs.Count > 0)
{
db.Insertable<ToolMoldMaintainRecordD>(spotInsRecordDs).ExecuteCommand();
}
});
if (!dbResult.IsSuccess)
{
Console.WriteLine(dbResult.ErrorMessage);
Log.Error(dbResult.ErrorMessage);
}
else
{
List<ToolMolds> toolMoldList = await db.Queryable<ToolMolds>().ToListAsync();
foreach (var item in toolMoldTobeCreateList)
{
try
{
if (toolMoldList.FirstOrDefault(x => x.id == item.mold_id) == null)
continue;
var users = new List<string>() { item.execute_user_id };
var title = toolMoldList.FirstOrDefault(x => x.id == item.mold_id).mold_code + "模具保养提醒";
await _sendMessageService.SentMessage(users, title, title);
}
catch (Exception e)
{
Log.Error(e.Message);
}
}
}
Log.Information($"---------------生成{toolMoldTobeCreateList.Count}个计划---------------");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Log.Error(e.Message);
}
finally
{
db.Dispose();
}
Log.Information("----------------------结束生成设备保养计划----------------------");
Log.Information("----------------------结束生成保养计划----------------------");
}
}
}

View File

@@ -17,9 +17,9 @@ namespace JNPF.TaskScheduler.Listener
{
Log.Information("----------------------开始生成特种设备检验计划----------------------");
var db = _repository.CopyNew();
try
{
var db = _repository.AsSugarClient();
List<string?> ids = _repository.GetList(x => x.status == "0").Select(x => x.equip_id).Distinct()
.ToList();
@@ -86,7 +86,10 @@ namespace JNPF.TaskScheduler.Listener
Console.WriteLine(e.Message);
Log.Error(e.Message);
}
finally
{
db.Dispose();
}
Log.Information("----------------------结束生成特种设备检验计划----------------------");
}

View File

@@ -22,9 +22,9 @@ namespace JNPF.TaskScheduler.Listener
{
Log.Information("----------------------开始生成点巡检计划----------------------");
var db = _repository.CopyNew();
try
{
var db = _repository.CopyNew();
List<EqpSpotInsTemEquipH> eqpSpotInsTemEquipHsByOne = await db.Queryable<EqpSpotInsTemEquipH>().Where(x => x.is_start == "1" && x.plan_cycle_unit == "1").ToListAsync();
List<EqpSpotInsTemEquipH> eqpSpotInsTemEquipHsByCirculate = await db.Queryable<EqpSpotInsTemEquipH>().Where(x => x.is_start == "1" && x.plan_cycle_unit == "2").ToListAsync();
List<EqpSpotInsRecordH> tobeCreateList = new List<EqpSpotInsRecordH>();
@@ -199,7 +199,10 @@ namespace JNPF.TaskScheduler.Listener
Console.WriteLine(e.Message);
Log.Error(e.Message);
}
finally
{
db.Dispose();
}
Log.Information("----------------------结束生成点巡检计划----------------------");
}