244 lines
9.8 KiB
C#
244 lines
9.8 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;
|
|
using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.GoodsSkus;
|
|
using ARW.Model.Dto.Business.GoodsManager.GoodsSpecs.GoodsSkus;
|
|
using ARW.Service.Business.IBusinessService.GoodsManager.GoodsServicess.GoodsServicesRels;
|
|
using ARW.Model.Dto.Business.GoodsManager.GoodsServicess.GoodsServicesRels;
|
|
using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.GoodsSpecRels;
|
|
using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.Specs;
|
|
using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.SpecValues;
|
|
|
|
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;
|
|
private readonly IGoodsSkuService _GoodsSkuService;
|
|
private readonly IGoodsServicesRelService _GoodsServicesRelService;
|
|
private readonly ISpecService _SepcIService;
|
|
private readonly ISpecValueService _SpecValueIService;
|
|
private readonly IGoodsSpecRelService _GoodsSpecRelService;
|
|
|
|
|
|
/// <summary>
|
|
/// 依赖注入
|
|
/// </summary>
|
|
/// <param name="GoodsService">商品服务</param>
|
|
/// <param name="goodsSkuService">商品Sku服务</param>
|
|
/// <param name="goodsServicesRelService">商品服务与承诺服务</param>
|
|
/// <param name="sepcIService">规格组服务</param>
|
|
/// <param name="specValueIService">规格值服务</param>
|
|
/// <param name="goodsSpecRelService">商品规格关系服务</param>
|
|
public GoodsController(IGoodsService GoodsService, IGoodsSkuService goodsSkuService, IGoodsServicesRelService goodsServicesRelService, ISpecService sepcIService, ISpecValueService specValueIService, IGoodsSpecRelService goodsSpecRelService)
|
|
{
|
|
_GoodsService = GoodsService;
|
|
_GoodsSkuService = goodsSkuService;
|
|
_GoodsServicesRelService = goodsServicesRelService;
|
|
_SepcIService = sepcIService;
|
|
_SpecValueIService = specValueIService;
|
|
_GoodsSpecRelService = goodsSpecRelService;
|
|
}
|
|
|
|
|
|
/// <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>
|
|
/// 获取商品Sku
|
|
/// </summary>
|
|
/// <param name="parm">查询参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet("getGoodsSkuList")]
|
|
public async Task<IActionResult> GetGoodsSkuList([FromQuery] GoodsSkuQueryDto parm)
|
|
{
|
|
var res = await _GoodsSkuService.GetGoodsSkuList(parm);
|
|
return SUCCESS(res);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取商品服务与承诺
|
|
/// </summary>
|
|
/// <param name="parm">查询参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet("getCurrentGoodsServiceList")]
|
|
public async Task<IActionResult> GetCurrentGoodsServiceList([FromQuery] GoodsServicesRelQueryDto parm)
|
|
{
|
|
var res = await _GoodsServicesRelService.GetCurrentGoodsServicesRel(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 async Task<IActionResult> Delete(string ids)
|
|
{
|
|
int[] idsArr = Tools.SpitIntArrary(ids);
|
|
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
|
|
|
// 删除关于该商品的关联
|
|
//var goodsList = await _GoodsService.GetListAsync(s => idsArr.Contains(s.GoodsId));
|
|
//var goodsGuids = goodsList.Select(s => s.GoodsGuid).ToList();
|
|
|
|
//var specList = await _GoodsSpecRelService.GetListAsync(s => goodsGuids.Contains(s.GoodsGuid));
|
|
//var specIds = specList.Select(s => s.SpecId).ToList();
|
|
|
|
//await _SepcIService.DeleteAsync(s => specIds.Contains(s.SpecId));
|
|
//await _SpecValueIService.DeleteAsync(s => specIds.Contains(s.SpecId));
|
|
|
|
//await _GoodsSpecRelService.DeleteAsync(s => goodsGuids.Contains(s.GoodsGuid));
|
|
//await _GoodsSkuService.DeleteAsync(s => goodsGuids.Contains(s.GoodsGuid));
|
|
//await _GoodsServicesRelService.DeleteAsync(s => goodsGuids.Contains(s.GoodsGuid));
|
|
|
|
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 });
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|