NewClientDataRepository.cs 61 KB

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