NewClientDataRepository.cs 61 KB

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