NewClientDataRepository.cs 60 KB

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