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
{
///
/// 物料服务
///
[ApiController, Route("api/materials")]
public class MaterialsController : ControllerBase
{
private readonly Application.Command.Interfaces.IMaterialService _materialCommandService; // 物料信息命令服务
private readonly Application.Query.Interfaces.IMaterialService _materialQueryService; // 物料信息查询服务
///
/// 依赖注入
///
public MaterialsController(
Application.Command.Interfaces.IMaterialService materialCommandService,
Application.Query.Interfaces.IMaterialService materialQueryService)
{
_materialCommandService = materialCommandService;
_materialQueryService = materialQueryService;
}
///
/// 获取物料列表
///
/// 成功
[HttpGet]
[ProducesResponseType(200)]
public async Task 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() });
}
}
///
/// 导出库存列表
///
/// 成功
[HttpGet("excel")]
[ProducesResponseType(200)]
public async Task 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");
}
///
/// 获取物料
///
/// 成功
/// 不存在
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task GetMaterialItem(int id)
{
var material = await _materialQueryService.GetMaterialItem(id);
return material == null ?
StatusCode(404) :
StatusCode(200, material);
}
///
/// 创建物料
///
/// 成功
/// 信息已存在
/// 信息已删除
/// 请求内容错误
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(409)]
[ProducesResponseType(410)]
[ProducesResponseType(422)]
public async Task 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)
};
}
///
/// 修改物料
///
/// 成功
/// 信息已存在
/// 信息已删除
/// 请求内容错误
[HttpPut("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(409)]
[ProducesResponseType(410)]
[ProducesResponseType(422)]
public async Task 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)
};
}
///
/// 批量处理物料
///
/// 成功
/// 方法不支持
[HttpPost("batch")]
[ProducesResponseType(200)]
[ProducesResponseType(405)]
public async Task BatchMaterial(Batch batch)
{
return batch.Method switch
{
BatchMethod.Delete => StatusCode(200, await _materialCommandService.DeleteMaterial(batch.Data)),
_ => StatusCode(405)
};
}
///
/// 获取品类列表
///
/// 成功
[HttpGet("categories")]
[ProducesResponseType(200)]
public async Task GetCategoryTree()
{
return StatusCode(200, await _materialQueryService.GetCategoryTree());
}
///
/// 获取品类列表
///
/// 成功
[HttpGet("goodsCategories")]
[ProducesResponseType(200)]
public async Task GetCategory()
{
return StatusCode(200, await _materialQueryService.GetCategorys());
}
///
/// 创建品类
///
/// 成功
/// 信息已存在
/// 信息已删除
/// 请求内容错误
[HttpPost("categories")]
[ProducesResponseType(201)]
[ProducesResponseType(409)]
[ProducesResponseType(410)]
[ProducesResponseType(422)]
public async Task 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)
};
}
///
/// 修改品类
///
/// 成功
/// 信息已存在
/// 信息已删除
/// 请求内容错误
[HttpPut("categories/{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(409)]
[ProducesResponseType(410)]
[ProducesResponseType(422)]
public async Task 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)
};
}
///
/// 批量处理品类
///
/// 成功
/// 方法不支持
[HttpPost("categories/batch")]
[ProducesResponseType(200)]
[ProducesResponseType(405)]
public async Task BatchCategory(Batch batch)
{
return batch.Method switch
{
BatchMethod.Delete => StatusCode(200, await _materialCommandService.DeleteCategory(batch.Data)),
_ => StatusCode(405)
};
}
///
/// 获取安全库存列表
///
/// 成功
[HttpGet("safeties")]
[ProducesResponseType(200)]
public async Task 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() });
}
}
///
/// 创建安全库存
///
/// 成功
/// 信息已存在
/// 信息已删除
/// 请求内容错误
[HttpPost("safeties")]
[ProducesResponseType(201)]
[ProducesResponseType(409)]
[ProducesResponseType(410)]
[ProducesResponseType(422)]
public async Task 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)
};
}
///
/// 修改安全库存
///
/// 成功
/// 信息已存在
/// 信息已删除
/// 请求内容错误
[HttpPut("safeties/{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(409)]
[ProducesResponseType(410)]
[ProducesResponseType(422)]
public async Task 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)
};
}
///
/// 批量处理安全库存
///
/// 成功
/// 方法不支持
[HttpPost("safeties/batch")]
[ProducesResponseType(200)]
[ProducesResponseType(405)]
public async Task Batch(Batch batch)
{
return batch.Method switch
{
BatchMethod.Delete => StatusCode(200, await _materialCommandService.DeleteSafety(batch.Data)),
_ => StatusCode(405)
};
}
}
}