using System.Linq.Expressions;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Code.Dtos;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Info.Dtos;
using DS.WMS.Core.Info.Interface;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using Mapster;
namespace DS.WMS.Core.Info.Method
{
///
/// 客户参数
///
public class ClientParamService : ServiceBase, IClientParamService
{
///
/// 初始化
///
///
public ClientParamService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 获取客户参数
///
/// 参数值的类型
/// 业务ID
/// 参数名
/// 客户类别连接条件
///
public async Task> GetParamAsync(long businessId, string paramName,
Expression> joinExpression)
{
Param param = new() { Name = paramName };
var model = await TenantDb.Queryable()
.InnerJoin(joinExpression)
.Where((x, y) => y.Id == businessId && x.ParamCode == paramName)
.Select((x, y) => new
{
x.ItemCode,
x.ItemName
}).FirstAsync();
param.DisplayValue = model?.ItemName;
if (model == null)
return param;
var type = typeof(T);
var targetType = Nullable.GetUnderlyingType(type);
if (targetType != null)
type = targetType;
if (type.IsEnum && Enum.TryParse(type, model.ItemCode, true, out object? enumValue)) //枚举特殊处理
{
param.Value = (T)enumValue;
}
else
{
param.Value = (T)Convert.ChangeType(model.ItemCode, type);
}
return param;
}
///
/// 列表
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = request.GetConditionalModels(Db);
var data = TenantDb.Queryable()
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
///
/// 编辑
///
///
///
public DataResult EditClientParam(ClientParamReq req)
{
if (req.Id == 0)
{
if (TenantDb.Queryable().Where(x => x.ParamId == req.ParamId && x.CustomerId == req.CustomerId).Any())
{
return DataResult.Failed("往来单位参数已存在!", MultiLanguageConst.ClientParamExist);
}
var data = req.Adapt();
var entity = TenantDb.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = TenantDb.Queryable().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
TenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
///
/// 详情
///
///
///
public DataResult GetClientParamInfo(string id)
{
var data = TenantDb.Queryable()
.Where(a => a.Id == long.Parse(id))
.Select()
.First();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
public DataResult BatchDelClientParam(IdModel req)
{
var list = TenantDb.Queryable().Where(x => req.Ids.Contains(x.Id)).ToList();
if (list.Count > 0)
{
TenantDb.Deleteable(list).ExecuteCommand();
}
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
public DataResult> GetTenantParamSelectList()
{
var data = Db.Queryable().Where(x => x.Status == StatusEnum.Enable)
.Select()
.ToList();
return DataResult>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
public DataResult> GetTenantParamDataSelectList(string code = "")
{
var data = Db.Queryable().Where(x => x.Status == StatusEnum.Enable)
.WhereIF(code.IsNotEmptyOrNull(), x => x.ParamCode.ToLower() == code.ToLower())
.Select()
.ToList();
return DataResult>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
}
}