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