zhuangpei-mesbackend/ZR.Service/MES/dev/DeviceAccountService.cs
qianhao.xu 42870e62cf 修正
2024-06-13 10:45:08 +08:00

510 lines
19 KiB
C#

using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Repository;
using System.Linq;
using ZR.Model.MES.dev;
using ZR.Model.MES.dev.Dto;
using ZR.Service.MES.dev.IService;
namespace ZR.Service.MES.dev
{
/// <summary>
/// 设备台账Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IDeviceAccountService), ServiceLifetime = LifeTime.Transient)]
public class DeviceAccountService : BaseService<DeviceAccount>, IDeviceAccountService
{
/// <summary>
/// 查询设备台账列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<DeviceAccountDto> GetList(DeviceAccountQueryDto parm)
{
// 全部设备parm.FkDeviceType == 1
// 提取parentId
DeviceType typeItem = Context.Queryable<DeviceType>()
.Where(it => it.Id == parm.FkDeviceType)
.Where(it => it.Status == 1)
.First();
int[] typeIds = [];
// 二级菜单
if (typeItem != null && typeItem.ParentId == 1)
{
typeIds = Context.Queryable<DeviceType>()
.Where(it => it.ParentId == parm.FkDeviceType)
.Where(it => it.Status == 1)
.Select(it => it.Id)
.ToArray();
}
// 非全部设备
var predicate = Expressionable.Create<DeviceAccount>()
.AndIF(!string.IsNullOrEmpty(parm.DeviceName), it => it.DeviceName.Contains(parm.DeviceName))
.AndIF(!string.IsNullOrEmpty(parm.DeviceSpecification), it => it.DeviceSpecification.Contains(parm.DeviceSpecification))
.AndIF(parm.Status > -1, it => it.Status == parm.Status)
.AndIF(typeItem != null && typeItem.ParentId > 1, it => it.FkDeviceType == parm.FkDeviceType)
.AndIF(typeItem != null && typeItem.ParentId == 1, it => typeIds.Contains(it.FkDeviceType))
;
var response = Queryable()
.Where(predicate.ToExpression())
.OrderByDescending(it => it.UpdatedTime)
.OrderByDescending(it => it.CreatedTime)
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
return response;
}
public PagedInfo<DeviceAccountDto> GetList_Route(DeviceAccountQueryDto2 parm)
{
var predicate = Expressionable.Create<DeviceAccount, DeviceRelRpAt>()
.AndIF(!string.IsNullOrEmpty(parm.DeviceName), (a, r) => a.DeviceName.Contains(parm.DeviceName))
.AndIF(!string.IsNullOrEmpty(parm.DeviceSpecification), (a, r) => a.DeviceSpecification.Contains(parm.DeviceSpecification))
.And((a, r) => a.Status == 1)
.AndIF(parm.FkDeviceType > 0, (a, r) => a.FkDeviceType == parm.FkDeviceType)
.AndIF(parm.Flag == 1, (a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
.AndIF(parm.Flag == 0, (a, r) => r.FkRouteInspectionPlanId != parm.fkRouteInspectionPlanId || r.FkRouteInspectionPlanId == null)
;
var response = Context.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelRpAt>((a, r) => a.Id == r.FkDeviceAccountId)
.Where(predicate.ToExpression())
.Select((a, r) => a)
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public DeviceAccount GetInfo(int Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加设备台账
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DeviceAccount AddDeviceAccount(DeviceAccount model)
{
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改设备台账
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateDeviceAccount(DeviceAccount model)
{
//var response = Update(w => w.Id == model.Id, it => new DeviceAccount()
//{
// FkDeviceType = model.FkDeviceType,
// DeviceName = model.DeviceName,
// DeviceCode = model.DeviceCode,
// Workshop = model.Workshop,
// Workline = model.Workline,
// Status = model.Status,
// DeviceImage = model.DeviceImage,
// DeviceFile = model.DeviceFile,
// DeviceSpecification = model.DeviceSpecification,
// ResponsiblePerson = model.ResponsiblePerson,
// Remark = model.Remark,
// CreatedBy = model.CreatedBy,
// CreatedTime = model.CreatedTime,
// UpdatedBy = model.UpdatedBy,
// UpdatedTime = model.UpdatedTime,
//});
//return response;
return Update(model, true);
}
public List<SelectTreeDto> GetSelectTree(DeviceAccountQueryDto parm)
{
try
{
var predicate = Expressionable.Create<DeviceAccount>()
.And(it => it.Status == 1);
List<SelectTreeDto> accountList = Queryable()
.Where(predicate.ToExpression())
.OrderByDescending(it => it.UpdatedTime)
.OrderByDescending(it => it.CreatedTime)
.Select(it => new SelectTreeDto
{
Id = null,
ParentId = it.FkDeviceType.ToString(),
Label = (it.DeviceName + '-' + it.DeviceCode),
Value = it.Id.ToString()
})
.ToList();
int[] fkDeviceTypeIds = accountList.Select(it => int.Parse(it.Value)).ToArray();
List<SelectTreeDto> deviceTypeList = Context.Queryable<DeviceType>()
.Where(it => fkDeviceTypeIds.Contains(it.Id))
.Select(it => new SelectTreeDto
{
Id = it.Id.ToString(),
ParentId = it.ParentId.ToString(),
Label = it.Name,
Value = it.Name
}).ToList();
List<SelectTreeDto> list = deviceTypeList.Concat(accountList).ToList();
return list;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 添加绑定关系 巡检计划和设备台账
/// </summary>
/// <param name="parm"></param>
/// <param name="CreatedBy"></param>
/// <returns></returns>
public int AddRelation(DeviceAccount_routeinspect_Dto parm, string CreatedBy)
{
List<DeviceRelRpAt> DeviceRelRpAt_list = new List<DeviceRelRpAt>();
if (parm.FkDeviceAccountIdList.Length > 0)
{
foreach (var id in parm.FkDeviceAccountIdList)
{
DeviceRelRpAt rel = new DeviceRelRpAt();
rel.FkRouteInspectionPlanId = parm.FkRouteInspectionPlanId;
rel.FkDeviceAccountId = id;
rel.CreatedBy = CreatedBy;
rel.CreatedTime = DateTime.Now;
DeviceRelRpAt_list.Add(rel);
}
}
int result = Context.Insertable(DeviceRelRpAt_list).ExecuteCommand();
return result;
}
/// <summary>
/// 移除关系
/// </summary>
/// <param name="FkRouteInspectionPlanId"></param>
/// <param name="FkDeviceAccountId"></param>
/// <returns></returns>
public int Remove_relation(string FkRouteInspectionPlanId, int FkDeviceAccountId)
{
return Context.Deleteable<DeviceRelRpAt>()
.Where(it => it.FkRouteInspectionPlanId == FkRouteInspectionPlanId)
.Where(it => it.FkDeviceAccountId == FkDeviceAccountId)
.ExecuteCommand();
}
#region
public PagedInfo<DeviceAccountDto> GetList_Point(DeviceAccountQueryDto3 parm)
{
var predicate = Expressionable.Create<DeviceAccount, DeviceRelPpAt>()
.AndIF(!string.IsNullOrEmpty(parm.DeviceName), (a, r) => a.DeviceName.Contains(parm.DeviceName))
.AndIF(!string.IsNullOrEmpty(parm.DeviceSpecification), (a, r) => a.DeviceSpecification.Contains(parm.DeviceSpecification))
.And((a, r) => a.Status == 1)
.AndIF(parm.FkDeviceType > 0, (a, r) => a.FkDeviceType == parm.FkDeviceType)
.AndIF(parm.Flag == 1, (a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
.AndIF(parm.Flag == 0, (a, r) => r.FkPointInspectionPlanId != parm.fkPointInspectionPlanId || r.FkPointInspectionPlanId == null)
;
var response = Context.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelPpAt>((a, r) => a.Id == r.FkDeviceAccountId)
.Where(predicate.ToExpression())
.Select((a, r) => a)
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
return response;
}
/// <summary>
/// 添加绑定关系 巡检计划和设备台账
/// </summary>
/// <param name="parm"></param>
/// <param name="CreatedBy"></param>
/// <returns></returns>
public int AddRelationPointAccount(DeviceAccount_pointinspect_Dto parm, string CreatedBy)
{
List<DeviceRelPpAt> DeviceRelRpAt_list = new List<DeviceRelPpAt>();
if (parm.FkDeviceAccountIdList.Length > 0)
{
foreach (var id in parm.FkDeviceAccountIdList)
{
DeviceRelPpAt rel = new DeviceRelPpAt();
rel.FkPointInspectionPlanId = parm.FkPointInspectionPlanId;
rel.FkDeviceAccountId = id;
rel.CreatedBy = CreatedBy;
rel.CreatedTime = DateTime.Now;
DeviceRelRpAt_list.Add(rel);
}
}
int result = Context.Insertable(DeviceRelRpAt_list).ExecuteCommand();
return result;
}
/// <summary>
/// 移除关系
/// </summary>
/// <param name="FkRouteInspectionPlanId"></param>
/// <param name="FkDeviceAccountId"></param>
/// <returns></returns>
public int RemoveRelationPointAccount(string FkPointInspectionPlanId, int FkDeviceAccountId)
{
return Context.Deleteable<DeviceRelPpAt>()
.Where(it => it.FkPointInspectionPlanId == FkPointInspectionPlanId)
.Where(it => it.FkDeviceAccountId == FkDeviceAccountId)
.ExecuteCommand();
}
#endregion
/// <summary>
/// 设备状态
/// </summary>
/// <param name="devicetype_id"> 设备类型id</param>
/// <returns></returns>
public DeviceStatusAnalysisDto GetDeviceStatus(int devicetype_id)
{
DeviceStatusAnalysisDto analysis = new DeviceStatusAnalysisDto();
// 车间
string Workshop = "";
// 设备总数
int AllTotal = 0;
//正常设备数
int NormalTotal = 0;
// 未巡点总数
int UnmaintainedTotal = 0;
// 报修中总数
int DamageTotal = 0;
// 停用总数
int NoUseTotal = 0;
// 1 获取设备类型下面所有的线
List<DeviceType> All_device_type = null;
// DeviceType line = Context.Queryable<DeviceType>().Where(it => it.Id == devicetype_id).First();
//if (line.ParentId == 1)
//{
// Workshop = line.Name;
// All_device_type = Context.Queryable<DeviceType>().Where(it => it.ParentId == devicetype_id).ToList();
//}
//else
//{
// Workshop = Context.Queryable<DeviceType>().Where(it => it.ParentId == line.ParentId).First().Name;
// All_device_type = new List<DeviceType> { line };
//}
All_device_type = FindAllLeafNodes(devicetype_id);
DeviceType final_parent = FindFinalParentNode(devicetype_id);
Workshop = final_parent.Name;
List<LineDetail> LineDetailList = new List<LineDetail>();
// 2 获取每个线下的所有设备
if (All_device_type.Count > 0)
{
foreach (DeviceType type in All_device_type)
{
LineDetail lineDetail = new LineDetail();
lineDetail.Workshop = Workshop;
lineDetail.Workline = type.Name;
List<DeviceAccount> accounts = Context.Queryable<DeviceAccount>()
.Where(it => it.FkDeviceType == type.Id).ToList();
lineDetail.Total = accounts.Count;
AllTotal = AllTotal + lineDetail.Total;
List<DeviceInfo> Children = new List<DeviceInfo>();
if (accounts.Count > 0)
{
// 3 获取每个设备所有状态
foreach (var item in accounts)
{
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.Id = item.Id;
deviceInfo.DeviceName = item.DeviceName;
deviceInfo.DeviceCode = item.DeviceCode;
//0.4断是否停止中
if (item.Status == 0)
{
NoUseTotal++;
deviceInfo.DeviceStatus = 4;
NormalTotal++;
Children.Add(deviceInfo);
continue;
}
//0.3 判断是否报修中
bool isExist = Context.Queryable<DeviceRepair>()
.Where(it => it.FkDeviceId == item.Id)
.Where(it => it.Status == 0 || it.Status == 1 || it.Status == 3).Any();
if (isExist)
{
DamageTotal++;
deviceInfo.DeviceStatus = 3;
NormalTotal++;
Children.Add(deviceInfo);
continue;
}
//0.2 判断是否维护中
//处理点检
int isExist_point = Context.Queryable<DeviceTaskExecute>()
.LeftJoin<DevicePointInspectionPlan>((e, p) => e.TaskId == p.Id)
.LeftJoin<DeviceRelPpAt>((e, p, r) => p.Id == r.FkPointInspectionPlanId)
.Where((e, p, r) => r.FkDeviceAccountId == item.Id)
.Where((e, p, r) => e.Status == 0 || e.Status == 1 || e.Status == 3)
.Count();
UnmaintainedTotal = UnmaintainedTotal + isExist_point;
if (isExist_point>0)
{
deviceInfo.DeviceStatus = 2;
NormalTotal++;
Children.Add(deviceInfo);
continue;
}
//处理巡检
int isExist_route = Context.Queryable<DeviceTaskExecute>()
.LeftJoin<DeviceRouteInspectionPlan>((e, p) => e.TaskId == p.Id)
.LeftJoin<DeviceRelPpAt>((e, p, r) => p.Id == r.FkPointInspectionPlanId)
.Where((e, p, r) => r.FkDeviceAccountId == item.Id)
.Where((e, p, r) => e.Status == 0 || e.Status == 1 || e.Status == 3)
.Count();
UnmaintainedTotal = UnmaintainedTotal + isExist_route;
if (isExist_route>0)
{
deviceInfo.DeviceStatus = 2;
NormalTotal++;
Children.Add(deviceInfo);
continue;
}
deviceInfo.DeviceStatus = 1;
NormalTotal++;
Children.Add(deviceInfo);
}
}
lineDetail.Children = Children;
LineDetailList.Add(lineDetail);
}
}
analysis.AllTotal = AllTotal;
analysis.UnmaintainedTotal = UnmaintainedTotal;
analysis.LineDetailList = LineDetailList;
analysis.NormalTotal = NormalTotal;
return analysis;
}
/// <summary>
/// 查找指定节点的所有最终子节点
/// </summary>
/// <param name="targetId"></param>
/// <returns></returns>
public List<DeviceType> FindAllLeafNodes(int targetId)
{
List<int> childIds = new List<int> { targetId };
List<DeviceType> result = new List<DeviceType>();
while (childIds.Any())
{
int parentId = childIds.First();
childIds.RemoveAt(0);
List<int> children = Context.Queryable<DeviceType>().Where(n => n.ParentId == parentId).Select(n => n.Id).ToList();
if (children.Any())
{
childIds.AddRange(children);
}
else
{
result.Add(Context.Queryable<DeviceType>().First(n => n.Id == parentId));
}
}
return result;
}
/// <summary>
/// 用于在父子节点对象中,根据输入的节点 ID 找到最终的父节点:
/// </summary>
/// <param name="nodeId"></param>
/// <returns></returns>
public DeviceType FindFinalParentNode(int nodeId)
{
DeviceType currentNode = Context.Queryable<DeviceType>().Where(n => n.Id == nodeId).First();
if (currentNode == null)
{
return null;
}
DeviceType parentNode = Context.Queryable<DeviceType>().Where(n => n.Id == currentNode.ParentId).First();
while (parentNode != null)
{
currentNode = parentNode;
parentNode = Context.Queryable<DeviceType>().Where(n => n.Id == currentNode.ParentId).First();
}
return currentNode;
}
}
}