NewClientDataRepository.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. using AutoMapper;
  2. using AutoMapper.Execution;
  3. using MySqlX.XDevAPI.Relational;
  4. using NPOI.OpenXmlFormats.Dml.Diagram;
  5. using NPOI.SS.Formula.Functions;
  6. using NPOI.SS.UserModel;
  7. using OASystem.Domain;
  8. using OASystem.Domain.Dtos;
  9. using OASystem.Domain.Dtos.CRM;
  10. using OASystem.Domain.Entities.Customer;
  11. using OASystem.Domain.Entities.Resource;
  12. using OASystem.Domain.Enums;
  13. using OASystem.Domain.ViewModels.CRM;
  14. using OASystem.Domain.ViewModels.QiYeWeChat;
  15. using SqlSugar;
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using System.ComponentModel.Design;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Xml.Linq;
  24. using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
  25. using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
  26. namespace OASystem.Infrastructure.Repositories.CRM
  27. {
  28. public class NewClientDataRepository : BaseRepository<Crm_NewClientData, NewClientDataView>
  29. {
  30. private readonly IMapper _mapper;
  31. public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  32. base(sqlSugar)
  33. {
  34. _mapper = mapper;
  35. }
  36. /// <summary>
  37. /// 客户资料
  38. /// 基础数据源
  39. /// </summary>
  40. /// <param name="dto"></param>
  41. /// <returns></returns>
  42. public async Task<Result> _Init(MarketCustomerInitDto Dto)
  43. {
  44. Result result = new Result() { Code = -2, Msg = "未知错误" };
  45. var portType = Dto.PortType;
  46. if (portType == 1 || portType == 2 || portType == 3)
  47. {
  48. #region 下拉框初始化数据
  49. //负责人下拉框
  50. //List<dynamic> _Users = new List<dynamic>();
  51. //var _Users = _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0).Select(x => new
  52. //{
  53. // x.Id,
  54. // Name = x.CnName
  55. //}).ToList();
  56. var _Users = GetNewExistClient(Dto.UserId);
  57. //List<Sys_Users> users = GetNewExistClient();
  58. //foreach (Sys_Users user in users)
  59. //{
  60. // var data = new
  61. // {
  62. // Id = user.Id,
  63. // Name = user.CnName
  64. // };
  65. // _Users.Add(data);
  66. //};
  67. List<Sys_SetData> initData = _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToList();
  68. //客户级别数据
  69. List<dynamic> _level = new List<dynamic>();
  70. List<Sys_SetData> level = initData.Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  71. foreach (Sys_SetData item in level)
  72. {
  73. var data = new
  74. {
  75. Id = item.Id,
  76. Name = item.Name
  77. };
  78. _level.Add(data);
  79. };
  80. //客户类别
  81. List<dynamic> _CustomerClass = new List<dynamic>();
  82. List<Sys_SetData> CustomerClass = initData.Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  83. foreach (Sys_SetData item in CustomerClass)
  84. {
  85. var data = new
  86. {
  87. Id = item.Id,
  88. Name = item.Name
  89. };
  90. _CustomerClass.Add(data);
  91. };
  92. //业务分类
  93. List<dynamic> _ServiceClass = new List<dynamic>();
  94. List<Sys_SetData> ServiceClass = initData.Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  95. foreach (Sys_SetData item in ServiceClass)
  96. {
  97. var data = new
  98. {
  99. Id = item.Id,
  100. Name = item.Name
  101. };
  102. _ServiceClass.Add(data);
  103. };
  104. //身份分类
  105. List<dynamic> _ProvinceClass = new List<dynamic>();
  106. List<Sys_SetData> ProvinceClass = initData.Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  107. foreach (Sys_SetData item in ProvinceClass)
  108. {
  109. var data = new
  110. {
  111. Id = item.Id,
  112. Name = item.Name
  113. };
  114. _ProvinceClass.Add(data);
  115. };
  116. #endregion
  117. var data1 = new {
  118. Users = _Users,
  119. Level = _level,
  120. CustomerClass = _CustomerClass,
  121. ServiceClass= _ServiceClass,
  122. ProvinceClass = _ProvinceClass
  123. };
  124. return result = new Result()
  125. {
  126. Code = 0,
  127. Msg = "查询成功",
  128. Data = data1
  129. };
  130. }
  131. else
  132. {
  133. result.Msg = string.Format("请传入有效的PortType参数!");
  134. }
  135. return result;
  136. }
  137. /// <summary>
  138. /// 市场客户资料数据
  139. /// 详情
  140. /// </summary>
  141. /// <param name="dto"></param>
  142. /// <returns></returns>
  143. public async Task<Result> _Details(int portType,int id)
  144. {
  145. Result result = new Result() { Code = -2, Msg = "未知错误" };
  146. if (portType == 1 || portType == 2 || portType == 3)
  147. {
  148. if (id < 0)
  149. {
  150. result.Msg = string.Format("请传入有效的Id参数!");
  151. return result;
  152. }
  153. string infoSql = string.Format(@" Select * From Crm_NewClientData Where Isdel = 0 And Id = {0}", id);
  154. var info = await _sqlSugar.SqlQueryable<DetailsView>(infoSql).FirstAsync();
  155. if (info != null )
  156. {
  157. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  158. ("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();
  159. info.AscribedUser = AscribedUser.Select(it => it.UserId).ToList();
  160. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  161. ("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();
  162. info.AscribedDepartment = AscribedDepartment.Select(it => it.Id).ToList();
  163. result.Code = 0;
  164. result.Data = info;
  165. }
  166. }
  167. else
  168. {
  169. result.Msg = string.Format("请传入有效的PortType参数!");
  170. }
  171. return result;
  172. }
  173. /// <summary>
  174. /// 客户资料初识初始化
  175. /// </summary>
  176. /// <param name="dto"></param>
  177. /// <returns></returns>
  178. public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
  179. {
  180. Result result = new Result() { Code = -2, Msg = "未知错误" };
  181. try
  182. {
  183. #region 交集
  184. List<int> NewClientDataId1 = new List<int>();
  185. List<int> NewClientDataId2 = new List<int>();
  186. string NewClientDataId = "";
  187. int state = 0;
  188. #region 负责人
  189. if (dto.OperationUserId != 21 && dto.OperationUserId != 95)
  190. {
  191. dto.Userid = dto.OperationUserId.ToString();
  192. }
  193. if (!string.IsNullOrWhiteSpace(dto.Userid))
  194. {
  195. 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);
  196. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  197. if (ascribedUsers.Count != 0)
  198. {
  199. foreach (var ascribedUser in ascribedUsers)
  200. {
  201. if (ascribedUser.NewClientDataId != 0)
  202. {
  203. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  204. }
  205. }
  206. }
  207. else
  208. {
  209. result = new Result() { Code = -1, Msg = "暂无数据" };
  210. }
  211. state = -1;
  212. }
  213. #endregion
  214. #region 业务归属
  215. if (!string.IsNullOrWhiteSpace(dto.Business))
  216. {
  217. 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);
  218. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  219. if (AscribedDepartment.Count != 0)
  220. {
  221. foreach (var item in AscribedDepartment)
  222. {
  223. if (item.NewClientDataId != 0)
  224. {
  225. NewClientDataId2.Add(item.NewClientDataId);
  226. }
  227. }
  228. }
  229. else
  230. {
  231. result = new Result() { Code = -1, Msg = "暂无数据" };
  232. }
  233. state = -1;
  234. }
  235. #endregion
  236. List<int> intList = new List<int>();
  237. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  238. {
  239. intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  240. }
  241. else if (NewClientDataId1.Count != 0)
  242. {
  243. intList = NewClientDataId1;
  244. }
  245. else if (NewClientDataId2.Count != 0)
  246. {
  247. intList = NewClientDataId2;
  248. }
  249. #endregion
  250. foreach (var item in intList)
  251. {
  252. NewClientDataId += item + ",";
  253. }
  254. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  255. {
  256. NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
  257. }
  258. string sqlWhere = string.Empty;
  259. if (dto.PortType == 1)
  260. {
  261. #region 联系人条件
  262. if (!string.IsNullOrWhiteSpace(dto.Contact))
  263. {
  264. sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
  265. }
  266. #endregion
  267. #region 地区条件
  268. if (!string.IsNullOrWhiteSpace(dto.Location))
  269. {
  270. sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
  271. }
  272. #endregion
  273. #region 单位条件
  274. if (!string.IsNullOrWhiteSpace(dto.Client))
  275. {
  276. sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
  277. }
  278. #endregion
  279. }
  280. else if (dto.PortType == 2 || dto.PortType == 3)
  281. {
  282. sqlWhere += string.Format("And (Contact like '%{0}%' or Location like '%{0}%' or Client like '%{0}%' )", dto.Client);
  283. }
  284. if (state == -1)
  285. {
  286. if (string.IsNullOrWhiteSpace(NewClientDataId))
  287. {
  288. NewClientDataId = "0";
  289. }
  290. sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
  291. }
  292. #region 地市州条件
  293. if (dto.Lvlid != 0)
  294. {
  295. sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid);
  296. }
  297. #endregion
  298. #region 省域条件
  299. if (dto.Range != 0)
  300. {
  301. string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0 ";
  302. switch (dto.Range)
  303. {
  304. case 419:
  305. setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
  306. break;
  307. case 421:
  308. setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
  309. break;
  310. case 420:
  311. setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
  312. break;
  313. case 423:
  314. setDataSql += " and (Name like '%重庆%')";
  315. break;
  316. case 422:
  317. setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
  318. break;
  319. case 578:
  320. setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
  321. break;
  322. case 605:
  323. setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
  324. break;
  325. case 606:
  326. setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
  327. break;
  328. case 625:
  329. setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
  330. break;
  331. case 634:
  332. setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
  333. break;
  334. }
  335. var RangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
  336. string lvlds = string.Join(',', RangeSetDataList).TrimEnd(',');
  337. if (!string.IsNullOrEmpty(lvlds))
  338. {
  339. sqlWhere += string.Format(@" And s.Lvlid in ({0}) ", lvlds);
  340. }
  341. }
  342. #endregion
  343. #region 客户类别
  344. if (dto.Category != 0)
  345. {
  346. sqlWhere += string.Format(@" And s.Category = {0}", dto.Category);
  347. }
  348. #endregion
  349. sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0);
  350. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  351. {
  352. Regex r = new Regex("And");
  353. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  354. }
  355. int pIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
  356. int pSize = dto.PageIndex * dto.PageSize;
  357. 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()
  358. 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
  359. RowNumber between {1} and {2} ", sqlWhere, pIndex, pSize);
  360. List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
  361. foreach (var item in NewClientDataView)
  362. {
  363. Sys_SetData CategoryStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Category);
  364. if (CategoryStr != null)
  365. {
  366. item.CategoryStr = CategoryStr != null ? CategoryStr.Name : null;
  367. }
  368. Sys_SetData lvlStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Lvlid);
  369. if (lvlStr != null)
  370. {
  371. item.LvlidStr = lvlStr != null ? lvlStr.Name : null;
  372. }
  373. }
  374. #region 下拉框初始化数据
  375. //负责人下拉框
  376. List<dynamic> _Users = new List<dynamic>();
  377. List<Sys_Users> users = GetNewExistClient(dto.OperationUserId);
  378. foreach (Sys_Users user in users)
  379. {
  380. var data = new
  381. {
  382. Id = user.Id,
  383. Name = user.CnName
  384. };
  385. _Users.Add(data);
  386. };
  387. //省域数据
  388. List<dynamic> _Province = new List<dynamic>();
  389. List<Sys_SetData> province = _sqlSugar.Queryable<Sys_SetData>()
  390. .Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  391. foreach (Sys_SetData item in province)
  392. {
  393. var data = new
  394. {
  395. Id = item.Id,
  396. Name = item.Name
  397. };
  398. _Province.Add(data);
  399. };
  400. //客户级别数据
  401. List<dynamic> _level = new List<dynamic>();
  402. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  403. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  404. foreach (Sys_SetData item in level)
  405. {
  406. var data = new
  407. {
  408. Id = item.Id,
  409. Name = item.Name
  410. };
  411. _level.Add(data);
  412. };
  413. //客户类别
  414. List<dynamic> _CustomerClass = new List<dynamic>();
  415. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  416. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  417. foreach (Sys_SetData item in CustomerClass)
  418. {
  419. var data = new
  420. {
  421. Id = item.Id,
  422. Name = item.Name
  423. };
  424. _CustomerClass.Add(data);
  425. };
  426. //业务分类
  427. List<dynamic> _ServiceClass = new List<dynamic>();
  428. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  429. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  430. foreach (Sys_SetData item in ServiceClass)
  431. {
  432. var data = new
  433. {
  434. Id = item.Id,
  435. Name = item.Name
  436. };
  437. _ServiceClass.Add(data);
  438. };
  439. #endregion
  440. var groupNumber = await QueryNumberGroups();
  441. if (NewClientDataView.Count > 0)
  442. {
  443. int count = NewClientDataView[0].countPage;
  444. float totalPage = (float)count / dto.PageSize;//总页数
  445. if (totalPage == 0) totalPage = 1;
  446. else totalPage = (int)Math.Ceiling((double)totalPage);
  447. if (dto.PortType == 1)
  448. {
  449. foreach (var item in NewClientDataView)
  450. {
  451. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  452. ("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();
  453. item.AscribedUser = AscribedUser;
  454. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  455. ("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();
  456. item.AscribedDepartment = AscribedDepartment;
  457. }
  458. var Data = new
  459. {
  460. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  461. Users = _Users,
  462. Province = _Province,
  463. level = _level,
  464. CustomerClass = _CustomerClass,
  465. ServiceClass = _ServiceClass,
  466. groupNumber = groupNumber.Data,
  467. };
  468. return result = new Result()
  469. {
  470. Code = 0,
  471. Msg = "查询成功",
  472. Data = Data
  473. };
  474. }
  475. else if (dto.PortType == 2 || dto.PortType == 3)
  476. {
  477. List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
  478. foreach (var item in NewClientDataView)
  479. {
  480. newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
  481. {
  482. RowNumber = item.RowNumber,
  483. Id = item.Id,
  484. Client = item.Client,
  485. Contact = item.Contact,
  486. Job = item.Job,
  487. Telephone = item.Telephone,
  488. Location = item.Location,
  489. });
  490. }
  491. result = new Result()
  492. {
  493. Code = 0,
  494. Msg = "查询成功",
  495. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
  496. };
  497. }
  498. }
  499. else
  500. {
  501. if (dto.PortType == 2 || dto.PortType == 3)
  502. {
  503. var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
  504. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  505. }
  506. else
  507. {
  508. var Data = new
  509. {
  510. ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  511. Users = _Users,
  512. Province = _Province,
  513. level = _level,
  514. CustomerClass = _CustomerClass,
  515. ServiceClass = _ServiceClass,
  516. groupNumber = groupNumber.Data,
  517. };
  518. result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
  519. }
  520. }
  521. }
  522. catch (Exception ex)
  523. {
  524. result = new Result() { Code = -2, Msg = "未知错误" };
  525. }
  526. return result;
  527. }
  528. public Result QueryUserSelect()
  529. {
  530. Result result = new Result() { Code = -2, Msg = "未知错误" };
  531. try
  532. {
  533. //负责人下拉框
  534. List<dynamic> _Users = new List<dynamic>();
  535. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  536. .Where(u => u.IsDel == 0).ToList();
  537. foreach (Sys_Users user in users)
  538. {
  539. var data = new
  540. {
  541. Id = user.Id,
  542. Name = user.CnName
  543. };
  544. _Users.Add(data);
  545. };
  546. if (_Users.Count == 0)
  547. {
  548. result = new Result() { Code = -1, Msg = "暂无数据" };
  549. }
  550. result = new Result() { Code = 0, Msg = "查询成功!", Data = _Users };
  551. }
  552. catch (Exception)
  553. {
  554. result = new Result() { Code = -2, Msg = "未知错误" };
  555. }
  556. return result;
  557. }
  558. public async Task<Result> NewClientOp(NewClientOpDto dto)
  559. {
  560. Result result = new Result() { Code = -2, Msg = "未知错误" };
  561. bool istrue = false;
  562. int AddReturnId = -1;
  563. string BirthdayStr = string.Empty;
  564. if (!string.IsNullOrWhiteSpace(dto.Birthday))
  565. {
  566. DateTime Birthday = new DateTime();
  567. var isParse = DateTime.TryParse(dto.Birthday, out Birthday);
  568. BirthdayStr = isParse ? Birthday.ToString("yyyy-MM-dd") : "";
  569. }
  570. try
  571. {
  572. BeginTran();
  573. if (dto.Status == 1)//添加
  574. {
  575. string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' And Contact='{1}' And IsDel={2}"
  576. , dto.Client, dto.Contact, 0);
  577. var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
  578. if (NewClientData == null)
  579. {
  580. if (string.IsNullOrWhiteSpace(dto.PassportDate))
  581. {
  582. dto.PassportDate = null;
  583. }
  584. Crm_NewClientData _NewClientData = _mapper.Map<Crm_NewClientData>(dto);
  585. _NewClientData.Birthday = BirthdayStr;
  586. int id = await AddAsyncReturnId(_NewClientData); //添加市场客户资料表数据
  587. if (id == 0)
  588. {
  589. result = new Result() { Code = -1, Msg = "添加失败!" };
  590. }
  591. else
  592. {
  593. result = new Result() { Code = 0, Msg = "添加成功!",Data = id };
  594. istrue = true;
  595. AddReturnId = id;
  596. CommitTran();
  597. }
  598. }
  599. else
  600. {
  601. result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  602. }
  603. }
  604. else if (dto.Status == 2)//修改
  605. {
  606. DateTime? PassportDate = null;
  607. try
  608. {
  609. PassportDate = DateTime.Parse(dto.PassportDate);
  610. }
  611. catch (Exception)
  612. {
  613. PassportDate = null;
  614. }
  615. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Crm_NewClientData
  616. {
  617. Number = dto.Number,
  618. Lvlid = dto.Lvlid,
  619. Client = dto.Client,
  620. Weight = dto.Weight,
  621. ClientShort = dto.Clientshort,
  622. Contact = dto.Contact,
  623. Gender = dto.Gender,
  624. Passport = dto.Passport,
  625. PassportDate = PassportDate,
  626. Job = dto.Job,
  627. Telephone = dto.Telephone,
  628. Phone = dto.Phone,
  629. Email = dto.Email,
  630. Location = dto.Location,
  631. Address = dto.Address,
  632. Birthday = BirthdayStr,
  633. OtherInfo = dto.Otherinfo,
  634. Wechat = dto.Wechat,
  635. Category = dto.Category,
  636. PreDele = dto.Predele,
  637. FinlishedDele = dto.FinlishedDele,
  638. Remark = dto.Remark,
  639. });
  640. if (res)
  641. {
  642. istrue = true;
  643. AddReturnId = dto.Id == 0 ? -1 : dto.Id;
  644. if (AddReturnId != -1)
  645. {
  646. await _sqlSugar.Updateable<Crm_ClientDataAndUser>().Where(x=>x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndUser()
  647. {
  648. IsDel = 1,
  649. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  650. }).ExecuteCommandAsync();
  651. await _sqlSugar.Updateable<Crm_ClientDataAndBusiness>().Where(x => x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndBusiness()
  652. {
  653. IsDel = 1,
  654. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  655. }).ExecuteCommandAsync();
  656. }
  657. result = new Result() { Code = 0, Msg = "修改成功!" };
  658. }
  659. else
  660. {
  661. result = new Result() { Code = -1, Msg = "修改失败!" };
  662. }
  663. }
  664. else
  665. {
  666. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  667. }
  668. if (istrue)
  669. {
  670. Adds<Crm_ClientDataAndUser>(dto.AscribedUser.Select(x => new Crm_ClientDataAndUser
  671. {
  672. CreateTime = DateTime.Now,
  673. CreateUserId = dto.CreateUserId,
  674. IsDel = 0,
  675. NewClientDataId = AddReturnId,
  676. usersId = x
  677. }).ToList());
  678. Adds<Crm_ClientDataAndBusiness>(dto.AscribedDepartment.Select(x => new Crm_ClientDataAndBusiness
  679. {
  680. CreateUserId = dto.CreateUserId,
  681. IsDel = 0,
  682. CreateTime = DateTime.Now,
  683. NewClientDataId = AddReturnId,
  684. SetDataId = x,
  685. }).ToList());
  686. CommitTran();
  687. result.Data = AddReturnId;
  688. }
  689. else
  690. {
  691. RollbackTran();
  692. }
  693. }
  694. catch (Exception)
  695. {
  696. RollbackTran();
  697. result = new Result() { Code = -2, Msg = "未知错误" };
  698. }
  699. return result;
  700. }
  701. public async Task<Result> QueryNumberGroups()
  702. {
  703. Result result = new Result();
  704. //preDeleAll 预计总量
  705. //finlishedDeleAll 已出总量
  706. DataTable preDeleAndfinlishedDeleAll = await GetDataTableAsync("select SUM(PreDele) as PreDeleAll ,SUM(FinlishedDele) as FinlishedDeleAll from Crm_NewClientData");
  707. var preDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["PreDeleAll"].ToString();
  708. var finlishedDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["finlishedDeleAll"].ToString();
  709. result.Code = 0;
  710. result.Msg = "成功!";
  711. result.Data = new
  712. {
  713. preDeleAll,
  714. finlishedDeleAll
  715. };
  716. return result;
  717. }
  718. /// <summary>
  719. /// 获取下拉列表数据和单条数据信息
  720. /// </summary>
  721. /// <param name="dto"></param>
  722. public async Task<Result> QuerySelectAndSingleData(QuerySingleDto dto)
  723. {
  724. Result rest = new Result();
  725. var QueryData = await GetAsync<Crm_NewClientData>(x => x.Id == dto.Id);
  726. NewClientDataView MapQueryData = null;
  727. if (QueryData != null)
  728. {
  729. MapQueryData = _mapper.Map<NewClientDataView>(QueryData);
  730. MapQueryData.AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  731. ("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();
  732. MapQueryData.AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  733. ("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();
  734. }
  735. #region 下拉框初始化数据
  736. //负责人下拉框
  737. List<dynamic> _Users = new List<dynamic>();
  738. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  739. .Where(u => u.IsDel == 0).ToList();
  740. foreach (Sys_Users user in users)
  741. {
  742. var data = new
  743. {
  744. Id = user.Id,
  745. Name = user.CnName
  746. };
  747. _Users.Add(data);
  748. };
  749. //客户级别数据
  750. List<dynamic> _level = new List<dynamic>();
  751. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  752. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  753. foreach (Sys_SetData item in level)
  754. {
  755. var data = new
  756. {
  757. Id = item.Id,
  758. Name = item.Name
  759. };
  760. _level.Add(data);
  761. };
  762. //客户类别
  763. List<dynamic> _CustomerClass = new List<dynamic>();
  764. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  765. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  766. foreach (Sys_SetData item in CustomerClass)
  767. {
  768. var data = new
  769. {
  770. Id = item.Id,
  771. Name = item.Name
  772. };
  773. _CustomerClass.Add(data);
  774. };
  775. //业务分类
  776. List<dynamic> _ServiceClass = new List<dynamic>();
  777. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  778. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  779. foreach (Sys_SetData item in ServiceClass)
  780. {
  781. var data = new
  782. {
  783. Id = item.Id,
  784. Name = item.Name
  785. };
  786. _ServiceClass.Add(data);
  787. };
  788. #endregion
  789. rest.Code = 0;
  790. rest.Data = new
  791. {
  792. data = MapQueryData,
  793. Users = _Users,
  794. level = _level,
  795. CustomerClass = _CustomerClass,
  796. ServiceClass = _ServiceClass,
  797. };
  798. rest.Msg = "获取成功!";
  799. return rest;
  800. }
  801. /// <summary>
  802. /// 删除市场客户资料数据
  803. /// </summary>
  804. /// <param name="dto"></param>
  805. /// <returns></returns>
  806. public async Task<Result> DelNewClientData(DelBaseDto dto)
  807. {
  808. Result AcrionResult = new Result();
  809. BeginTran();
  810. var DBresult = await SoftDeleteByIdAsync<Crm_NewClientData>(dto.Id.ToString(), dto.DeleteUserId);
  811. try
  812. {
  813. if (DBresult)
  814. {
  815. AcrionResult.Code = 0;
  816. string sqlSet = $"isdel = 1, DeleteUserId = {dto.DeleteUserId} ,DeleteTime = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
  817. string sql = $" update Crm_ClientDataAndUser set {sqlSet} where NewClientDataId = {dto.Id} ";
  818. await ExecuteCommandAsync(sql);
  819. sql = $" update Crm_ClientDataAndBusiness set {sqlSet} where NewClientDataId = {dto.Id} ";
  820. await ExecuteCommandAsync(sql);
  821. CommitTran();
  822. AcrionResult.Code = 0;
  823. }
  824. }
  825. catch (Exception ex)
  826. {
  827. RollbackTran();
  828. AcrionResult.Msg = ex.Message;
  829. AcrionResult.Code = -1;
  830. }
  831. return AcrionResult;
  832. }
  833. /// <summary>
  834. /// 市场客户资料数据
  835. /// 批量指派
  836. /// </summary>
  837. /// <param name="dto"></param>
  838. /// <returns></returns>
  839. public async Task<Result> _BatchAssignment(BatchAssignmentDto dto)
  840. {
  841. Result AcrionResult = new Result() { Code = -1,Msg="操作失败"};
  842. if (dto.UserIdItem == null || dto.UserIdItem.Count < 1)
  843. {
  844. AcrionResult.Msg = "用户ID集合不能为空!";
  845. return AcrionResult;
  846. }
  847. if (dto.ClientDataIdItem == null || dto.ClientDataIdItem.Count < 1)
  848. {
  849. AcrionResult.Msg = "客户资料ID集合不能为空!";
  850. return AcrionResult;
  851. }
  852. List<Crm_ClientDataAndUser> _ClientDataAndUsers = new List<Crm_ClientDataAndUser>();
  853. List<Crm_ClientDataAndUser> _ClientDataAndUsers1 = await _sqlSugar.Queryable<Crm_ClientDataAndUser>()
  854. .Where(it =>
  855. it.IsDel == 0 &&
  856. dto.ClientDataIdItem.Contains(it.NewClientDataId) &&
  857. dto.UserIdItem.Contains(it.usersId)
  858. )
  859. .ToListAsync();
  860. foreach (var clientDataId in dto.ClientDataIdItem)
  861. {
  862. foreach (var userId1 in dto.UserIdItem)
  863. {
  864. Crm_ClientDataAndUser _ClientDataAndUsers2 = _ClientDataAndUsers1.Where(it => it.NewClientDataId == clientDataId &&
  865. it.usersId == userId1
  866. ).FirstOrDefault();
  867. if (_ClientDataAndUsers2 == null)
  868. {
  869. _ClientDataAndUsers.Add(new Crm_ClientDataAndUser()
  870. {
  871. CreateUserId = dto.UserId,
  872. NewClientDataId = clientDataId,
  873. usersId = userId1
  874. });
  875. }
  876. }
  877. }
  878. if (_ClientDataAndUsers.Count > 0)
  879. {
  880. var adds = await _sqlSugar.Insertable(_ClientDataAndUsers).ExecuteCommandAsync();
  881. if (adds > 0)
  882. {
  883. #region 客户资料表操作记录 批量添加
  884. List<Crm_TableOperationRecord> _TableOperationRecords = new List<Crm_TableOperationRecord>();
  885. foreach (var item in _ClientDataAndUsers)
  886. {
  887. _TableOperationRecords.Add(
  888. new Crm_TableOperationRecord() {
  889. TableName = "Crm_TableOperationRecord",
  890. PortType = dto.PortType,
  891. OperationItem = OperationEnum.BatchAssignment,
  892. DataId = item.NewClientDataId,
  893. CreateUserId = dto.UserId,
  894. CreateTime = DateTime.Now,
  895. Remark = "",
  896. IsDel = 0
  897. });
  898. }
  899. if (_TableOperationRecords.Count > 0)
  900. {
  901. await _sqlSugar.Insertable(_TableOperationRecords).ExecuteCommandAsync();
  902. }
  903. #endregion
  904. AcrionResult.Code = 0;
  905. return AcrionResult;
  906. }
  907. }
  908. return AcrionResult;
  909. }
  910. public List<Sys_Users> GetNewExistClient(int userid)
  911. {
  912. 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";
  913. var userArr = _sqlSugar.SqlQueryable<Sys_Users>(sql).ToList();
  914. var dic = new Dictionary<int, int[]>()
  915. {
  916. { 95, new int []{ 95 , 337 , 302} }
  917. };
  918. if (dic.Keys.Contains(userid))
  919. {
  920. return userArr.Where(x=> dic[userid].Contains(x.Id)).ToList();
  921. }
  922. return userArr;
  923. }
  924. }
  925. }