diff --git a/Myshipping.Application/Service/DataSync/DataSyncService.cs b/Myshipping.Application/Service/DataSync/DataSyncService.cs index d8daad3c..f96c6e12 100644 --- a/Myshipping.Application/Service/DataSync/DataSyncService.cs +++ b/Myshipping.Application/Service/DataSync/DataSyncService.cs @@ -37,6 +37,7 @@ using Myshipping.Application.Enum; using Myshipping.Core.Helper; using Furion.TaskScheduler; using System.Linq.Expressions; +using Myshipping.Core.Service.Dict.Dto; namespace Myshipping.Application { @@ -265,50 +266,48 @@ namespace Myshipping.Application [SqlSugarUnitOfWork] public async Task SyncCustomer(List model) { - var userlist = await _repUser.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID).ToListAsync(); - var mlist = await _djycustomer.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID).ToListAsync(); - var shiplist = await _codeForwarder.AsQueryable().ToListAsync(); - foreach (var item in model) + foreach (DjyCustomerSyncDto item in model) { if (string.IsNullOrWhiteSpace(item.ShortName)) { throw Oops.Bah("简称未录入"); } - var m = mlist.Where(x => x.ShortName == item.ShortName).FirstOrDefault(); - var entity = item.Adapt(); - if (!string.IsNullOrEmpty(entity.SALE)) - { - entity.SALEID = userlist.Where(x => x.Name == entity.SALE).Count() == 0 ? "" : userlist.Where(x => x.Name == entity.SALE).Select(x => x.Id).FirstOrDefault().ToString(); - } - if (!string.IsNullOrEmpty(entity.OP)) - { - entity.OPID = userlist.Where(x => x.Name == entity.OP).Count() == 0 ? "" : userlist.Where(x => x.Name == entity.OP).Select(x => x.Id).FirstOrDefault().ToString(); - } - if (!string.IsNullOrEmpty(entity.DOC)) - { - entity.DOCID = userlist.Where(x => x.Name == entity.DOC).Count() == 0 ? "" : userlist.Where(x => x.Name == entity.DOC).Select(x => x.Id).FirstOrDefault().ToString(); - } - if (!string.IsNullOrEmpty(entity.CUSTSERVICE)) - { - entity.CUSTSERVICEID = userlist.Where(x => x.Name == entity.CUSTSERVICE).Count() == 0 ? "" : userlist.Where(x => x.Name == entity.CUSTSERVICE).Select(x => x.Id).FirstOrDefault().ToString(); - - } - if (entity.Level == "R3" && !string.IsNullOrEmpty(entity.CUSTSERVICE)) - { + DjyCustomer customer = await _djycustomer.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID && x.ShortName == item.ShortName).FirstAsync(); + var shiplist = await _codeForwarder.AsQueryable().ToListAsync(); - throw Oops.Bah("等级为R3,客服为空"); - - } - if ((entity.Level == "R1" || entity.Level == "R2") && string.IsNullOrEmpty(entity.CUSTSERVICE)) - { - - throw Oops.Bah("等级为R1/R2,客服必填"); - - } - entity.PropString = entity.PropString.Replace("forwarder", "booking_agent"); - if (m == null) + if (customer == null) { - + var entity = item.Adapt(); + if (!string.IsNullOrEmpty(entity.SALE)) + { + var sysUser = await _repUser.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID && x.Name == entity.SALE).FirstAsync(); + entity.SALEID = sysUser?.Id.ToString() ?? ""; + } + //2023-11-14确认东胜没有下面三种人员,所以不做处理 + //if (!string.IsNullOrEmpty(entity.OP)) + //{ + // var sysUser = await _repUser.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID && x.Name == entity.OP).FirstAsync(); + // entity.OPID = sysUser?.Id.ToString() ?? ""; + //} + //if (!string.IsNullOrEmpty(entity.DOC)) + //{ + // var sysUser = await _repUser.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID && x.Name == entity.DOC).FirstAsync(); + // entity.DOCID = sysUser?.Id.ToString() ?? ""; + //} + //if (!string.IsNullOrEmpty(entity.CUSTSERVICE)) + //{ + // var sysUser = await _repUser.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID && x.Name == entity.CUSTSERVICE).FirstAsync(); + // entity.CUSTSERVICEID = sysUser?.Id.ToString() ?? ""; + //} + //if (entity.Level == "R3" && !string.IsNullOrEmpty(entity.CUSTSERVICE)) + //{ + // throw Oops.Bah("等级为R3,客服为空"); + //} + //if ((entity.Level == "R1" || entity.Level == "R2") && string.IsNullOrEmpty(entity.CUSTSERVICE)) + //{ + // throw Oops.Bah("等级为R1/R2,客服必填"); + //} + entity.PropString = entity.PropString.Replace("forwarder", "booking_agent"); entity.TenantId = UserManager.TENANT_ID; await _djycustomer.InsertAsync(entity); foreach (var it in item.ContactList) @@ -320,7 +319,6 @@ namespace Myshipping.Application } if (entity.PropString.Contains("shipagency")) { - var ship = shiplist.Where(x => x.Code == entity.CodeName).FirstOrDefault(); if (ship == null) { @@ -333,7 +331,6 @@ namespace Myshipping.Application Code = entity.CodeName, Name = entity.ShortName }); - } else { @@ -343,63 +340,122 @@ namespace Myshipping.Application ship.ModifyTime = DateTime.Now; ship.ModifyUser = UserManager.DjyUserId; await _codeForwarder.UpdateAsync(ship); - } - - await _commonDBService.GetAllForwarder(false); } } else { - entity.Id = m.Id; - await _djycustomer.AsUpdateable(entity).IgnoreColumns(it => new - { - it.TenantId, - it.TIDANINFO - }).ExecuteCommandAsync(); - await _djycustomercontact.DeleteAsync(x => x.CustomerId == entity.Id); - foreach (var it in item.ContactList) + DjyCustomer updateModel = new(); + updateModel.Id = customer.Id; + _djycustomer.Context.Tracking(updateModel); + + // 字段分类1:取值和东胜相同的字段 + updateModel.CodeName = item.CodeName; + updateModel.FullName = item.FullName; + updateModel.Chief = item.Chief; + updateModel.Tel = item.Tel; + updateModel.Email = item.Email; + updateModel.QQ = item.QQ; + updateModel.WebUrl = item.WebUrl; + updateModel.Province = item.Province; + updateModel.City = item.City; + updateModel.Addr = item.Addr; + //updateModel.FullNameEN = item.FullNameEN; + //updateModel.AddrEN = item.AddrEN; + updateModel.InvTitle = item.InvTitle; + updateModel.TaxNO = item.TaxNO; + updateModel.InvAddrTel = item.InvAddrTel; + updateModel.RMBBank = item.RMBBank; + updateModel.RmbAccount = item.RmbAccount; + updateModel.USDBank = item.USDBank; + updateModel.USDAccount = item.USDAccount; + + // 字段分类2:特殊处理 + if (!string.IsNullOrEmpty(item.PropString) && item.PropString != customer.PropString) + { + if (string.IsNullOrEmpty(customer.PropString)) + { + updateModel.PropString = item.PropString; + } + else + { + // 原有的客户属性code列表 + List oldProps = customer.PropString.Split("][").Select(o => o.Replace("[", "").Replace("]", "")).ToList(); + + // 东胜客户属性在大简云中存在的code列表 + List dictDataDtos = await _cache.GetAllDictData(); + List dsCustPropCodeList = dictDataDtos.Where(d => d.TypeCode == "djy_cust_prop_map_ds")?.Select(v => v.Code)?.ToList(); + if (dsCustPropCodeList != null && dsCustPropCodeList.Count > 0) + { + // 新的客户属性code列表 + List newProps = new(); + + // 判断:如果原有的客户属性code列表中,存在东胜没有的,则保留 + oldProps.ForEach(o => + { + if (!dsCustPropCodeList.Contains(o)) + { + newProps.Add(o); + } + }); + + // 添加上东胜传过来的客户属性code列表 + List dsProps = item.PropString.Split("][").Select(o => o.Replace("[", "").Replace("]", "")).ToList(); + newProps.AddRange(dsProps); + + // 去重 + newProps = newProps.Distinct().ToList(); + + // 赋值 + string newPropsStr = "[" + string.Join("][", newProps) + "]"; + updateModel.PropString = newPropsStr; + } + else + { + throw Oops.Bah("未检索到【djy_cust_prop_map_ds】字典信息"); + } + } + } + + // 字段分类3:东胜传过来的值不为空则更新,否则保持不变 + if (!string.IsNullOrEmpty(item.SALE)) { - var contact = it.Adapt(); - contact.CustomerId = entity.Id; - contact.TenantId = UserManager.TENANT_ID; - await _djycustomercontact.InsertAsync(contact); + updateModel.SALE = item.SALE; + var sysUser = await _repUser.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID && x.Name == item.SALE).FirstAsync(); + updateModel.SALEID = sysUser?.Id.ToString() ?? ""; } - if (entity.PropString.Contains("shipagency")) + + // 执行更新 + await _djycustomer.Context.Updateable(updateModel).ExecuteCommandAsync(); + + if (item.PropString.Contains("shipagency")) { //船代 - var ship = shiplist.Where(x => x.Code == entity.CodeName).FirstOrDefault(); + var ship = shiplist.Where(x => x.Code == item.CodeName).FirstOrDefault(); if (ship == null) { - await _codeForwarder.InsertAsync(new CodeForwarder { GID = Guid.NewGuid().ToString(), CreateTime = DateTime.Now, CreateUser = UserManager.DjyUserId, - Code = entity.CodeName, - Name = entity.ShortName + Code = item.CodeName, + Name = item.ShortName }); - } else { - - ship.Code = entity.CodeName; - ship.Name = entity.ShortName; + ship.Code = item.CodeName; + ship.Name = item.ShortName; ship.ModifyTime = DateTime.Now; ship.ModifyUser = UserManager.DjyUserId; await _codeForwarder.UpdateAsync(ship); - } - await _commonDBService.GetAllForwarder(false); - } } } - }