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.

206 lines
6.8 KiB
C#

using System.Text;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Info.Dtos;
using DS.WMS.Core.Info.Entity;
using DS.WMS.Core.Info.Interface;
using Mapster;
namespace DS.WMS.Core.Info.Method;
/// <summary>
/// 往来单位银行
/// </summary>
public class ClientBankService : ServiceBase, IClientBankService
{
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public ClientBankService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<ClientBankRes>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = request.GetConditionalModels(Db);
var result = TenantDb.Queryable<InfoClientBank>()
.Where(whereList)
.Select<ClientBankRes>().ToQueryPage(request.PageCondition);
if (result.Data?.Count > 0)
{
var ids = result.Data.Select(x => x.Id);
var list = TenantDb.Queryable<InvoiceHeader>().Where(x => ids.Contains(x.RelativeId)).ToList();
foreach (var item in result.Data)
{
item.InvoiceHeaders = list.FindAll(x => x.RelativeId == item.Id);
}
}
return result;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult> EditClientBankAsync(ClientBankReq req)
{
if (req.Id == 0)
{
if (await TenantDb.Queryable<InfoClientBank>().Where(x => x.BankName == req.BankName && x.BankAccountNo == req.BankAccountNo).AnyAsync())
return DataResult.Failed("客户银行信息已存在!", MultiLanguageConst.ClientBankExist);
var data = req.Adapt<InfoClientBank>();
var entity = await TenantDb.InsertNav(data).Include(x => x.InvoiceHeaders).ExecuteReturnEntityAsync();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = await TenantDb.Queryable<InfoClientBank>().Where(x => x.Id == req.Id).FirstAsync();
if (info == null)
{
return DataResult.FailedWithDesc(MultiLanguageConst.EmptyData);
}
info = req.Adapt(info);
await TenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
//创建或更新发票抬头
if (req.InvoiceHeaders?.Count > 0)
await TenantDb.Storageable(req.InvoiceHeaders).DefaultAddElseUpdate().ExecuteCommandAsync();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<DataResult<ClientBankRes>> GetClientBankInfoAsync(long id)
{
var data = await TenantDb.Queryable<InfoClientBank>()
.Where(a => a.Id == id)
.Select<ClientBankRes>()
.FirstAsync();
if (data != null)
data.InvoiceHeaders = await TenantDb.Queryable<InvoiceHeader>().Where(x => x.RelativeId == data.Id).ToListAsync();
return DataResult<ClientBankRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 批量删除
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult> DeleteAsync(IdModel req)
{
//删除客户信息
int rows = await TenantDb.Deleteable<InfoClientBank>().Where(x => req.Ids.Contains(x.Id)).ExecuteCommandAsync();
return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
/// <summary>
/// 导入客户银行
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public async Task<DataResult> ImportAsync(List<InfoClientBankModel> list)
{
long userId = long.Parse(User.UserId);
DateTime dtNow = DateTime.Now;
var clientNames = list.Select(x => x.CompanyName).Distinct();
var clients = await TenantDb.Queryable<InfoClient>().Where(x => clientNames.Contains(x.ShortName))
.Select(x => new
{
x.Id,
x.ShortName,
x.OrgId
}).ToListAsync();
StringBuilder sb = new();
List<InfoClientBank> banks = new List<InfoClientBank>(list.Count);
foreach (var model in list)
{
var client = clients.Find(x => x.ShortName == model.CompanyName);
if (client == null)
{
sb.Append("," + model.CompanyName);
continue;
}
InfoClientBank bank = new()
{
ClientId = client.Id,
OrgId = client.OrgId,
BankName = model.BankName,
Account = model.BankAccount,
BankAddress = model.BankAddress,
BankAccountNo = model.BankAccount,
IsInvoiceDefault = model.IsDefault,
CreateBy = userId,
CreateUserName = User.UserName,
CreateTime = dtNow,
InvoiceHeaders = []
};
if (model.Currency == "人民币")
bank.Currency = FeeCurrency.RMB_CODE;
else if (model.Currency == "美元")
bank.Currency = FeeCurrency.USD_CODE;
bank.InvoiceHeaders.Add(new InvoiceHeader
{
Header = model.InvoiceHeader,
AddressTel = model.InvoiceAddress + " " + model.InvoiceTel
});
banks.Add(bank);
}
if (sb.Length > 0)
{
sb.Remove(0, 1);
return DataResult.Failed("下列【公司名称】匹配不到客户/供应商:" + sb.ToString());
}
await TenantDb.Ado.BeginTranAsync();
try
{
await TenantDb.Fastest<InfoClientBank>().BulkMergeAsync(banks);
foreach (var bank in banks)
{
foreach (var item in bank.InvoiceHeaders)
item.RelativeId = bank.Id;
}
var invoiceHeaders = banks.SelectMany(x => x.InvoiceHeaders).ToList();
await TenantDb.Fastest<InvoiceHeader>().BulkMergeAsync(invoiceHeaders);
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.Failed("导入失败:" + ex.Message);
}
}
}