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 { /// /// 设备台账Service业务层处理 /// [AppService(ServiceType = typeof(IDeviceAccountService), ServiceLifetime = LifeTime.Transient)] public class DeviceAccountService : BaseService, IDeviceAccountService { /// /// 查询设备台账列表 /// /// /// public PagedInfo GetList(DeviceAccountQueryDto parm) { 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(parm.FkDeviceType > 0, it => it.FkDeviceType == parm.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) .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() .LeftJoin((a, r) => a.Id == r.FkDeviceAccountId) .Where(predicate.ToExpression()) .Select((a, r) => a) .ToPage(parm); return response; } /// /// 获取详情 /// /// /// 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 } }