NewClientDataRepository.cs 75 KB

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