127 lines
4.8 KiB
C#
127 lines
4.8 KiB
C#
using Model;
|
|
using Model.Dto.Config;
|
|
using Model.Entity.Config;
|
|
using Model.Mapper;
|
|
using ORM;
|
|
using Service.Interface.Config;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Service.Implement.Config
|
|
{
|
|
/// <summary>
|
|
/// 物模型点 服务实现(产品/设备通用,按 OwnerType+OwnerId 归属)
|
|
/// </summary>
|
|
public class ThingPointService : IThingPointService
|
|
{
|
|
public async Task<Result<List<ThingModelPointDto>>> GetListAsync(int ownerType, long ownerId, string? keyword)
|
|
{
|
|
try
|
|
{
|
|
var list = await SqlSugarContext.DbContext.Queryable<ThingModelPointEntity>()
|
|
.Where(x => x.IsDel == 0)
|
|
.Where(x => x.OwnerType == (ThingOwnerTypeEnum)ownerType && x.OwnerId == ownerId)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(keyword),
|
|
x => x.Code.Contains(keyword!) || (x.Name != null && x.Name.Contains(keyword!)))
|
|
.OrderBy(x => x.Sort)
|
|
.ToListAsync();
|
|
return Result<List<ThingModelPointDto>>.Success(list.ToDtoList());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<List<ThingModelPointDto>>.Error("查询物模型点失败", ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<ThingModelPointDto>> GetByIdAsync(long id)
|
|
{
|
|
try
|
|
{
|
|
var entity = await SqlSugarContext.DbContext.Queryable<ThingModelPointEntity>()
|
|
.Where(x => x.Id == id && x.IsDel == 0)
|
|
.FirstAsync();
|
|
if (entity == null)
|
|
return Result<ThingModelPointDto>.Error("物模型点不存在或已被删除");
|
|
return Result<ThingModelPointDto>.Success(entity.ToDto());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<ThingModelPointDto>.Error("查询物模型点失败", ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> AddAsync(ThingModelPointDto dto)
|
|
{
|
|
if (dto == null || string.IsNullOrWhiteSpace(dto.Code))
|
|
return Result.Error("点编码不能为空");
|
|
if (dto.OwnerId == null || !long.TryParse(dto.OwnerId, out var ownerId) || ownerId <= 0)
|
|
return Result.Error("归属对象Id无效");
|
|
|
|
try
|
|
{
|
|
bool exists = await SqlSugarContext.DbContext.Queryable<ThingModelPointEntity>()
|
|
.AnyAsync(x => x.OwnerType == dto.OwnerType && x.OwnerId == ownerId
|
|
&& x.Code == dto.Code && x.IsDel == 0);
|
|
if (exists)
|
|
return Result.Error($"点编码【{dto.Code}】在该归属下已存在");
|
|
|
|
var entity = dto.ToEntity();
|
|
entity.CreateTime = DateTime.Now;
|
|
await SqlSugarContext.DbContext.Insertable(entity).ExecuteCommandAsync();
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Error("新增物模型点失败", ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> UpdateAsync(ThingModelPointDto dto)
|
|
{
|
|
var entity = dto?.ToEntity();
|
|
if (entity == null || entity.Id <= 0)
|
|
return Result.Error("物模型点Id无效");
|
|
|
|
try
|
|
{
|
|
bool exists = await SqlSugarContext.DbContext.Queryable<ThingModelPointEntity>()
|
|
.AnyAsync(x => x.OwnerType == entity.OwnerType && x.OwnerId == entity.OwnerId
|
|
&& x.Code == entity.Code && x.IsDel == 0 && x.Id != entity.Id);
|
|
if (exists)
|
|
return Result.Error($"点编码【{entity.Code}】在该归属下已存在");
|
|
|
|
await SqlSugarContext.DbContext.Updateable(entity)
|
|
.IgnoreColumns(x => new { x.CreateTime, x.IsDel, x.OwnerType, x.OwnerId })
|
|
.ExecuteCommandAsync();
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Error("修改物模型点失败", ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> DeleteAsync(long id)
|
|
{
|
|
if (id <= 0)
|
|
return Result.Error("物模型点Id无效");
|
|
|
|
try
|
|
{
|
|
await SqlSugarContext.DbContext.Updateable<ThingModelPointEntity>()
|
|
.SetColumns(x => x.IsDel == 1)
|
|
.Where(x => x.Id == id)
|
|
.ExecuteCommandAsync();
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Error("删除物模型点失败", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|