NewClientDataRepository.cs 63 KB

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