添加ORM的依赖注入,添加appsettings的gitignore
This commit is contained in:
@@ -1,12 +1,266 @@
|
||||
using Model;
|
||||
using Model.Entity.Asset;
|
||||
using ORM;
|
||||
using Service.Interface;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Service.Implement
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备台账 服务实现
|
||||
/// 设备台账服务实现(资产管理)
|
||||
/// </summary>
|
||||
public class EquipmentService : IEquipmentService
|
||||
public class EquipmentService : BaseService<EquipmentEntity>, IEquipmentService
|
||||
{
|
||||
// TODO: 实现 设备台账 相关方法
|
||||
private readonly SqlSugarRepository<EquipmentStatusRecordEntity> _statusRecordRepository;
|
||||
|
||||
public EquipmentService(SqlSugarRepository<EquipmentEntity> repository,
|
||||
SqlSugarRepository<EquipmentStatusRecordEntity> statusRecordRepository)
|
||||
: base(repository)
|
||||
{
|
||||
_statusRecordRepository = statusRecordRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询全部设备(过滤已删除数据)
|
||||
/// </summary>
|
||||
public override async Task<Result<List<EquipmentEntity>>> GetAllAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.Where(x => x.IsDel == 0)
|
||||
.OrderBy(x => x.CreateTime, OrderByType.Desc)
|
||||
.ToListAsync();
|
||||
return Result<List<EquipmentEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<EquipmentEntity>>.Error("查询设备台账失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询设备台账(支持关键字、分类、状态筛选)
|
||||
/// </summary>
|
||||
public async Task<Result<List<EquipmentEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total,
|
||||
string? keyword, long categoryId = 0, EquipmentStatusEnum? status = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.Where(x => x.IsDel == 0)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(keyword),
|
||||
x => x.Code!.Contains(keyword!) || x.Name!.Contains(keyword!))
|
||||
.WhereIF(categoryId > 0, x => x.CategoryId == categoryId)
|
||||
.WhereIF(status.HasValue, x => x.Status == status!.Value)
|
||||
.OrderBy(x => x.CreateTime, OrderByType.Desc)
|
||||
.ToPageListAsync(pageIndex, pageSize, total);
|
||||
total.Value = (int)Math.Ceiling((double)total.Value / pageSize);
|
||||
return Result<List<EquipmentEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<EquipmentEntity>>.Error("分页查询设备台账失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id 获取设备台账详情
|
||||
/// </summary>
|
||||
public async Task<Result<EquipmentEntity>> GetByIdAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var entity = await _repository.Entities
|
||||
.Where(x => x.Id == id && x.IsDel == 0)
|
||||
.FirstAsync();
|
||||
if (entity == null)
|
||||
{
|
||||
return Result<EquipmentEntity>.Error("设备不存在或已被删除");
|
||||
}
|
||||
return Result<EquipmentEntity>.Success(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<EquipmentEntity>.Error("查询设备详情失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增设备台账(校验编号唯一性,补充创建时间)
|
||||
/// </summary>
|
||||
public override async Task<Result<bool>> InsertAsync(EquipmentEntity entity)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(entity.Code))
|
||||
{
|
||||
return Result<bool>.Error("设备编号不能为空");
|
||||
}
|
||||
try
|
||||
{
|
||||
// 设备编号唯一性校验(未删除范围内)
|
||||
bool exists = await _repository.Entities
|
||||
.AnyAsync(x => x.Code == entity.Code && x.IsDel == 0);
|
||||
if (exists)
|
||||
{
|
||||
return Result<bool>.Error($"设备编号【{entity.Code}】已存在");
|
||||
}
|
||||
|
||||
entity.CreateTime = DateTime.Now;
|
||||
var result = await _repository.Context.Insertable(entity).ExecuteCommandAsync();
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("新增设备失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新设备台账
|
||||
/// </summary>
|
||||
public async Task<Result<bool>> UpdateAsync(EquipmentEntity entity)
|
||||
{
|
||||
if (entity.Id <= 0)
|
||||
{
|
||||
return Result<bool>.Error("设备 Id 无效");
|
||||
}
|
||||
try
|
||||
{
|
||||
var old = await _repository.Entities
|
||||
.Where(x => x.Id == entity.Id && x.IsDel == 0)
|
||||
.FirstAsync();
|
||||
if (old == null)
|
||||
{
|
||||
return Result<bool>.Error("设备不存在或已被删除");
|
||||
}
|
||||
|
||||
// 设备编号变更时校验唯一性
|
||||
if (!string.IsNullOrWhiteSpace(entity.Code) && entity.Code != old.Code)
|
||||
{
|
||||
bool exists = await _repository.Entities
|
||||
.AnyAsync(x => x.Code == entity.Code && x.IsDel == 0 && x.Id != entity.Id);
|
||||
if (exists)
|
||||
{
|
||||
return Result<bool>.Error($"设备编号【{entity.Code}】已存在");
|
||||
}
|
||||
}
|
||||
|
||||
entity.IsDel = old.IsDel;
|
||||
entity.CreateTime = old.CreateTime;
|
||||
entity.UpdateTime = DateTime.Now;
|
||||
var result = await _repository.Context.Updateable(entity).ExecuteCommandAsync();
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("更新设备失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 软删除设备台账(置 IsDel = 1)
|
||||
/// </summary>
|
||||
public async Task<Result<bool>> DeleteEquipmentAsync(long id)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
return Result<bool>.Error("设备 Id 无效");
|
||||
}
|
||||
try
|
||||
{
|
||||
var result = await _repository.Context.Updateable<EquipmentEntity>()
|
||||
.SetColumns(x => x.IsDel == 1)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteCommandAsync();
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("删除设备失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 变更设备状态,并记录状态变更历史(事务保证一致性)
|
||||
/// </summary>
|
||||
public async Task<Result<bool>> ChangeStatusAsync(long equipmentId, EquipmentStatusEnum toStatus, string? operatorName, string? remark)
|
||||
{
|
||||
if (equipmentId <= 0)
|
||||
{
|
||||
return Result<bool>.Error("设备 Id 无效");
|
||||
}
|
||||
try
|
||||
{
|
||||
var equipment = await _repository.Entities
|
||||
.Where(x => x.Id == equipmentId && x.IsDel == 0)
|
||||
.FirstAsync();
|
||||
if (equipment == null)
|
||||
{
|
||||
return Result<bool>.Error("设备不存在或已被删除");
|
||||
}
|
||||
if (equipment.Status == toStatus)
|
||||
{
|
||||
return Result<bool>.Error("目标状态与当前状态一致,无需变更");
|
||||
}
|
||||
|
||||
var now = DateTime.Now;
|
||||
var record = new EquipmentStatusRecordEntity
|
||||
{
|
||||
EquipmentId = equipmentId,
|
||||
FromStatus = equipment.Status,
|
||||
ToStatus = toStatus,
|
||||
ChangeTime = now,
|
||||
Operator = operatorName,
|
||||
Remark = remark,
|
||||
CreateTime = now
|
||||
};
|
||||
|
||||
// 事务:更新设备状态 + 插入状态变更记录
|
||||
_repository.BeginTran();
|
||||
try
|
||||
{
|
||||
equipment.Status = toStatus;
|
||||
equipment.UpdateTime = now;
|
||||
await _repository.Context.Updateable(equipment)
|
||||
.UpdateColumns(x => new { x.Status, x.UpdateTime })
|
||||
.ExecuteCommandAsync();
|
||||
await _statusRecordRepository.Context.Insertable(record).ExecuteCommandAsync();
|
||||
_repository.CommitTran();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_repository.RollbackTran();
|
||||
throw;
|
||||
}
|
||||
|
||||
return Result<bool>.Success(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("变更设备状态失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定设备的状态变更历史记录(按变更时间倒序)
|
||||
/// </summary>
|
||||
public async Task<Result<List<EquipmentStatusRecordEntity>>> GetStatusRecordsAsync(long equipmentId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _statusRecordRepository.Entities
|
||||
.Where(x => x.EquipmentId == equipmentId && x.IsDel == 0)
|
||||
.OrderBy(x => x.ChangeTime, OrderByType.Desc)
|
||||
.ToListAsync();
|
||||
return Result<List<EquipmentStatusRecordEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<EquipmentStatusRecordEntity>>.Error("查询状态变更记录失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user