zhuangpei-mesbackend/ZR.Service/MES/dev/DeviceAccountService.cs
2024-05-28 17:29:43 +08:00

158 lines
6.1 KiB
C#

using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;
using System.Linq;
using ZR.Model.MES.dev;
using ZR.Model.MES.dev.Dto;
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)
{
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(parm.FkDeviceType > 0, it => it.FkDeviceType == parm.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(!string.IsNullOrEmpty(parm.fkRouteInspectionPlanId), (a, r) =>r.FkRouteInspectionPlanId== parm.fkRouteInspectionPlanId)
.AndIF(string.IsNullOrEmpty(parm.fkRouteInspectionPlanId), (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);
}
}
}
}