129 lines
4.0 KiB
C#
129 lines
4.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Model;
|
||
using Model.Dto.Config;
|
||
using Service.Interface.Config;
|
||
using SqlSugar;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace WebAPI.Controllers
|
||
{
|
||
/// <summary>
|
||
/// 产品管理(含产品分类)
|
||
/// </summary>
|
||
[ApiController]
|
||
[Route("api/config/product")]
|
||
public class ProductController : ControllerBase
|
||
{
|
||
private readonly IProductService _productService;
|
||
|
||
public ProductController(IProductService productService)
|
||
{
|
||
_productService = productService;
|
||
}
|
||
|
||
// ===================== 产品分类 =====================
|
||
|
||
/// <summary>
|
||
/// 产品分类列表
|
||
/// </summary>
|
||
[HttpGet("category/list")]
|
||
public async Task<IActionResult> GetCategories()
|
||
{
|
||
return Ok(await _productService.GetCategoriesAsync());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增产品分类
|
||
/// </summary>
|
||
[HttpPost("category/add")]
|
||
public async Task<IActionResult> AddCategory([FromBody] ProductCategoryDto dto)
|
||
{
|
||
return Ok(await _productService.AddCategoryAsync(dto));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改产品分类
|
||
/// </summary>
|
||
[HttpPut("category/update")]
|
||
public async Task<IActionResult> UpdateCategory([FromBody] ProductCategoryDto dto)
|
||
{
|
||
return Ok(await _productService.UpdateCategoryAsync(dto));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除产品分类(软删除)
|
||
/// </summary>
|
||
[HttpDelete("category/{id}")]
|
||
public async Task<IActionResult> DeleteCategory(long id)
|
||
{
|
||
return Ok(await _productService.DeleteCategoryAsync(id));
|
||
}
|
||
|
||
// ===================== 产品 =====================
|
||
|
||
/// <summary>
|
||
/// 产品下拉选项(设备选择所属产品)
|
||
/// </summary>
|
||
[HttpGet("options")]
|
||
public async Task<IActionResult> GetOptions()
|
||
{
|
||
return Ok(await _productService.GetOptionsAsync());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 产品列表(分页)
|
||
/// </summary>
|
||
/// <param name="pageIndex">页码(从1开始,默认1)</param>
|
||
/// <param name="pageSize">每页数量(默认10)</param>
|
||
/// <param name="keyword">关键字(模糊匹配型号/名称)</param>
|
||
/// <param name="categoryId">分类Id(0 表示不过滤)</param>
|
||
[HttpGet("list")]
|
||
public async Task<IActionResult> GetList(int pageIndex = 1, int pageSize = 10, string? keyword = null, long categoryId = 0)
|
||
{
|
||
RefAsync<int> total = 0;
|
||
var result = await _productService.GetPagedAsync(pageIndex, pageSize, total, keyword, categoryId);
|
||
Response.Headers["X-Total-Count"] = total.Value.ToString();
|
||
return result.IsSuccess
|
||
? Ok(Result<List<ProductDto>>.Success(result.Data))
|
||
: Ok(Result<List<ProductDto>>.Error(result.Msg));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 产品详情
|
||
/// </summary>
|
||
[HttpGet("{id}")]
|
||
public async Task<IActionResult> GetById(long id)
|
||
{
|
||
return Ok(await _productService.GetByIdAsync(id));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增产品
|
||
/// </summary>
|
||
[HttpPost]
|
||
public async Task<IActionResult> Add([FromBody] ProductDto dto)
|
||
{
|
||
return Ok(await _productService.AddAsync(dto));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改产品
|
||
/// </summary>
|
||
[HttpPut]
|
||
public async Task<IActionResult> Update([FromBody] ProductDto dto)
|
||
{
|
||
return Ok(await _productService.UpdateAsync(dto));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除产品(软删除)
|
||
/// </summary>
|
||
[HttpDelete("{id}")]
|
||
public async Task<IActionResult> Delete(long id)
|
||
{
|
||
return Ok(await _productService.DeleteAsync(id));
|
||
}
|
||
}
|
||
}
|