NewClientDataRepository.cs 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. using AutoMapper;
  2. using AutoMapper.Execution;
  3. using EyeSoft.Extensions;
  4. using MySqlX.XDevAPI.Relational;
  5. using Newtonsoft.Json;
  6. using NPOI.OpenXmlFormats.Dml.Diagram;
  7. using NPOI.SS.Formula.Functions;
  8. using NPOI.SS.UserModel;
  9. using OASystem.Domain;
  10. using OASystem.Domain.AesEncryption;
  11. using OASystem.Domain.Dtos;
  12. using OASystem.Domain.Dtos.CRM;
  13. using OASystem.Domain.Entities.Customer;
  14. using OASystem.Domain.Entities.Resource;
  15. using OASystem.Domain.Enums;
  16. using OASystem.Domain.ViewModels.CRM;
  17. using OASystem.Domain.ViewModels.JuHeExchangeRate;
  18. using OASystem.Domain.ViewModels.QiYeWeChat;
  19. using SqlSugar;
  20. using System;
  21. using System.Collections;
  22. using System.Collections.Generic;
  23. using System.ComponentModel.Design;
  24. using System.Diagnostics;
  25. using System.Diagnostics.Contracts;
  26. using System.Linq;
  27. using System.Reflection.Metadata;
  28. using System.Runtime.Intrinsics.Arm;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31. using System.Xml.Linq;
  32. using XAct;
  33. using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
  34. using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
  35. namespace OASystem.Infrastructure.Repositories.CRM
  36. {
  37. public class NewClientDataRepository : BaseRepository<Crm_NewClientData, NewClientDataView>
  38. {
  39. private readonly IMapper _mapper;
  40. public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  41. base(sqlSugar)
  42. {
  43. _mapper = mapper;
  44. }
  45. /// <summary>
  46. /// 客户资料
  47. /// 基础数据源
  48. /// </summary>
  49. /// <param name="dto"></param>
  50. /// <returns></returns>
  51. public async Task<Result> _Init(MarketCustomerInitDto Dto)
  52. {
  53. Result result = new Result() { Code = -2, Msg = "未知错误" };
  54. var portType = Dto.PortType;
  55. if (portType == 1 || portType == 2 || portType == 3)
  56. {
  57. #region 下拉框初始化数据
  58. //负责人下拉框
  59. //List<dynamic> _Users = new List<dynamic>();
  60. //var _Users = _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0).Select(x => new
  61. //{
  62. // x.Id,
  63. // Name = x.CnName
  64. //}).ToList();
  65. var _Users = GetNewExistClient(Dto.UserId)
  66. .Select(x => new
  67. {
  68. x.Id,
  69. Name = x.CnName
  70. }).ToList();
  71. //List<Sys_Users> users = GetNewExistClient();
  72. //foreach (Sys_Users user in users)
  73. //{
  74. // var data = new
  75. // {
  76. // Id = user.Id,
  77. // Name = user.CnName
  78. // };
  79. // _Users.Add(data);
  80. //};
  81. List<Sys_SetData> initData = _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToList();
  82. //客户级别数据
  83. List<dynamic> _level = new List<dynamic>();
  84. List<Sys_SetData> level = initData.Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  85. foreach (Sys_SetData item in level)
  86. {
  87. var data = new
  88. {
  89. Id = item.Id,
  90. Name = item.Name
  91. };
  92. _level.Add(data);
  93. };
  94. //客户类别
  95. ArrayList _CustomerClass = new ArrayList();
  96. List<Sys_SetData> CustomerClass = initData.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  97. foreach (Sys_SetData item in CustomerClass)
  98. {
  99. var data = new
  100. {
  101. Id = item.Id,
  102. Name = item.Name,
  103. item.Remark
  104. };
  105. _CustomerClass.Add(data);
  106. };
  107. // 创建比较器实例
  108. IComparer remakeComparer = new RemakeComparer();
  109. _CustomerClass.Sort(remakeComparer);
  110. //业务分类
  111. List<dynamic> _ServiceClass = new List<dynamic>();
  112. List<Sys_SetData> ServiceClass = initData.Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  113. foreach (Sys_SetData item in ServiceClass)
  114. {
  115. var data = new
  116. {
  117. Id = item.Id,
  118. Name = item.Name
  119. };
  120. _ServiceClass.Add(data);
  121. };
  122. //身份分类
  123. List<dynamic> _ProvinceClass = new List<dynamic>();
  124. List<Sys_SetData> ProvinceClass = initData.Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  125. foreach (Sys_SetData item in ProvinceClass)
  126. {
  127. var data = new
  128. {
  129. Id = item.Id,
  130. Name = item.Name
  131. };
  132. _ProvinceClass.Add(data);
  133. };
  134. #endregion
  135. var data1 = new {
  136. Users = _Users,
  137. Level = _level,
  138. CustomerClass = _CustomerClass,
  139. ServiceClass= _ServiceClass,
  140. ProvinceClass = _ProvinceClass
  141. };
  142. return result = new Result()
  143. {
  144. Code = 0,
  145. Msg = "查询成功",
  146. Data = data1
  147. };
  148. }
  149. else
  150. {
  151. result.Msg = string.Format("请传入有效的PortType参数!");
  152. }
  153. return result;
  154. }
  155. /// <summary>
  156. /// 市场客户资料数据
  157. /// 详情
  158. /// </summary>
  159. /// <param name="dto"></param>
  160. /// <returns></returns>
  161. public async Task<Result> _Details(int portType,int id)
  162. {
  163. Result result = new Result() { Code = -2, Msg = "未知错误" };
  164. if (portType == 1 || portType == 2 || portType == 3)
  165. {
  166. if (id < 0)
  167. {
  168. result.Msg = string.Format("请传入有效的Id参数!");
  169. return result;
  170. }
  171. string infoSql = string.Format(@" Select * From Crm_NewClientData Where Isdel = 0 And Id = {0}", id);
  172. var info = await _sqlSugar.SqlQueryable<DetailsView>(infoSql).FirstAsync();
  173. if (info != null)
  174. {
  175. EncryptionProcessor.DecryptProperties(info);
  176. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  177. ("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();
  178. info.AscribedUser = AscribedUser.Select(it => it.UserId).ToList();
  179. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  180. ("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();
  181. info.AscribedDepartment = AscribedDepartment.Select(it => it.Id).ToList();
  182. result.Code = 0;
  183. result.Data = info;
  184. }
  185. }
  186. else result.Msg = MsgTips.Port;
  187. return result;
  188. }
  189. /// <summary>
  190. /// 客户资料初识初始化
  191. /// </summary>
  192. /// <param name="dto"></param>
  193. /// <returns></returns>
  194. public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
  195. {
  196. Stopwatch stopwatch = Stopwatch.StartNew();
  197. Result result = new Result() { Code = -2, Msg = "未知错误" };
  198. int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
  199. try
  200. {
  201. #region 交集
  202. List<int> NewClientDataId1 = new List<int>();
  203. List<int> NewClientDataId2 = new List<int>();
  204. int state = 0;
  205. #region 负责人
  206. if (dto.OperationUserId != 21)
  207. {
  208. if (string.IsNullOrWhiteSpace(dto.Userid))
  209. {
  210. dto.Userid = dto.OperationUserId.ToString();
  211. }
  212. }
  213. if (!string.IsNullOrWhiteSpace(dto.Userid))
  214. {
  215. 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);
  216. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  217. if (ascribedUsers.Count != 0)
  218. {
  219. foreach (var ascribedUser in ascribedUsers)
  220. {
  221. if (ascribedUser.NewClientDataId != 0)
  222. {
  223. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  224. }
  225. }
  226. }
  227. else
  228. {
  229. result = new Result() { Code = -1, Msg = "暂无数据" };
  230. }
  231. state = -1;
  232. }
  233. #endregion
  234. #region 业务归属
  235. if (!string.IsNullOrWhiteSpace(dto.Business))
  236. {
  237. 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);
  238. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  239. if (AscribedDepartment.Count != 0)
  240. {
  241. foreach (var item in AscribedDepartment)
  242. {
  243. if (item.NewClientDataId != 0)
  244. {
  245. NewClientDataId2.Add(item.NewClientDataId);
  246. }
  247. }
  248. }
  249. else
  250. {
  251. result = new Result() { Code = -1, Msg = "暂无数据" };
  252. }
  253. state = -1;
  254. }
  255. #endregion
  256. List<int> intList = new List<int>();
  257. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  258. {
  259. intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  260. }
  261. else if (NewClientDataId1.Count != 0)
  262. {
  263. intList = NewClientDataId1;
  264. }
  265. else if (NewClientDataId2.Count != 0)
  266. {
  267. if (dto.OperationUserId == 21)
  268. {
  269. intList = NewClientDataId2;
  270. }
  271. }
  272. if (state == -1)
  273. {
  274. if (intList.Count < 1) intList.Add(-1);
  275. }
  276. #endregion
  277. #region 省域条件
  278. var rangeSetDataList = new List<int>();
  279. if (dto.Range != 0)
  280. {
  281. string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
  282. switch (dto.Range)
  283. {
  284. case 419:
  285. setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
  286. break;
  287. case 421:
  288. setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
  289. break;
  290. case 420:
  291. setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
  292. break;
  293. case 423:
  294. setDataSql += " and (Name like '%重庆%')";
  295. break;
  296. case 422:
  297. setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
  298. break;
  299. case 578:
  300. setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
  301. break;
  302. case 605:
  303. setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
  304. break;
  305. case 606:
  306. setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
  307. break;
  308. case 625:
  309. setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
  310. break;
  311. case 634:
  312. setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
  313. break;
  314. }
  315. rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  316. }
  317. #endregion
  318. var NewClientDataView = new List<NewClientDataView>();
  319. var count = 0;
  320. string contact = dto.Contact, location = dto.Location, clientDto = dto.Client;
  321. var isSelectSearch = false;
  322. var searchDataIds = new List<int>();
  323. if (string.IsNullOrEmpty(contact) || string.IsNullOrEmpty(location) || string.IsNullOrEmpty(clientDto))
  324. {
  325. isSelectSearch = true;
  326. var searchClientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  327. .Where(x => x.IsDel == 0)
  328. .Select(x => new Crm_NewClientData() { Id = x.Id, Client = x.Client, Location = x.Location, Contact = x.Contact })
  329. .ToListAsync();
  330. foreach (var item in searchClientDatas) EncryptionProcessor.DecryptProperties(item);
  331. if (dto.PortType == 1)
  332. {
  333. searchDataIds = searchClientDatas
  334. .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact)) //联系人条件
  335. .WhereIF(!string.IsNullOrEmpty(location), x => !string.IsNullOrEmpty(x.Location) && x.Location.Contains(location)) //地区条件
  336. .WhereIF(!string.IsNullOrEmpty(clientDto), x => !string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)) //单位条件
  337. .Select(x => x.Id)
  338. .ToList();
  339. }
  340. else if (dto.PortType == 2 || dto.PortType == 3)
  341. {
  342. searchDataIds = searchClientDatas
  343. .Where(x => (!string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(clientDto)) ||
  344. (!string.IsNullOrEmpty(x.Location) && x.Location.Contains(clientDto)) ||
  345. (!string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)))
  346. .Select(x => x.Id)
  347. .ToList();
  348. }
  349. if (searchDataIds.Count < 1) searchDataIds.Add(0);
  350. }
  351. RefAsync<int> total = 0;
  352. var clientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  353. .Where(x => x.IsDel == 0)
  354. .WhereIF(state == -1 && intList.Count > 0, x => intList.Contains(x.Id))
  355. .WhereIF(dto.Lvlid != 0, x => x.Lvlid == dto.Lvlid) //地市州条件
  356. .WhereIF(rangeSetDataList.Count > 0, x => rangeSetDataList.Contains(x.Lvlid)) //省域条件
  357. .WhereIF(dto.Category > 0, x => x.Category == dto.Category) //客户类别
  358. .WhereIF(isSelectSearch && searchDataIds.Count > 0 , x => searchDataIds.Contains(x.Id)) //条件模糊查询
  359. .OrderByDescending(x => x.CreateTime)
  360. .ToPageListAsync(pageIndex, pageSize, total);
  361. NewClientDataView = _mapper.Map<List<NewClientDataView>>(clientDatas);
  362. count = total;
  363. var setDatas = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToList();
  364. #region 下拉框初始化数据
  365. //负责人下拉框
  366. var _Users = GetNewExistClient(dto.OperationUserId).Select(x => new { x.Id, Name = x.CnName }).ToList();
  367. //省域数据
  368. var _Province = setDatas.Where(u => u.STid == 42).Select(x => new { x.Id, x.Name }).ToList();
  369. //客户级别数据
  370. var _level = setDatas.Where(u => u.STid == 33).Select(x => new { x.Id, x.Name }).ToList();
  371. //客户类别
  372. ArrayList _CustomerClass = new ArrayList();
  373. List<Sys_SetData> CustomerClass = setDatas.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  374. foreach (Sys_SetData item in CustomerClass)
  375. {
  376. var data = new
  377. {
  378. Id = item.Id,
  379. Name = item.Name,
  380. item.Remark
  381. };
  382. _CustomerClass.Add(data);
  383. };
  384. // 创建比较器实例
  385. IComparer remakeComparer = new RemakeComparer();
  386. _CustomerClass.Sort(remakeComparer);
  387. //业务分类
  388. var _ServiceClass = setDatas.Where(u => u.STid == 36).Select(x => new { x.Id, x.Name }).ToList();
  389. #endregion
  390. var groupNumber = await QueryNumberGroups();
  391. if (NewClientDataView.Count > 0)
  392. {
  393. float totalPage = (float)count / dto.PageSize;//总页数
  394. if (totalPage == 0) totalPage = 1;
  395. else totalPage = (int)Math.Ceiling((double)totalPage);
  396. if (dto.PortType == 1)
  397. {
  398. int index = 1;
  399. foreach (var item in NewClientDataView)
  400. {
  401. //EncryptionProcessor.DecryptProperties(item); //解密
  402. item.Weight = AesEncryptionHelper.Decrypt(item.Weight);
  403. item.Client = AesEncryptionHelper.Decrypt(item.Client);
  404. item.Contact = AesEncryptionHelper.Decrypt(item.Contact);
  405. item.Job = AesEncryptionHelper.Decrypt(item.Job);
  406. item.Telephone = AesEncryptionHelper.Decrypt(item.Telephone);
  407. item.Phone = AesEncryptionHelper.Decrypt(item.Phone);
  408. item.Location = AesEncryptionHelper.Decrypt(item.Location);
  409. item.Remark = AesEncryptionHelper.Decrypt(item.Remark);
  410. item.RowNumber = index;
  411. item.CategoryStr = setDatas.Find(x => x.Id == item.Category)?.Name ?? "-";
  412. item.LvlidStr = setDatas.Find(x => x.Id == item.Lvlid)?.Name ?? "-";
  413. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  414. ("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();
  415. item.AscribedUser = AscribedUser;
  416. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  417. ("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();
  418. item.AscribedDepartment = AscribedDepartment;
  419. index++;
  420. }
  421. var Data = new
  422. {
  423. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  424. Users = _Users,
  425. Province = _Province,
  426. level = _level,
  427. CustomerClass = _CustomerClass,
  428. ServiceClass = _ServiceClass,
  429. groupNumber = groupNumber.Data,
  430. };
  431. return result = new Result()
  432. {
  433. Code = 0,
  434. Msg = $"查询成功!解密耗时: {stopwatch.ElapsedMilliseconds} 毫秒",
  435. Data = Data
  436. };
  437. }
  438. else if (dto.PortType == 2 || dto.PortType == 3)
  439. {
  440. List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
  441. int index = 1;
  442. foreach (var item in NewClientDataView)
  443. {
  444. newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
  445. {
  446. RowNumber = index,
  447. Id = item.Id,
  448. Client = AesEncryptionHelper.Decrypt(item.Client),
  449. Contact = AesEncryptionHelper.Decrypt(item.Contact),
  450. Job = AesEncryptionHelper.Decrypt(item.Job),
  451. Telephone = AesEncryptionHelper.Decrypt(item.Telephone),
  452. Location = AesEncryptionHelper.Decrypt(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. ArrayList _CustomerClass = new ArrayList();
  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. item.Remark
  749. };
  750. _CustomerClass.Add(data);
  751. };
  752. // 创建比较器实例
  753. IComparer remakeComparer = new RemakeComparer();
  754. _CustomerClass.Sort(remakeComparer);
  755. //业务分类
  756. List<dynamic> _ServiceClass = new List<dynamic>();
  757. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  758. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  759. foreach (Sys_SetData item in ServiceClass)
  760. {
  761. var data = new
  762. {
  763. Id = item.Id,
  764. Name = item.Name
  765. };
  766. _ServiceClass.Add(data);
  767. };
  768. #endregion
  769. var groupNumber = await QueryNumberGroups();
  770. if (NewClientDataView.Count > 0)
  771. {
  772. int count = NewClientDataView[0].countPage;
  773. float totalPage = (float)count / dto.PageSize;//总页数
  774. if (totalPage == 0) totalPage = 1;
  775. else totalPage = (int)Math.Ceiling((double)totalPage);
  776. if (dto.PortType == 1)
  777. {
  778. foreach (var item in NewClientDataView)
  779. {
  780. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  781. ("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();
  782. item.AscribedUser = AscribedUser;
  783. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  784. ("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();
  785. item.AscribedDepartment = AscribedDepartment;
  786. }
  787. var Data = new
  788. {
  789. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  790. Users = _Users,
  791. Province = _Province,
  792. level = _level,
  793. CustomerClass = _CustomerClass,
  794. ServiceClass = _ServiceClass,
  795. groupNumber = groupNumber.Data,
  796. };
  797. return result = new Result()
  798. {
  799. Code = 0,
  800. Msg = "查询成功",
  801. Data = Data
  802. };
  803. }
  804. else if (dto.PortType == 2 || dto.PortType == 3)
  805. {
  806. List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
  807. foreach (var item in NewClientDataView)
  808. {
  809. newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
  810. {
  811. RowNumber = item.RowNumber,
  812. Id = item.Id,
  813. Client = item.Client,
  814. Contact = item.Contact,
  815. Job = item.Job,
  816. Telephone = item.Telephone,
  817. Location = item.Location,
  818. });
  819. }
  820. result = new Result()
  821. {
  822. Code = 0,
  823. Msg = "查询成功",
  824. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
  825. };
  826. }
  827. }
  828. else
  829. {
  830. if (dto.PortType == 2 || dto.PortType == 3)
  831. {
  832. var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
  833. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  834. }
  835. else
  836. {
  837. var Data = new
  838. {
  839. ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  840. Users = _Users,
  841. Province = _Province,
  842. level = _level,
  843. CustomerClass = _CustomerClass,
  844. ServiceClass = _ServiceClass,
  845. groupNumber = groupNumber.Data,
  846. };
  847. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  848. }
  849. }
  850. }
  851. catch (Exception ex)
  852. {
  853. result = new Result() { Code = -2, Msg = "未知错误" };
  854. }
  855. return result;
  856. }
  857. public Result QueryUserSelect()
  858. {
  859. Result result = new Result() { Code = -2, Msg = "未知错误" };
  860. try
  861. {
  862. //负责人下拉框
  863. List<dynamic> _Users = new List<dynamic>();
  864. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  865. .Where(u => u.IsDel == 0).ToList();
  866. foreach (Sys_Users user in users)
  867. {
  868. var data = new
  869. {
  870. Id = user.Id,
  871. Name = user.CnName
  872. };
  873. _Users.Add(data);
  874. };
  875. if (_Users.Count == 0)
  876. {
  877. result = new Result() { Code = -1, Msg = "暂无数据" };
  878. }
  879. result = new Result() { Code = 0, Msg = "查询成功!", Data = _Users };
  880. }
  881. catch (Exception)
  882. {
  883. result = new Result() { Code = -2, Msg = "未知错误" };
  884. }
  885. return result;
  886. }
  887. public async Task<Result> NewClientOp(NewClientOpDto dto)
  888. {
  889. Result result = new Result() { Code = -2, Msg = "未知错误" };
  890. bool istrue = false;
  891. int AddReturnId = -1;
  892. string BirthdayStr = string.Empty;
  893. if (!string.IsNullOrWhiteSpace(dto.Birthday))
  894. {
  895. DateTime Birthday = new DateTime();
  896. var isParse = DateTime.TryParse(dto.Birthday, out Birthday);
  897. BirthdayStr = isParse ? Birthday.ToString("yyyy-MM-dd") : "";
  898. }
  899. if (string.IsNullOrWhiteSpace(dto.PassportDate))
  900. {
  901. dto.PassportDate = null;
  902. }
  903. var newClientDataUnEncrypted = _mapper.Map<Crm_NewClientData>(dto);
  904. newClientDataUnEncrypted.Birthday = BirthdayStr;
  905. var newClientDataEncrypted = newClientDataUnEncrypted;
  906. EncryptionProcessor.EncryptProperties(newClientDataEncrypted); //加密
  907. try
  908. {
  909. BeginTran();
  910. if (dto.Status == 1)//添加
  911. {
  912. string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' And Contact='{1}' And IsDel={2}"
  913. , newClientDataEncrypted.Client, newClientDataEncrypted.Contact, 0);
  914. var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
  915. if (NewClientData != null)
  916. {
  917. result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  918. }
  919. int id = await AddAsyncReturnId(newClientDataEncrypted); //添加市场客户资料表数据
  920. if (id == 0)
  921. {
  922. result = new Result() { Code = -1, Msg = "添加失败!" };
  923. }
  924. else
  925. {
  926. result = new Result() { Code = 0, Msg = "添加成功!", Data = id };
  927. istrue = true;
  928. AddReturnId = id;
  929. newClientDataUnEncrypted.Id = id;
  930. }
  931. }
  932. else if (dto.Status == 2)//修改
  933. {
  934. DateTime? PassportDate = null;
  935. try
  936. {
  937. PassportDate = DateTime.Parse(dto.PassportDate);
  938. }
  939. catch (Exception)
  940. {
  941. PassportDate = null;
  942. }
  943. var res = await _sqlSugar.Updateable(newClientDataEncrypted).IgnoreColumns(x => new { x.DeleteTime, x.DeleteUserId, x.CreateTime, x.CreateUserId }).ExecuteCommandAsync();
  944. if (res > 0)
  945. {
  946. istrue = true;
  947. AddReturnId = dto.Id == 0 ? -1 : dto.Id;
  948. if (AddReturnId != -1)
  949. {
  950. await _sqlSugar.Updateable<Crm_ClientDataAndUser>().Where(x=>x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndUser()
  951. {
  952. IsDel = 1,
  953. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  954. }).ExecuteCommandAsync();
  955. await _sqlSugar.Updateable<Crm_ClientDataAndBusiness>().Where(x => x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndBusiness()
  956. {
  957. IsDel = 1,
  958. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  959. }).ExecuteCommandAsync();
  960. }
  961. newClientDataUnEncrypted.Id = AddReturnId;
  962. result = new Result() { Code = 0, Msg = "修改成功!" };
  963. }
  964. else
  965. {
  966. result = new Result() { Code = -1, Msg = "修改失败!" };
  967. }
  968. }
  969. else
  970. {
  971. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  972. }
  973. if (istrue)
  974. {
  975. Adds<Crm_ClientDataAndUser>(dto.AscribedUser.Select(x => new Crm_ClientDataAndUser
  976. {
  977. CreateTime = DateTime.Now,
  978. CreateUserId = dto.CreateUserId,
  979. IsDel = 0,
  980. NewClientDataId = AddReturnId,
  981. usersId = x
  982. }).ToList());
  983. Adds<Crm_ClientDataAndBusiness>(dto.AscribedDepartment.Select(x => new Crm_ClientDataAndBusiness
  984. {
  985. CreateUserId = dto.CreateUserId,
  986. IsDel = 0,
  987. CreateTime = DateTime.Now,
  988. NewClientDataId = AddReturnId,
  989. SetDataId = x,
  990. }).ToList());
  991. CommitTran();
  992. ////缓存添加信息
  993. //var optype = 0;
  994. //if (dto.Status == 1) optype = 3;
  995. //else if (dto.Status == 2) optype = 4;
  996. ////操作缓存(未加密数据)
  997. //await NewClientDataRedis(optype, newClientDataUnEncrypted);
  998. result.Data = AddReturnId;
  999. }
  1000. else
  1001. {
  1002. RollbackTran();
  1003. }
  1004. }
  1005. catch (Exception)
  1006. {
  1007. RollbackTran();
  1008. result = new Result() { Code = -2, Msg = "未知错误" };
  1009. }
  1010. return result;
  1011. }
  1012. public async Task<Result> QueryNumberGroups()
  1013. {
  1014. Result result = new Result();
  1015. //preDeleAll 预计总量
  1016. //finlishedDeleAll 已出总量
  1017. DataTable preDeleAndfinlishedDeleAll = await GetDataTableAsync("select SUM(PreDele) as PreDeleAll ,SUM(FinlishedDele) as FinlishedDeleAll from Crm_NewClientData");
  1018. var preDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["PreDeleAll"].ToString();
  1019. var finlishedDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["finlishedDeleAll"].ToString();
  1020. result.Code = 0;
  1021. result.Msg = "成功!";
  1022. result.Data = new
  1023. {
  1024. preDeleAll,
  1025. finlishedDeleAll
  1026. };
  1027. return result;
  1028. }
  1029. /// <summary>
  1030. /// 获取下拉列表数据和单条数据信息
  1031. /// </summary>
  1032. /// <param name="dto"></param>
  1033. public async Task<Result> QuerySelectAndSingleData(QuerySingleDto dto)
  1034. {
  1035. Result rest = new Result();
  1036. //var QueryData1 = await NewClientDataRedis(2, new Crm_NewClientData() { Id = dto.Id });
  1037. //var QueryData = QueryData1.FirstOrDefault();
  1038. var QueryData = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0 && x.Id == dto.Id).FirstAsync();
  1039. NewClientDataView MapQueryData = null;
  1040. if (QueryData != null)
  1041. {
  1042. EncryptionProcessor.DecryptProperties(QueryData);
  1043. MapQueryData = _mapper.Map<NewClientDataView>(QueryData);
  1044. MapQueryData.AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  1045. ("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();
  1046. MapQueryData.AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  1047. ("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();
  1048. }
  1049. #region 下拉框初始化数据
  1050. //负责人下拉框
  1051. List<dynamic> _Users = new List<dynamic>();
  1052. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  1053. .Where(u => u.IsDel == 0).ToList();
  1054. foreach (Sys_Users user in users)
  1055. {
  1056. var data = new
  1057. {
  1058. Id = user.Id,
  1059. Name = user.CnName
  1060. };
  1061. _Users.Add(data);
  1062. };
  1063. //客户级别数据
  1064. List<dynamic> _level = new List<dynamic>();
  1065. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  1066. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  1067. foreach (Sys_SetData item in level)
  1068. {
  1069. var data = new
  1070. {
  1071. Id = item.Id,
  1072. Name = item.Name
  1073. };
  1074. _level.Add(data);
  1075. };
  1076. //客户类别
  1077. ArrayList _CustomerClass = new ArrayList();
  1078. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  1079. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  1080. foreach (Sys_SetData item in CustomerClass)
  1081. {
  1082. var data = new
  1083. {
  1084. Id = item.Id,
  1085. Name = item.Name,
  1086. item.Remark
  1087. };
  1088. _CustomerClass.Add(data);
  1089. };
  1090. // 创建比较器实例
  1091. IComparer remakeComparer = new RemakeComparer();
  1092. _CustomerClass.Sort(remakeComparer);
  1093. //业务分类
  1094. List<dynamic> _ServiceClass = new List<dynamic>();
  1095. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  1096. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  1097. foreach (Sys_SetData item in ServiceClass)
  1098. {
  1099. var data = new
  1100. {
  1101. Id = item.Id,
  1102. Name = item.Name
  1103. };
  1104. _ServiceClass.Add(data);
  1105. };
  1106. #endregion
  1107. rest.Code = 0;
  1108. rest.Data = new
  1109. {
  1110. data = MapQueryData,
  1111. Users = _Users,
  1112. level = _level,
  1113. CustomerClass = _CustomerClass,
  1114. ServiceClass = _ServiceClass,
  1115. };
  1116. rest.Msg = "获取成功!";
  1117. return rest;
  1118. }
  1119. /// <summary>
  1120. /// 删除市场客户资料数据
  1121. /// </summary>
  1122. /// <param name="dto"></param>
  1123. /// <returns></returns>
  1124. public async Task<Result> DelNewClientData(DelBaseDto dto)
  1125. {
  1126. Result AcrionResult = new Result();
  1127. BeginTran();
  1128. var DBresult = await SoftDeleteByIdAsync<Crm_NewClientData>(dto.Id.ToString(), dto.DeleteUserId);
  1129. try
  1130. {
  1131. if (DBresult)
  1132. {
  1133. AcrionResult.Code = 0;
  1134. string sqlSet = $"isdel = 1, DeleteUserId = {dto.DeleteUserId} ,DeleteTime = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
  1135. string sql = $" update Crm_ClientDataAndUser set {sqlSet} where NewClientDataId = {dto.Id} ";
  1136. await ExecuteCommandAsync(sql);
  1137. sql = $" update Crm_ClientDataAndBusiness set {sqlSet} where NewClientDataId = {dto.Id} ";
  1138. await ExecuteCommandAsync(sql);
  1139. CommitTran();
  1140. //await NewClientDataRedis(5, new Crm_NewClientData() { Id = dto.Id });
  1141. AcrionResult.Code = 0;
  1142. }
  1143. }
  1144. catch (Exception ex)
  1145. {
  1146. RollbackTran();
  1147. AcrionResult.Msg = ex.Message;
  1148. AcrionResult.Code = -1;
  1149. }
  1150. return AcrionResult;
  1151. }
  1152. /// <summary>
  1153. /// 市场客户资料数据
  1154. /// 批量指派
  1155. /// </summary>
  1156. /// <param name="dto"></param>
  1157. /// <returns></returns>
  1158. public async Task<Result> _BatchAssignment(BatchAssignmentDto dto)
  1159. {
  1160. Result AcrionResult = new Result() { Code = -1,Msg="操作失败"};
  1161. if (dto.UserIdItem == null || dto.UserIdItem.Count < 1)
  1162. {
  1163. AcrionResult.Msg = "用户ID集合不能为空!";
  1164. return AcrionResult;
  1165. }
  1166. if (dto.ClientDataIdItem == null || dto.ClientDataIdItem.Count < 1)
  1167. {
  1168. AcrionResult.Msg = "客户资料ID集合不能为空!";
  1169. return AcrionResult;
  1170. }
  1171. List<Crm_ClientDataAndUser> _ClientDataAndUsers = new List<Crm_ClientDataAndUser>();
  1172. List<Crm_ClientDataAndUser> _ClientDataAndUsers1 = await _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  1173. .Where(it =>
  1174. it.IsDel == 0 &&
  1175. dto.ClientDataIdItem.Contains(it.NewClientDataId) &&
  1176. dto.UserIdItem.Contains(it.usersId)
  1177. )
  1178. .ToListAsync();
  1179. var existsCount = 0;
  1180. foreach (var clientDataId in dto.ClientDataIdItem)
  1181. {
  1182. foreach (var userId1 in dto.UserIdItem)
  1183. {
  1184. Crm_ClientDataAndUser _ClientDataAndUsers2 = _ClientDataAndUsers1.Where(it => it.NewClientDataId == clientDataId &&
  1185. it.usersId == userId1
  1186. ).FirstOrDefault();
  1187. if (_ClientDataAndUsers2 == null)
  1188. {
  1189. _ClientDataAndUsers.Add(new Crm_ClientDataAndUser()
  1190. {
  1191. CreateUserId = dto.UserId,
  1192. NewClientDataId = clientDataId,
  1193. usersId = userId1
  1194. });
  1195. }
  1196. else
  1197. {
  1198. existsCount++;
  1199. }
  1200. }
  1201. }
  1202. if (_ClientDataAndUsers.Count > 0)
  1203. {
  1204. var adds = await _sqlSugar.Insertable(_ClientDataAndUsers).ExecuteCommandAsync();
  1205. if (adds > 0)
  1206. {
  1207. #region 客户资料表操作记录 批量添加
  1208. List<Crm_TableOperationRecord> _TableOperationRecords = new List<Crm_TableOperationRecord>();
  1209. foreach (var item in _ClientDataAndUsers)
  1210. {
  1211. _TableOperationRecords.Add(
  1212. new Crm_TableOperationRecord() {
  1213. TableName = "Crm_TableOperationRecord",
  1214. PortType = dto.PortType,
  1215. OperationItem = OperationEnum.BatchAssignment,
  1216. DataId = item.NewClientDataId,
  1217. CreateUserId = dto.UserId,
  1218. CreateTime = DateTime.Now,
  1219. Remark = "",
  1220. IsDel = 0
  1221. });
  1222. }
  1223. if (_TableOperationRecords.Count > 0)
  1224. {
  1225. await _sqlSugar.Insertable(_TableOperationRecords).ExecuteCommandAsync();
  1226. }
  1227. #endregion
  1228. AcrionResult.Code = 0;
  1229. return AcrionResult;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. if(existsCount > 0)
  1235. AcrionResult.Msg += $" {existsCount}条数据已被指派,无法重复指派!";
  1236. }
  1237. return AcrionResult;
  1238. }
  1239. public List<Sys_Users> GetNewExistClient(int userid)
  1240. {
  1241. 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";
  1242. var userArr = _sqlSugar.SqlQueryable<Sys_Users>(sql).ToList();
  1243. var dic = new Dictionary<int, int[]>()
  1244. {
  1245. { 95, new int []{ 95 , 337 , 302} }
  1246. };
  1247. if (dic.Keys.Contains(userid))
  1248. {
  1249. return userArr.Where(x=> dic[userid].Contains(x.Id)).ToList();
  1250. }
  1251. return userArr;
  1252. }
  1253. /// <summary>
  1254. /// 公司客户名单 缓存
  1255. /// </summary>
  1256. /// <param name="opType">操作类型 1 获取全部 2 单条详情 3 添加 4 修改 5 删除</param>
  1257. /// <param name="data"></param>
  1258. /// <returns></returns>
  1259. public async Task<List<Crm_NewClientData>> NewClientDataRedis(int opType, Crm_NewClientData data = null)
  1260. {
  1261. var datas = new List<Crm_NewClientData>();
  1262. var opTypes = new List<int>() { 1, 2, 3, 4 };
  1263. if (!opTypes.Any(x => x == opType)) return datas;
  1264. var keyName = $"newClientDataStr";
  1265. var newClientJson = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>(keyName);//string 取
  1266. if (!string.IsNullOrEmpty(newClientJson)) datas = JsonConvert.DeserializeObject<List<Crm_NewClientData>>(newClientJson);
  1267. if (opType == 1) //查询全部
  1268. {
  1269. if (datas != null && datas.Count > 0) return datas;
  1270. datas = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0).ToListAsync();
  1271. foreach (var item in datas) EncryptionProcessor.DecryptProperties(item);
  1272. }
  1273. else if (opType == 2) //单条数据
  1274. {
  1275. if (datas != null && datas.Count > 0) return datas.Where(x => x.Id == data.Id).ToList();
  1276. datas = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0).ToListAsync();
  1277. foreach (var item in datas) EncryptionProcessor.DecryptProperties(item);
  1278. await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync(keyName, JsonConvert.SerializeObject(datas));//string 存
  1279. return datas.Where(x => x.Id == data.Id).ToList();
  1280. }
  1281. else if (opType == 3) //添加
  1282. {
  1283. datas.Add(data);
  1284. }
  1285. else if (opType == 4) //修改
  1286. {
  1287. var updData = datas.Where(x => x.Id != data.Id).FirstOrDefault();
  1288. if (updData != null) datas.Remove(updData);
  1289. datas.Add(data);
  1290. }
  1291. else if (opType == 5) //删除
  1292. {
  1293. var delData = datas.Where(x => x.Id == data.Id).FirstOrDefault();
  1294. if (delData != null) datas.Remove(delData);
  1295. }
  1296. await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync(keyName, JsonConvert.SerializeObject(datas));//string 存
  1297. return datas;
  1298. }
  1299. }
  1300. }