using System.Text;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
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 ClientContactService : ServiceBase, IClientContactService
{
///
/// 初始化
///
///
public ClientContactService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 列表
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = request.GetConditionalModels(Db);
var data = TenantDb.Queryable()
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
///
/// 编辑
///
///
///
public DataResult EditClientContact(ClientContactReq req)
{
if (req.Id == 0)
{
if (TenantDb.Queryable().Any(x => x.Name == req.Name && x.Email == req.Email && x.Mobile == req.Mobile))
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.ClientContactExist));
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 GetClientContactInfo(string id)
{
var data = TenantDb.Queryable()
.Where(a => a.Id == long.Parse(id))
.Select()
.First();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
///
/// 根据ID批量删除
///
///
///
public async Task DeleteAsync(IdModel model)
{
int rows = await TenantDb.Deleteable().Where(x => model.Ids.Contains(x.Id)).ExecuteCommandAsync();
return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
///
/// 导入客户联系人
///
///
///
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 contacts = new(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)
{
InfoClientContact contact = new()
{
ClientId = item.Id,
OrgId = item.OrgId,
CreateBy = userId,
CreateUserName = User.UserName,
CreateTime = dtNow,
Name = model.ContactName,
EnName = model.ContactEnName,
Job = model.Job,
Email = model.Email,
Mobile = model.Mobile,
Tel = model.Tel,
QQ = model.QQ,
IsDefault = model.IsDefault
};
contacts.Add(contact);
}
}
//if (sb.Length > 0)
//{
// sb.Remove(0, 1);
// return DataResult.Failed("下列【所属公司】匹配不到客户/供应商:" + sb.ToString());
//}
await TenantDb.Ado.BeginTranAsync();
try
{
await TenantDb.Fastest().BulkMergeAsync(contacts);
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.Failed("导入失败:" + ex.Message);
}
}
}