zhuangpei-mesbackend/ZR.Service/MES/dev/DeviceTaskExecuteService.cs

353 lines
12 KiB
C#
Raw Normal View History

2024-05-31 10:17:33 +08:00
using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Repository;
2024-06-03 18:26:07 +08:00
2024-05-31 10:17:33 +08:00
using System.Linq;
2024-06-03 18:26:07 +08:00
2024-06-01 11:20:28 +08:00
using JinianNet.JNTemplate;
2024-06-03 15:45:21 +08:00
using Microsoft.AspNetCore.Authentication;
2024-06-03 18:26:07 +08:00
using ZR.Model.MES.dev;
using ZR.Model.MES.dev.Dto;
using ZR.Service.MES.dev.IService;
2024-05-31 10:17:33 +08:00
namespace ZR.Service.MES.dev
{
/// <summary>
/// 任务执行Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IDeviceTaskExecuteService), ServiceLifetime = LifeTime.Transient)]
public class DeviceTaskExecuteService : BaseService<DeviceTaskExecute>, IDeviceTaskExecuteService
{
/// <summary>
/// 查询任务执行列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<DeviceTaskExecuteDto> GetList(DeviceTaskExecuteQueryDto parm)
{
var predicate = Expressionable.Create<DeviceTaskExecute>();
var response = Queryable()
//.OrderBy("Id asc")
.Where(predicate.ToExpression())
.ToPage<DeviceTaskExecute, DeviceTaskExecuteDto>(parm);
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public DeviceTaskExecute GetInfo(int Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加任务执行
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DeviceTaskExecute AddDeviceTaskExecute(DeviceTaskExecute model)
{
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改任务执行
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateDeviceTaskExecute(DeviceTaskExecute model)
{
//var response = Update(w => w.Id == model.Id, it => new DeviceTaskExecute()
//{
// TaskName = model.TaskName,
// Type = model.Type,
// DistributedTime = model.DistributedTime,
// StartTime = model.StartTime,
// EndTime = model.EndTime,
// Status = model.Status,
// Remark = model.Remark,
// CreatedBy = model.CreatedBy,
// CreatedTime = model.CreatedTime,
// UpdatedBy = model.UpdatedBy,
// UpdatedTime = model.UpdatedTime,
//});
//return response;
return Update(model, true);
}
/// <summary>
/// 清空任务执行
/// </summary>
/// <returns></returns>
public bool TruncateDeviceTaskExecute()
{
var newTableName = $"device_task_execute_{DateTime.Now:yyyyMMdd}";
if (Queryable().Any() && !Context.DbMaintenance.IsAnyTable(newTableName))
{
Context.DbMaintenance.BackupTable("device_task_execute", newTableName);
}
2024-06-01 11:20:28 +08:00
2024-05-31 10:17:33 +08:00
return Truncate();
}
/// <summary>
/// 导入任务执行
/// </summary>
/// <returns></returns>
public (string, object, object) ImportDeviceTaskExecute(List<DeviceTaskExecute> list)
{
var x = Context.Storageable(list)
.SplitInsert(it => !it.Any())
.SplitError(x => x.Item.Type.IsEmpty(), "任务类型1是巡检2是点检不能为空")
//.WhereColumns(it => it.UserName)//如果不是主键可以这样实现多字段it=>new{it.x1,it.x2}
.ToStorage();
var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;
2024-06-01 11:20:28 +08:00
string msg = $"插入{x.InsertList.Count} 更新{x.UpdateList.Count} 错误数据{x.ErrorList.Count} 不计算数据{x.IgnoreList.Count} 删除数据{x.DeleteList.Count} 总共{x.TotalList.Count}";
2024-05-31 10:17:33 +08:00
Console.WriteLine(msg);
//输出错误信息
foreach (var item in x.ErrorList)
{
Console.WriteLine("错误" + item.StorageMessage);
}
foreach (var item in x.IgnoreList)
{
Console.WriteLine("忽略" + item.StorageMessage);
}
return (msg, x.ErrorList, x.IgnoreList);
}
2024-06-01 11:20:28 +08:00
/// <summary>
/// 任务计划调度 扫描每日巡检任务
/// </summary>
/// <returns></returns>
2024-06-03 15:45:21 +08:00
public int ScanEveryTask()
2024-06-01 11:20:28 +08:00
{
int result = 0;
// 今日时间
DateTime CurrentTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
//1. 扫描巡检
List<DeviceRouteInspectionPlan> deviceRouteInspectionPlan_execute_List = new List<DeviceRouteInspectionPlan>();
#region 1.1
List<DeviceRouteInspectionPlan> RouteInspectionPlan_day_List = Context.Queryable<DeviceRouteInspectionPlan>().Where(it => it.Type == 1)
.Where(it => it.Status == 1).ToList();
if (RouteInspectionPlan_day_List != null && RouteInspectionPlan_day_List.Count > 0)
{
foreach (var item in RouteInspectionPlan_day_List)
{
//开始日期
DateTime start_cal = (DateTime)item.CreatedTime;
start_cal = new DateTime(start_cal.Year, start_cal.Month, start_cal.Day, 0, 0, 0);
//循环周期
int cycle_period = (int)item.DayNum;
DateTime date = start_cal;
do
{
date = date.AddDays(cycle_period);
if (date == CurrentTime)
{
//今日要执行任务
deviceRouteInspectionPlan_execute_List.Add(item);
break;
}
} while (date <= CurrentTime);
}
}
#endregion
#region 1.2
List<DeviceRouteInspectionPlan> DeviceRouteInspectionPlan_week_list = Context.Queryable<DeviceRouteInspectionPlan>().Where(it => it.Type == 2).Where(it => it.Status == 1).ToList();
int dayofweek = (int)CurrentTime.DayOfWeek;
if (CurrentTime.DayOfWeek == DayOfWeek.Sunday)
{
dayofweek = 7;
}
if (DeviceRouteInspectionPlan_week_list != null && DeviceRouteInspectionPlan_week_list.Count > 0)
{
foreach (var item in RouteInspectionPlan_day_List)
{
string[] cycle_period = item.WeekList.Split(",");
for (int i = 0; i < cycle_period.Length; i++)
{
if (dayofweek == Convert.ToInt32(cycle_period[i]))
{
deviceRouteInspectionPlan_execute_List.Add((DeviceRouteInspectionPlan)item); break;
}
}
}
}
#endregion
#region 1.3
List<DeviceRouteInspectionPlan> DeviceRouteInspectionPlan_month_list = Context.Queryable<DeviceRouteInspectionPlan>().Where(it => it.Type == 3).Where(it => it.Status == 1).ToList();
2024-06-03 15:45:21 +08:00
2024-06-01 11:20:28 +08:00
if (DeviceRouteInspectionPlan_week_list != null && DeviceRouteInspectionPlan_week_list.Count > 0)
{
foreach (var item in RouteInspectionPlan_day_List)
{
string[] cycle_period = item.WeekList.Split(",");
for (int i = 0; i < cycle_period.Length; i++)
{
if (CurrentTime.Day == Convert.ToInt32(cycle_period[i]))
{
deviceRouteInspectionPlan_execute_List.Add((DeviceRouteInspectionPlan)item); break;
}
}
}
}
#endregion
// 插入数据库
2024-06-03 15:45:21 +08:00
List<DeviceTaskExecute> executes = new List<DeviceTaskExecute>();
foreach (var item in deviceRouteInspectionPlan_execute_List)
2024-06-01 11:20:28 +08:00
{
2024-06-03 15:45:21 +08:00
DeviceTaskExecute taskExecute = new DeviceTaskExecute();
taskExecute.TaskName = item.Name;
2024-06-01 11:20:28 +08:00
taskExecute.TaskName = item.Id;
2024-06-03 15:45:21 +08:00
taskExecute.Type = 1;
taskExecute.DistributedTime = DateTime.Now;
taskExecute.Status = 0;
2024-06-01 11:20:28 +08:00
taskExecute.CreatedTime = DateTime.Now;
executes.Add(taskExecute);
}
2024-06-03 15:45:21 +08:00
if (executes.Count > 0)
{
result = Context.Insertable(executes).ExecuteCommand();
2024-06-01 11:20:28 +08:00
}
return result;
}
/// <summary>
/// 派发点检任务
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2024-06-03 15:45:21 +08:00
public int ExecutionTask_point(string id)
2024-06-01 11:20:28 +08:00
{
2024-06-03 15:45:21 +08:00
DevicePointInspectionPlan point = Context.Queryable<DevicePointInspectionPlan>().Where(it => it.Id == id).First();
2024-06-01 11:20:28 +08:00
DeviceTaskExecute taskExecute = new DeviceTaskExecute();
taskExecute.TaskName = point.Name;
2024-06-03 15:45:21 +08:00
taskExecute.TaskId = point.Id;
taskExecute.Type = 2;
2024-06-01 11:20:28 +08:00
taskExecute.DistributedTime = DateTime.Now;
taskExecute.Status = 0;
taskExecute.CreatedTime = DateTime.Now;
2024-06-03 15:45:21 +08:00
return Context.Insertable(taskExecute).ExecuteCommand();
2024-06-01 11:20:28 +08:00
}
2024-06-03 15:45:21 +08:00
/// <summary>
/// 获取任务执行绑定的巡检任务和点检任务绑定的设备
/// </summary>
public List<DeviceAccount> AchieveTaskbindDevice(int id)
{
List<DeviceAccount> resul = null;
DeviceTaskExecute executeTask = Context.Queryable<DeviceTaskExecute>().Where(it => it.Id == id).First();
if (executeTask != null)
{
// 类型为巡检
if (executeTask.Type == 1)
{
resul = Context.Queryable<DeviceRelRpAt>()
.LeftJoin<DeviceAccount>((r, a) => r.FkDeviceAccountId == a.Id)
.Where(o => o.FkRouteInspectionPlanId == executeTask.TaskId)
.Select((r, a) => a)
.ToList();
}
// 类型为点检
else if (executeTask.Type == 2)
{
resul = Context.Queryable<DeviceRelPpAt>()
.LeftJoin<DeviceAccount>((p, a) => p.FkDeviceAccountId == a.Id)
.Where(p => p.FkPointInspectionPlanId == executeTask.TaskId)
.Select((r, a) => a)
.ToList();
}
}
return resul;
}
/// <summary>
/// 获取设备绑定的检查项
/// </summary>
public List<DeviceInspect> AchieveDevicebindInspect(int fk_device_id)
{
List<DeviceInspect> resul = null;
resul=Context.Queryable<DeviceRelAccountInspect>()
.LeftJoin<DeviceInspect>((r, i) => r.FkInspectId == i.Id)
.Where(r => r.FkAccountId == fk_device_id)
.Select((r, i) => i)
.ToList();
return resul;
}
/// <summary>
/// 获取检查项绑定的检查表单
/// </summary>
public List<DeviceFormConfig> AchieveInspectbindForm(string fk_device_inspect_id)
{
List<DeviceFormConfig> result = null;
result= Context.Queryable<DeviceFormConfig>()
.Where(it => it.FkDeviceInspectId == fk_device_inspect_id)
.ToList();
return result;
}
2024-05-31 10:17:33 +08:00
}
}