You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.3 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using DS.WMS.Common.Enum;
using DS.WMS.Core.FeeModule.Dtos;
using DS.WMS.Core.FeeModule.Entity;
using DS.WMS.Core.FeeModule.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.FeeModule.Method;
public class WmsFeeRateService : IWmsFeeRateService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public WmsFeeRateService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
}
public DataResult<List<WmsFeeRateListPageModel>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<OP_WMS_FEERATE>()
.Select<WmsFeeRateListPageModel>()
.Where(whereList).Where(a => a.INUSE == true).ToQueryPage(request.PageCondition);
return data;
}
public DataResult<WmsFeeRateInfoViewModel> GetWmsFeeRateInfo(string id)
{
var gid = Guid.Parse(id);
var data = db.Queryable<OP_WMS_FEERATE>()
.Select<WmsFeeRateInfoViewModel>()
.Where((a) => a.FEERATEID == gid)
.First();
return DataResult<WmsFeeRateInfoViewModel>.Success(data);
}
public DataResult GetWmsFeeRateDetailList(string id)
{
var data = db.Queryable<OP_WMS_FEERATE_DETAIL>()
.Select<FeeRateDetailListModel>()
// .LeftJoin<OP_WMS_STOREHOUSE_AREA>((a, b) => a.AREACODE == b.AREACODE)
// .Select((a, b) => new FeeRateDetailListModel()
// {
// AREANAME = b.AREANAME //手动指定一列在自动映射
// },
// true)
.Mapper(it => { it.FEETYPENAME = it.FEETYPE == 1 ? "应收" : "应付"; })
.Where(a => a.FEERATEID == Guid.Parse(id)).ToList();
return DataResult.Successed("查询成功!", data);
}
public DataResult EditWmsFeeRateInfo(WmsFeeRateInput model)
{
if (model.FEERATEID.ToString().IsNullOrEmpty())
{
var exitInfo = db.Queryable<OP_WMS_FEERATE>()
.Where(x => x.ClientId == model.ClientId && x.GoodsFeeTypeGID == model.GoodsFeeTypeGID).First();
if (exitInfo.IsNotNull())
{
return DataResult.Failed("存在同一客户计费大类的协议费率!");
}
try
{
//开启事务
db.Ado.BeginTran();
var gid = Guid.NewGuid();
model.FEERATEID = gid;
var rate = model.Adapt<OP_WMS_FEERATE>();
db.Insertable(rate).ExecuteCommand();
#region 处理明细
if (model.List.Count() > 0)
{
foreach (var item in model.List)
{
item.ID = Guid.NewGuid();
item.FEERATEID = gid;
var detail = item.Adapt<OP_WMS_FEERATE_DETAIL>();
db.Insertable(detail).ExecuteCommand();
}
}
#endregion
db.Ado.CommitTran();
return DataResult.Successed("添加成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("添加失败!" + ex);
}
}
else
{
try
{
var gid = (Guid)model.FEERATEID;
var exitInfo = db.Queryable<OP_WMS_FEERATE>().First(x => x.FEERATEID == gid);
//开启事务
db.Ado.BeginTran();
var rate = model.Adapt(exitInfo);
db.Updateable(rate).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
#region 处理明细
if (model.List.Count() > 0)
{
//先删除 明细
var existDetails = db.Queryable<OP_WMS_FEERATE_DETAIL>().Where(x => x.FEERATEID == gid)
.Select(n => n.ID.ToString()).ToArray();
var inDetails = model.List.Select(x => x.ID.ToString().ToUpper()).ToArray();
IEnumerable<string> delDetails = existDetails.AsQueryable().Except(inDetails);
if (delDetails.Count() > 0)
{
foreach (var item in delDetails)
{
var existInfo = db.Queryable<OP_WMS_FEERATE_DETAIL>()
.Where(x => x.FEERATEID == gid && x.ID == Guid.Parse(item)).First();
db.Deleteable(existInfo).ExecuteCommand();
}
}
//更新 明细
var updateList = model.List.Where(x =>
x.FEERATEID != null && !delDetails.Contains(x.ID.ToString()))
.ToList();
foreach (var item in updateList)
{
var detail = item.Adapt<OP_WMS_FEERATE_DETAIL>();
db.Updateable(detail).ExecuteCommand();
}
//追加 明细
var addList = model.List.Where(x => x.ID == null).ToList();
foreach (var item in addList)
{
item.ID = Guid.NewGuid();
item.FEERATEID = gid;
var detail = item.Adapt<OP_WMS_FEERATE_DETAIL>();
db.Insertable(detail).ExecuteCommand();
}
}
#endregion
db.Ado.CommitTran();
return DataResult.Successed("更新成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("更新失败!" + ex);
}
}
}
}