181 lines
6.4 KiB
C#
181 lines
6.4 KiB
C#
using Infrastructure;
|
|
using Infrastructure.Attribute;
|
|
using Infrastructure.Enums;
|
|
using Infrastructure.Model;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using ARW.Admin.WebApi.Extensions;
|
|
using ARW.Admin.WebApi.Filters;
|
|
using ARW.Common;
|
|
using ARW.Model.Dto.Business.GoodsManager.Goodss;
|
|
using ARW.Service.Business.IBusinessService.GoodsManager.Goodss;
|
|
using ARW.Admin.WebApi.Controllers;
|
|
using ARW.Model.Models.Business.GoodsManager.Goodss;
|
|
using ARW.Model.Vo.Business.GoodsManager.Goodss;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using ARW.Admin.WebApi.Framework;
|
|
|
|
|
|
namespace ARW.WebApi.Controllers.Business.GoodsManager.Goodss
|
|
{
|
|
/// <summary>
|
|
/// 商品控制器
|
|
///
|
|
/// @author lwh
|
|
/// @date 2023-06-19
|
|
/// </summary>
|
|
[Verify]
|
|
[Route("business/[controller]")]
|
|
public class GoodsController : BaseController
|
|
{
|
|
private readonly IGoodsService _GoodsService;
|
|
|
|
/// <summary>
|
|
/// 依赖注入
|
|
/// </summary>
|
|
/// <param name="GoodsService">商品服务</param>
|
|
public GoodsController(IGoodsService GoodsService)
|
|
{
|
|
_GoodsService = GoodsService;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取商品列表
|
|
/// </summary>
|
|
/// <param name="parm">查询参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet("getGoodsList")]
|
|
[ActionPermissionFilter(Permission = "business:goods:list")]
|
|
public async Task<IActionResult> GetGoodsList([FromQuery] GoodsQueryDto parm)
|
|
{
|
|
var res = await _GoodsService.GetGoodsList(parm);
|
|
return SUCCESS(res);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加或修改商品
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("addOrUpdateGoods")]
|
|
[ActionPermissionFilter(Permission = "business:goods:addOrUpdate")]
|
|
[Log(Title = "添加或修改商品", BusinessType = BusinessType.ADDORUPDATE)]
|
|
public async Task<IActionResult> AddOrUpdateGoods([FromBody] GoodsDto parm)
|
|
{
|
|
if (parm == null) { throw new CustomException("请求参数错误"); }
|
|
|
|
var modal = new Goods();
|
|
if (parm.GoodsId != 0) modal = parm.Adapt<Goods>().ToUpdate(HttpContext);
|
|
else modal = parm.Adapt<Goods>().ToCreate(HttpContext);
|
|
|
|
var res = await _GoodsService.AddOrUpdateGoods(modal);
|
|
return SUCCESS(res);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除商品
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpDelete("{ids}")]
|
|
[ActionPermissionFilter(Permission = "business:goods:delete")]
|
|
[Log(Title = "商品删除", BusinessType = BusinessType.DELETE)]
|
|
public IActionResult Delete(string ids)
|
|
{
|
|
long[] idsArr = Tools.SpitLongArrary(ids);
|
|
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
|
var response = _GoodsService.Delete(idsArr);
|
|
return SUCCESS("删除成功!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导入商品
|
|
/// </summary>
|
|
/// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
|
|
/// <param name="updateSupport">是否需要更新</param>
|
|
/// <returns></returns>
|
|
[HttpPost("importData")]
|
|
[Log(Title = "商品导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = false)]
|
|
[ActionPermissionFilter(Permission = "business:goods:import")]
|
|
public async Task<IActionResult> ImportExcel([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
|
|
{
|
|
var isUpdateSupport = updateSupport;
|
|
IEnumerable<GoodsVo> parm = ExcelHelper<GoodsVo>.ImportData(formFile.OpenReadStream());
|
|
|
|
var i = 0;
|
|
var msgList = new List<string>();
|
|
foreach (GoodsVo item in parm)
|
|
{
|
|
i++;
|
|
var Goods = await _GoodsService.HandleImportData(item);
|
|
var modal = Goods.Adapt<Goods>().ToCreate(HttpContext);
|
|
var user = JwtUtil.GetLoginUser(App.HttpContext).UserName;
|
|
var msg = await _GoodsService.ImportExcel(modal, i, isUpdateSupport, user);
|
|
msgList.Add(msg);
|
|
}
|
|
|
|
return SUCCESS(msgList.ToArray());
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 商品导入模板下载
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("importTemplate")]
|
|
[Log(Title = "商品模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = false, IsSaveResponseData = false)]
|
|
[AllowAnonymous]
|
|
public IActionResult ImportTemplateExcel()
|
|
{
|
|
List<GoodsVo> Goods = new List<GoodsVo>();
|
|
MemoryStream stream = new MemoryStream();
|
|
|
|
// 示例数据
|
|
var values = new List<string>() { "111", "222", "333" };
|
|
string sFileName = DownloadImportTemplate(Goods, stream, "商品导入模板", values);
|
|
|
|
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{sFileName}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出商品
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Log(Title = "商品导出", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
|
|
[HttpGet("exportGoods")]
|
|
[ActionPermissionFilter(Permission = "business:goods:export")]
|
|
public async Task<IActionResult> ExportExcel([FromQuery] GoodsQueryDto parm)
|
|
{
|
|
parm.PageSize = 10000;
|
|
var list = await _GoodsService.GetGoodsList(parm);
|
|
var data = list.Result;
|
|
|
|
// 选中数据
|
|
if (!string.IsNullOrEmpty(parm.ids))
|
|
{
|
|
int[] idsArr = Tools.SpitIntArrary(parm.ids);
|
|
var selectDataList = new List<GoodsVo>();
|
|
foreach (var item in idsArr)
|
|
{
|
|
var select_data = data.Where(s => s.GoodsId == item).First();
|
|
selectDataList.Add(select_data);
|
|
}
|
|
data = selectDataList;
|
|
}
|
|
|
|
|
|
|
|
// 导出数据处理
|
|
var handleData = await _GoodsService.HandleExportData(data);
|
|
|
|
string sFileName = ExportExcel(handleData, "Goods", "商品列表");
|
|
return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|