NewClientDataRepository.cs 61 KB

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