设备类添加产品字段、添加物模型类,新增产品CRUD方法

This commit is contained in:
2026-09-03 17:16:28 +08:00
parent cfa8e78d33
commit bcd6484f45
29 changed files with 1611 additions and 23 deletions
+247 -4
View File
@@ -1,12 +1,255 @@
using Service.Interface;
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
namespace Service.Implement.Config
{
/// <summary>
/// 产品管理 服务实现
/// 产品管理(含产品分类)服务实现
/// </summary>
public class ProductService : IProductService
{
// TODO: 实现 产品管理 相关方法
// ===================== 产品分类 =====================
public async Task<Result<List<ProductCategoryDto>>> GetCategoriesAsync()
{
try
{
var list = await SqlSugarContext.DbContext.Queryable<ProductCategoryEntity>()
.Where(x => x.IsDel == 0)
.OrderBy(x => x.Sort)
.ToListAsync();
return Result<List<ProductCategoryDto>>.Success(list.ToDtoList());
}
catch (Exception ex)
{
return Result<List<ProductCategoryDto>>.Error("查询产品分类失败", ex);
}
}
public async Task<Result> AddCategoryAsync(ProductCategoryDto dto)
{
if (dto == null || string.IsNullOrWhiteSpace(dto.Code))
return Result.Error("分类编码不能为空");
try
{
bool exists = await SqlSugarContext.DbContext.Queryable<ProductCategoryEntity>()
.AnyAsync(x => 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> UpdateCategoryAsync(ProductCategoryDto dto)
{
var entity = dto?.ToEntity();
if (entity == null || entity.Id <= 0)
return Result.Error("分类Id无效");
try
{
bool exists = await SqlSugarContext.DbContext.Queryable<ProductCategoryEntity>()
.AnyAsync(x => 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 })
.ExecuteCommandAsync();
return Result.Success();
}
catch (Exception ex)
{
return Result.Error("修改产品分类失败", ex);
}
}
public async Task<Result> DeleteCategoryAsync(long id)
{
if (id <= 0)
return Result.Error("分类Id无效");
try
{
await SqlSugarContext.DbContext.Updateable<ProductCategoryEntity>()
.SetColumns(x => x.IsDel == 1)
.Where(x => x.Id == id)
.ExecuteCommandAsync();
return Result.Success();
}
catch (Exception ex)
{
return Result.Error("删除产品分类失败", ex);
}
}
// ===================== 产品 =====================
public async Task<Result<List<ProductDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword, long categoryId)
{
try
{
var list = await SqlSugarContext.DbContext.Queryable<ProductEntity>()
.Where(x => x.IsDel == 0)
.WhereIF(!string.IsNullOrWhiteSpace(keyword),
x => x.Model.Contains(keyword!) || (x.Name != null && x.Name.Contains(keyword!)))
.WhereIF(categoryId > 0, x => x.CategoryId == categoryId)
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageListAsync(pageIndex, pageSize, total);
var dtos = list.ToDtoList();
await FillCategoryNames(dtos);
return Result<List<ProductDto>>.Success(dtos);
}
catch (Exception ex)
{
return Result<List<ProductDto>>.Error("查询产品列表失败", ex);
}
}
public async Task<Result<List<ProductOptionDto>>> GetOptionsAsync()
{
try
{
var list = await SqlSugarContext.DbContext.Queryable<ProductEntity>()
.Where(x => x.IsDel == 0 && x.IsEnabled)
.OrderBy(x => x.Model)
.ToListAsync();
return Result<List<ProductOptionDto>>.Success(list.ToOptionDtoList());
}
catch (Exception ex)
{
return Result<List<ProductOptionDto>>.Error("查询产品选项失败", ex);
}
}
public async Task<Result<ProductDto>> GetByIdAsync(long id)
{
try
{
var entity = await SqlSugarContext.DbContext.Queryable<ProductEntity>()
.Where(x => x.Id == id && x.IsDel == 0)
.FirstAsync();
if (entity == null)
return Result<ProductDto>.Error("产品不存在或已被删除");
var dto = entity.ToDto();
await FillCategoryNames(new List<ProductDto> { dto });
return Result<ProductDto>.Success(dto);
}
catch (Exception ex)
{
return Result<ProductDto>.Error("查询产品详情失败", ex);
}
}
public async Task<Result> AddAsync(ProductDto dto)
{
if (dto == null || string.IsNullOrWhiteSpace(dto.Model))
return Result.Error("产品型号不能为空");
try
{
bool exists = await SqlSugarContext.DbContext.Queryable<ProductEntity>()
.AnyAsync(x => x.Model == dto.Model && x.IsDel == 0);
if (exists)
return Result.Error($"产品型号【{dto.Model}】已存在");
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(ProductDto dto)
{
var entity = dto?.ToEntity();
if (entity == null || entity.Id <= 0)
return Result.Error("产品Id无效");
try
{
bool exists = await SqlSugarContext.DbContext.Queryable<ProductEntity>()
.AnyAsync(x => x.Model == entity.Model && x.IsDel == 0 && x.Id != entity.Id);
if (exists)
return Result.Error($"产品型号【{entity.Model}】已存在");
await SqlSugarContext.DbContext.Updateable(entity)
.IgnoreColumns(x => new { x.CreateTime, x.IsDel })
.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<ProductEntity>()
.SetColumns(x => x.IsDel == 1)
.Where(x => x.Id == id)
.ExecuteCommandAsync();
return Result.Success();
}
catch (Exception ex)
{
return Result.Error("删除产品失败", ex);
}
}
/// <summary>
/// 给产品 DTO 填充分类名称(供列表/详情展示)
/// </summary>
private async Task FillCategoryNames(List<ProductDto> dtos)
{
if (dtos == null || dtos.Count == 0)
return;
var ids = dtos.Where(x => x.CategoryId != null && long.TryParse(x.CategoryId, out _))
.Select(x => long.Parse(x.CategoryId!)).Distinct().ToList();
if (ids.Count == 0)
return;
var categories = await SqlSugarContext.DbContext.Queryable<ProductCategoryEntity>()
.Where(x => ids.Contains(x.Id) && x.IsDel == 0)
.ToListAsync();
var map = categories.ToDictionary(c => c.Id, c => c.Name);
foreach (var d in dtos)
{
if (long.TryParse(d.CategoryId, out var cid) && map.TryGetValue(cid, out var name))
d.CategoryName = name;
}
}
}
}