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 = string.Empty;
  903. //}
  904. var newClientDataUnEncrypted = _mapper.Map<Crm_NewClientData>(dto);
  905. newClientDataUnEncrypted.Birthday = BirthdayStr;
  906. if (newClientDataUnEncrypted.PassportDate == DateTime.MinValue) newClientDataUnEncrypted.PassportDate = null;
  907. var newClientDataEncrypted = newClientDataUnEncrypted;
  908. EncryptionProcessor.EncryptProperties(newClientDataEncrypted); //加密
  909. try
  910. {
  911. BeginTran();
  912. if (dto.Status == 1)//添加
  913. {
  914. string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' And Contact='{1}' And IsDel={2}"
  915. , newClientDataEncrypted.Client, newClientDataEncrypted.Contact, 0);
  916. var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
  917. if (NewClientData != null)
  918. {
  919. result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  920. }
  921. int id = await AddAsyncReturnId(newClientDataEncrypted); //添加市场客户资料表数据
  922. if (id == 0)
  923. {
  924. result = new Result() { Code = -1, Msg = "添加失败!" };
  925. }
  926. else
  927. {
  928. result = new Result() { Code = 0, Msg = "添加成功!", Data = id };
  929. istrue = true;
  930. AddReturnId = id;
  931. newClientDataUnEncrypted.Id = id;
  932. }
  933. }
  934. else if (dto.Status == 2)//修改
  935. {
  936. //DateTime? PassportDate = null;
  937. //try
  938. //{
  939. // PassportDate = DateTime.Parse(dto.PassportDate);
  940. //}
  941. //catch (Exception)
  942. //{
  943. // PassportDate = null;
  944. //}
  945. var res = await _sqlSugar.Updateable(newClientDataEncrypted).IgnoreColumns(x => new { x.DeleteTime, x.DeleteUserId, x.CreateTime, x.CreateUserId }).ExecuteCommandAsync();
  946. if (res > 0)
  947. {
  948. istrue = true;
  949. AddReturnId = dto.Id == 0 ? -1 : dto.Id;
  950. if (AddReturnId != -1)
  951. {
  952. await _sqlSugar.Updateable<Crm_ClientDataAndUser>().Where(x=>x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndUser()
  953. {
  954. IsDel = 1,
  955. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  956. }).ExecuteCommandAsync();
  957. await _sqlSugar.Updateable<Crm_ClientDataAndBusiness>().Where(x => x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndBusiness()
  958. {
  959. IsDel = 1,
  960. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  961. }).ExecuteCommandAsync();
  962. }
  963. newClientDataUnEncrypted.Id = AddReturnId;
  964. result = new Result() { Code = 0, Msg = "修改成功!" };
  965. }
  966. else
  967. {
  968. result = new Result() { Code = -1, Msg = "修改失败!" };
  969. }
  970. }
  971. else
  972. {
  973. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  974. }
  975. if (istrue)
  976. {
  977. Adds<Crm_ClientDataAndUser>(dto.AscribedUser.Select(x => new Crm_ClientDataAndUser
  978. {
  979. CreateTime = DateTime.Now,
  980. CreateUserId = dto.CreateUserId,
  981. IsDel = 0,
  982. NewClientDataId = AddReturnId,
  983. usersId = x
  984. }).ToList());
  985. Adds<Crm_ClientDataAndBusiness>(dto.AscribedDepartment.Select(x => new Crm_ClientDataAndBusiness
  986. {
  987. CreateUserId = dto.CreateUserId,
  988. IsDel = 0,
  989. CreateTime = DateTime.Now,
  990. NewClientDataId = AddReturnId,
  991. SetDataId = x,
  992. }).ToList());
  993. CommitTran();
  994. ////缓存添加信息
  995. //var optype = 0;
  996. //if (dto.Status == 1) optype = 3;
  997. //else if (dto.Status == 2) optype = 4;
  998. ////操作缓存(未加密数据)
  999. //await NewClientDataRedis(optype, newClientDataUnEncrypted);
  1000. result.Data = AddReturnId;
  1001. }
  1002. else
  1003. {
  1004. RollbackTran();
  1005. }
  1006. }
  1007. catch (Exception)
  1008. {
  1009. RollbackTran();
  1010. result = new Result() { Code = -2, Msg = "未知错误" };
  1011. }
  1012. return result;
  1013. }
  1014. public async Task<Result> QueryNumberGroups()
  1015. {
  1016. Result result = new Result();
  1017. //preDeleAll 预计总量
  1018. //finlishedDeleAll 已出总量
  1019. DataTable preDeleAndfinlishedDeleAll = await GetDataTableAsync("select SUM(PreDele) as PreDeleAll ,SUM(FinlishedDele) as FinlishedDeleAll from Crm_NewClientData");
  1020. var preDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["PreDeleAll"].ToString();
  1021. var finlishedDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["finlishedDeleAll"].ToString();
  1022. result.Code = 0;
  1023. result.Msg = "成功!";
  1024. result.Data = new
  1025. {
  1026. preDeleAll,
  1027. finlishedDeleAll
  1028. };
  1029. return result;
  1030. }
  1031. /// <summary>
  1032. /// 获取下拉列表数据和单条数据信息
  1033. /// </summary>
  1034. /// <param name="dto"></param>
  1035. public async Task<Result> QuerySelectAndSingleData(QuerySingleDto dto)
  1036. {
  1037. Result rest = new Result();
  1038. //var QueryData1 = await NewClientDataRedis(2, new Crm_NewClientData() { Id = dto.Id });
  1039. //var QueryData = QueryData1.FirstOrDefault();
  1040. var QueryData = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0 && x.Id == dto.Id).FirstAsync();
  1041. NewClientDataView MapQueryData = null;
  1042. if (QueryData != null)
  1043. {
  1044. EncryptionProcessor.DecryptProperties(QueryData);
  1045. MapQueryData = _mapper.Map<NewClientDataView>(QueryData);
  1046. MapQueryData.AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  1047. ("select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + dto.Id + " and u1.isdel = 0").ToListAsync();
  1048. MapQueryData.AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  1049. ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + dto.Id + " and d1.isdel = 0").ToListAsync();
  1050. }
  1051. #region 下拉框初始化数据
  1052. //负责人下拉框
  1053. List<dynamic> _Users = new List<dynamic>();
  1054. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  1055. .Where(u => u.IsDel == 0).ToList();
  1056. foreach (Sys_Users user in users)
  1057. {
  1058. var data = new
  1059. {
  1060. Id = user.Id,
  1061. Name = user.CnName
  1062. };
  1063. _Users.Add(data);
  1064. };
  1065. //客户级别数据
  1066. List<dynamic> _level = new List<dynamic>();
  1067. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  1068. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  1069. foreach (Sys_SetData item in level)
  1070. {
  1071. var data = new
  1072. {
  1073. Id = item.Id,
  1074. Name = item.Name
  1075. };
  1076. _level.Add(data);
  1077. };
  1078. //客户类别
  1079. ArrayList _CustomerClass = new ArrayList();
  1080. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  1081. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  1082. foreach (Sys_SetData item in CustomerClass)
  1083. {
  1084. var data = new
  1085. {
  1086. Id = item.Id,
  1087. Name = item.Name,
  1088. item.Remark
  1089. };
  1090. _CustomerClass.Add(data);
  1091. };
  1092. // 创建比较器实例
  1093. IComparer remakeComparer = new RemakeComparer();
  1094. _CustomerClass.Sort(remakeComparer);
  1095. //业务分类
  1096. List<dynamic> _ServiceClass = new List<dynamic>();
  1097. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  1098. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  1099. foreach (Sys_SetData item in ServiceClass)
  1100. {
  1101. var data = new
  1102. {
  1103. Id = item.Id,
  1104. Name = item.Name
  1105. };
  1106. _ServiceClass.Add(data);
  1107. };
  1108. #endregion
  1109. rest.Code = 0;
  1110. rest.Data = new
  1111. {
  1112. data = MapQueryData,
  1113. Users = _Users,
  1114. level = _level,
  1115. CustomerClass = _CustomerClass,
  1116. ServiceClass = _ServiceClass,
  1117. };
  1118. rest.Msg = "获取成功!";
  1119. return rest;
  1120. }
  1121. /// <summary>
  1122. /// 删除市场客户资料数据
  1123. /// </summary>
  1124. /// <param name="dto"></param>
  1125. /// <returns></returns>
  1126. public async Task<Result> DelNewClientData(DelBaseDto dto)
  1127. {
  1128. Result AcrionResult = new Result();
  1129. BeginTran();
  1130. var DBresult = await SoftDeleteByIdAsync<Crm_NewClientData>(dto.Id.ToString(), dto.DeleteUserId);
  1131. try
  1132. {
  1133. if (DBresult)
  1134. {
  1135. AcrionResult.Code = 0;
  1136. string sqlSet = $"isdel = 1, DeleteUserId = {dto.DeleteUserId} ,DeleteTime = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
  1137. string sql = $" update Crm_ClientDataAndUser set {sqlSet} where NewClientDataId = {dto.Id} ";
  1138. await ExecuteCommandAsync(sql);
  1139. sql = $" update Crm_ClientDataAndBusiness set {sqlSet} where NewClientDataId = {dto.Id} ";
  1140. await ExecuteCommandAsync(sql);
  1141. CommitTran();
  1142. //await NewClientDataRedis(5, new Crm_NewClientData() { Id = dto.Id });
  1143. AcrionResult.Code = 0;
  1144. }
  1145. }
  1146. catch (Exception ex)
  1147. {
  1148. RollbackTran();
  1149. AcrionResult.Msg = ex.Message;
  1150. AcrionResult.Code = -1;
  1151. }
  1152. return AcrionResult;
  1153. }
  1154. /// <summary>
  1155. /// 市场客户资料数据
  1156. /// 批量指派
  1157. /// </summary>
  1158. /// <param name="dto"></param>
  1159. /// <returns></returns>
  1160. public async Task<Result> _BatchAssignment(BatchAssignmentDto dto)
  1161. {
  1162. Result AcrionResult = new Result() { Code = -1,Msg="操作失败"};
  1163. if (dto.UserIdItem == null || dto.UserIdItem.Count < 1)
  1164. {
  1165. AcrionResult.Msg = "用户ID集合不能为空!";
  1166. return AcrionResult;
  1167. }
  1168. if (dto.ClientDataIdItem == null || dto.ClientDataIdItem.Count < 1)
  1169. {
  1170. AcrionResult.Msg = "客户资料ID集合不能为空!";
  1171. return AcrionResult;
  1172. }
  1173. List<Crm_ClientDataAndUser> _ClientDataAndUsers = new List<Crm_ClientDataAndUser>();
  1174. List<Crm_ClientDataAndUser> _ClientDataAndUsers1 = await _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  1175. .Where(it =>
  1176. it.IsDel == 0 &&
  1177. dto.ClientDataIdItem.Contains(it.NewClientDataId) &&
  1178. dto.UserIdItem.Contains(it.usersId)
  1179. )
  1180. .ToListAsync();
  1181. var existsCount = 0;
  1182. foreach (var clientDataId in dto.ClientDataIdItem)
  1183. {
  1184. foreach (var userId1 in dto.UserIdItem)
  1185. {
  1186. Crm_ClientDataAndUser _ClientDataAndUsers2 = _ClientDataAndUsers1.Where(it => it.NewClientDataId == clientDataId &&
  1187. it.usersId == userId1
  1188. ).FirstOrDefault();
  1189. if (_ClientDataAndUsers2 == null)
  1190. {
  1191. _ClientDataAndUsers.Add(new Crm_ClientDataAndUser()
  1192. {
  1193. CreateUserId = dto.UserId,
  1194. NewClientDataId = clientDataId,
  1195. usersId = userId1
  1196. });
  1197. }
  1198. else
  1199. {
  1200. existsCount++;
  1201. }
  1202. }
  1203. }
  1204. if (_ClientDataAndUsers.Count > 0)
  1205. {
  1206. var adds = await _sqlSugar.Insertable(_ClientDataAndUsers).ExecuteCommandAsync();
  1207. if (adds > 0)
  1208. {
  1209. #region 客户资料表操作记录 批量添加
  1210. //List<Crm_TableOperationRecord> _TableOperationRecords = new List<Crm_TableOperationRecord>();
  1211. //foreach (var item in _ClientDataAndUsers)
  1212. //{
  1213. // _TableOperationRecords.Add(
  1214. // new Crm_TableOperationRecord() {
  1215. // TableName = "Crm_TableOperationRecord",
  1216. // PortType = dto.PortType,
  1217. // OperationItem = OperationEnum.BatchAssignment,
  1218. // DataId = item.NewClientDataId,
  1219. // CreateUserId = dto.UserId,
  1220. // CreateTime = DateTime.Now,
  1221. // Remark = "",
  1222. // IsDel = 0
  1223. // });
  1224. //}
  1225. //if (_TableOperationRecords.Count > 0)
  1226. //{
  1227. // await _sqlSugar.Insertable(_TableOperationRecords).ExecuteCommandAsync();
  1228. //}
  1229. #endregion
  1230. AcrionResult.Code = 0;
  1231. return AcrionResult;
  1232. }
  1233. }
  1234. else
  1235. {
  1236. if(existsCount > 0)
  1237. AcrionResult.Msg += $" {existsCount}条数据已被指派,无法重复指派!";
  1238. }
  1239. return AcrionResult;
  1240. }
  1241. public List<Sys_Users> GetNewExistClient(int userid)
  1242. {
  1243. string sql = "SELECT * FROM Sys_Users su where su.id in (\r\n\t select distinct usersid from Crm_ClientDataAndUser WHERE IsDel = 0 \r\n) \r\n";
  1244. var userArr = _sqlSugar.SqlQueryable<Sys_Users>(sql).ToList();
  1245. var dic = new Dictionary<int, int[]>()
  1246. {
  1247. { 95, new int []{ 95 , 337 , 302, 350, 355, 357, 353 } }
  1248. };
  1249. if (dic.Keys.Contains(userid))
  1250. {
  1251. return userArr.Where(x=> dic[userid].Contains(x.Id)).ToList();
  1252. }
  1253. return userArr;
  1254. }
  1255. /// <summary>
  1256. /// 客户资料
  1257. /// excel download
  1258. /// </summary>
  1259. /// <param name="dto"></param>
  1260. /// <returns></returns>
  1261. public async Task<List<NewClientDataExcelDownloadView>> NewClientDataExcelDownload(NewClientDataExcelDownloadDto dto)
  1262. {
  1263. var dt = new List<NewClientDataExcelDownloadView>();
  1264. try
  1265. {
  1266. #region 交集
  1267. List<int> NewClientDataId1 = new List<int>();
  1268. List<int> NewClientDataId2 = new List<int>();
  1269. int state = 0;
  1270. #region 负责人
  1271. if (dto.OperationUserId != 21)
  1272. {
  1273. if (string.IsNullOrWhiteSpace(dto.Userid))
  1274. {
  1275. dto.Userid = dto.OperationUserId.ToString();
  1276. }
  1277. }
  1278. if (!string.IsNullOrWhiteSpace(dto.Userid))
  1279. {
  1280. string sql = string.Format(@"select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0}) and u1.IsDel = 0", dto.Userid);
  1281. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  1282. if (ascribedUsers.Count != 0)
  1283. {
  1284. foreach (var ascribedUser1 in ascribedUsers)
  1285. {
  1286. if (ascribedUser1.NewClientDataId != 0)
  1287. {
  1288. NewClientDataId1.Add(ascribedUser1.NewClientDataId);
  1289. }
  1290. }
  1291. }
  1292. else
  1293. {
  1294. dt = null;
  1295. }
  1296. state = -1;
  1297. }
  1298. #endregion
  1299. #region 业务归属
  1300. if (!string.IsNullOrWhiteSpace(dto.Business))
  1301. {
  1302. string sql = string.Format(@"select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0}) and d1.isdel = 0", dto.Business);
  1303. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  1304. if (AscribedDepartment.Count != 0)
  1305. {
  1306. foreach (var item in AscribedDepartment)
  1307. {
  1308. if (item.NewClientDataId != 0)
  1309. {
  1310. NewClientDataId2.Add(item.NewClientDataId);
  1311. }
  1312. }
  1313. }
  1314. else
  1315. {
  1316. dt = null;
  1317. }
  1318. state = -1;
  1319. }
  1320. #endregion
  1321. List<int> intList = new List<int>();
  1322. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  1323. {
  1324. intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  1325. }
  1326. else if (NewClientDataId1.Count != 0)
  1327. {
  1328. intList = NewClientDataId1;
  1329. }
  1330. else if (NewClientDataId2.Count != 0)
  1331. {
  1332. if (dto.OperationUserId == 21)
  1333. {
  1334. intList = NewClientDataId2;
  1335. }
  1336. }
  1337. if (state == -1)
  1338. {
  1339. if (intList.Count < 1) intList.Add(-1);
  1340. }
  1341. #endregion
  1342. #region 省域条件
  1343. var rangeSetDataList = new List<int>();
  1344. if (dto.Range != 0)
  1345. {
  1346. string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
  1347. switch (dto.Range)
  1348. {
  1349. case 419:
  1350. setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
  1351. break;
  1352. case 421:
  1353. setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
  1354. break;
  1355. case 420:
  1356. setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
  1357. break;
  1358. case 423:
  1359. setDataSql += " and (Name like '%重庆%')";
  1360. break;
  1361. case 422:
  1362. setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
  1363. break;
  1364. case 578:
  1365. setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
  1366. break;
  1367. case 605:
  1368. setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
  1369. break;
  1370. case 606:
  1371. setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
  1372. break;
  1373. case 625:
  1374. setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
  1375. break;
  1376. case 634:
  1377. setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
  1378. break;
  1379. }
  1380. rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  1381. }
  1382. #endregion
  1383. var NewClientDataView = new List<NewClientDataExcelDownloadView>();
  1384. //var count = 0;
  1385. string contact = dto.Contact, location = dto.Location, clientDto = dto.Client;
  1386. var isSelectSearch = false;
  1387. var searchDataIds = new List<int>();
  1388. if (string.IsNullOrEmpty(contact) || string.IsNullOrEmpty(location) || string.IsNullOrEmpty(clientDto))
  1389. {
  1390. isSelectSearch = true;
  1391. var searchClientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  1392. .Where(x => x.IsDel == 0)
  1393. .Select(x => new Crm_NewClientData() { Id = x.Id, Client = x.Client, Location = x.Location, Contact = x.Contact })
  1394. .ToListAsync();
  1395. foreach (var item in searchClientDatas) EncryptionProcessor.DecryptProperties(item);
  1396. if (dto.PortType == 1)
  1397. {
  1398. searchDataIds = searchClientDatas
  1399. .WhereIF(!string.IsNullOrEmpty(contact), x => !string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(contact)) //联系人条件
  1400. .WhereIF(!string.IsNullOrEmpty(location), x => !string.IsNullOrEmpty(x.Location) && x.Location.Contains(location)) //地区条件
  1401. .WhereIF(!string.IsNullOrEmpty(clientDto), x => !string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)) //单位条件
  1402. .Select(x => x.Id)
  1403. .ToList();
  1404. }
  1405. else if (dto.PortType == 2 || dto.PortType == 3)
  1406. {
  1407. searchDataIds = searchClientDatas
  1408. .Where(x => (!string.IsNullOrEmpty(x.Contact) && x.Contact.Contains(clientDto)) ||
  1409. (!string.IsNullOrEmpty(x.Location) && x.Location.Contains(clientDto)) ||
  1410. (!string.IsNullOrEmpty(x.Client) && x.Client.Contains(clientDto)))
  1411. .Select(x => x.Id)
  1412. .ToList();
  1413. }
  1414. if (searchDataIds.Count < 1) searchDataIds.Add(0);
  1415. }
  1416. var clientDatas = await _sqlSugar.Queryable<Crm_NewClientData>()
  1417. .Where(x => x.IsDel == 0)
  1418. .WhereIF(state == -1 && intList.Count > 0, x => intList.Contains(x.Id))
  1419. .WhereIF(dto.Lvlid != 0, x => x.Lvlid == dto.Lvlid) //地市州条件
  1420. .WhereIF(rangeSetDataList.Count > 0, x => rangeSetDataList.Contains(x.Lvlid)) //省域条件
  1421. .WhereIF(dto.Category > 0, x => x.Category == dto.Category) //客户类别
  1422. .WhereIF(isSelectSearch && searchDataIds.Count > 0, x => searchDataIds.Contains(x.Id)) //条件模糊查询
  1423. .OrderByDescending(x => x.CreateTime)
  1424. .ToListAsync();
  1425. NewClientDataView = _mapper.Map<List<NewClientDataExcelDownloadView>>(clientDatas);
  1426. if (!NewClientDataView.Any()) return dt;
  1427. var userDatas = await _sqlSugar.Queryable<Sys_Users>().ToListAsync();
  1428. var setDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToListAsync();
  1429. var ascribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  1430. ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id AND u1.ISDEL = 0").ToListAsync();
  1431. var ascribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  1432. ("select d2.Id,d2.Name,d1.NewClientDataId from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id AND d1.ISDEL = 0").ToListAsync();
  1433. int index = 1;
  1434. foreach (var item in NewClientDataView)
  1435. {
  1436. EncryptionProcessor.DecryptProperties(item); //解密
  1437. item.RowNumber = index;
  1438. item.CreateUserName = userDatas.Find(x => x.Id == item.CreateUserId)?.CnName ?? "-";
  1439. item.CategoryStr = setDatas.Find(x => x.Id == item.Category)?.Name ?? "-";
  1440. item.LvlidStr = setDatas.Find(x => x.Id == item.Lvlid)?.Name ?? "-";
  1441. var currAscribedUser = ascribedUser.Where(x => x.NewClientDataId == item.Id).ToList();
  1442. if (currAscribedUser.Any()) item.AscribedUserLable = string.Join("、", currAscribedUser.Select(x => x.CnName).ToList());
  1443. var currAscribedDepartment = ascribedDepartment.Where(x => x.NewClientDataId == item.Id).ToList();
  1444. if (currAscribedDepartment.Any()) item.AscribedDepartmentLable = string.Join("、", currAscribedDepartment.Select(x => x.Name).ToList());
  1445. index++;
  1446. }
  1447. return NewClientDataView;
  1448. }
  1449. catch (Exception ex)
  1450. {
  1451. return dt;
  1452. }
  1453. }
  1454. }
  1455. }