Files
kunshan-bzfm-mes-backend/DOAN.Service/MES/Device/DeviceAccountService.cs
2024-12-10 14:34:13 +08:00

690 lines
26 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <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;
}
/// <summary>
/// 通常在数据库层面执行联表查询JOIN会比在程序层面逐一查询左表和右表更快一些。
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<DeviceAccountDto> GetList_Route2(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)
.And((a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
;
var temp = Context.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelRpAt>((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<DeviceAccount>().Where(it => !exist.Contains(it.Id)).Distinct();
}
var reponse = temp
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
return reponse;
}
public PagedInfo<DeviceAccountDto> GetList_Route(DeviceAccountQueryDto2 parm)
{
//未绑定
if (parm.Flag == 0)
{
int[] account_array = Context
.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelRpAt>((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
.Select((a, r) => a.Id)
.ToArray();
var predicate = Expressionable.Create<DeviceAccount>()
.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<DeviceAccount>()
.Where(it => !account_array.Contains(it.Id))
.Where(predicate.ToExpression())
.ToPage<DeviceAccount, DeviceAccountDto>(parm)
;
}
else if (parm.Flag == 1)
{
// 获取绑定的account
return Context.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelRpAt>((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkRouteInspectionPlanId == parm.fkRouteInspectionPlanId)
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
}
return null;
}
public PagedInfo<DeviceAccountDto> GetList_Point2(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)
.And((a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
;
var temp = Context.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelPpAt>((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<DeviceAccount>().Where(it => !exist.Contains(it.Id)).Distinct();
}
var reponse = temp
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
return reponse;
}
public PagedInfo<DeviceAccountDto> GetList_Point(DeviceAccountQueryDto3 parm)
{
//未绑定
if (parm.Flag == 0)
{
int[] account_array = Context
.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelPpAt>((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
.Select((a, r) => a.Id)
.ToArray();
var predicate = Expressionable.Create<DeviceAccount>()
.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<DeviceAccount>()
.Where(it => !account_array.Contains(it.Id))
.Where(predicate.ToExpression())
.ToPage<DeviceAccount, DeviceAccountDto>(parm)
;
}
else if (parm.Flag == 1)
{
// 获取绑定的account
return Context.Queryable<DeviceAccount>()
.LeftJoin<DeviceRelPpAt>((a, r) => a.Id == r.FkDeviceAccountId)
.Where((a, r) => r.FkPointInspectionPlanId == parm.fkPointInspectionPlanId)
.ToPage<DeviceAccount, DeviceAccountDto>(parm);
}
return null;
}
/// <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);
}
/// <summary>
/// 获取树节点
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public List<SelectTreeDto> GetSelectTree2(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.FkDeviceType.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>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public List<SelectTreeDto> GetSelectTree(DeviceAccountQueryDto parm)
{
// 获取 类型父子节点
List<SelectTreeDto> deviceTypeList = Context.Queryable<DeviceType>()
.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<SelectTreeDto> accountList = Context.Queryable<DeviceAccount>().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();
}
/// <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
/// <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();
// 设备总数
int AllTotal = 0;
//正常设备数
int NormalTotal = 0;
// 未巡点总数
int UnmaintainedTotal = 0;
// 报修中总数
int DamageTotal = 0;
// 停用总数
int NoUseTotal = 0;
Stopwatch stopwatch = new Stopwatch();
// 开始计时
stopwatch.Start();
// 1 获取设备类型下面所有的线
List<DeviceType> All_device_type = null;
All_device_type = FindAllLeafNodes(devicetype_id);
List<LineDetail> LineDetailList = new List<LineDetail>();
// 停止计时
stopwatch.Stop();
// 输出执行时间
Console.WriteLine("代码段执行时间: {0} 秒", stopwatch.ElapsedMilliseconds / 1000);
// 2 获取每个线下的所有设备
if (All_device_type.Count > 0)
{
List<DeviceType> all_deviceTyepe_s = Context.Queryable<DeviceType>().ToList();
List<DeviceAccount> all_account_s = Context.Queryable<DeviceAccount>().ToList();
List<DeviceRepair> all_deviceRepair_s = Context.Queryable<DeviceRepair>().ToList();
var query_point = Context.Queryable<DeviceTaskExecute>()
.LeftJoin<DevicePointInspectionPlan>((e, p) => e.PlanId == p.Id)
.LeftJoin<DeviceRelPpAt>((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<DeviceTaskExecute>()
.LeftJoin<DeviceRouteInspectionPlan>((e, p) => e.PlanId == p.Id)
.LeftJoin<DeviceRelPpAt>((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<DeviceAccount> accounts = all_account_s.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;
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;
}
/// <summary>
/// 查找指定节点的所有最终子节点
/// </summary>
/// <param name="targetId"></param>
/// <returns></returns>
public List<DeviceType> FindAllLeafNodes(int targetId)
{
List<DeviceType> list_device_types = Context.Queryable<DeviceType>().ToList();
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 = 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;
}
/// <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;
}
}
}