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; using System.ComponentModel; namespace ZR.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; } public PagedInfo GetList_Route(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)); } var reponse = temp .ToPage(parm); return reponse; } /// /// 获取详情 /// /// /// 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 GetSelectTree(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.Id.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 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 PagedInfo GetList_Point(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) .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() .LeftJoin((a, r) => a.Id == r.FkDeviceAccountId) .Where(predicate.ToExpression()) .Select((a, r) => a) .ToPage(parm); return response; } /// /// 添加绑定关系 巡检计划和设备台账 /// /// /// /// 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; // 1 获取设备类型下面所有的线 List All_device_type = null; // DeviceType line = Context.Queryable().Where(it => it.Id == devicetype_id).First(); //if (line.ParentId == 1) //{ // Workshop = line.Name; // All_device_type = Context.Queryable().Where(it => it.ParentId == devicetype_id).ToList(); //} //else //{ // Workshop = Context.Queryable().Where(it => it.ParentId == line.ParentId).First().Name; // All_device_type = new List { line }; //} All_device_type = FindAllLeafNodes(devicetype_id); // DeviceType final_parent = FindFinalParentNode(devicetype_id); //Workshop = final_parent.Name; List LineDetailList = new List(); // 2 获取每个线下的所有设备 if (All_device_type.Count > 0) { foreach (DeviceType type in All_device_type) { LineDetail lineDetail = new LineDetail(); lineDetail.Workshop = Context.Queryable().Where(n => n.Id == type.ParentId).Select(n => n.Name).First(); lineDetail.Workline = type.Name; List accounts = Context.Queryable() .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 = Context.Queryable() .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; } //0.2 判断是否维护中 //处理点检 int isExist_point = Context.Queryable() .LeftJoin((e, p) => e.TaskId == p.Id) .LeftJoin((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; UnmaintainedTotal++; Children.Add(deviceInfo); continue; } //处理巡检 int isExist_route = Context.Queryable() .LeftJoin((e, p) => e.TaskId == p.Id) .LeftJoin((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; 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 childIds = new List { targetId }; List result = new List(); while (childIds.Any()) { int parentId = childIds.First(); childIds.RemoveAt(0); List children = Context.Queryable().Where(n => n.ParentId == parentId).Select(n => n.Id).ToList(); if (children.Any()) { childIds.AddRange(children); } else { result.Add(Context.Queryable().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; } } }