12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634 |
- using AutoMapper;
- using AutoMapper.Execution;
- using EyeSoft.Extensions;
- using MySqlX.XDevAPI.Relational;
- using Newtonsoft.Json;
- using NPOI.OpenXmlFormats.Dml.Diagram;
- using NPOI.SS.Formula.Functions;
- using NPOI.SS.UserModel;
- using OASystem.Domain;
- using OASystem.Domain.AesEncryption;
- using OASystem.Domain.Dtos;
- using OASystem.Domain.Dtos.CRM;
- using OASystem.Domain.Entities.Customer;
- using OASystem.Domain.Entities.Resource;
- using OASystem.Domain.Enums;
- using OASystem.Domain.ViewModels.CRM;
- using OASystem.Domain.ViewModels.JuHeExchangeRate;
- using OASystem.Domain.ViewModels.QiYeWeChat;
- using OASystem.Infrastructure.Tools;
- using SqlSugar;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel.Design;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- using System.Linq;
- using System.Reflection.Metadata;
- using System.Runtime.Intrinsics.Arm;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- using XAct;
- using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
- using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
- namespace OASystem.Infrastructure.Repositories.CRM
- {
- public class NewClientDataRepository : BaseRepository<Crm_NewClientData, NewClientDataView>
- {
- private readonly IMapper _mapper;
- public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
- base(sqlSugar)
- {
- _mapper = mapper;
- }
- /// <summary>
- /// 客户资料
- /// 基础数据源
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> _Init(MarketCustomerInitDto Dto)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- var portType = Dto.PortType;
- if (portType == 1 || portType == 2 || portType == 3)
- {
- #region 下拉框初始化数据
- //负责人下拉框
- //List<dynamic> _Users = new List<dynamic>();
- //var _Users = _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0).Select(x => new
- //{
- // x.Id,
- // Name = x.CnName
- //}).ToList();
- var _Users = GetNewExistClient(Dto.UserId)
- .Select(x => new
- {
- x.Id,
- Name = x.CnName
- }).ToList();
- //List<Sys_Users> users = GetNewExistClient();
- //foreach (Sys_Users user in users)
- //{
- // var data = new
- // {
- // Id = user.Id,
- // Name = user.CnName
- // };
- // _Users.Add(data);
- //};
- List<Sys_SetData> initData = _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToList();
- //客户级别数据
- List<dynamic> _level = new List<dynamic>();
- List<Sys_SetData> level = initData.Where(u => u.STid == 33 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in level)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _level.Add(data);
- };
- //客户类别
- ArrayList _CustomerClass = new ArrayList();
- List<Sys_SetData> CustomerClass = initData.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in CustomerClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name,
- item.Remark
- };
- _CustomerClass.Add(data);
- };
- // 创建比较器实例
- IComparer remakeComparer = new RemakeComparer();
- _CustomerClass.Sort(remakeComparer);
- //业务分类
- List<dynamic> _ServiceClass = new List<dynamic>();
- List<Sys_SetData> ServiceClass = initData.Where(u => u.STid == 36 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in ServiceClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _ServiceClass.Add(data);
- };
- //身份分类
- List<dynamic> _ProvinceClass = new List<dynamic>();
- List<Sys_SetData> ProvinceClass = initData.Where(u => u.STid == 42 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in ProvinceClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _ProvinceClass.Add(data);
- };
- #endregion
- var data1 = new {
- Users = _Users,
- Level = _level,
- CustomerClass = _CustomerClass,
- ServiceClass= _ServiceClass,
- ProvinceClass = _ProvinceClass
- };
- return result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = data1
- };
- }
- else
- {
- result.Msg = string.Format("请传入有效的PortType参数!");
- }
- return result;
- }
- /// <summary>
- /// 市场客户资料数据
- /// 详情
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> _Details(int portType,int id)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- if (portType == 1 || portType == 2 || portType == 3)
- {
- if (id < 0)
- {
- result.Msg = string.Format("请传入有效的Id参数!");
- return result;
- }
- string infoSql = string.Format(@" Select * From Crm_NewClientData Where Isdel = 0 And Id = {0}", id);
- var info = await _sqlSugar.SqlQueryable<DetailsView>(infoSql).FirstAsync();
- if (info != null)
- {
- EncryptionProcessor.DecryptProperties(info);
- List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
- ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + info.Id + " AND u1.ISDEL = 0").ToListAsync();
- info.AscribedUser = AscribedUser.Select(it => it.UserId).ToList();
- List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
- ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + info.Id + " AND d1.ISDEL = 0").ToListAsync();
- info.AscribedDepartment = AscribedDepartment.Select(it => it.Id).ToList();
- result.Code = 0;
- result.Data = info;
- }
- }
- else result.Msg = MsgTips.Port;
- return result;
- }
- /// <summary>
- /// 客户资料初识初始化
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
- {
- Stopwatch stopwatch = Stopwatch.StartNew();
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
- try
- {
- #region 交集
- List<int> NewClientDataId1 = new List<int>();
- List<int> NewClientDataId2 = new List<int>();
- int state = 0;
- #region 负责人
- if (dto.OperationUserId != 21)
- {
- if (string.IsNullOrWhiteSpace(dto.Userid))
- {
- dto.Userid = dto.OperationUserId.ToString();
- }
- }
- if (!string.IsNullOrWhiteSpace(dto.Userid))
- {
- string sql = string.Format(@"select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0}) and u1.IsDel = 0", dto.Userid);
- List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
- if (ascribedUsers.Count != 0)
- {
- foreach (var ascribedUser in ascribedUsers)
- {
- if (ascribedUser.NewClientDataId != 0)
- {
- NewClientDataId1.Add(ascribedUser.NewClientDataId);
- }
- }
- }
- else
- {
- result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- state = -1;
- }
- #endregion
- #region 业务归属
- if (!string.IsNullOrWhiteSpace(dto.Business))
- {
- string sql = string.Format(@"select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0}) and d1.isdel = 0", dto.Business);
- List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
- if (AscribedDepartment.Count != 0)
- {
- foreach (var item in AscribedDepartment)
- {
- if (item.NewClientDataId != 0)
- {
- NewClientDataId2.Add(item.NewClientDataId);
- }
- }
- }
- else
- {
- result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- state = -1;
- }
- #endregion
- List<int> intList = new List<int>();
- if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
- {
- intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
- }
- else if (NewClientDataId1.Count != 0)
- {
- intList = NewClientDataId1;
- }
- else if (NewClientDataId2.Count != 0)
- {
- if (dto.OperationUserId == 21)
- {
- intList = NewClientDataId2;
- }
- }
- if (state == -1)
- {
- if (intList.Count < 1) intList.Add(-1);
- }
-
- #endregion
- #region 省域条件
- var rangeSetDataList = new List<int>();
- if (dto.Range != 0)
- {
- string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
- switch (dto.Range)
- {
- case 419:
- setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
- break;
- case 421:
- setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
- break;
- case 420:
- setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
- break;
- case 423:
- setDataSql += " and (Name like '%重庆%')";
- break;
- case 422:
- setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
- break;
- case 578:
- setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
- break;
- case 605:
- setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
- break;
- case 606:
- setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
- break;
- case 625:
- setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
- break;
- case 634:
- setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
- break;
- }
- rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
- }
- #endregion
- var NewClientDataView = new List<NewClientDataView>();
- var count = 0;
- string contact = dto.Contact, location = dto.Location, clientDto = dto.Client;
- var isSelectSearch = false;
- var searchDataIds = new List<int>();
- if (string.IsNullOrEmpty(contact) || string.IsNullOrEmpty(location) || string.IsNullOrEmpty(clientDto))
- {
- isSelectSearch = true;
- var searchClientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
- .Where(x => x.IsDel == 0)
- .Select(x => new Crm_NewClientData() { Id = x.Id, Client = x.Client, Location = x.Location, Contact = x.Contact })
- .ToListAsync();
- foreach (var item in searchClientDatas) EncryptionProcessor.DecryptProperties(item);
- if (dto.PortType == 1)
- {
- searchDataIds = searchClientDatas
- .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact)) //联系人条件
- .WhereIF(!string.IsNullOrEmpty(location), x => !string.IsNullOrEmpty(x.Location) && x.Location.Contains(location)) //地区条件
- .WhereIF(!string.IsNullOrEmpty(clientDto), x => !string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)) //单位条件
- .Select(x => x.Id)
- .ToList();
- }
- else if (dto.PortType == 2 || dto.PortType == 3)
- {
- searchDataIds = searchClientDatas
- .Where(x => (!string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(clientDto)) ||
- (!string.IsNullOrEmpty(x.Location) && x.Location.Contains(clientDto)) ||
- (!string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)))
- .Select(x => x.Id)
- .ToList();
- }
- if (searchDataIds.Count < 1) searchDataIds.Add(0);
- }
- RefAsync<int> total = 0;
- var clientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
- .Where(x => x.IsDel == 0)
- .WhereIF(state == -1 && intList.Count > 0, x => intList.Contains(x.Id))
- .WhereIF(dto.Lvlid != 0, x => x.Lvlid == dto.Lvlid) //地市州条件
- .WhereIF(rangeSetDataList.Count > 0, x => rangeSetDataList.Contains(x.Lvlid)) //省域条件
- .WhereIF(dto.Category > 0, x => x.Category == dto.Category) //客户类别
- .WhereIF(isSelectSearch && searchDataIds.Count > 0 , x => searchDataIds.Contains(x.Id)) //条件模糊查询
- .OrderByDescending(x => x.CreateTime)
- .ToPageListAsync(pageIndex, pageSize, total);
- NewClientDataView = _mapper.Map<List<NewClientDataView>>(clientDatas);
- count = total;
- var setDatas = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToList();
-
- #region 下拉框初始化数据
- //负责人下拉框
- var _Users = GetNewExistClient(dto.OperationUserId).Select(x => new { x.Id, Name = x.CnName }).ToList();
- //省域数据
- var _Province = setDatas.Where(u => u.STid == 42).Select(x => new { x.Id, x.Name }).ToList();
- //客户级别数据
- var _level = setDatas.Where(u => u.STid == 33).Select(x => new { x.Id, x.Name }).ToList();
- //客户类别
- ArrayList _CustomerClass = new ArrayList();
- List<Sys_SetData> CustomerClass = setDatas.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in CustomerClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name,
- item.Remark
- };
- _CustomerClass.Add(data);
- };
- // 创建比较器实例
- IComparer remakeComparer = new RemakeComparer();
- _CustomerClass.Sort(remakeComparer);
- //业务分类
- var _ServiceClass = setDatas.Where(u => u.STid == 36).Select(x => new { x.Id, x.Name }).ToList();
- #endregion
- var groupNumber = await QueryNumberGroups();
- if (NewClientDataView.Count > 0)
- {
- float totalPage = (float)count / dto.PageSize;//总页数
- if (totalPage == 0) totalPage = 1;
- else totalPage = (int)Math.Ceiling((double)totalPage);
- if (dto.PortType == 1)
- {
- int index = 1;
- foreach (var item in NewClientDataView)
- {
- //EncryptionProcessor.DecryptProperties(item); //解密
- item.Weight = AesEncryptionHelper.Decrypt(item.Weight);
- item.Client = AesEncryptionHelper.Decrypt(item.Client);
- item.Contact = AesEncryptionHelper.Decrypt(item.Contact);
- item.Job = AesEncryptionHelper.Decrypt(item.Job);
- item.Telephone = AesEncryptionHelper.Decrypt(item.Telephone);
- item.Phone = AesEncryptionHelper.Decrypt(item.Phone);
- item.Location = AesEncryptionHelper.Decrypt(item.Location);
- item.Remark = AesEncryptionHelper.Decrypt(item.Remark);
- item.RowNumber = index;
- item.CategoryStr = setDatas.Find(x => x.Id == item.Category)?.Name ?? "-";
- item.LvlidStr = setDatas.Find(x => x.Id == item.Lvlid)?.Name ?? "-";
- List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
- ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + item.Id + " AND u1.ISDEL = 0").ToListAsync();
- item.AscribedUser = AscribedUser;
- List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
- ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + item.Id + " AND d1.ISDEL = 0").ToListAsync();
- item.AscribedDepartment = AscribedDepartment;
- index++;
- }
- var Data = new
- {
- ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
- Users = _Users,
- Province = _Province,
- level = _level,
- CustomerClass = _CustomerClass,
- ServiceClass = _ServiceClass,
- groupNumber = groupNumber.Data,
- };
- return result = new Result()
- {
- Code = 0,
- Msg = $"查询成功!解密耗时: {stopwatch.ElapsedMilliseconds} 毫秒",
- Data = Data
- };
- }
- else if (dto.PortType == 2 || dto.PortType == 3)
- {
- List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
- int index = 1;
- foreach (var item in NewClientDataView)
- {
- newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
- {
- RowNumber = index,
- Id = item.Id,
- Client = AesEncryptionHelper.Decrypt(item.Client),
- Contact = AesEncryptionHelper.Decrypt(item.Contact),
- Job = AesEncryptionHelper.Decrypt(item.Job),
- Telephone = AesEncryptionHelper.Decrypt(item.Telephone),
- Location = AesEncryptionHelper.Decrypt(item.Location),
- });
- index++;
- }
- result = new Result()
- {
- Code = 0,
- Msg = $"查询成功!解密耗时: {stopwatch.ElapsedMilliseconds} 毫秒",
- Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
- };
- }
- }
- else
- {
- if (dto.PortType == 2 || dto.PortType == 3)
- {
- var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
- result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
- }
- else
- {
- var Data = new
- {
- ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
- Users = _Users,
- Province = _Province,
- level = _level,
- CustomerClass = _CustomerClass,
- ServiceClass = _ServiceClass,
- groupNumber = groupNumber.Data,
- };
- result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
- }
- }
- }
- catch (Exception ex)
- {
- result = new Result() { Code = -2, Msg = "未知错误" };
- }
- stopwatch.Stop();
- result.Msg = $"耗时: {stopwatch.ElapsedMilliseconds} 毫秒";
- return result;
- }
- /// <summary>
- /// 客户资料初识初始化
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> QueryNewClientData1(NewClientDataQueryDto dto)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- try
- {
- #region 交集
- List<int> NewClientDataId1 = new List<int>();
- List<int> NewClientDataId2 = new List<int>();
- string NewClientDataId = "";
- int state = 0;
- #region 负责人
- if (dto.OperationUserId != 21)
- {
- if (string.IsNullOrWhiteSpace(dto.Userid))
- {
- dto.Userid = dto.OperationUserId.ToString();
- }
- }
- if (!string.IsNullOrWhiteSpace(dto.Userid))
- {
- string sql = string.Format(@"select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0}) and u1.IsDel = 0", dto.Userid);
- List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
- if (ascribedUsers.Count != 0)
- {
- foreach (var ascribedUser in ascribedUsers)
- {
- if (ascribedUser.NewClientDataId != 0)
- {
- NewClientDataId1.Add(ascribedUser.NewClientDataId);
- }
- }
- }
- else
- {
- result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- state = -1;
- }
- #endregion
- #region 业务归属
- if (!string.IsNullOrWhiteSpace(dto.Business))
- {
- string sql = string.Format(@"select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0}) and d1.isdel = 0", dto.Business);
- List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
- if (AscribedDepartment.Count != 0)
- {
- foreach (var item in AscribedDepartment)
- {
- if (item.NewClientDataId != 0)
- {
- NewClientDataId2.Add(item.NewClientDataId);
- }
- }
- }
- else
- {
- result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- state = -1;
- }
- #endregion
- List<int> intList = new List<int>();
- if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
- {
- intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
- }
- else if (NewClientDataId1.Count != 0)
- {
- intList = NewClientDataId1;
- }
- else if (NewClientDataId2.Count != 0)
- {
- intList = NewClientDataId2;
- }
- #endregion
- foreach (var item in intList)
- {
- NewClientDataId += item + ",";
- }
- if (!string.IsNullOrWhiteSpace(NewClientDataId))
- {
- NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
- }
- string sqlWhere = string.Empty;
- if (dto.PortType == 1)
- {
- #region 联系人条件
- if (!string.IsNullOrWhiteSpace(dto.Contact))
- {
- sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
- }
- #endregion
- #region 地区条件
- if (!string.IsNullOrWhiteSpace(dto.Location))
- {
- sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
- }
- #endregion
- #region 单位条件
- if (!string.IsNullOrWhiteSpace(dto.Client))
- {
- sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
- }
- #endregion
- }
- else if (dto.PortType == 2 || dto.PortType == 3)
- {
- sqlWhere += string.Format("And (Contact like '%{0}%' or Location like '%{0}%' or Client like '%{0}%' )", dto.Client);
- }
- if (state == -1)
- {
- if (string.IsNullOrWhiteSpace(NewClientDataId))
- {
- NewClientDataId = "0";
- }
- sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
- }
- #region 地市州条件
- if (dto.Lvlid != 0)
- {
- sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid);
- }
- #endregion
- #region 省域条件
- if (dto.Range != 0)
- {
- string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
- switch (dto.Range)
- {
- case 419:
- setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
- break;
- case 421:
- setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
- break;
- case 420:
- setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
- break;
- case 423:
- setDataSql += " and (Name like '%重庆%')";
- break;
- case 422:
- setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
- break;
- case 578:
- setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
- break;
- case 605:
- setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
- break;
- case 606:
- setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
- break;
- case 625:
- setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
- break;
- case 634:
- setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
- break;
- }
- var RangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
- string lvlds = string.Join(',', RangeSetDataList).TrimEnd(',');
- if (!string.IsNullOrEmpty(lvlds))
- {
- sqlWhere += string.Format(@" And s.Lvlid in ({0}) ", lvlds);
- }
- }
- #endregion
- #region 客户类别
- if (dto.Category != 0)
- {
- sqlWhere += string.Format(@" And s.Category = {0}", dto.Category);
- }
- #endregion
- sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0);
- if (!string.IsNullOrEmpty(sqlWhere.Trim()))
- {
- Regex r = new Regex("And");
- sqlWhere = r.Replace(sqlWhere, "Where", 1);
- }
- int pIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
- int pSize = dto.PageIndex * dto.PageSize;
- string sqlNew = string.Format(@"select (SELECT COUNT(1) FROM Crm_NewClientData s left Join Sys_SetData s1 On s.Lvlid=s1.Id {0}) AS countPage,* from (select ROW_NUMBER()
- over(order by s.CreateTime desc) RowNumber,s.*,s1.Name as 'LvlName' from Crm_NewClientData s left Join Sys_SetData s1 On s.Lvlid=s1.Id {0}) as co where
- RowNumber between {1} and {2} ", sqlWhere, pIndex, pSize);
- List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
- foreach (var item in NewClientDataView)
- {
- Sys_SetData CategoryStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Category);
- if (CategoryStr != null)
- {
- item.CategoryStr = CategoryStr != null ? CategoryStr.Name : null;
- }
- Sys_SetData lvlStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Lvlid);
- if (lvlStr != null)
- {
- item.LvlidStr = lvlStr != null ? lvlStr.Name : null;
- }
- }
- #region 下拉框初始化数据
- //负责人下拉框
- List<dynamic> _Users = new List<dynamic>();
- List<Sys_Users> users = GetNewExistClient(dto.OperationUserId);
- foreach (Sys_Users user in users)
- {
- var data = new
- {
- Id = user.Id,
- Name = user.CnName
- };
- _Users.Add(data);
- };
- //省域数据
- List<dynamic> _Province = new List<dynamic>();
- List<Sys_SetData> province = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 42 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in province)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _Province.Add(data);
- };
- //客户级别数据
- List<dynamic> _level = new List<dynamic>();
- List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in level)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _level.Add(data);
- };
- //客户类别
- ArrayList _CustomerClass = new ArrayList();
- List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in CustomerClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name,
- item.Remark
- };
- _CustomerClass.Add(data);
- };
- // 创建比较器实例
- IComparer remakeComparer = new RemakeComparer();
- _CustomerClass.Sort(remakeComparer);
- //业务分类
- List<dynamic> _ServiceClass = new List<dynamic>();
- List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in ServiceClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _ServiceClass.Add(data);
- };
- #endregion
- var groupNumber = await QueryNumberGroups();
- if (NewClientDataView.Count > 0)
- {
- int count = NewClientDataView[0].countPage;
- float totalPage = (float)count / dto.PageSize;//总页数
- if (totalPage == 0) totalPage = 1;
- else totalPage = (int)Math.Ceiling((double)totalPage);
- if (dto.PortType == 1)
- {
- foreach (var item in NewClientDataView)
- {
- List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
- ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + item.Id + " AND u1.ISDEL = 0").ToListAsync();
- item.AscribedUser = AscribedUser;
- List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
- ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + item.Id + " AND d1.ISDEL = 0").ToListAsync();
- item.AscribedDepartment = AscribedDepartment;
- }
- var Data = new
- {
- ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
- Users = _Users,
- Province = _Province,
- level = _level,
- CustomerClass = _CustomerClass,
- ServiceClass = _ServiceClass,
- groupNumber = groupNumber.Data,
- };
- return result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = Data
- };
- }
- else if (dto.PortType == 2 || dto.PortType == 3)
- {
- List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
- foreach (var item in NewClientDataView)
- {
- newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
- {
- RowNumber = item.RowNumber,
- Id = item.Id,
- Client = item.Client,
- Contact = item.Contact,
- Job = item.Job,
- Telephone = item.Telephone,
- Location = item.Location,
- });
- }
- result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
- };
- }
- }
- else
- {
- if (dto.PortType == 2 || dto.PortType == 3)
- {
- var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
- result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
- }
- else
- {
- var Data = new
- {
- ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
- Users = _Users,
- Province = _Province,
- level = _level,
- CustomerClass = _CustomerClass,
- ServiceClass = _ServiceClass,
- groupNumber = groupNumber.Data,
- };
- result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
- }
-
- }
- }
- catch (Exception ex)
- {
- result = new Result() { Code = -2, Msg = "未知错误" };
- }
- return result;
- }
- public Result QueryUserSelect()
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- try
- {
- //负责人下拉框
- List<dynamic> _Users = new List<dynamic>();
- List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
- .Where(u => u.IsDel == 0).ToList();
- foreach (Sys_Users user in users)
- {
- var data = new
- {
- Id = user.Id,
- Name = user.CnName
- };
- _Users.Add(data);
- };
- if (_Users.Count == 0)
- {
- result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- result = new Result() { Code = 0, Msg = "查询成功!", Data = _Users };
- }
- catch (Exception)
- {
- result = new Result() { Code = -2, Msg = "未知错误" };
- }
- return result;
- }
- public async Task<Result> NewClientOp(NewClientOpDto dto)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- bool istrue = false;
- int AddReturnId = -1;
-
- string BirthdayStr = string.Empty;
- if (!string.IsNullOrWhiteSpace(dto.Birthday))
- {
- DateTime Birthday = new DateTime();
- var isParse = DateTime.TryParse(dto.Birthday, out Birthday);
- BirthdayStr = isParse ? Birthday.ToString("yyyy-MM-dd") : "";
- }
- if (string.IsNullOrWhiteSpace(dto.PassportDate))
- {
- dto.PassportDate = null;
- }
- var newClientDataUnEncrypted = _mapper.Map<Crm_NewClientData>(dto);
- newClientDataUnEncrypted.Birthday = BirthdayStr;
-
- var newClientDataEncrypted = newClientDataUnEncrypted;
- EncryptionProcessor.EncryptProperties(newClientDataEncrypted); //加密
- try
- {
- BeginTran();
- if (dto.Status == 1)//添加
- {
- string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' And Contact='{1}' And IsDel={2}"
- , newClientDataEncrypted.Client, newClientDataEncrypted.Contact, 0);
- var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
- if (NewClientData != null)
- {
- result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
- }
- int id = await AddAsyncReturnId(newClientDataEncrypted); //添加市场客户资料表数据
- if (id == 0)
- {
- result = new Result() { Code = -1, Msg = "添加失败!" };
- }
- else
- {
- result = new Result() { Code = 0, Msg = "添加成功!", Data = id };
- istrue = true;
- AddReturnId = id;
- newClientDataUnEncrypted.Id = id;
- }
- }
- else if (dto.Status == 2)//修改
- {
- DateTime? PassportDate = null;
- try
- {
- PassportDate = DateTime.Parse(dto.PassportDate);
- }
- catch (Exception)
- {
- PassportDate = null;
- }
- var res = await _sqlSugar.Updateable(newClientDataEncrypted).IgnoreColumns(x => new { x.DeleteTime, x.DeleteUserId, x.CreateTime, x.CreateUserId }).ExecuteCommandAsync();
- if (res > 0)
- {
- istrue = true;
- AddReturnId = dto.Id == 0 ? -1 : dto.Id;
- if (AddReturnId != -1)
- {
- await _sqlSugar.Updateable<Crm_ClientDataAndUser>().Where(x=>x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndUser()
- {
- IsDel = 1,
- DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
- }).ExecuteCommandAsync();
- await _sqlSugar.Updateable<Crm_ClientDataAndBusiness>().Where(x => x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndBusiness()
- {
- IsDel = 1,
- DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
- }).ExecuteCommandAsync();
- }
- newClientDataUnEncrypted.Id = AddReturnId;
- result = new Result() { Code = 0, Msg = "修改成功!" };
- }
- else
- {
- result = new Result() { Code = -1, Msg = "修改失败!" };
- }
- }
- else
- {
- result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
- }
- if (istrue)
- {
- Adds<Crm_ClientDataAndUser>(dto.AscribedUser.Select(x => new Crm_ClientDataAndUser
- {
- CreateTime = DateTime.Now,
- CreateUserId = dto.CreateUserId,
- IsDel = 0,
- NewClientDataId = AddReturnId,
- usersId = x
- }).ToList());
- Adds<Crm_ClientDataAndBusiness>(dto.AscribedDepartment.Select(x => new Crm_ClientDataAndBusiness
- {
- CreateUserId = dto.CreateUserId,
- IsDel = 0,
- CreateTime = DateTime.Now,
- NewClientDataId = AddReturnId,
- SetDataId = x,
- }).ToList());
- CommitTran();
- ////缓存添加信息
- //var optype = 0;
- //if (dto.Status == 1) optype = 3;
- //else if (dto.Status == 2) optype = 4;
- ////操作缓存(未加密数据)
- //await NewClientDataRedis(optype, newClientDataUnEncrypted);
- result.Data = AddReturnId;
- }
- else
- {
- RollbackTran();
- }
- }
- catch (Exception)
- {
- RollbackTran();
- result = new Result() { Code = -2, Msg = "未知错误" };
- }
- return result;
- }
- public async Task<Result> QueryNumberGroups()
- {
- Result result = new Result();
- //preDeleAll 预计总量
- //finlishedDeleAll 已出总量
- DataTable preDeleAndfinlishedDeleAll = await GetDataTableAsync("select SUM(PreDele) as PreDeleAll ,SUM(FinlishedDele) as FinlishedDeleAll from Crm_NewClientData");
- var preDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["PreDeleAll"].ToString();
- var finlishedDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["finlishedDeleAll"].ToString();
- result.Code = 0;
- result.Msg = "成功!";
- result.Data = new
- {
- preDeleAll,
- finlishedDeleAll
- };
- return result;
- }
- /// <summary>
- /// 获取下拉列表数据和单条数据信息
- /// </summary>
- /// <param name="dto"></param>
- public async Task<Result> QuerySelectAndSingleData(QuerySingleDto dto)
- {
- Result rest = new Result();
- //var QueryData1 = await NewClientDataRedis(2, new Crm_NewClientData() { Id = dto.Id });
- //var QueryData = QueryData1.FirstOrDefault();
- var QueryData = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0 && x.Id == dto.Id).FirstAsync();
- NewClientDataView MapQueryData = null;
- if (QueryData != null)
- {
- EncryptionProcessor.DecryptProperties(QueryData);
- MapQueryData = _mapper.Map<NewClientDataView>(QueryData);
- MapQueryData.AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
- ("select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + dto.Id + " and u1.isdel = 0").ToListAsync();
- MapQueryData.AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
- ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + dto.Id + " and d1.isdel = 0").ToListAsync();
- }
- #region 下拉框初始化数据
- //负责人下拉框
- List<dynamic> _Users = new List<dynamic>();
- List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
- .Where(u => u.IsDel == 0).ToList();
- foreach (Sys_Users user in users)
- {
- var data = new
- {
- Id = user.Id,
- Name = user.CnName
- };
- _Users.Add(data);
- };
-
- //客户级别数据
- List<dynamic> _level = new List<dynamic>();
- List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in level)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _level.Add(data);
- };
- //客户类别
- ArrayList _CustomerClass = new ArrayList();
- List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in CustomerClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name,
- item.Remark
- };
- _CustomerClass.Add(data);
- };
- // 创建比较器实例
- IComparer remakeComparer = new RemakeComparer();
- _CustomerClass.Sort(remakeComparer);
- //业务分类
- List<dynamic> _ServiceClass = new List<dynamic>();
- List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
- .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
- foreach (Sys_SetData item in ServiceClass)
- {
- var data = new
- {
- Id = item.Id,
- Name = item.Name
- };
- _ServiceClass.Add(data);
- };
- #endregion
- rest.Code = 0;
- rest.Data = new
- {
- data = MapQueryData,
- Users = _Users,
- level = _level,
- CustomerClass = _CustomerClass,
- ServiceClass = _ServiceClass,
- };
- rest.Msg = "获取成功!";
- return rest;
- }
- /// <summary>
- /// 删除市场客户资料数据
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> DelNewClientData(DelBaseDto dto)
- {
- Result AcrionResult = new Result();
- BeginTran();
- var DBresult = await SoftDeleteByIdAsync<Crm_NewClientData>(dto.Id.ToString(), dto.DeleteUserId);
- try
- {
- if (DBresult)
- {
- AcrionResult.Code = 0;
- string sqlSet = $"isdel = 1, DeleteUserId = {dto.DeleteUserId} ,DeleteTime = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
- string sql = $" update Crm_ClientDataAndUser set {sqlSet} where NewClientDataId = {dto.Id} ";
- await ExecuteCommandAsync(sql);
- sql = $" update Crm_ClientDataAndBusiness set {sqlSet} where NewClientDataId = {dto.Id} ";
- await ExecuteCommandAsync(sql);
- CommitTran();
- //await NewClientDataRedis(5, new Crm_NewClientData() { Id = dto.Id });
- AcrionResult.Code = 0;
- }
- }
- catch (Exception ex)
- {
- RollbackTran();
- AcrionResult.Msg = ex.Message;
- AcrionResult.Code = -1;
- }
- return AcrionResult;
- }
- /// <summary>
- /// 市场客户资料数据
- /// 批量指派
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> _BatchAssignment(BatchAssignmentDto dto)
- {
- Result AcrionResult = new Result() { Code = -1,Msg="操作失败"};
- if (dto.UserIdItem == null || dto.UserIdItem.Count < 1)
- {
- AcrionResult.Msg = "用户ID集合不能为空!";
- return AcrionResult;
- }
- if (dto.ClientDataIdItem == null || dto.ClientDataIdItem.Count < 1)
- {
- AcrionResult.Msg = "客户资料ID集合不能为空!";
- return AcrionResult;
- }
- List<Crm_ClientDataAndUser> _ClientDataAndUsers = new List<Crm_ClientDataAndUser>();
- List<Crm_ClientDataAndUser> _ClientDataAndUsers1 = await _sqlSugar.Queryable<Crm_ClientDataAndUser>()
- .Where(it =>
- it.IsDel == 0 &&
- dto.ClientDataIdItem.Contains(it.NewClientDataId) &&
- dto.UserIdItem.Contains(it.usersId)
- )
- .ToListAsync();
- var existsCount = 0;
- foreach (var clientDataId in dto.ClientDataIdItem)
- {
- foreach (var userId1 in dto.UserIdItem)
- {
- Crm_ClientDataAndUser _ClientDataAndUsers2 = _ClientDataAndUsers1.Where(it => it.NewClientDataId == clientDataId &&
- it.usersId == userId1
- ).FirstOrDefault();
- if (_ClientDataAndUsers2 == null)
- {
- _ClientDataAndUsers.Add(new Crm_ClientDataAndUser()
- {
- CreateUserId = dto.UserId,
- NewClientDataId = clientDataId,
- usersId = userId1
- });
- }
- else
- {
- existsCount++;
- }
- }
- }
- if (_ClientDataAndUsers.Count > 0)
- {
- var adds = await _sqlSugar.Insertable(_ClientDataAndUsers).ExecuteCommandAsync();
- if (adds > 0)
- {
- #region 客户资料表操作记录 批量添加
- List<Crm_TableOperationRecord> _TableOperationRecords = new List<Crm_TableOperationRecord>();
- foreach (var item in _ClientDataAndUsers)
- {
- _TableOperationRecords.Add(
- new Crm_TableOperationRecord() {
- TableName = "Crm_TableOperationRecord",
- PortType = dto.PortType,
- OperationItem = OperationEnum.BatchAssignment,
- DataId = item.NewClientDataId,
- CreateUserId = dto.UserId,
- CreateTime = DateTime.Now,
- Remark = "",
- IsDel = 0
- });
- }
- if (_TableOperationRecords.Count > 0)
- {
- await _sqlSugar.Insertable(_TableOperationRecords).ExecuteCommandAsync();
- }
- #endregion
- AcrionResult.Code = 0;
- return AcrionResult;
- }
- }
- else
- {
- if(existsCount > 0)
- AcrionResult.Msg += $" {existsCount}条数据已被指派,无法重复指派!";
- }
- return AcrionResult;
- }
- public List<Sys_Users> GetNewExistClient(int userid)
- {
- string sql = "SELECT * FROM Sys_Users su where su.id in (\r\n\t select distinct usersid from Crm_ClientDataAndUser WHERE IsDel = 0 \r\n) \r\n";
- var userArr = _sqlSugar.SqlQueryable<Sys_Users>(sql).ToList();
- var dic = new Dictionary<int, int[]>()
- {
- { 95, new int []{ 95 , 337 , 302, 350, 355, 357, 353 } }
- };
- if (dic.Keys.Contains(userid))
- {
- return userArr.Where(x=> dic[userid].Contains(x.Id)).ToList();
- }
- return userArr;
- }
- /// <summary>
- /// 客户资料
- /// excel download
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<List<NewClientDataExcelDownloadView>> NewClientDataExcelDownload(NewClientDataExcelDownloadDto dto)
- {
- var dt = new List<NewClientDataExcelDownloadView>();
- try
- {
- #region 交集
- List<int> NewClientDataId1 = new List<int>();
- List<int> NewClientDataId2 = new List<int>();
- int state = 0;
- #region 负责人
- if (dto.OperationUserId != 21)
- {
- if (string.IsNullOrWhiteSpace(dto.Userid))
- {
- dto.Userid = dto.OperationUserId.ToString();
- }
- }
- if (!string.IsNullOrWhiteSpace(dto.Userid))
- {
- string sql = string.Format(@"select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0}) and u1.IsDel = 0", dto.Userid);
- List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
- if (ascribedUsers.Count != 0)
- {
- foreach (var ascribedUser1 in ascribedUsers)
- {
- if (ascribedUser1.NewClientDataId != 0)
- {
- NewClientDataId1.Add(ascribedUser1.NewClientDataId);
- }
- }
- }
- else
- {
- dt = null;
- }
- state = -1;
- }
- #endregion
- #region 业务归属
- if (!string.IsNullOrWhiteSpace(dto.Business))
- {
- string sql = string.Format(@"select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0}) and d1.isdel = 0", dto.Business);
- List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
- if (AscribedDepartment.Count != 0)
- {
- foreach (var item in AscribedDepartment)
- {
- if (item.NewClientDataId != 0)
- {
- NewClientDataId2.Add(item.NewClientDataId);
- }
- }
- }
- else
- {
- dt = null;
- }
- state = -1;
- }
- #endregion
- List<int> intList = new List<int>();
- if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
- {
- intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
- }
- else if (NewClientDataId1.Count != 0)
- {
- intList = NewClientDataId1;
- }
- else if (NewClientDataId2.Count != 0)
- {
- if (dto.OperationUserId == 21)
- {
- intList = NewClientDataId2;
- }
- }
- if (state == -1)
- {
- if (intList.Count < 1) intList.Add(-1);
- }
- #endregion
- #region 省域条件
- var rangeSetDataList = new List<int>();
- if (dto.Range != 0)
- {
- string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
- switch (dto.Range)
- {
- case 419:
- setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
- break;
- case 421:
- setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
- break;
- case 420:
- setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
- break;
- case 423:
- setDataSql += " and (Name like '%重庆%')";
- break;
- case 422:
- setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
- break;
- case 578:
- setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
- break;
- case 605:
- setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
- break;
- case 606:
- setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
- break;
- case 625:
- setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
- break;
- case 634:
- setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
- break;
- }
- rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
- }
- #endregion
- var NewClientDataView = new List<NewClientDataExcelDownloadView>();
- var count = 0;
- string contact = dto.Contact, location = dto.Location, clientDto = dto.Client;
- var isSelectSearch = false;
- var searchDataIds = new List<int>();
- if (string.IsNullOrEmpty(contact) || string.IsNullOrEmpty(location) || string.IsNullOrEmpty(clientDto))
- {
- isSelectSearch = true;
- var searchClientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
- .Where(x => x.IsDel == 0)
- .Select(x => new Crm_NewClientData() { Id = x.Id, Client = x.Client, Location = x.Location, Contact = x.Contact })
- .ToListAsync();
- foreach (var item in searchClientDatas) EncryptionProcessor.DecryptProperties(item);
- if (dto.PortType == 1)
- {
- searchDataIds = searchClientDatas
- .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact)) //联系人条件
- .WhereIF(!string.IsNullOrEmpty(location), x => !string.IsNullOrEmpty(x.Location) && x.Location.Contains(location)) //地区条件
- .WhereIF(!string.IsNullOrEmpty(clientDto), x => !string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)) //单位条件
- .Select(x => x.Id)
- .ToList();
- }
- else if (dto.PortType == 2 || dto.PortType == 3)
- {
- searchDataIds = searchClientDatas
- .Where(x => (!string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(clientDto)) ||
- (!string.IsNullOrEmpty(x.Location) && x.Location.Contains(clientDto)) ||
- (!string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)))
- .Select(x => x.Id)
- .ToList();
- }
- if (searchDataIds.Count < 1) searchDataIds.Add(0);
- }
- var clientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
- .Where(x => x.IsDel == 0)
- .WhereIF(state == -1 && intList.Count > 0, x => intList.Contains(x.Id))
- .WhereIF(dto.Lvlid != 0, x => x.Lvlid == dto.Lvlid) //地市州条件
- .WhereIF(rangeSetDataList.Count > 0, x => rangeSetDataList.Contains(x.Lvlid)) //省域条件
- .WhereIF(dto.Category > 0, x => x.Category == dto.Category) //客户类别
- .WhereIF(isSelectSearch && searchDataIds.Count > 0, x => searchDataIds.Contains(x.Id)) //条件模糊查询
- .OrderByDescending(x => x.CreateTime)
- .ToListAsync();
- NewClientDataView = _mapper.Map<List<NewClientDataExcelDownloadView>>(clientDatas);
- if (!NewClientDataView.Any()) return dt;
- var userDatas = await _sqlSugar.Queryable<Sys_Users>().ToListAsync();
- var setDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToListAsync();
- var ascribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
- ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id AND u1.ISDEL = 0").ToListAsync();
- var ascribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
- ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id AND d1.ISDEL = 0").ToListAsync();
- int index = 1;
- foreach (var item in NewClientDataView)
- {
- EncryptionProcessor.DecryptProperties(item); //解密
- item.RowNumber = index;
- item.CreateUserName = userDatas.Find(x => x.Id == item.CreateUserId)?.CnName ?? "-";
- item.CategoryStr = setDatas.Find(x => x.Id == item.Category)?.Name ?? "-";
- item.LvlidStr = setDatas.Find(x => x.Id == item.Lvlid)?.Name ?? "-";
- var currAscribedUser = ascribedUser.Where(x => x.NewClientDataId == item.Id).ToList();
- if (currAscribedUser.Any()) item.AscribedUserLable = string.Join("、", currAscribedUser.Select(x => x.CnName).ToList());
- var currAscribedDepartment = ascribedDepartment.Where(x => x.NewClientDataId == item.Id).ToList();
- if (currAscribedDepartment.Any()) item.AscribedDepartmentLable = string.Join("、", currAscribedDepartment.Select(x => x.Name).ToList());
- index++;
- }
- return NewClientDataView;
- }
- catch (Exception ex)
- {
- return dt;
- }
- }
- }
- }
|