NewClientDataRepository.cs 71 KB

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