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;
///
/// 往来单位银行
///
public class ClientBankService : ServiceBase, IClientBankService
{
///
///
///
///
public ClientBankService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 列表
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = request.GetConditionalModels(Db);
var result = TenantDb.Queryable()
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return result;
}
///
/// 编辑
///
///
///
public async Task EditClientBankAsync(ClientBankReq req)
{
if (req.Id == 0)
{
if (await TenantDb.Queryable().Where(x => x.ClientId == req.ClientId && x.BankAccountNo == req.BankAccountNo && x.Account == req.Account && x.Currency == req.Currency).AnyAsync())
return DataResult.Failed("银行信息已存在!", MultiLanguageConst.ClientBankExist);
var data = req.Adapt();
var entity = await TenantDb.Insertable(data).ExecuteReturnEntityAsync();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = await TenantDb.Queryable().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();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
///
/// 详情
///
///
///
public async Task> GetClientBankInfoAsync(long id)
{
var data = await TenantDb.Queryable()
.Where(a => a.Id == id)
.Select()
.FirstAsync();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
///
/// 批量删除
///
///
///
public async Task DeleteAsync(IdModel req)
{
//删除客户信息
int rows = await TenantDb.Deleteable().Where(x => req.Ids.Contains(x.Id)).ExecuteCommandAsync();
return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
const int PAGE_SIZE = 200;
///
/// 导入客户银行
///
///
///
public async Task ImportAsync(List list)
{
long userId = long.Parse(User.UserId);
DateTime dtNow = DateTime.Now;
TenantDb.QueryFilter.Clear();
TenantDb.QueryFilter.Clear();
var clientNames = list.Select(x => x.CompanyName).Distinct();
var clients = await TenantDb.Queryable().Where(x => clientNames.Contains(x.ShortName) || clientNames.Contains(x.Description))
.Select(x => new
{
x.Id,
x.ShortName,
x.Description,
x.OrgId
}).ToListAsync();
StringBuilder sb = new();
List banks = new List(list.Count);
foreach (var model in list)
{
var clientList = clients.FindAll(x => x.ShortName == model.CompanyName || x.Description == model.CompanyName);
if (clientList.Count == 0)
{
sb.Append("," + model.CompanyName);
continue;
}
foreach (var item in clientList)
{
InfoClientBank bank = new()
{
ClientId = item.Id,
OrgId = item.OrgId,
CodeName = PinYinUtil.GetFristLetter(model.BankName),
BankName = model.BankName,
Account = model.InvoiceHeader,
AccountName = model.InvoiceHeader,
BankAddress = model.BankAddress,
BankAccountNo = model.BankAccount,
IsInvoiceDefault = model.IsDefault,
CreateBy = userId,
CreateUserName = User.UserName,
CreateTime = dtNow,
};
if (bank.CodeName.Length > 50)
bank.CodeName = bank.CodeName.Substring(0, 50);
if (model.Currency == "人民币")
bank.Currency = FeeCurrency.RMB_CODE;
else if (model.Currency == "美元")
bank.Currency = FeeCurrency.USD_CODE;
banks.Add(bank);
}
}
//if (sb.Length > 0)
//{
// sb.Remove(0, 1);
// return DataResult.Failed("下列【公司名称】匹配不到客户/供应商:" + sb.ToString());
//}
await TenantDb.Ado.BeginTranAsync();
try
{
//await TenantDb.Fastest().BulkMergeAsync(banks);
int page = Convert.ToInt32(Math.Ceiling(banks.Count / Convert.ToDouble(PAGE_SIZE)));
for (int i = 0; i < page; i++)
{
var batchList = banks.Skip(i * PAGE_SIZE).Take(PAGE_SIZE).ToList();
await TenantDb.Insertable(batchList).ExecuteCommandAsync();
}
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.Failed("导入失败:" + ex.Message);
}
}
}