using Infrastructure.Attribute; using Microsoft.AspNetCore.Http; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Infrastructure; using ARW.Model; using ARW.Repository; using ARW.Repository.Business.GoodsManager.Goodss; using ARW.Service.Business.IBusinessService.GoodsManager.Goodss; using ARW.Model.Dto.Business.GoodsManager.Goodss; using ARW.Model.Models.Business.GoodsManager.Goodss; using ARW.Model.Vo.Business.GoodsManager.Goodss; using ARW.Model.Models.Business.ShopManager.Shops; using ARW.Repository.Business.GoodsManager.GoodsServicess; using ARW.Repository.Business.GoodsManager.GoodsServicess.GoodsServicesRels; using ARW.Service.Business.IBusinessService.GoodsManager.GoodsServicess.GoodsServicesRels; using AngleSharp.Html; using ARW.Model.Models.Business.GoodsManager.GoodsServicess.GoodsServicesRels; using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.Specs; using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.SpecValues; using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.GoodsSpecRels; using Senparc.Weixin.MP.AdvancedAPIs.Semantic; using ARW.Model.Models.Business.GoodsManager.GoodsSpecs.Specs; using ARW.Model.Models.Business.GoodsManager.GoodsSpecs.SpecValues; namespace ARW.Service.Business.BusinessService.GoodsManager.Goodss { /// /// 商品接口实现类 /// /// @author lwh /// @date 2023-06-19 /// [AppService(ServiceType = typeof(IGoodsService), ServiceLifetime = LifeTime.Transient)] public class GoodsServiceImpl : BaseService, IGoodsService { private readonly GoodsRepository _GoodsRepository; private readonly IGoodsServicesRelService _GoodsServicesRelIService; private readonly ISpecService _SepcIService; private readonly ISpecValueService _SpecValueIService; private readonly IGoodsSpecRelService _GoodsSpecRelService; public GoodsServiceImpl(GoodsRepository GoodsRepository, IGoodsServicesRelService goodsServicesRelIService, ISpecService sepcIService, ISpecValueService specValueIService, IGoodsSpecRelService goodsSpecRelService) { this._GoodsRepository = GoodsRepository; _GoodsServicesRelIService = goodsServicesRelIService; _SepcIService = sepcIService; _SpecValueIService = specValueIService; _GoodsSpecRelService = goodsSpecRelService; } #region 业务逻辑代码 /// /// 查询商品分页列表 /// public async Task> GetGoodsList(GoodsQueryDto parm) { //开始拼装查询条件d var predicate = Expressionable.Create(); predicate = predicate.AndIF(parm.ShopGuid != null, s => s.ShopGuid == parm.ShopGuid); predicate = predicate.AndIF(parm.GoodsCategoryGuid != null, s => s.GoodsCategoryGuid == parm.GoodsCategoryGuid); predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.GoodsName), s => s.GoodsName.Contains(parm.GoodsName)); predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.GoodsCoding), s => s.GoodsCoding.Contains(parm.GoodsCoding)); var query = _GoodsRepository .Queryable() .LeftJoin((s, c) => s.ShopGuid == c.ShopGuid) .Where(predicate.ToExpression()) .OrderBy(s => s.GoodsSort, OrderByType.Asc) .Select((s, c) => new GoodsVo { GoodsId = s.GoodsId, GoodsGuid = s.GoodsGuid, ShopGuid = s.ShopGuid, ShopName = c.ShopName, GoodsCategoryGuid = s.GoodsCategoryGuid, GoodsSkuIds = s.GoodsSkuIds, DeliveryGuid = s.DeliveryGuid, GoodsName = s.GoodsName, GoodsCoding = s.GoodsCoding, GoodsMainImageVideo = s.GoodsMainImageVideo, GoodsVideoCover = s.GoodsVideoCover, GoodsPicture = s.GoodsPicture, GoodsSellingPoint = s.GoodsSellingPoint, GoodsSpecType = s.GoodsSpecType, GoodsPriceLowest = s.GoodsPriceLowest, GoodsPriceHighest = s.GoodsPriceHighest, GoodsDashedPriceLowest = s.GoodsDashedPriceLowest, GoodsTotalInventory = s.GoodsTotalInventory, GoodsSalesInitial = s.GoodsSalesInitial, GoodsSalesActual = s.GoodsSalesActual, GoodsDetails = s.GoodsDetails, GoodsDeductStockType = s.GoodsDeductStockType, GoodsIsPointsGift = s.GoodsIsPointsGift, GoodsIsPointsDiscount = s.GoodsIsPointsDiscount, GoodsIsAlonePointsDiscount = s.GoodsIsAlonePointsDiscount, GoodsPointsDiscountConfig = s.GoodsPointsDiscountConfig, GoodsIsEnableGrade = s.GoodsIsEnableGrade, GoodsIsAloneGrade = s.GoodsIsAloneGrade, GoodsAloneGradeEquity = s.GoodsAloneGradeEquity, GoodsShelfStatus = s.GoodsShelfStatus, GoodsSort = s.GoodsSort, }); return await query.ToPageAsync(parm); } /// /// 添加或修改商品 /// public async Task AddOrUpdateGoods(Goods model) { if (model.GoodsId != 0) { var response = await _GoodsRepository.UpdateAsync(model); return "修改成功!"; } else { await HandleBeforeAdd(model); var response = await _GoodsRepository.InsertReturnSnowflakeIdAsync(model); model.GoodsGuid = response; await HandleAfterAdd(model); return "添加成功!"; } } #region Excel处理 /// /// 数据导入处理 /// public async Task HandleImportData(GoodsVo Goods) { return Goods; } /// /// Excel导入 /// public async Task ImportExcel(Goods Goods, int index, bool isUpdateSupport, string user) { try { // 空值判断 // if (Goods.GoodsId == null) throw new CustomException("商品不能为空"); if (isUpdateSupport) { // 判断唯一值 var model = await GetFirstAsync(s => s.GoodsId == Goods.GoodsId); // 如果为空就新增数据 if (model == null) { // 开启事务 var res = await UseTranAsync(async () => { var addRes = await AddOrUpdateGoods(Goods); }); var addStr = $"第 {index} 行 => 商品:【{Goods.GoodsId}】新增成功!
"; return addStr; } else { // 如果有数据就进行修改 // 开启事务 await UseTranAsync(async () => { Goods.GoodsId = model.GoodsId; Goods.GoodsGuid = model.GoodsGuid; Goods.Update_by = user; Goods.Update_time = DateTime.Now; var editRes = await AddOrUpdateGoods(Goods); }); var editStr = $"第 {index} 行 => 商品:【{Goods.GoodsId}】更新成功!
"; return editStr; } } else { // 开启事务 var res = await UseTranAsync(async () => { var addRes = await AddOrUpdateGoods(Goods); }); //Console.WriteLine(res.IsSuccess); var addStr = $"第 {index} 行 => 商品:【{Goods.GoodsId}】新增成功!
"; return addStr; } } catch (Exception ex) { var errorRes = $"第 {index} 行 => 商品:【{Goods.GoodsId}】导入失败!{ex.Message}
"; return errorRes; throw; } } /// /// Excel数据导出处理 /// public async Task> HandleExportData(List data) { return data; } #endregion #region 商品格式处理 /// /// 商品添加前处理处理 /// /// 商品Model /// public async Task HandleBeforeAdd(Goods model) { // 单规格(最高价格 最高划线价格 = 最低价格 最低划线价格) model = HandleSingleSpec(model); return model; } /// /// 商品添加后处理 /// /// 商品Model /// public async Task HandleAfterAdd(Goods model) { // 服务与承诺 if (!string.IsNullOrEmpty(model.GoodsServicesIds)) { var servicesIds = model.GoodsServicesIds.Split(','); var servicesList = new List(); foreach (var id in servicesIds) { //var service = await _GoodsServicesRepository.GetFirstAsync(s => s.GoodsServicesId == Convert.ToInt32(id)); var service = new GoodsServicesRel { ShopGuid = model.ShopGuid, GoodsGuid = model.GoodsGuid, ServiceId = Convert.ToInt32(id) }; servicesList.Add(service); } await _GoodsServicesRelIService.InsertGoodsServicesRelAsync(servicesList); } // Sku列表 if (model.GoodsSpecType == "2") { // 规格组和规格值 foreach (var item in model.GoodsSpecList) { var spec = new Spec { SpecName = item.SpecName }; var specId = await _SepcIService.InsertReturnIdentityAsync(spec); foreach (var i in item.Props) { var specValue = new SpecValue { SpecId = specId, SpecValueName = i.SpecValueName, }; await _SpecValueIService.InsertReturnIdentityAsync(specValue); } } } } /// /// 商品单规格处理 /// /// 商品Model /// public Goods HandleSingleSpec(Goods model) { // 单规格 if (model.GoodsSpecType == "1") { // 价格 model.GoodsPriceLowest = model.GoodsPriceHighest; // 划线价格 if (model.GoodsDashedPriceHighest != 0) { model.GoodsDashedPriceLowest = model.GoodsDashedPriceHighest; } } return model; } #endregion #endregion } }