NewClientDataRepository.cs 71 KB

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