You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
363 lines
14 KiB
363 lines
14 KiB
3 months ago
|
using Kean.Application.Command.ViewModels;
|
||
|
using Kean.Domain;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using System.Linq;
|
||
|
using System.Threading.Tasks;
|
||
|
using OfficeOpenXml;
|
||
|
|
||
|
namespace Kean.Presentation.Rest.Controllers
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 物料服务
|
||
|
/// </summary>
|
||
|
[ApiController, Route("api/materials")]
|
||
|
public class MaterialsController : ControllerBase
|
||
|
{
|
||
|
private readonly Application.Command.Interfaces.IMaterialService _materialCommandService; // 物料信息命令服务
|
||
|
private readonly Application.Query.Interfaces.IMaterialService _materialQueryService; // 物料信息查询服务
|
||
|
|
||
|
/// <summary>
|
||
|
/// 依赖注入
|
||
|
/// </summary>
|
||
|
public MaterialsController(
|
||
|
Application.Command.Interfaces.IMaterialService materialCommandService,
|
||
|
Application.Query.Interfaces.IMaterialService materialQueryService)
|
||
|
{
|
||
|
_materialCommandService = materialCommandService;
|
||
|
_materialQueryService = materialQueryService;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取物料列表
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
[HttpGet]
|
||
|
[ProducesResponseType(200)]
|
||
|
public async Task<IActionResult> GetMaterialList(
|
||
|
[FromQuery] string code,
|
||
|
[FromQuery] string name,
|
||
|
[FromQuery] string group,
|
||
|
[FromQuery] int[] category,
|
||
|
[FromQuery] string sort,
|
||
|
[FromQuery] int? offset,
|
||
|
[FromQuery] int? limit)
|
||
|
{
|
||
|
if (category.Length == 0)
|
||
|
{
|
||
|
category = null;
|
||
|
}
|
||
|
var items = await _materialQueryService.GetMaterialList(code, name, group, category, sort, offset, limit);
|
||
|
if (offset.HasValue || limit.HasValue)
|
||
|
{
|
||
|
var total = await _materialQueryService.GetMaterialCount(code, name, group, category);
|
||
|
return StatusCode(200, new { items, total });
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return StatusCode(200, new { items, total = items.Count() });
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 导出库存列表
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
[HttpGet("excel")]
|
||
|
[ProducesResponseType(200)]
|
||
|
public async Task<IActionResult> Export(
|
||
|
[FromQuery] string code,
|
||
|
[FromQuery] string name,
|
||
|
[FromQuery] string group,
|
||
|
[FromQuery] int[] category,
|
||
|
[FromQuery] string sort,
|
||
|
[FromQuery] int? offset,
|
||
|
[FromQuery] int? limit)
|
||
|
{
|
||
|
if (category.Length == 0)
|
||
|
{
|
||
|
category = null;
|
||
|
}
|
||
|
using var package = new ExcelPackage();
|
||
|
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
|
||
|
var column = 0;
|
||
|
worksheet.Cells[1, ++column].Value = "物料编码";
|
||
|
worksheet.Cells[1, ++column].Value = "物料名称";
|
||
|
worksheet.Cells[1, ++column].Value = "品类";
|
||
|
worksheet.Cells[1, ++column].Value = "计量单位";
|
||
|
var index = 1;
|
||
|
|
||
|
foreach (var item in await _materialQueryService.GetMaterialList(code, name, group, category, sort, offset, limit))
|
||
|
{
|
||
|
index++;
|
||
|
column = 0;
|
||
|
worksheet.Cells[index, ++column].Value = item.Code;
|
||
|
worksheet.Cells[index, ++column].Value = item.Name;
|
||
|
worksheet.Cells[index, ++column].Value = item.Category;
|
||
|
worksheet.Cells[index, ++column].Value = item.Unit;
|
||
|
}
|
||
|
return File(package.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取物料
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="404">不存在</response>
|
||
|
[HttpGet("{id}")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(404)]
|
||
|
public async Task<IActionResult> GetMaterialItem(int id)
|
||
|
{
|
||
|
var material = await _materialQueryService.GetMaterialItem(id);
|
||
|
return material == null ?
|
||
|
StatusCode(404) :
|
||
|
StatusCode(200, material);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建物料
|
||
|
/// </summary>
|
||
|
/// <response code="201">成功</response>
|
||
|
/// <response code="409">信息已存在</response>
|
||
|
/// <response code="410">信息已删除</response>
|
||
|
/// <response code="422">请求内容错误</response>
|
||
|
[HttpPost]
|
||
|
[ProducesResponseType(201)]
|
||
|
[ProducesResponseType(409)]
|
||
|
[ProducesResponseType(410)]
|
||
|
[ProducesResponseType(422)]
|
||
|
public async Task<IActionResult> CreateMaterial(Material material)
|
||
|
{
|
||
|
var result = await _materialCommandService.CreateMaterial(material);
|
||
|
return result switch
|
||
|
{
|
||
|
{ Id: > 0 } => StatusCode(201, result.Id),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
||
|
_ => StatusCode(422, result.Failure)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 修改物料
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="409">信息已存在</response>
|
||
|
/// <response code="410">信息已删除</response>
|
||
|
/// <response code="422">请求内容错误</response>
|
||
|
[HttpPut("{id}")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(409)]
|
||
|
[ProducesResponseType(410)]
|
||
|
[ProducesResponseType(422)]
|
||
|
public async Task<IActionResult> ModifyMaterial(int id, Material material)
|
||
|
{
|
||
|
material.Id = id;
|
||
|
var result = await _materialCommandService.ModifyMaterial(material);
|
||
|
return result switch
|
||
|
{
|
||
|
{ Success: true } => StatusCode(200),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
||
|
_ => StatusCode(422, result.Failure)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 批量处理物料
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="405">方法不支持</response>
|
||
|
[HttpPost("batch")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(405)]
|
||
|
public async Task<IActionResult> BatchMaterial(Batch<int> batch)
|
||
|
{
|
||
|
return batch.Method switch
|
||
|
{
|
||
|
BatchMethod.Delete => StatusCode(200, await _materialCommandService.DeleteMaterial(batch.Data)),
|
||
|
_ => StatusCode(405)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取品类列表
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
[HttpGet("categories")]
|
||
|
[ProducesResponseType(200)]
|
||
|
public async Task<IActionResult> GetCategoryTree()
|
||
|
{
|
||
|
return StatusCode(200, await _materialQueryService.GetCategoryTree());
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取品类列表
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
[HttpGet("goodsCategories")]
|
||
|
[ProducesResponseType(200)]
|
||
|
public async Task<IActionResult> GetCategory()
|
||
|
{
|
||
|
return StatusCode(200, await _materialQueryService.GetCategorys());
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建品类
|
||
|
/// </summary>
|
||
|
/// <response code="201">成功</response>
|
||
|
/// <response code="409">信息已存在</response>
|
||
|
/// <response code="410">信息已删除</response>
|
||
|
/// <response code="422">请求内容错误</response>
|
||
|
[HttpPost("categories")]
|
||
|
[ProducesResponseType(201)]
|
||
|
[ProducesResponseType(409)]
|
||
|
[ProducesResponseType(410)]
|
||
|
[ProducesResponseType(422)]
|
||
|
public async Task<IActionResult> CreateCategory(Category category)
|
||
|
{
|
||
|
var result = await _materialCommandService.CreateCategory(category);
|
||
|
return result switch
|
||
|
{
|
||
|
{ Id: > 0 } => StatusCode(201, result.Id),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
||
|
_ => StatusCode(422, result.Failure)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 修改品类
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="409">信息已存在</response>
|
||
|
/// <response code="410">信息已删除</response>
|
||
|
/// <response code="422">请求内容错误</response>
|
||
|
[HttpPut("categories/{id}")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(409)]
|
||
|
[ProducesResponseType(410)]
|
||
|
[ProducesResponseType(422)]
|
||
|
public async Task<IActionResult> ModifyCategory(int id, Category category)
|
||
|
{
|
||
|
category.Id = id;
|
||
|
var result = await _materialCommandService.ModifyCategory(category);
|
||
|
return result switch
|
||
|
{
|
||
|
{ Success: true } => StatusCode(200),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
||
|
_ => StatusCode(422, result.Failure)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 批量处理品类
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="405">方法不支持</response>
|
||
|
[HttpPost("categories/batch")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(405)]
|
||
|
public async Task<IActionResult> BatchCategory(Batch<int> batch)
|
||
|
{
|
||
|
return batch.Method switch
|
||
|
{
|
||
|
BatchMethod.Delete => StatusCode(200, await _materialCommandService.DeleteCategory(batch.Data)),
|
||
|
_ => StatusCode(405)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取安全库存列表
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
[HttpGet("safeties")]
|
||
|
[ProducesResponseType(200)]
|
||
|
public async Task<IActionResult> GetSafetyList(
|
||
|
[FromQuery] string code,
|
||
|
[FromQuery] string name,
|
||
|
[FromQuery] int? warehouse,
|
||
|
[FromQuery] string sort,
|
||
|
[FromQuery] int? offset,
|
||
|
[FromQuery] int? limit)
|
||
|
{
|
||
|
var items = await _materialQueryService.GetSafetyList(code, name, warehouse, sort, offset, limit);
|
||
|
if (offset.HasValue || limit.HasValue)
|
||
|
{
|
||
|
var total = await _materialQueryService.GetSafetyCount(code, name, warehouse);
|
||
|
return StatusCode(200, new { items, total });
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return StatusCode(200, new { items, total = items.Count() });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建安全库存
|
||
|
/// </summary>
|
||
|
/// <response code="201">成功</response>
|
||
|
/// <response code="409">信息已存在</response>
|
||
|
/// <response code="410">信息已删除</response>
|
||
|
/// <response code="422">请求内容错误</response>
|
||
|
[HttpPost("safeties")]
|
||
|
[ProducesResponseType(201)]
|
||
|
[ProducesResponseType(409)]
|
||
|
[ProducesResponseType(410)]
|
||
|
[ProducesResponseType(422)]
|
||
|
public async Task<IActionResult> Create(Safety safety)
|
||
|
{
|
||
|
var result = await _materialCommandService.CreateSafety(safety);
|
||
|
return result switch
|
||
|
{
|
||
|
{ Id: > 0 } => StatusCode(201, result.Id),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
||
|
_ => StatusCode(422, result.Failure)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 修改安全库存
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="409">信息已存在</response>
|
||
|
/// <response code="410">信息已删除</response>
|
||
|
/// <response code="422">请求内容错误</response>
|
||
|
[HttpPut("safeties/{id}")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(409)]
|
||
|
[ProducesResponseType(410)]
|
||
|
[ProducesResponseType(422)]
|
||
|
public async Task<IActionResult> Modify(int id, Safety safety)
|
||
|
{
|
||
|
safety.Id = id;
|
||
|
var result = await _materialCommandService.ModifySafety(safety);
|
||
|
return result switch
|
||
|
{
|
||
|
{ Success: true } => StatusCode(200),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
||
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
||
|
_ => StatusCode(422, result.Failure)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 批量处理安全库存
|
||
|
/// </summary>
|
||
|
/// <response code="200">成功</response>
|
||
|
/// <response code="405">方法不支持</response>
|
||
|
[HttpPost("safeties/batch")]
|
||
|
[ProducesResponseType(200)]
|
||
|
[ProducesResponseType(405)]
|
||
|
public async Task<IActionResult> Batch(Batch<int> batch)
|
||
|
{
|
||
|
return batch.Method switch
|
||
|
{
|
||
|
BatchMethod.Delete => StatusCode(200, await _materialCommandService.DeleteSafety(batch.Data)),
|
||
|
_ => StatusCode(405)
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|