NewClientDataRepository.cs 62 KB

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