using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using DOAN.Model;
using DOAN.Repository;
using System.Linq;
using DOAN.Model.MES.dev;
using DOAN.Model.MES.dev.Dto;
using DOAN.Service.MES.dev.IService;
using System.ComponentModel;
using System.Diagnostics;
namespace DOAN.Service.MES.dev
{
///
/// 设备台账Service业务层处理
///
[AppService(ServiceType = typeof(IDeviceAccountService), ServiceLifetime = LifeTime.Transient)]
public class DeviceAccountService : BaseService, IDeviceAccountService
{
///
/// 查询设备台账列表
///
///
///
public PagedInfo GetList(DeviceAccountQueryDto parm)
{
// 全部设备parm.FkDeviceType == 1
// 提取parentId
DeviceType typeItem = Context.Queryable()
.Where(it => it.Id == parm.FkDeviceType)
.Where(it => it.Status == 1)
.First();
int[] typeIds = [];
// 二级菜单
if (typeItem != null && typeItem.ParentId == 1)
{
typeIds = Context.Queryable()
.Where(it => it.ParentId == parm.FkDeviceType)
.Where(it => it.Status == 1)
.Select(it => it.Id)
.ToArray();
}
// 非全部设备
var predicate = Expressionable.Create()
.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(parm);
return response;
}
///
/// 通常在数据库层面执行联表查询(JOIN)会比在程序层面逐一查询左表和右表更快一些。
///
///
///
public PagedInfo GetList_Route2(DeviceAccountQueryDto2 parm)
{
var predicate = Expressionable.Create()
.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)
.And((a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
;
var temp = Context.Queryable()
.LeftJoin((a, r) => a.Id == r.FkDeviceAccountId)
.Where(predicate.ToExpression()).Select((a, r) => a);
if (parm.Flag == 0)
{
int[] exist = temp.Select(a => a.Id).ToArray();
temp = Context.Queryable().Where(it => !exist.Contains(it.Id)).Distinct();
}
var reponse = temp
.ToPage(parm);
return reponse;
}
public PagedInfo GetList_Route(DeviceAccountQueryDto2 parm)
{
//未绑定
if (parm.Flag == 0)
{
int[] account_array = Context
.Queryable()
.LeftJoin((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
.Select((a, r) => a.Id)
.ToArray();
var predicate = Expressionable.Create()
.AndIF(!string.IsNullOrEmpty(parm.DeviceName), (a) => a.DeviceName.Contains(parm.DeviceName))
.AndIF(!string.IsNullOrEmpty(parm.DeviceSpecification), (a) => a.DeviceSpecification.Contains(parm.DeviceSpecification))
.And((a) => a.Status == 1)
.AndIF(parm.FkDeviceType > 0, (a) => a.FkDeviceType == parm.FkDeviceType)
;
return Context.Queryable()
.Where(it => !account_array.Contains(it.Id))
.Where(predicate.ToExpression())
.ToPage(parm)
;
}
else if (parm.Flag == 1)
{
// 获取绑定的account
return Context.Queryable()
.LeftJoin((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
.ToPage(parm);
}
return null;
}
public PagedInfo GetList_Point2(DeviceAccountQueryDto3 parm)
{
var predicate = Expressionable.Create()
.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)
.And((a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
;
var temp = Context.Queryable()
.LeftJoin((a, r) => a.Id == r.FkDeviceAccountId)
.Where(predicate.ToExpression()).Select((a, r) => a);
if (parm.Flag == 0)
{
int[] exist = temp.Select(a => a.Id).ToArray();
temp = Context.Queryable().Where(it => !exist.Contains(it.Id)).Distinct();
}
var reponse = temp
.ToPage(parm);
return reponse;
}
public PagedInfo GetList_Point(DeviceAccountQueryDto3 parm)
{
//未绑定
if (parm.Flag == 0)
{
int[] account_array = Context
.Queryable()
.LeftJoin((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
.Select((a, r) => a.Id)
.ToArray();
var predicate = Expressionable.Create()
.AndIF(!string.IsNullOrEmpty(parm.DeviceName), (a) => a.DeviceName.Contains(parm.DeviceName))
.AndIF(!string.IsNullOrEmpty(parm.DeviceSpecification), (a) => a.DeviceSpecification.Contains(parm.DeviceSpecification))
.And((a) => a.Status == 1)
.AndIF(parm.FkDeviceType > 0, (a) => a.FkDeviceType == parm.FkDeviceType)
;
return Context.Queryable()
.Where(it => !account_array.Contains(it.Id))
.Where(predicate.ToExpression())
.ToPage(parm)
;
}
else if (parm.Flag == 1)
{
// 获取绑定的account
return Context.Queryable()
.LeftJoin((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
.ToPage(parm);
}
return null;
}
///
/// 获取详情
///
///
///
public DeviceAccount GetInfo(int Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
///
/// 添加设备台账
///
///
///
public DeviceAccount AddDeviceAccount(DeviceAccount model)
{
return Context.Insertable(model).ExecuteReturnEntity();
}
///
/// 修改设备台账
///
///
///
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 GetSelectTree2(DeviceAccountQueryDto parm)
{
try
{
var predicate = Expressionable.Create()
.And(it => it.Status == 1);
List 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.FkDeviceType.ToString()
})
.ToList();
int[] fkDeviceTypeIds = accountList.Select(it => int.Parse(it.Value)).ToArray();
List deviceTypeList = Context.Queryable()
.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 list = deviceTypeList.Concat(accountList).ToList();
return list;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
/// 获取树节点
///
///
///
///
public List GetSelectTree(DeviceAccountQueryDto parm)
{
// 获取 类型父子节点
List deviceTypeList = Context.Queryable()
.Where(it => it.Status == 1)
.OrderBy(it => it.Id)
.Select(it => new SelectTreeDto
{
Id =(it.Id * 10000).ToString(),
ParentId = (it.ParentId*10000).ToString(),
Label = it.Name,
Value = it.Name
})
.ToList();
// 获取 类型表 绑定的设备 父子节点
List accountList = Context.Queryable().Where(it => it.Status == 1).OrderBy(it => it.Id)
.Select(it => new SelectTreeDto
{
// Id = it.Id.ToString() + it.FkDeviceType.ToString(),//解决合并后id重复
Id = it.Id.ToString(),//解决合并后id重复
ParentId = (it.FkDeviceType*10000).ToString(),
Label = it.DeviceCode + "||" +it.DeviceName ,
Value = it.Id.ToString()
}).ToList();
return deviceTypeList.Concat(accountList).OrderBy(it => int.Parse(it.Id)).ToList();
}
///
/// 添加绑定关系 巡检计划和设备台账
///
///
///
///
public int AddRelation(DeviceAccount_routeinspect_Dto parm, string CreatedBy)
{
List DeviceRelRpAt_list = new List();
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;
}
///
/// 移除关系
///
///
///
///
public int Remove_relation(string FkRouteInspectionPlanId, int FkDeviceAccountId)
{
return Context.Deleteable()
.Where(it => it.FkRouteInspectionPlanId == FkRouteInspectionPlanId)
.Where(it => it.FkDeviceAccountId == FkDeviceAccountId)
.ExecuteCommand();
}
#region 点检和设备绑定关系
///
/// 添加绑定关系 巡检计划和设备台账
///
///
///
///
public int AddRelationPointAccount(DeviceAccount_pointinspect_Dto parm, string CreatedBy)
{
List DeviceRelRpAt_list = new List();
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;
}
///
/// 移除关系
///
///
///
///
public int RemoveRelationPointAccount(string FkPointInspectionPlanId, int FkDeviceAccountId)
{
return Context.Deleteable()
.Where(it => it.FkPointInspectionPlanId == FkPointInspectionPlanId)
.Where(it => it.FkDeviceAccountId == FkDeviceAccountId)
.ExecuteCommand();
}
#endregion
///
/// 设备状态
///
/// 设备类型id
///
public DeviceStatusAnalysisDto GetDeviceStatus(int devicetype_id)
{
DeviceStatusAnalysisDto analysis = new DeviceStatusAnalysisDto();
// 设备总数
int AllTotal = 0;
//正常设备数
int NormalTotal = 0;
// 未巡点总数
int UnmaintainedTotal = 0;
// 报修中总数
int DamageTotal = 0;
// 停用总数
int NoUseTotal = 0;
Stopwatch stopwatch = new Stopwatch();
// 开始计时
stopwatch.Start();
// 1 获取设备类型下面所有的线
List All_device_type = null;
All_device_type = FindAllLeafNodes(devicetype_id);
List LineDetailList = new List();
// 停止计时
stopwatch.Stop();
// 输出执行时间
Console.WriteLine("代码段执行时间: {0} 秒", stopwatch.ElapsedMilliseconds / 1000);
// 2 获取每个线下的所有设备
if (All_device_type.Count > 0)
{
List all_deviceTyepe_s = Context.Queryable().ToList();
List all_account_s = Context.Queryable().ToList();
List all_deviceRepair_s = Context.Queryable().ToList();
var query_point = Context.Queryable()
.LeftJoin((e, p) => e.PlanId == p.Id)
.LeftJoin((e, p, r) => p.Id == r.FkPointInspectionPlanId)
.Where((e, p, r) => e.Status == 0 || e.Status == 1 || e.Status == 3)
.Select((e, p, r) => new
{
FkDeviceAccountId = r.FkDeviceAccountId,
}).ToList();
var query_route = Context.Queryable()
.LeftJoin((e, p) => e.PlanId == p.Id)
.LeftJoin((e, p, r) => p.Id == r.FkPointInspectionPlanId)
.Where((e, p, r) => e.Status == 0 || e.Status == 1 || e.Status == 3)
.Select((e, p, r) => new
{
FkDeviceAccountId = r.FkDeviceAccountId,
}).ToList();
foreach (DeviceType type in All_device_type)
{
LineDetail lineDetail = new LineDetail();
lineDetail.Workshop = all_deviceTyepe_s.Where(n => n.Id == type.ParentId).Select(n => n.Name).First();
lineDetail.Workline = type.Name;
List accounts = all_account_s.Where(it => it.FkDeviceType == type.Id).ToList();
lineDetail.Total = accounts.Count;
AllTotal = AllTotal + lineDetail.Total;
List Children = new List();
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;
Children.Add(deviceInfo);
continue;
}
//0.3 判断是否报修中
bool isExist = all_deviceRepair_s
.Where(it => it.FkDeviceId == item.Id)
.Where(it => it.Status == 0 || it.Status == 1 || it.Status == 3).Any();
if (isExist)
{
DamageTotal++;
deviceInfo.DeviceStatus = 3;
Children.Add(deviceInfo);
continue;
}
//var query = from person in people
// join address in addresses on person.Id equals address.PersonId into addressGroup
// from addressItem in addressGroup.DefaultIfEmpty()
// join phoneNumber in phoneNumbers on person.Id equals phoneNumber.PersonId into phoneGroup
// from phoneNumberItem in phoneGroup.DefaultIfEmpty()
// select new
// {
// PersonName = person.Name,
// Address = addressItem?.AddressLine ?? "No address available",
// PhoneNumber = phoneNumberItem?.Number ?? "No phone number available"
// };
//处理点检
int isExist_point = query_point
.Where(it => it.FkDeviceAccountId == item.Id).Count();
// UnmaintainedTotal = UnmaintainedTotal + isExist_point;
if (isExist_point > 0)
{
deviceInfo.DeviceStatus = 2;
UnmaintainedTotal++;
Children.Add(deviceInfo);
continue;
}
//处理巡检
int isExist_route = query_route.Where(it => it.FkDeviceAccountId == item.Id).Count();
if (isExist_route > 0)
{
deviceInfo.DeviceStatus = 2;
UnmaintainedTotal++;
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.DamageTotal = DamageTotal;
analysis.NormalTotal = NormalTotal;
analysis.NoUseTotal = NoUseTotal;
return analysis;
}
///
/// 查找指定节点的所有最终子节点
///
///
///
public List FindAllLeafNodes(int targetId)
{
List list_device_types = Context.Queryable().ToList();
List childIds = new List { targetId };
List result = new List();
while (childIds.Any())
{
int parentId = childIds.First();
childIds.RemoveAt(0);
List children = list_device_types.Where(n => n.ParentId == parentId).Select(n => n.Id).ToList();
if (children.Any())
{
childIds.AddRange(children);
}
else
{
result.Add(list_device_types.First(n => n.Id == parentId));
}
}
return result;
}
///
/// 用于在父子节点对象中,根据输入的节点 ID 找到最终的父节点:
///
///
///
public DeviceType FindFinalParentNode(int nodeId)
{
DeviceType currentNode = Context.Queryable().Where(n => n.Id == nodeId).First();
if (currentNode == null)
{
return null;
}
DeviceType parentNode = Context.Queryable().Where(n => n.Id == currentNode.ParentId).First();
while (parentNode != null)
{
currentNode = parentNode;
parentNode = Context.Queryable().Where(n => n.Id == currentNode.ParentId).First();
}
return currentNode;
}
}
}