309 lines
12 KiB
C#
309 lines
12 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;
|
||
using ARW.Service.Business.IBusinessService.ShopManager.Shops;
|
||
using ARW.Model.Dto.Business.ShopManager.Shops;
|
||
|
||
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;
|
||
private readonly IShopService _ShopService;
|
||
|
||
|
||
/// <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, IShopService shopService)
|
||
{
|
||
_GoodsService = GoodsService;
|
||
_GoodsSkuService = goodsSkuService;
|
||
_GoodsServicesRelService = goodsServicesRelService;
|
||
_SepcIService = sepcIService;
|
||
_SpecValueIService = specValueIService;
|
||
_GoodsSpecRelService = goodsSpecRelService;
|
||
_ShopService = shopService;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取商品列表
|
||
/// </summary>
|
||
/// <param name="parm">查询参数</param>
|
||
/// <returns></returns>
|
||
[HttpGet("getGoodsList")]
|
||
[ActionPermissionFilter(Permission = "business:goods:list")]
|
||
public async Task<IActionResult> GetGoodsList([FromQuery] GoodsQueryDto parm)
|
||
{
|
||
var user = JwtUtil.GetLoginUser(App.HttpContext);
|
||
if (user.UserId != 1)
|
||
{
|
||
var shop = await _ShopService.GetFirstAsync(s => s.ShopUserId == user.UserId);
|
||
if (shop == null) throw new Exception("当前用户没有店铺");
|
||
parm.ShopGuid = shop.ShopGuid;
|
||
}
|
||
|
||
var res = await _GoodsService.GetGoodsList(parm);
|
||
return SUCCESS(res);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取全部商品列表
|
||
/// </summary>
|
||
/// <param name="parm">查询参数</param>
|
||
/// <returns></returns>
|
||
[HttpGet("getAllGoodsList")]
|
||
public async Task<IActionResult> GetAllGoodsList([FromQuery] GoodsQueryDto parm)
|
||
{
|
||
var res = await _GoodsService.GetAllGoodsList(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 user = JwtUtil.GetLoginUser(App.HttpContext);
|
||
if (user.UserId != 1)
|
||
{
|
||
var shop = await _ShopService.GetFirstAsync(s => s.ShopUserId == user.UserId);
|
||
if (shop == null) throw new Exception("当前用户没有店铺");
|
||
modal.ShopGuid = shop.ShopGuid;
|
||
}
|
||
|
||
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 user = JwtUtil.GetLoginUser(App.HttpContext);
|
||
var msg = await _GoodsService.ImportExcel(item, 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>() {
|
||
"三星官方旗舰店",
|
||
"手机/Galaxy S23系列",
|
||
"三星 Galaxy S21 5G(SM-G9910)双模5G 骁龙888 超高清专业摄像 120Hz护目屏 游戏手机 8G+128G",
|
||
"R301",
|
||
"此款商品美观大方 性价比较高 不容错过",
|
||
"http://localhost:8888/Uploads/Goods/20230627/67232B8C22C2A8DD.jpg,http://localhost:8888/Uploads/Goods/20230627/19693DB03E8D2355.jpg",
|
||
"4969.00",
|
||
"5000",
|
||
"100",
|
||
"0",
|
||
"0",
|
||
"<p><img src=\"http://localhost:8888/Uploads/uploads/20230625/A28F7B2C82855C61.png\" alt=\"\" data-href=\"\" style=\"\"/></p>",
|
||
"上架",
|
||
"100"
|
||
};
|
||
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 });
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 上下架商品
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPut("shelfGoods")]
|
||
public async Task<IActionResult> ShelfGoods([FromBody] GoodsShelfDto param)
|
||
{
|
||
int[] idsArr = Tools.SpitIntArrary(param.ids);
|
||
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"操作失败 Id 不能为空")); }
|
||
|
||
var msgList = new List<string>();
|
||
foreach (var item in idsArr)
|
||
{
|
||
var msg = await _GoodsService.ShelfGoods(item, param.GoodsShelfStatus);
|
||
msgList.Add(msg);
|
||
}
|
||
|
||
return SUCCESS(msgList.ToArray());
|
||
}
|
||
|
||
|
||
}
|
||
}
|