NewClientDataRepository.cs 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. using AutoMapper;
  2. using AutoMapper.Execution;
  3. using EyeSoft.Extensions;
  4. using MySqlX.XDevAPI.Relational;
  5. using Newtonsoft.Json;
  6. using NPOI.OpenXmlFormats.Dml.Diagram;
  7. using NPOI.SS.Formula.Functions;
  8. using NPOI.SS.UserModel;
  9. using OASystem.Domain;
  10. using OASystem.Domain.AesEncryption;
  11. using OASystem.Domain.Dtos;
  12. using OASystem.Domain.Dtos.CRM;
  13. using OASystem.Domain.Entities.Customer;
  14. using OASystem.Domain.Entities.Resource;
  15. using OASystem.Domain.Enums;
  16. using OASystem.Domain.ViewModels.CRM;
  17. using OASystem.Domain.ViewModels.JuHeExchangeRate;
  18. using OASystem.Domain.ViewModels.QiYeWeChat;
  19. using OASystem.Infrastructure.Tools;
  20. using SqlSugar;
  21. using System;
  22. using System.Collections;
  23. using System.Collections.Generic;
  24. using System.ComponentModel.Design;
  25. using System.Diagnostics;
  26. using System.Diagnostics.Contracts;
  27. using System.Linq;
  28. using System.Reflection.Metadata;
  29. using System.Runtime.Intrinsics.Arm;
  30. using System.Text;
  31. using System.Threading.Tasks;
  32. using System.Xml.Linq;
  33. using XAct;
  34. using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
  35. using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
  36. namespace OASystem.Infrastructure.Repositories.CRM
  37. {
  38. public class NewClientDataRepository : BaseRepository<Crm_NewClientData, NewClientDataView>
  39. {
  40. private readonly IMapper _mapper;
  41. public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  42. base(sqlSugar)
  43. {
  44. _mapper = mapper;
  45. }
  46. /// <summary>
  47. /// 客户资料
  48. /// 基础数据源
  49. /// </summary>
  50. /// <param name="dto"></param>
  51. /// <returns></returns>
  52. public async Task<Result> _Init(MarketCustomerInitDto Dto)
  53. {
  54. Result result = new Result() { Code = -2, Msg = "未知错误" };
  55. var portType = Dto.PortType;
  56. if (portType == 1 || portType == 2 || portType == 3)
  57. {
  58. #region 下拉框初始化数据
  59. //负责人下拉框
  60. //List<dynamic> _Users = new List<dynamic>();
  61. //var _Users = _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0).Select(x => new
  62. //{
  63. // x.Id,
  64. // Name = x.CnName
  65. //}).ToList();
  66. var _Users = GetNewExistClient(Dto.UserId)
  67. .Select(x => new
  68. {
  69. x.Id,
  70. Name = x.CnName
  71. }).ToList();
  72. //List<Sys_Users> users = GetNewExistClient();
  73. //foreach (Sys_Users user in users)
  74. //{
  75. // var data = new
  76. // {
  77. // Id = user.Id,
  78. // Name = user.CnName
  79. // };
  80. // _Users.Add(data);
  81. //};
  82. List<Sys_SetData> initData = _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToList();
  83. //客户级别数据
  84. List<dynamic> _level = new List<dynamic>();
  85. List<Sys_SetData> level = initData.Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  86. foreach (Sys_SetData item in level)
  87. {
  88. var data = new
  89. {
  90. Id = item.Id,
  91. Name = item.Name
  92. };
  93. _level.Add(data);
  94. };
  95. //客户类别
  96. ArrayList _CustomerClass = new ArrayList();
  97. List<Sys_SetData> CustomerClass = initData.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  98. foreach (Sys_SetData item in CustomerClass)
  99. {
  100. var data = new
  101. {
  102. Id = item.Id,
  103. Name = item.Name,
  104. item.Remark
  105. };
  106. _CustomerClass.Add(data);
  107. };
  108. // 创建比较器实例
  109. IComparer remakeComparer = new RemakeComparer();
  110. _CustomerClass.Sort(remakeComparer);
  111. //业务分类
  112. List<dynamic> _ServiceClass = new List<dynamic>();
  113. List<Sys_SetData> ServiceClass = initData.Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  114. foreach (Sys_SetData item in ServiceClass)
  115. {
  116. var data = new
  117. {
  118. Id = item.Id,
  119. Name = item.Name
  120. };
  121. _ServiceClass.Add(data);
  122. };
  123. //身份分类
  124. List<dynamic> _ProvinceClass = new List<dynamic>();
  125. List<Sys_SetData> ProvinceClass = initData.Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  126. foreach (Sys_SetData item in ProvinceClass)
  127. {
  128. var data = new
  129. {
  130. Id = item.Id,
  131. Name = item.Name
  132. };
  133. _ProvinceClass.Add(data);
  134. };
  135. #endregion
  136. var data1 = new {
  137. Users = _Users,
  138. Level = _level,
  139. CustomerClass = _CustomerClass,
  140. ServiceClass= _ServiceClass,
  141. ProvinceClass = _ProvinceClass
  142. };
  143. return result = new Result()
  144. {
  145. Code = 0,
  146. Msg = "查询成功",
  147. Data = data1
  148. };
  149. }
  150. else
  151. {
  152. result.Msg = string.Format("请传入有效的PortType参数!");
  153. }
  154. return result;
  155. }
  156. /// <summary>
  157. /// 市场客户资料数据
  158. /// 详情
  159. /// </summary>
  160. /// <param name="dto"></param>
  161. /// <returns></returns>
  162. public async Task<Result> _Details(int portType,int id)
  163. {
  164. Result result = new Result() { Code = -2, Msg = "未知错误" };
  165. if (portType == 1 || portType == 2 || portType == 3)
  166. {
  167. if (id < 0)
  168. {
  169. result.Msg = string.Format("请传入有效的Id参数!");
  170. return result;
  171. }
  172. string infoSql = string.Format(@" Select * From Crm_NewClientData Where Isdel = 0 And Id = {0}", id);
  173. var info = await _sqlSugar.SqlQueryable<DetailsView>(infoSql).FirstAsync();
  174. if (info != null)
  175. {
  176. EncryptionProcessor.DecryptProperties(info);
  177. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  178. ("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();
  179. info.AscribedUser = AscribedUser.Select(it => it.UserId).ToList();
  180. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  181. ("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();
  182. info.AscribedDepartment = AscribedDepartment.Select(it => it.Id).ToList();
  183. result.Code = 0;
  184. result.Data = info;
  185. }
  186. }
  187. else result.Msg = MsgTips.Port;
  188. return result;
  189. }
  190. /// <summary>
  191. /// 客户资料初识初始化
  192. /// </summary>
  193. /// <param name="dto"></param>
  194. /// <returns></returns>
  195. public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
  196. {
  197. Stopwatch stopwatch = Stopwatch.StartNew();
  198. Result result = new Result() { Code = -2, Msg = "未知错误" };
  199. int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
  200. try
  201. {
  202. #region 交集
  203. List<int> NewClientDataId1 = new List<int>();
  204. List<int> NewClientDataId2 = new List<int>();
  205. int state = 0;
  206. #region 负责人
  207. if (dto.OperationUserId != 21)
  208. {
  209. if (string.IsNullOrWhiteSpace(dto.Userid))
  210. {
  211. dto.Userid = dto.OperationUserId.ToString();
  212. }
  213. }
  214. if (!string.IsNullOrWhiteSpace(dto.Userid))
  215. {
  216. 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);
  217. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  218. if (ascribedUsers.Count != 0)
  219. {
  220. foreach (var ascribedUser in ascribedUsers)
  221. {
  222. if (ascribedUser.NewClientDataId != 0)
  223. {
  224. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  225. }
  226. }
  227. }
  228. else
  229. {
  230. result = new Result() { Code = -1, Msg = "暂无数据" };
  231. }
  232. state = -1;
  233. }
  234. #endregion
  235. #region 业务归属
  236. if (!string.IsNullOrWhiteSpace(dto.Business))
  237. {
  238. 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);
  239. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  240. if (AscribedDepartment.Count != 0)
  241. {
  242. foreach (var item in AscribedDepartment)
  243. {
  244. if (item.NewClientDataId != 0)
  245. {
  246. NewClientDataId2.Add(item.NewClientDataId);
  247. }
  248. }
  249. }
  250. else
  251. {
  252. result = new Result() { Code = -1, Msg = "暂无数据" };
  253. }
  254. state = -1;
  255. }
  256. #endregion
  257. List<int> intList = new List<int>();
  258. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  259. {
  260. intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  261. }
  262. else if (NewClientDataId1.Count != 0)
  263. {
  264. intList = NewClientDataId1;
  265. }
  266. else if (NewClientDataId2.Count != 0)
  267. {
  268. if (dto.OperationUserId == 21)
  269. {
  270. intList = NewClientDataId2;
  271. }
  272. }
  273. if (state == -1)
  274. {
  275. if (intList.Count < 1) intList.Add(-1);
  276. }
  277. #endregion
  278. #region 省域条件
  279. var rangeSetDataList = new List<int>();
  280. if (dto.Range != 0)
  281. {
  282. string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
  283. switch (dto.Range)
  284. {
  285. case 419:
  286. setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
  287. break;
  288. case 421:
  289. setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
  290. break;
  291. case 420:
  292. setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
  293. break;
  294. case 423:
  295. setDataSql += " and (Name like '%重庆%')";
  296. break;
  297. case 422:
  298. setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
  299. break;
  300. case 578:
  301. setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
  302. break;
  303. case 605:
  304. setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
  305. break;
  306. case 606:
  307. setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
  308. break;
  309. case 625:
  310. setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
  311. break;
  312. case 634:
  313. setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
  314. break;
  315. }
  316. rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  317. }
  318. #endregion
  319. var NewClientDataView = new List<NewClientDataView>();
  320. var count = 0;
  321. string contact = dto.Contact, location = dto.Location, clientDto = dto.Client;
  322. var isSelectSearch = false;
  323. var searchDataIds = new List<int>();
  324. if (string.IsNullOrEmpty(contact) || string.IsNullOrEmpty(location) || string.IsNullOrEmpty(clientDto))
  325. {
  326. isSelectSearch = true;
  327. var searchClientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  328. .Where(x => x.IsDel == 0)
  329. .Select(x => new Crm_NewClientData() { Id = x.Id, Client = x.Client, Location = x.Location, Contact = x.Contact })
  330. .ToListAsync();
  331. foreach (var item in searchClientDatas) EncryptionProcessor.DecryptProperties(item);
  332. if (dto.PortType == 1)
  333. {
  334. searchDataIds = searchClientDatas
  335. .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact)) //联系人条件
  336. .WhereIF(!string.IsNullOrEmpty(location), x => !string.IsNullOrEmpty(x.Location) && x.Location.Contains(location)) //地区条件
  337. .WhereIF(!string.IsNullOrEmpty(clientDto), x => !string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)) //单位条件
  338. .Select(x => x.Id)
  339. .ToList();
  340. }
  341. else if (dto.PortType == 2 || dto.PortType == 3)
  342. {
  343. searchDataIds = searchClientDatas
  344. .Where(x => (!string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(clientDto)) ||
  345. (!string.IsNullOrEmpty(x.Location) && x.Location.Contains(clientDto)) ||
  346. (!string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)))
  347. .Select(x => x.Id)
  348. .ToList();
  349. }
  350. if (searchDataIds.Count < 1) searchDataIds.Add(0);
  351. }
  352. RefAsync<int> total = 0;
  353. var clientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  354. .Where(x => x.IsDel == 0)
  355. .WhereIF(state == -1 && intList.Count > 0, x => intList.Contains(x.Id))
  356. .WhereIF(dto.Lvlid != 0, x => x.Lvlid == dto.Lvlid) //地市州条件
  357. .WhereIF(rangeSetDataList.Count > 0, x => rangeSetDataList.Contains(x.Lvlid)) //省域条件
  358. .WhereIF(dto.Category > 0, x => x.Category == dto.Category) //客户类别
  359. .WhereIF(isSelectSearch && searchDataIds.Count > 0 , x => searchDataIds.Contains(x.Id)) //条件模糊查询
  360. .OrderByDescending(x => x.CreateTime)
  361. .ToPageListAsync(pageIndex, pageSize, total);
  362. NewClientDataView = _mapper.Map<List<NewClientDataView>>(clientDatas);
  363. count = total;
  364. var setDatas = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToList();
  365. #region 下拉框初始化数据
  366. //负责人下拉框
  367. var _Users = GetNewExistClient(dto.OperationUserId).Select(x => new { x.Id, Name = x.CnName }).ToList();
  368. //省域数据
  369. var _Province = setDatas.Where(u => u.STid == 42).Select(x => new { x.Id, x.Name }).ToList();
  370. //客户级别数据
  371. var _level = setDatas.Where(u => u.STid == 33).Select(x => new { x.Id, x.Name }).ToList();
  372. //客户类别
  373. ArrayList _CustomerClass = new ArrayList();
  374. List<Sys_SetData> CustomerClass = setDatas.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  375. foreach (Sys_SetData item in CustomerClass)
  376. {
  377. var data = new
  378. {
  379. Id = item.Id,
  380. Name = item.Name,
  381. item.Remark
  382. };
  383. _CustomerClass.Add(data);
  384. };
  385. // 创建比较器实例
  386. IComparer remakeComparer = new RemakeComparer();
  387. _CustomerClass.Sort(remakeComparer);
  388. //业务分类
  389. var _ServiceClass = setDatas.Where(u => u.STid == 36).Select(x => new { x.Id, x.Name }).ToList();
  390. #endregion
  391. var groupNumber = await QueryNumberGroups();
  392. if (NewClientDataView.Count > 0)
  393. {
  394. float totalPage = (float)count / dto.PageSize;//总页数
  395. if (totalPage == 0) totalPage = 1;
  396. else totalPage = (int)Math.Ceiling((double)totalPage);
  397. if (dto.PortType == 1)
  398. {
  399. int index = 1;
  400. foreach (var item in NewClientDataView)
  401. {
  402. //EncryptionProcessor.DecryptProperties(item); //解密
  403. item.Weight = AesEncryptionHelper.Decrypt(item.Weight);
  404. item.Client = AesEncryptionHelper.Decrypt(item.Client);
  405. item.Contact = AesEncryptionHelper.Decrypt(item.Contact);
  406. item.Job = AesEncryptionHelper.Decrypt(item.Job);
  407. item.Telephone = AesEncryptionHelper.Decrypt(item.Telephone);
  408. item.Phone = AesEncryptionHelper.Decrypt(item.Phone);
  409. item.Location = AesEncryptionHelper.Decrypt(item.Location);
  410. item.Remark = AesEncryptionHelper.Decrypt(item.Remark);
  411. item.RowNumber = index;
  412. item.CategoryStr = setDatas.Find(x => x.Id == item.Category)?.Name ?? "-";
  413. item.LvlidStr = setDatas.Find(x => x.Id == item.Lvlid)?.Name ?? "-";
  414. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  415. ("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();
  416. item.AscribedUser = AscribedUser;
  417. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  418. ("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();
  419. item.AscribedDepartment = AscribedDepartment;
  420. index++;
  421. }
  422. var Data = new
  423. {
  424. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  425. Users = _Users,
  426. Province = _Province,
  427. level = _level,
  428. CustomerClass = _CustomerClass,
  429. ServiceClass = _ServiceClass,
  430. groupNumber = groupNumber.Data,
  431. };
  432. return result = new Result()
  433. {
  434. Code = 0,
  435. Msg = $"查询成功!解密耗时: {stopwatch.ElapsedMilliseconds} 毫秒",
  436. Data = Data
  437. };
  438. }
  439. else if (dto.PortType == 2 || dto.PortType == 3)
  440. {
  441. List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
  442. int index = 1;
  443. foreach (var item in NewClientDataView)
  444. {
  445. newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
  446. {
  447. RowNumber = index,
  448. Id = item.Id,
  449. Client = AesEncryptionHelper.Decrypt(item.Client),
  450. Contact = AesEncryptionHelper.Decrypt(item.Contact),
  451. Job = AesEncryptionHelper.Decrypt(item.Job),
  452. Telephone = AesEncryptionHelper.Decrypt(item.Telephone),
  453. Location = AesEncryptionHelper.Decrypt(item.Location),
  454. });
  455. index++;
  456. }
  457. result = new Result()
  458. {
  459. Code = 0,
  460. Msg = $"查询成功!解密耗时: {stopwatch.ElapsedMilliseconds} 毫秒",
  461. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
  462. };
  463. }
  464. }
  465. else
  466. {
  467. if (dto.PortType == 2 || dto.PortType == 3)
  468. {
  469. var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
  470. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  471. }
  472. else
  473. {
  474. var Data = new
  475. {
  476. ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  477. Users = _Users,
  478. Province = _Province,
  479. level = _level,
  480. CustomerClass = _CustomerClass,
  481. ServiceClass = _ServiceClass,
  482. groupNumber = groupNumber.Data,
  483. };
  484. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  485. }
  486. }
  487. }
  488. catch (Exception ex)
  489. {
  490. result = new Result() { Code = -2, Msg = "未知错误" };
  491. }
  492. stopwatch.Stop();
  493. result.Msg = $"耗时: {stopwatch.ElapsedMilliseconds} 毫秒";
  494. return result;
  495. }
  496. /// <summary>
  497. /// 客户资料初识初始化
  498. /// </summary>
  499. /// <param name="dto"></param>
  500. /// <returns></returns>
  501. public async Task<Result> QueryNewClientData1(NewClientDataQueryDto dto)
  502. {
  503. Result result = new Result() { Code = -2, Msg = "未知错误" };
  504. try
  505. {
  506. #region 交集
  507. List<int> NewClientDataId1 = new List<int>();
  508. List<int> NewClientDataId2 = new List<int>();
  509. string NewClientDataId = "";
  510. int state = 0;
  511. #region 负责人
  512. if (dto.OperationUserId != 21)
  513. {
  514. if (string.IsNullOrWhiteSpace(dto.Userid))
  515. {
  516. dto.Userid = dto.OperationUserId.ToString();
  517. }
  518. }
  519. if (!string.IsNullOrWhiteSpace(dto.Userid))
  520. {
  521. 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);
  522. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  523. if (ascribedUsers.Count != 0)
  524. {
  525. foreach (var ascribedUser in ascribedUsers)
  526. {
  527. if (ascribedUser.NewClientDataId != 0)
  528. {
  529. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  530. }
  531. }
  532. }
  533. else
  534. {
  535. result = new Result() { Code = -1, Msg = "暂无数据" };
  536. }
  537. state = -1;
  538. }
  539. #endregion
  540. #region 业务归属
  541. if (!string.IsNullOrWhiteSpace(dto.Business))
  542. {
  543. 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);
  544. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  545. if (AscribedDepartment.Count != 0)
  546. {
  547. foreach (var item in AscribedDepartment)
  548. {
  549. if (item.NewClientDataId != 0)
  550. {
  551. NewClientDataId2.Add(item.NewClientDataId);
  552. }
  553. }
  554. }
  555. else
  556. {
  557. result = new Result() { Code = -1, Msg = "暂无数据" };
  558. }
  559. state = -1;
  560. }
  561. #endregion
  562. List<int> intList = new List<int>();
  563. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  564. {
  565. intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  566. }
  567. else if (NewClientDataId1.Count != 0)
  568. {
  569. intList = NewClientDataId1;
  570. }
  571. else if (NewClientDataId2.Count != 0)
  572. {
  573. intList = NewClientDataId2;
  574. }
  575. #endregion
  576. foreach (var item in intList)
  577. {
  578. NewClientDataId += item + ",";
  579. }
  580. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  581. {
  582. NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
  583. }
  584. string sqlWhere = string.Empty;
  585. if (dto.PortType == 1)
  586. {
  587. #region 联系人条件
  588. if (!string.IsNullOrWhiteSpace(dto.Contact))
  589. {
  590. sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
  591. }
  592. #endregion
  593. #region 地区条件
  594. if (!string.IsNullOrWhiteSpace(dto.Location))
  595. {
  596. sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
  597. }
  598. #endregion
  599. #region 单位条件
  600. if (!string.IsNullOrWhiteSpace(dto.Client))
  601. {
  602. sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
  603. }
  604. #endregion
  605. }
  606. else if (dto.PortType == 2 || dto.PortType == 3)
  607. {
  608. sqlWhere += string.Format("And (Contact like '%{0}%' or Location like '%{0}%' or Client like '%{0}%' )", dto.Client);
  609. }
  610. if (state == -1)
  611. {
  612. if (string.IsNullOrWhiteSpace(NewClientDataId))
  613. {
  614. NewClientDataId = "0";
  615. }
  616. sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
  617. }
  618. #region 地市州条件
  619. if (dto.Lvlid != 0)
  620. {
  621. sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid);
  622. }
  623. #endregion
  624. #region 省域条件
  625. if (dto.Range != 0)
  626. {
  627. string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
  628. switch (dto.Range)
  629. {
  630. case 419:
  631. setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
  632. break;
  633. case 421:
  634. setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
  635. break;
  636. case 420:
  637. setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
  638. break;
  639. case 423:
  640. setDataSql += " and (Name like '%重庆%')";
  641. break;
  642. case 422:
  643. setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
  644. break;
  645. case 578:
  646. setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
  647. break;
  648. case 605:
  649. setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
  650. break;
  651. case 606:
  652. setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
  653. break;
  654. case 625:
  655. setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
  656. break;
  657. case 634:
  658. setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
  659. break;
  660. }
  661. var RangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  662. string lvlds = string.Join(',', RangeSetDataList).TrimEnd(',');
  663. if (!string.IsNullOrEmpty(lvlds))
  664. {
  665. sqlWhere += string.Format(@" And s.Lvlid in ({0}) ", lvlds);
  666. }
  667. }
  668. #endregion
  669. #region 客户类别
  670. if (dto.Category != 0)
  671. {
  672. sqlWhere += string.Format(@" And s.Category = {0}", dto.Category);
  673. }
  674. #endregion
  675. sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0);
  676. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  677. {
  678. Regex r = new Regex("And");
  679. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  680. }
  681. int pIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
  682. int pSize = dto.PageIndex * dto.PageSize;
  683. 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()
  684. 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
  685. RowNumber between {1} and {2} ", sqlWhere, pIndex, pSize);
  686. List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
  687. foreach (var item in NewClientDataView)
  688. {
  689. Sys_SetData CategoryStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Category);
  690. if (CategoryStr != null)
  691. {
  692. item.CategoryStr = CategoryStr != null ? CategoryStr.Name : null;
  693. }
  694. Sys_SetData lvlStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Lvlid);
  695. if (lvlStr != null)
  696. {
  697. item.LvlidStr = lvlStr != null ? lvlStr.Name : null;
  698. }
  699. }
  700. #region 下拉框初始化数据
  701. //负责人下拉框
  702. List<dynamic> _Users = new List<dynamic>();
  703. List<Sys_Users> users = GetNewExistClient(dto.OperationUserId);
  704. foreach (Sys_Users user in users)
  705. {
  706. var data = new
  707. {
  708. Id = user.Id,
  709. Name = user.CnName
  710. };
  711. _Users.Add(data);
  712. };
  713. //省域数据
  714. List<dynamic> _Province = new List<dynamic>();
  715. List<Sys_SetData> province = _sqlSugar.Queryable<Sys_SetData>()
  716. .Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  717. foreach (Sys_SetData item in province)
  718. {
  719. var data = new
  720. {
  721. Id = item.Id,
  722. Name = item.Name
  723. };
  724. _Province.Add(data);
  725. };
  726. //客户级别数据
  727. List<dynamic> _level = new List<dynamic>();
  728. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  729. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  730. foreach (Sys_SetData item in level)
  731. {
  732. var data = new
  733. {
  734. Id = item.Id,
  735. Name = item.Name
  736. };
  737. _level.Add(data);
  738. };
  739. //客户类别
  740. ArrayList _CustomerClass = new ArrayList();
  741. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  742. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  743. foreach (Sys_SetData item in CustomerClass)
  744. {
  745. var data = new
  746. {
  747. Id = item.Id,
  748. Name = item.Name,
  749. item.Remark
  750. };
  751. _CustomerClass.Add(data);
  752. };
  753. // 创建比较器实例
  754. IComparer remakeComparer = new RemakeComparer();
  755. _CustomerClass.Sort(remakeComparer);
  756. //业务分类
  757. List<dynamic> _ServiceClass = new List<dynamic>();
  758. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  759. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  760. foreach (Sys_SetData item in ServiceClass)
  761. {
  762. var data = new
  763. {
  764. Id = item.Id,
  765. Name = item.Name
  766. };
  767. _ServiceClass.Add(data);
  768. };
  769. #endregion
  770. var groupNumber = await QueryNumberGroups();
  771. if (NewClientDataView.Count > 0)
  772. {
  773. int count = NewClientDataView[0].countPage;
  774. float totalPage = (float)count / dto.PageSize;//总页数
  775. if (totalPage == 0) totalPage = 1;
  776. else totalPage = (int)Math.Ceiling((double)totalPage);
  777. if (dto.PortType == 1)
  778. {
  779. foreach (var item in NewClientDataView)
  780. {
  781. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  782. ("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();
  783. item.AscribedUser = AscribedUser;
  784. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  785. ("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();
  786. item.AscribedDepartment = AscribedDepartment;
  787. }
  788. var Data = new
  789. {
  790. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  791. Users = _Users,
  792. Province = _Province,
  793. level = _level,
  794. CustomerClass = _CustomerClass,
  795. ServiceClass = _ServiceClass,
  796. groupNumber = groupNumber.Data,
  797. };
  798. return result = new Result()
  799. {
  800. Code = 0,
  801. Msg = "查询成功",
  802. Data = Data
  803. };
  804. }
  805. else if (dto.PortType == 2 || dto.PortType == 3)
  806. {
  807. List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
  808. foreach (var item in NewClientDataView)
  809. {
  810. newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
  811. {
  812. RowNumber = item.RowNumber,
  813. Id = item.Id,
  814. Client = item.Client,
  815. Contact = item.Contact,
  816. Job = item.Job,
  817. Telephone = item.Telephone,
  818. Location = item.Location,
  819. });
  820. }
  821. result = new Result()
  822. {
  823. Code = 0,
  824. Msg = "查询成功",
  825. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
  826. };
  827. }
  828. }
  829. else
  830. {
  831. if (dto.PortType == 2 || dto.PortType == 3)
  832. {
  833. var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
  834. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  835. }
  836. else
  837. {
  838. var Data = new
  839. {
  840. ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  841. Users = _Users,
  842. Province = _Province,
  843. level = _level,
  844. CustomerClass = _CustomerClass,
  845. ServiceClass = _ServiceClass,
  846. groupNumber = groupNumber.Data,
  847. };
  848. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  849. }
  850. }
  851. }
  852. catch (Exception ex)
  853. {
  854. result = new Result() { Code = -2, Msg = "未知错误" };
  855. }
  856. return result;
  857. }
  858. public Result QueryUserSelect()
  859. {
  860. Result result = new Result() { Code = -2, Msg = "未知错误" };
  861. try
  862. {
  863. //负责人下拉框
  864. List<dynamic> _Users = new List<dynamic>();
  865. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  866. .Where(u => u.IsDel == 0).ToList();
  867. foreach (Sys_Users user in users)
  868. {
  869. var data = new
  870. {
  871. Id = user.Id,
  872. Name = user.CnName
  873. };
  874. _Users.Add(data);
  875. };
  876. if (_Users.Count == 0)
  877. {
  878. result = new Result() { Code = -1, Msg = "暂无数据" };
  879. }
  880. result = new Result() { Code = 0, Msg = "查询成功!", Data = _Users };
  881. }
  882. catch (Exception)
  883. {
  884. result = new Result() { Code = -2, Msg = "未知错误" };
  885. }
  886. return result;
  887. }
  888. public async Task<Result> NewClientOp(NewClientOpDto dto)
  889. {
  890. Result result = new Result() { Code = -2, Msg = "未知错误" };
  891. bool istrue = false;
  892. int AddReturnId = -1;
  893. string BirthdayStr = string.Empty;
  894. if (!string.IsNullOrWhiteSpace(dto.Birthday))
  895. {
  896. DateTime Birthday = new DateTime();
  897. var isParse = DateTime.TryParse(dto.Birthday, out Birthday);
  898. BirthdayStr = isParse ? Birthday.ToString("yyyy-MM-dd") : "";
  899. }
  900. if (string.IsNullOrWhiteSpace(dto.PassportDate))
  901. {
  902. dto.PassportDate = null;
  903. }
  904. var newClientDataUnEncrypted = _mapper.Map<Crm_NewClientData>(dto);
  905. newClientDataUnEncrypted.Birthday = BirthdayStr;
  906. var newClientDataEncrypted = newClientDataUnEncrypted;
  907. EncryptionProcessor.EncryptProperties(newClientDataEncrypted); //加密
  908. try
  909. {
  910. BeginTran();
  911. if (dto.Status == 1)//添加
  912. {
  913. string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' And Contact='{1}' And IsDel={2}"
  914. , newClientDataEncrypted.Client, newClientDataEncrypted.Contact, 0);
  915. var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
  916. if (NewClientData != null)
  917. {
  918. result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  919. }
  920. int id = await AddAsyncReturnId(newClientDataEncrypted); //添加市场客户资料表数据
  921. if (id == 0)
  922. {
  923. result = new Result() { Code = -1, Msg = "添加失败!" };
  924. }
  925. else
  926. {
  927. result = new Result() { Code = 0, Msg = "添加成功!", Data = id };
  928. istrue = true;
  929. AddReturnId = id;
  930. newClientDataUnEncrypted.Id = id;
  931. }
  932. }
  933. else if (dto.Status == 2)//修改
  934. {
  935. DateTime? PassportDate = null;
  936. try
  937. {
  938. PassportDate = DateTime.Parse(dto.PassportDate);
  939. }
  940. catch (Exception)
  941. {
  942. PassportDate = null;
  943. }
  944. var res = await _sqlSugar.Updateable(newClientDataEncrypted).IgnoreColumns(x => new { x.DeleteTime, x.DeleteUserId, x.CreateTime, x.CreateUserId }).ExecuteCommandAsync();
  945. if (res > 0)
  946. {
  947. istrue = true;
  948. AddReturnId = dto.Id == 0 ? -1 : dto.Id;
  949. if (AddReturnId != -1)
  950. {
  951. await _sqlSugar.Updateable<Crm_ClientDataAndUser>().Where(x=>x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndUser()
  952. {
  953. IsDel = 1,
  954. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  955. }).ExecuteCommandAsync();
  956. await _sqlSugar.Updateable<Crm_ClientDataAndBusiness>().Where(x => x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndBusiness()
  957. {
  958. IsDel = 1,
  959. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  960. }).ExecuteCommandAsync();
  961. }
  962. newClientDataUnEncrypted.Id = AddReturnId;
  963. result = new Result() { Code = 0, Msg = "修改成功!" };
  964. }
  965. else
  966. {
  967. result = new Result() { Code = -1, Msg = "修改失败!" };
  968. }
  969. }
  970. else
  971. {
  972. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  973. }
  974. if (istrue)
  975. {
  976. Adds<Crm_ClientDataAndUser>(dto.AscribedUser.Select(x => new Crm_ClientDataAndUser
  977. {
  978. CreateTime = DateTime.Now,
  979. CreateUserId = dto.CreateUserId,
  980. IsDel = 0,
  981. NewClientDataId = AddReturnId,
  982. usersId = x
  983. }).ToList());
  984. Adds<Crm_ClientDataAndBusiness>(dto.AscribedDepartment.Select(x => new Crm_ClientDataAndBusiness
  985. {
  986. CreateUserId = dto.CreateUserId,
  987. IsDel = 0,
  988. CreateTime = DateTime.Now,
  989. NewClientDataId = AddReturnId,
  990. SetDataId = x,
  991. }).ToList());
  992. CommitTran();
  993. ////缓存添加信息
  994. //var optype = 0;
  995. //if (dto.Status == 1) optype = 3;
  996. //else if (dto.Status == 2) optype = 4;
  997. ////操作缓存(未加密数据)
  998. //await NewClientDataRedis(optype, newClientDataUnEncrypted);
  999. result.Data = AddReturnId;
  1000. }
  1001. else
  1002. {
  1003. RollbackTran();
  1004. }
  1005. }
  1006. catch (Exception)
  1007. {
  1008. RollbackTran();
  1009. result = new Result() { Code = -2, Msg = "未知错误" };
  1010. }
  1011. return result;
  1012. }
  1013. public async Task<Result> QueryNumberGroups()
  1014. {
  1015. Result result = new Result();
  1016. //preDeleAll 预计总量
  1017. //finlishedDeleAll 已出总量
  1018. DataTable preDeleAndfinlishedDeleAll = await GetDataTableAsync("select SUM(PreDele) as PreDeleAll ,SUM(FinlishedDele) as FinlishedDeleAll from Crm_NewClientData");
  1019. var preDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["PreDeleAll"].ToString();
  1020. var finlishedDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["finlishedDeleAll"].ToString();
  1021. result.Code = 0;
  1022. result.Msg = "成功!";
  1023. result.Data = new
  1024. {
  1025. preDeleAll,
  1026. finlishedDeleAll
  1027. };
  1028. return result;
  1029. }
  1030. /// <summary>
  1031. /// 获取下拉列表数据和单条数据信息
  1032. /// </summary>
  1033. /// <param name="dto"></param>
  1034. public async Task<Result> QuerySelectAndSingleData(QuerySingleDto dto)
  1035. {
  1036. Result rest = new Result();
  1037. //var QueryData1 = await NewClientDataRedis(2, new Crm_NewClientData() { Id = dto.Id });
  1038. //var QueryData = QueryData1.FirstOrDefault();
  1039. var QueryData = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0 && x.Id == dto.Id).FirstAsync();
  1040. NewClientDataView MapQueryData = null;
  1041. if (QueryData != null)
  1042. {
  1043. EncryptionProcessor.DecryptProperties(QueryData);
  1044. MapQueryData = _mapper.Map<NewClientDataView>(QueryData);
  1045. MapQueryData.AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  1046. ("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();
  1047. MapQueryData.AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  1048. ("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();
  1049. }
  1050. #region 下拉框初始化数据
  1051. //负责人下拉框
  1052. List<dynamic> _Users = new List<dynamic>();
  1053. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  1054. .Where(u => u.IsDel == 0).ToList();
  1055. foreach (Sys_Users user in users)
  1056. {
  1057. var data = new
  1058. {
  1059. Id = user.Id,
  1060. Name = user.CnName
  1061. };
  1062. _Users.Add(data);
  1063. };
  1064. //客户级别数据
  1065. List<dynamic> _level = new List<dynamic>();
  1066. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  1067. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  1068. foreach (Sys_SetData item in level)
  1069. {
  1070. var data = new
  1071. {
  1072. Id = item.Id,
  1073. Name = item.Name
  1074. };
  1075. _level.Add(data);
  1076. };
  1077. //客户类别
  1078. ArrayList _CustomerClass = new ArrayList();
  1079. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  1080. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  1081. foreach (Sys_SetData item in CustomerClass)
  1082. {
  1083. var data = new
  1084. {
  1085. Id = item.Id,
  1086. Name = item.Name,
  1087. item.Remark
  1088. };
  1089. _CustomerClass.Add(data);
  1090. };
  1091. // 创建比较器实例
  1092. IComparer remakeComparer = new RemakeComparer();
  1093. _CustomerClass.Sort(remakeComparer);
  1094. //业务分类
  1095. List<dynamic> _ServiceClass = new List<dynamic>();
  1096. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  1097. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  1098. foreach (Sys_SetData item in ServiceClass)
  1099. {
  1100. var data = new
  1101. {
  1102. Id = item.Id,
  1103. Name = item.Name
  1104. };
  1105. _ServiceClass.Add(data);
  1106. };
  1107. #endregion
  1108. rest.Code = 0;
  1109. rest.Data = new
  1110. {
  1111. data = MapQueryData,
  1112. Users = _Users,
  1113. level = _level,
  1114. CustomerClass = _CustomerClass,
  1115. ServiceClass = _ServiceClass,
  1116. };
  1117. rest.Msg = "获取成功!";
  1118. return rest;
  1119. }
  1120. /// <summary>
  1121. /// 删除市场客户资料数据
  1122. /// </summary>
  1123. /// <param name="dto"></param>
  1124. /// <returns></returns>
  1125. public async Task<Result> DelNewClientData(DelBaseDto dto)
  1126. {
  1127. Result AcrionResult = new Result();
  1128. BeginTran();
  1129. var DBresult = await SoftDeleteByIdAsync<Crm_NewClientData>(dto.Id.ToString(), dto.DeleteUserId);
  1130. try
  1131. {
  1132. if (DBresult)
  1133. {
  1134. AcrionResult.Code = 0;
  1135. string sqlSet = $"isdel = 1, DeleteUserId = {dto.DeleteUserId} ,DeleteTime = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
  1136. string sql = $" update Crm_ClientDataAndUser set {sqlSet} where NewClientDataId = {dto.Id} ";
  1137. await ExecuteCommandAsync(sql);
  1138. sql = $" update Crm_ClientDataAndBusiness set {sqlSet} where NewClientDataId = {dto.Id} ";
  1139. await ExecuteCommandAsync(sql);
  1140. CommitTran();
  1141. //await NewClientDataRedis(5, new Crm_NewClientData() { Id = dto.Id });
  1142. AcrionResult.Code = 0;
  1143. }
  1144. }
  1145. catch (Exception ex)
  1146. {
  1147. RollbackTran();
  1148. AcrionResult.Msg = ex.Message;
  1149. AcrionResult.Code = -1;
  1150. }
  1151. return AcrionResult;
  1152. }
  1153. /// <summary>
  1154. /// 市场客户资料数据
  1155. /// 批量指派
  1156. /// </summary>
  1157. /// <param name="dto"></param>
  1158. /// <returns></returns>
  1159. public async Task<Result> _BatchAssignment(BatchAssignmentDto dto)
  1160. {
  1161. Result AcrionResult = new Result() { Code = -1,Msg="操作失败"};
  1162. if (dto.UserIdItem == null || dto.UserIdItem.Count < 1)
  1163. {
  1164. AcrionResult.Msg = "用户ID集合不能为空!";
  1165. return AcrionResult;
  1166. }
  1167. if (dto.ClientDataIdItem == null || dto.ClientDataIdItem.Count < 1)
  1168. {
  1169. AcrionResult.Msg = "客户资料ID集合不能为空!";
  1170. return AcrionResult;
  1171. }
  1172. List<Crm_ClientDataAndUser> _ClientDataAndUsers = new List<Crm_ClientDataAndUser>();
  1173. List<Crm_ClientDataAndUser> _ClientDataAndUsers1 = await _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  1174. .Where(it =>
  1175. it.IsDel == 0 &&
  1176. dto.ClientDataIdItem.Contains(it.NewClientDataId) &&
  1177. dto.UserIdItem.Contains(it.usersId)
  1178. )
  1179. .ToListAsync();
  1180. var existsCount = 0;
  1181. foreach (var clientDataId in dto.ClientDataIdItem)
  1182. {
  1183. foreach (var userId1 in dto.UserIdItem)
  1184. {
  1185. Crm_ClientDataAndUser _ClientDataAndUsers2 = _ClientDataAndUsers1.Where(it => it.NewClientDataId == clientDataId &&
  1186. it.usersId == userId1
  1187. ).FirstOrDefault();
  1188. if (_ClientDataAndUsers2 == null)
  1189. {
  1190. _ClientDataAndUsers.Add(new Crm_ClientDataAndUser()
  1191. {
  1192. CreateUserId = dto.UserId,
  1193. NewClientDataId = clientDataId,
  1194. usersId = userId1
  1195. });
  1196. }
  1197. else
  1198. {
  1199. existsCount++;
  1200. }
  1201. }
  1202. }
  1203. if (_ClientDataAndUsers.Count > 0)
  1204. {
  1205. var adds = await _sqlSugar.Insertable(_ClientDataAndUsers).ExecuteCommandAsync();
  1206. if (adds > 0)
  1207. {
  1208. #region 客户资料表操作记录 批量添加
  1209. List<Crm_TableOperationRecord> _TableOperationRecords = new List<Crm_TableOperationRecord>();
  1210. foreach (var item in _ClientDataAndUsers)
  1211. {
  1212. _TableOperationRecords.Add(
  1213. new Crm_TableOperationRecord() {
  1214. TableName = "Crm_TableOperationRecord",
  1215. PortType = dto.PortType,
  1216. OperationItem = OperationEnum.BatchAssignment,
  1217. DataId = item.NewClientDataId,
  1218. CreateUserId = dto.UserId,
  1219. CreateTime = DateTime.Now,
  1220. Remark = "",
  1221. IsDel = 0
  1222. });
  1223. }
  1224. if (_TableOperationRecords.Count > 0)
  1225. {
  1226. await _sqlSugar.Insertable(_TableOperationRecords).ExecuteCommandAsync();
  1227. }
  1228. #endregion
  1229. AcrionResult.Code = 0;
  1230. return AcrionResult;
  1231. }
  1232. }
  1233. else
  1234. {
  1235. if(existsCount > 0)
  1236. AcrionResult.Msg += $" {existsCount}条数据已被指派,无法重复指派!";
  1237. }
  1238. return AcrionResult;
  1239. }
  1240. public List<Sys_Users> GetNewExistClient(int userid)
  1241. {
  1242. 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";
  1243. var userArr = _sqlSugar.SqlQueryable<Sys_Users>(sql).ToList();
  1244. var dic = new Dictionary<int, int[]>()
  1245. {
  1246. { 95, new int []{ 95 , 337 , 302, 350, 355, 357, 353 } }
  1247. };
  1248. if (dic.Keys.Contains(userid))
  1249. {
  1250. return userArr.Where(x=> dic[userid].Contains(x.Id)).ToList();
  1251. }
  1252. return userArr;
  1253. }
  1254. /// <summary>
  1255. /// 客户资料
  1256. /// excel download
  1257. /// </summary>
  1258. /// <param name="dto"></param>
  1259. /// <returns></returns>
  1260. public async Task<List<NewClientDataExcelDownloadView>> NewClientDataExcelDownload(NewClientDataExcelDownloadDto dto)
  1261. {
  1262. var dt = new List<NewClientDataExcelDownloadView>();
  1263. try
  1264. {
  1265. #region 交集
  1266. List<int> NewClientDataId1 = new List<int>();
  1267. List<int> NewClientDataId2 = new List<int>();
  1268. int state = 0;
  1269. #region 负责人
  1270. if (dto.OperationUserId != 21)
  1271. {
  1272. if (string.IsNullOrWhiteSpace(dto.Userid))
  1273. {
  1274. dto.Userid = dto.OperationUserId.ToString();
  1275. }
  1276. }
  1277. if (!string.IsNullOrWhiteSpace(dto.Userid))
  1278. {
  1279. 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);
  1280. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  1281. if (ascribedUsers.Count != 0)
  1282. {
  1283. foreach (var ascribedUser1 in ascribedUsers)
  1284. {
  1285. if (ascribedUser1.NewClientDataId != 0)
  1286. {
  1287. NewClientDataId1.Add(ascribedUser1.NewClientDataId);
  1288. }
  1289. }
  1290. }
  1291. else
  1292. {
  1293. dt = null;
  1294. }
  1295. state = -1;
  1296. }
  1297. #endregion
  1298. #region 业务归属
  1299. if (!string.IsNullOrWhiteSpace(dto.Business))
  1300. {
  1301. 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);
  1302. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  1303. if (AscribedDepartment.Count != 0)
  1304. {
  1305. foreach (var item in AscribedDepartment)
  1306. {
  1307. if (item.NewClientDataId != 0)
  1308. {
  1309. NewClientDataId2.Add(item.NewClientDataId);
  1310. }
  1311. }
  1312. }
  1313. else
  1314. {
  1315. dt = null;
  1316. }
  1317. state = -1;
  1318. }
  1319. #endregion
  1320. List<int> intList = new List<int>();
  1321. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  1322. {
  1323. intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  1324. }
  1325. else if (NewClientDataId1.Count != 0)
  1326. {
  1327. intList = NewClientDataId1;
  1328. }
  1329. else if (NewClientDataId2.Count != 0)
  1330. {
  1331. if (dto.OperationUserId == 21)
  1332. {
  1333. intList = NewClientDataId2;
  1334. }
  1335. }
  1336. if (state == -1)
  1337. {
  1338. if (intList.Count < 1) intList.Add(-1);
  1339. }
  1340. #endregion
  1341. #region 省域条件
  1342. var rangeSetDataList = new List<int>();
  1343. if (dto.Range != 0)
  1344. {
  1345. string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
  1346. switch (dto.Range)
  1347. {
  1348. case 419:
  1349. setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
  1350. break;
  1351. case 421:
  1352. setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
  1353. break;
  1354. case 420:
  1355. setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
  1356. break;
  1357. case 423:
  1358. setDataSql += " and (Name like '%重庆%')";
  1359. break;
  1360. case 422:
  1361. setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
  1362. break;
  1363. case 578:
  1364. setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
  1365. break;
  1366. case 605:
  1367. setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
  1368. break;
  1369. case 606:
  1370. setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
  1371. break;
  1372. case 625:
  1373. setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
  1374. break;
  1375. case 634:
  1376. setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
  1377. break;
  1378. }
  1379. rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  1380. }
  1381. #endregion
  1382. var NewClientDataView = new List<NewClientDataExcelDownloadView>();
  1383. var count = 0;
  1384. string contact = dto.Contact, location = dto.Location, clientDto = dto.Client;
  1385. var isSelectSearch = false;
  1386. var searchDataIds = new List<int>();
  1387. if (string.IsNullOrEmpty(contact) || string.IsNullOrEmpty(location) || string.IsNullOrEmpty(clientDto))
  1388. {
  1389. isSelectSearch = true;
  1390. var searchClientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  1391. .Where(x => x.IsDel == 0)
  1392. .Select(x => new Crm_NewClientData() { Id = x.Id, Client = x.Client, Location = x.Location, Contact = x.Contact })
  1393. .ToListAsync();
  1394. foreach (var item in searchClientDatas) EncryptionProcessor.DecryptProperties(item);
  1395. if (dto.PortType == 1)
  1396. {
  1397. searchDataIds = searchClientDatas
  1398. .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact)) //联系人条件
  1399. .WhereIF(!string.IsNullOrEmpty(location), x => !string.IsNullOrEmpty(x.Location) && x.Location.Contains(location)) //地区条件
  1400. .WhereIF(!string.IsNullOrEmpty(clientDto), x => !string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)) //单位条件
  1401. .Select(x => x.Id)
  1402. .ToList();
  1403. }
  1404. else if (dto.PortType == 2 || dto.PortType == 3)
  1405. {
  1406. searchDataIds = searchClientDatas
  1407. .Where(x => (!string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(clientDto)) ||
  1408. (!string.IsNullOrEmpty(x.Location) && x.Location.Contains(clientDto)) ||
  1409. (!string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)))
  1410. .Select(x => x.Id)
  1411. .ToList();
  1412. }
  1413. if (searchDataIds.Count < 1) searchDataIds.Add(0);
  1414. }
  1415. var clientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  1416. .Where(x => x.IsDel == 0)
  1417. .WhereIF(state == -1 && intList.Count > 0, x => intList.Contains(x.Id))
  1418. .WhereIF(dto.Lvlid != 0, x => x.Lvlid == dto.Lvlid) //地市州条件
  1419. .WhereIF(rangeSetDataList.Count > 0, x => rangeSetDataList.Contains(x.Lvlid)) //省域条件
  1420. .WhereIF(dto.Category > 0, x => x.Category == dto.Category) //客户类别
  1421. .WhereIF(isSelectSearch && searchDataIds.Count > 0, x => searchDataIds.Contains(x.Id)) //条件模糊查询
  1422. .OrderByDescending(x => x.CreateTime)
  1423. .ToListAsync();
  1424. NewClientDataView = _mapper.Map<List<NewClientDataExcelDownloadView>>(clientDatas);
  1425. if (!NewClientDataView.Any()) return dt;
  1426. var userDatas = await _sqlSugar.Queryable<Sys_Users>().ToListAsync();
  1427. var setDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToListAsync();
  1428. var ascribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  1429. ("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();
  1430. var ascribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  1431. ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id AND d1.ISDEL = 0").ToListAsync();
  1432. int index = 1;
  1433. foreach (var item in NewClientDataView)
  1434. {
  1435. EncryptionProcessor.DecryptProperties(item); //解密
  1436. item.RowNumber = index;
  1437. item.CreateUserName = userDatas.Find(x => x.Id == item.CreateUserId)?.CnName ?? "-";
  1438. item.CategoryStr = setDatas.Find(x => x.Id == item.Category)?.Name ?? "-";
  1439. item.LvlidStr = setDatas.Find(x => x.Id == item.Lvlid)?.Name ?? "-";
  1440. var currAscribedUser = ascribedUser.Where(x => x.NewClientDataId == item.Id).ToList();
  1441. if (currAscribedUser.Any()) item.AscribedUserLable = string.Join("、", currAscribedUser.Select(x => x.CnName).ToList());
  1442. var currAscribedDepartment = ascribedDepartment.Where(x => x.NewClientDataId == item.Id).ToList();
  1443. if (currAscribedDepartment.Any()) item.AscribedDepartmentLable = string.Join("、", currAscribedDepartment.Select(x => x.Name).ToList());
  1444. index++;
  1445. }
  1446. return NewClientDataView;
  1447. }
  1448. catch (Exception ex)
  1449. {
  1450. return dt;
  1451. }
  1452. }
  1453. }
  1454. }