using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Myshipping.Core.Entity;
using System;
using Furion.FriendlyException;
namespace Myshipping.Core.Service
{
///
/// 客户参数
///
[ApiDescriptionSettings(Name = "DJYCustomerParam", Order = 1)]
public class DjyCustomerParamService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _repParam;
private readonly SqlSugarRepository _repParamItem;
private readonly SqlSugarRepository _repParamValue;
private readonly ILogger _logger;
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository _repCustomer;
public DjyCustomerParamService(SqlSugarRepository repParam,
SqlSugarRepository repParamItem,
SqlSugarRepository repParamValue,
ISysCacheService cache, ILogger logger,
SqlSugarRepository repCustomer)
{
_repParam = repParam;
_logger = logger;
_repParamItem = repParamItem;
_repParamValue = repParamValue;
_cache = cache;
_repCustomer = repCustomer;
}
#region 参数类别
///
/// 获取所有参数类别
///
///
[HttpPost("/DJYCustomerParam/ParamList")]
public async Task> ParamList()
{
var list = await _repParam.AsQueryable().ToListAsync();
return list.Adapt>();
}
#endregion
#region 参数值
///
/// 分页获取参数值
///
///
[HttpPost("/DJYCustomerParam/PageParamValue")]
public async Task> PageParamValue(DjyCustomerParamValueQueryInput input)
{
var list = await _repParamValue.AsQueryable().Filter(null, true)
.WhereIF(!string.IsNullOrEmpty(input.ParaCode), x => x.ParaCode == input.ParaCode)
.WhereIF(input.CustomerId.HasValue, x => x.CustomerId == input.CustomerId)
.OrderBy(x => x.Sort)
.ToPagedListAsync(input.PageNo, input.PageSize);
var rtn = list.Adapt>();
var listParam = await _repParam.AsQueryable().ToListAsync();
var listParamItem = await _repParamItem.AsQueryable().ToListAsync();
var listCustomer = await _repCustomer.AsQueryable().Select(x => new { x.Id, x.ShortName }).ToListAsync();
foreach (var item in rtn.Items)
{
var p = listParam.FirstOrDefault(x => x.ParaCode == item.ParaCode);
item.ParaName = p.ParaName;
var t = listCustomer.FirstOrDefault(x => x.Id == item.CustomerId);
item.CustomerName = t.ShortName;
var it = listParamItem.FirstOrDefault(x => x.ParaCode == item.ParaCode && x.ItemCode == item.ItemCode);
if (it != null)
{
item.ItemCodeName = it.ItemName;
}
}
return rtn;
}
///
/// 增加参数
///
///
[HttpPost("/DJYCustomerParam/saveParamValue")]
public async Task SaveParamValue(DjyCustomerParamValueSaveInput input)
{
DjyCustomerParamValue entity = null;
if (input.Id > 0)
{
entity = await _repParamValue.AsQueryable().FirstAsync(x => x.Id == input.Id);
input.Adapt(entity);
await _repParamValue.UpdateAsync(entity);
}
else
{
entity = input.Adapt();
await _repParamValue.InsertAsync(entity);
}
}
///
///删除参数
///
///
[HttpPost("/DJYCustomerParam/deleteParamValue")]
[SqlSugarUnitOfWork]
public async Task DeleteParamValue(long Id)
{
await _repParamValue.DeleteAsync(u => u.Id == Id);
}
#endregion
}
}