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;
namespace ARW.Service.Business.BusinessService.GoodsManager.Goodss
{
///
/// 商品接口实现类
///
/// @author 黎文豪
/// @date 2023-06-19
///
[AppService(ServiceType = typeof(IGoodsService), ServiceLifetime = LifeTime.Transient)]
public class GoodsServiceImpl : BaseService, IGoodsService
{
private readonly GoodsRepository _GoodsRepository;
public GoodsServiceImpl(GoodsRepository GoodsRepository)
{
this._GoodsRepository = GoodsRepository;
}
#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()
.Where(predicate.ToExpression())
.OrderBy(s => s.GoodsSort, OrderByType.Asc)
.Select(s => new GoodsVo
{
GoodsId = s.GoodsId,
GoodsGuid = s.GoodsGuid,
ShopGuid = s.ShopGuid,
GoodsCategoryGuid = s.GoodsCategoryGuid,
GoodsSkuGuid = s.GoodsSkuGuid,
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
{
var response = await _GoodsRepository.InsertReturnSnowflakeIdAsync(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
#endregion
}
}