NewClientDataRepository.cs 25 KB

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