NewClientDataRepository.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. using AutoMapper;
  2. using AutoMapper.Execution;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.CRM;
  5. using OASystem.Domain.Entities.Customer;
  6. using OASystem.Domain.Entities.Resource;
  7. using OASystem.Domain.ViewModels.CRM;
  8. using SqlSugar;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Xml.Linq;
  16. using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
  17. namespace OASystem.Infrastructure.Repositories.CRM
  18. {
  19. public class NewClientDataRepository : BaseRepository<Crm_NewClientData, Crm_NewClientData>
  20. {
  21. private readonly IMapper _mapper;
  22. public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  23. base(sqlSugar)
  24. {
  25. _mapper= mapper;
  26. }
  27. /// <summary>
  28. /// 客户资料初识初始化
  29. /// </summary>
  30. /// <param name="dto"></param>
  31. /// <returns></returns>
  32. public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
  33. {
  34. Result result = new Result() { Code = -2, Msg = "未知错误" };
  35. try
  36. {
  37. if (dto.PortType==1)
  38. {
  39. List<int> NewClientDataId1= new List<int>();
  40. List<int> NewClientDataId2 = new List<int>();
  41. string NewClientDataId = "";
  42. if (dto.Userid != 0)
  43. {
  44. string sql = string.Format(@"select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0})", dto.Userid);
  45. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  46. if (ascribedUsers.Count!=0)
  47. {
  48. foreach (var ascribedUser in ascribedUsers)
  49. {
  50. if (ascribedUser.NewClientDataId != 0)
  51. {
  52. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  53. }
  54. }
  55. }
  56. else
  57. {
  58. result = new Result() { Code = -1, Msg = "暂无数据" };
  59. }
  60. }
  61. if (dto.Business != 0)
  62. {
  63. string sql = string.Format(@"select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0})", dto.Business);
  64. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  65. if (AscribedDepartment.Count != 0)
  66. {
  67. foreach (var item in AscribedDepartment)
  68. {
  69. if (item.NewClientDataId!= 0)
  70. {
  71. NewClientDataId2.Add(item.NewClientDataId);
  72. }
  73. }
  74. }
  75. else
  76. {
  77. result = new Result() { Code = -1, Msg = "暂无数据" };
  78. }
  79. }
  80. List<int> intStr=new List<int>();
  81. if (NewClientDataId1.Count!=0 && NewClientDataId2.Count != 0)
  82. {
  83. intStr = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  84. }else if (NewClientDataId1.Count != 0)
  85. {
  86. intStr = NewClientDataId1;
  87. }else if (NewClientDataId2.Count != 0)
  88. {
  89. intStr = NewClientDataId2;
  90. }
  91. foreach (var item in intStr)
  92. {
  93. NewClientDataId += item + ",";
  94. }
  95. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  96. {
  97. NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
  98. }
  99. string sqlWhere = string.Empty;
  100. if (!string.IsNullOrWhiteSpace(dto.Contact))
  101. {
  102. sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
  103. }
  104. if (!string.IsNullOrWhiteSpace(dto.Location))
  105. {
  106. sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
  107. }
  108. if (!string.IsNullOrWhiteSpace(dto.Client))
  109. {
  110. sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
  111. }
  112. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  113. {
  114. sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
  115. }
  116. //if (!string.IsNullOrWhiteSpace(dto.Weight.ToString()))
  117. //{
  118. // sqlWhere += string.Format(@" And Weight )", dto.Weight);
  119. //}
  120. if (dto.Lvlid!=0)
  121. {
  122. sqlWhere += string.Format(@" And s.Lvlid={0}",dto.Lvlid);
  123. }
  124. sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0);
  125. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  126. {
  127. Regex r = new Regex("And");
  128. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  129. }
  130. dto.PageIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
  131. dto.PageSize = dto.PageIndex * dto.PageSize;
  132. 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()
  133. 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
  134. RowNumber between {1} and {2} ", sqlWhere,dto.PageIndex,dto.PageSize);
  135. List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
  136. #region 下拉框初始化数据
  137. //负责人下拉框
  138. List<dynamic> _Users = new List<dynamic>();
  139. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  140. .Where(u => u.CnName == "张海麟" || u.CnName == "安宁" || u.CnName == "李彩娟" || u.CnName == "舒庆" || u.CnName == "李媛媛" && u.IsDel == 0).ToList();
  141. foreach (Sys_Users user in users)
  142. {
  143. var data = new
  144. {
  145. Id = user.Id,
  146. Name = user.CnName
  147. };
  148. _Users.Add(data);
  149. };
  150. //省域数据
  151. List<dynamic> _Province = new List<dynamic>();
  152. List<Sys_SetData> province = _sqlSugar.Queryable<Sys_SetData>()
  153. .Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  154. foreach (Sys_SetData item in province)
  155. {
  156. var data = new
  157. {
  158. Id = item.Id,
  159. Name = item.Name
  160. };
  161. _Province.Add(data);
  162. };
  163. //客户级别数据
  164. List<dynamic> _level = new List<dynamic>();
  165. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  166. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  167. foreach (Sys_SetData item in level)
  168. {
  169. var data = new
  170. {
  171. Id = item.Id,
  172. Name = item.Name
  173. };
  174. _level.Add(data);
  175. };
  176. //客户类别
  177. List<dynamic> _CustomerClass = new List<dynamic>();
  178. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  179. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  180. foreach (Sys_SetData item in CustomerClass)
  181. {
  182. var data = new
  183. {
  184. Id = item.Id,
  185. Name = item.Name
  186. };
  187. _CustomerClass.Add(data);
  188. };
  189. //业务分类
  190. List<dynamic> _ServiceClass = new List<dynamic>();
  191. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  192. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  193. foreach (Sys_SetData item in province)
  194. {
  195. var data = new
  196. {
  197. Id = item.Id,
  198. Name = item.Name
  199. };
  200. _ServiceClass.Add(data);
  201. };
  202. #endregion
  203. if (NewClientDataView.Count!=0)
  204. {
  205. foreach (var item in NewClientDataView)
  206. {
  207. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  208. ("select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId="+ item .Id+ "").ToListAsync();
  209. item.AscribedUser = AscribedUser;
  210. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  211. ("select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId="+item.Id+"").ToListAsync();
  212. item.AscribedDepartment = AscribedDepartment;
  213. }
  214. if (dto.PageSize == 0 && dto.PageIndex == 0)
  215. {
  216. var Data = new
  217. {
  218. ClientTableData= NewClientDataView,
  219. Users = _Users,
  220. Province = _Province,
  221. level = _level,
  222. CustomerClass = _CustomerClass,
  223. ServiceClass = _ServiceClass
  224. };
  225. return result = new Result()
  226. {
  227. Code = 0,
  228. Msg = "查询成功",
  229. Data = Data,
  230. };
  231. }
  232. else
  233. {
  234. int count = NewClientDataView[0].countPage;
  235. float totalPage = (float)count / dto.PageSize;//总页数
  236. if (totalPage == 0) totalPage = 1;
  237. else totalPage = (int)Math.Ceiling((double)totalPage);
  238. List<NewClientDataView> NewClientView = new List<NewClientDataView>();
  239. for (int i = 0; i < dto.PageSize; i++)
  240. {
  241. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  242. if (RowIndex < NewClientDataView.Count)
  243. {
  244. NewClientView.Add(NewClientDataView[RowIndex]);
  245. }
  246. else
  247. {
  248. break;
  249. }
  250. }
  251. var Data = new
  252. {
  253. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientView },
  254. Users = _Users,
  255. Province = _Province,
  256. level = _level,
  257. CustomerClass = _CustomerClass,
  258. ServiceClass = _ServiceClass
  259. };
  260. return result = new Result()
  261. {
  262. Code = 0,
  263. Msg = "查询成功",
  264. Data = Data
  265. };
  266. }
  267. }
  268. else
  269. {
  270. result = new Result() { Code = -1, Msg = "暂无数据!" };
  271. }
  272. }
  273. else if(dto.PortType==2)
  274. {
  275. List<int> NewClientDataId1 = new List<int>();
  276. List<int> NewClientDataId2 = new List<int>();
  277. string NewClientDataId = "";
  278. if (dto.Userid != 0)
  279. {
  280. string sql = string.Format(@"select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0})", dto.Userid);
  281. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  282. if (ascribedUsers.Count != 0)
  283. {
  284. foreach (var ascribedUser in ascribedUsers)
  285. {
  286. if (ascribedUser.NewClientDataId != 0)
  287. {
  288. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  289. }
  290. }
  291. }
  292. else
  293. {
  294. result = new Result() { Code = -1, Msg = "暂无数据" };
  295. }
  296. }
  297. if (dto.Business != 0)
  298. {
  299. string sql = string.Format(@"select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in({0})", dto.Business);
  300. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  301. if (AscribedDepartment.Count != 0)
  302. {
  303. foreach (var item in AscribedDepartment)
  304. {
  305. if (item.NewClientDataId != 0)
  306. {
  307. NewClientDataId2.Add(item.NewClientDataId);
  308. }
  309. }
  310. }
  311. else
  312. {
  313. result = new Result() { Code = -1, Msg = "暂无数据" };
  314. }
  315. }
  316. List<int> intStr = new List<int>();
  317. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  318. {
  319. intStr = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  320. }
  321. else if (NewClientDataId1.Count != 0)
  322. {
  323. intStr = NewClientDataId1;
  324. }
  325. else if (NewClientDataId2.Count != 0)
  326. {
  327. intStr = NewClientDataId2;
  328. }
  329. foreach (var item in intStr)
  330. {
  331. NewClientDataId += item + ",";
  332. }
  333. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  334. {
  335. NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
  336. }
  337. string sqlWhere = string.Empty;
  338. if (!string.IsNullOrWhiteSpace(dto.Contact))
  339. {
  340. sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
  341. }
  342. if (!string.IsNullOrWhiteSpace(dto.Location))
  343. {
  344. sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
  345. }
  346. if (!string.IsNullOrWhiteSpace(dto.Client))
  347. {
  348. sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
  349. }
  350. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  351. {
  352. sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
  353. }
  354. //if (!string.IsNullOrWhiteSpace(dto.Weight.ToString()))
  355. //{
  356. // sqlWhere += string.Format(@" And Weight )", dto.Weight);
  357. //}
  358. if (dto.Lvlid != 0)
  359. {
  360. sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid);
  361. }
  362. sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0);
  363. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  364. {
  365. Regex r = new Regex("And");
  366. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  367. }
  368. dto.PageIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
  369. dto.PageSize = dto.PageIndex * dto.PageSize;
  370. 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()
  371. 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
  372. RowNumber between {1} and {2} ", sqlWhere, dto.PageIndex, dto.PageSize);
  373. List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
  374. if (dto.PageSize == 0 && dto.PageIndex == 0)
  375. {
  376. return result = new Result()
  377. {
  378. Code = 0,
  379. Msg = "查询成功",
  380. Data = NewClientDataView,
  381. };
  382. }
  383. else
  384. {
  385. int count = NewClientDataView[0].countPage;
  386. float totalPage = (float)count / dto.PageSize;//总页数
  387. if (totalPage == 0) totalPage = 1;
  388. else totalPage = (int)Math.Ceiling((double)totalPage);
  389. List<NewClientDataView> NewClientView = new List<NewClientDataView>();
  390. for (int i = 0; i < dto.PageSize; i++)
  391. {
  392. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  393. if (RowIndex < NewClientDataView.Count)
  394. {
  395. NewClientView.Add(NewClientDataView[RowIndex]);
  396. }
  397. else
  398. {
  399. break;
  400. }
  401. }
  402. return result = new Result()
  403. {
  404. Code = 0,
  405. Msg = "查询成功",
  406. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientView },
  407. };
  408. }
  409. }
  410. }
  411. catch (Exception ex)
  412. {
  413. result = new Result() { Code = -2, Msg = "未知错误" };
  414. throw;
  415. }
  416. return result;
  417. }
  418. public async Task<Result> QueryUserSelect()
  419. {
  420. Result result = new Result() { Code = -2, Msg = "未知错误" };
  421. try
  422. {
  423. //负责人下拉框
  424. List<dynamic> _Users = new List<dynamic>();
  425. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  426. .Where(u => u.CnName == "张海麟" || u.CnName == "安宁" || u.CnName == "李彩娟" || u.CnName == "舒庆" || u.CnName == "李媛媛" && u.IsDel == 0).ToList();
  427. foreach (Sys_Users user in users)
  428. {
  429. var data = new
  430. {
  431. Id = user.Id,
  432. Name = user.CnName
  433. };
  434. _Users.Add(data);
  435. };
  436. if (_Users.Count==0)
  437. {
  438. result = new Result() { Code = -1, Msg = "暂无数据" };
  439. }
  440. result = new Result() { Code = 0, Msg = "查询成功!",Data=_Users };
  441. }
  442. catch (Exception)
  443. {
  444. result = new Result() { Code = -2, Msg = "未知错误" };
  445. throw;
  446. }
  447. return result;
  448. }
  449. public async Task<Result> NewClientOp(NewClientOpDto dto)
  450. {
  451. Result result = new Result() { Code = -2, Msg = "未知错误" };
  452. try
  453. {
  454. if (dto.Status == 1)//添加
  455. {
  456. string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' Contact='{1}' And IsDel={2}"
  457. , dto.Client,dto.Contact,0);
  458. var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
  459. if (NewClientData != null)
  460. {
  461. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  462. }
  463. else//不存在,可添加
  464. {
  465. Crm_NewClientData _NewClientData = _mapper.Map<Crm_NewClientData>(dto);
  466. int id = await AddAsyncReturnId(_NewClientData);
  467. if (id == 0)
  468. {
  469. return result = new Result() { Code = -1, Msg = "添加失败!" };
  470. }
  471. return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  472. }
  473. }
  474. else if (dto.Status == 2)//修改
  475. {
  476. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Crm_NewClientData
  477. {
  478. Number=dto.Number,
  479. Lvlid= dto.Lvlid,
  480. Client= dto.Client,
  481. Weight= dto.Weight,
  482. ClientShort= dto.Clientshort,
  483. Contact= dto.Contact,
  484. Gender= dto.Gender,
  485. Passport= dto.Passport,
  486. PassportDate= dto.PassportDate,
  487. Job= dto.Job,
  488. Telephone= dto.Telephone,
  489. Phone= dto.Phone,
  490. Email= dto.Email,
  491. Location= dto.Location,
  492. Address= dto.Address,
  493. Birthday= dto.Birthday,
  494. OtherInfo= dto.Otherinfo,
  495. Wechat= dto.Wechat,
  496. Category= dto.Category,
  497. PreDele= dto.Predele,
  498. FinlishedDele= dto.FinlishedDele,
  499. Remark= dto.Remark,
  500. });
  501. if (!res)
  502. {
  503. return result = new Result() { Code = -1, Msg = "修改失败!" };
  504. }
  505. return result = new Result() { Code = 0, Msg = "修改成功!" };
  506. }
  507. else
  508. {
  509. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  510. }
  511. }
  512. catch (Exception)
  513. {
  514. result = new Result() { Code = -2, Msg = "未知错误" };
  515. throw;
  516. }
  517. return result;
  518. }
  519. }
  520. }