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;
///
///
///
///
public WmsFeeRateService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
}
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.Select()
.Where(whereList).Where(a => a.INUSE == true).ToQueryPage(request.PageCondition);
return data;
}
public DataResult GetWmsFeeRateInfo(string id)
{
var gid = Guid.Parse(id);
var data = db.Queryable()
.Select()
.Where((a) => a.FEERATEID == gid)
.First();
return DataResult.Success(data);
}
public DataResult GetWmsFeeRateDetailList(string id)
{
var data = db.Queryable()
.Select()
// .LeftJoin((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()
.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();
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();
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().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().Where(x => x.FEERATEID == gid)
.Select(n => n.ID.ToString()).ToArray();
var inDetails = model.List.Select(x => x.ID.ToString().ToUpper()).ToArray();
IEnumerable delDetails = existDetails.AsQueryable().Except(inDetails);
if (delDetails.Count() > 0)
{
foreach (var item in delDetails)
{
var existInfo = db.Queryable()
.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();
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();
db.Insertable(detail).ExecuteCommand();
}
}
#endregion
db.Ado.CommitTran();
return DataResult.Successed("更新成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("更新失败!" + ex);
}
}
}
}