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.

164 lines
5.3 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;
using Masuit.Tools.Models;
namespace DS.WMS.Core.Info.Method;
/// <summary>
/// 往来单位联系人
/// </summary>
public class ClientContactService : ServiceBase, IClientContactService
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider"></param>
public ClientContactService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<ClientContactRes>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = request.GetConditionalModels(Db);
var data = TenantDb.Queryable<InfoClientContact>()
.Where(whereList)
.Select<ClientContactRes>().ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditClientContact(ClientContactReq req)
{
if (req.Id == 0)
{
if (TenantDb.Queryable<InfoClientContact>().Any(x => x.Name == req.Name && x.Email == req.Email && x.Mobile == req.Mobile))
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.ClientContactExist));
var data = req.Adapt<InfoClientContact>();
var entity = TenantDb.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = TenantDb.Queryable<InfoClientContact>().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
TenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<ClientContactRes> GetClientContactInfo(string id)
{
var data = TenantDb.Queryable<InfoClientContact>()
.Where(a => a.Id == long.Parse(id))
.Select<ClientContactRes>()
.First();
return DataResult<ClientContactRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 根据ID批量删除
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<DataResult> DeleteAsync(IdModel model)
{
int rows = await TenantDb.Deleteable<InfoClientContact>().Where(x => model.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<InfoClientContactModel> 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<InfoClientContact> contacts = new List<InfoClientContact>(list.Count);
foreach (var model in list)
{
var client = clients.Find(x => x.ShortName == model.CompanyName);
if (client == null)
{
sb.Append("," + model.CompanyName);
continue;
}
InfoClientContact contact = new()
{
ClientId = client.Id,
OrgId = client.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<InfoClientContact>().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);
}
}
}