NewClientDataRepository.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. int pIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
  131. int pSize = 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, pIndex, pSize);
  135. List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
  136. foreach (var item in NewClientDataView)
  137. {
  138. Sys_SetData CategoryStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Category);
  139. item.CategoryStr = CategoryStr.Name;
  140. Sys_SetData lvlStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Lvlid);
  141. item.LvlidStr = lvlStr.Name;
  142. }
  143. #region 下拉框初始化数据
  144. //负责人下拉框
  145. List<dynamic> _Users = new List<dynamic>();
  146. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  147. .Where(u => u.CnName == "张海麟" || u.CnName == "安宁" || u.CnName == "李彩娟" || u.CnName == "舒庆" || u.CnName == "李媛媛" && u.IsDel == 0).ToList();
  148. foreach (Sys_Users user in users)
  149. {
  150. var data = new
  151. {
  152. Id = user.Id,
  153. Name = user.CnName
  154. };
  155. _Users.Add(data);
  156. };
  157. //省域数据
  158. List<dynamic> _Province = new List<dynamic>();
  159. List<Sys_SetData> province = _sqlSugar.Queryable<Sys_SetData>()
  160. .Where(u => u.STid == 42 && u.IsDel == 0).ToList();
  161. foreach (Sys_SetData item in province)
  162. {
  163. var data = new
  164. {
  165. Id = item.Id,
  166. Name = item.Name
  167. };
  168. _Province.Add(data);
  169. };
  170. //客户级别数据
  171. List<dynamic> _level = new List<dynamic>();
  172. List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
  173. .Where(u => u.STid == 33 && u.IsDel == 0).ToList();
  174. foreach (Sys_SetData item in level)
  175. {
  176. var data = new
  177. {
  178. Id = item.Id,
  179. Name = item.Name
  180. };
  181. _level.Add(data);
  182. };
  183. //客户类别
  184. List<dynamic> _CustomerClass = new List<dynamic>();
  185. List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
  186. .Where(u => u.STid == 37 && u.IsDel == 0).ToList();
  187. foreach (Sys_SetData item in CustomerClass)
  188. {
  189. var data = new
  190. {
  191. Id = item.Id,
  192. Name = item.Name
  193. };
  194. _CustomerClass.Add(data);
  195. };
  196. //业务分类
  197. List<dynamic> _ServiceClass = new List<dynamic>();
  198. List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
  199. .Where(u => u.STid == 36 && u.IsDel == 0).ToList();
  200. foreach (Sys_SetData item in province)
  201. {
  202. var data = new
  203. {
  204. Id = item.Id,
  205. Name = item.Name
  206. };
  207. _ServiceClass.Add(data);
  208. };
  209. #endregion
  210. if (NewClientDataView.Count!=0)
  211. {
  212. foreach (var item in NewClientDataView)
  213. {
  214. List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
  215. ("select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId="+ item .Id+ "").ToListAsync();
  216. item.AscribedUser = AscribedUser;
  217. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
  218. ("select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId="+item.Id+"").ToListAsync();
  219. item.AscribedDepartment = AscribedDepartment;
  220. }
  221. int count = NewClientDataView[0].countPage;
  222. float totalPage = (float)count / dto.PageSize;//总页数
  223. if (totalPage == 0) totalPage = 1;
  224. else totalPage = (int)Math.Ceiling((double)totalPage);
  225. var Data = new
  226. {
  227. ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  228. Users = _Users,
  229. Province = _Province,
  230. level = _level,
  231. CustomerClass = _CustomerClass,
  232. ServiceClass = _ServiceClass
  233. };
  234. return result = new Result()
  235. {
  236. Code = 0,
  237. Msg = "查询成功",
  238. Data = Data
  239. };
  240. }
  241. else
  242. {
  243. result = new Result() { Code = -1, Msg = "暂无数据!" };
  244. }
  245. }
  246. else if(dto.PortType==2)
  247. {
  248. List<int> NewClientDataId1 = new List<int>();
  249. List<int> NewClientDataId2 = new List<int>();
  250. string NewClientDataId = "";
  251. if (dto.Userid != 0)
  252. {
  253. 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);
  254. List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(sql).ToListAsync();
  255. if (ascribedUsers.Count != 0)
  256. {
  257. foreach (var ascribedUser in ascribedUsers)
  258. {
  259. if (ascribedUser.NewClientDataId != 0)
  260. {
  261. NewClientDataId1.Add(ascribedUser.NewClientDataId);
  262. }
  263. }
  264. }
  265. else
  266. {
  267. result = new Result() { Code = -1, Msg = "暂无数据" };
  268. }
  269. }
  270. if (dto.Business != 0)
  271. {
  272. 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);
  273. List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(sql).ToListAsync();
  274. if (AscribedDepartment.Count != 0)
  275. {
  276. foreach (var item in AscribedDepartment)
  277. {
  278. if (item.NewClientDataId != 0)
  279. {
  280. NewClientDataId2.Add(item.NewClientDataId);
  281. }
  282. }
  283. }
  284. else
  285. {
  286. result = new Result() { Code = -1, Msg = "暂无数据" };
  287. }
  288. }
  289. List<int> intStr = new List<int>();
  290. if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
  291. {
  292. intStr = NewClientDataId1.Intersect(NewClientDataId2).ToList();
  293. }
  294. else if (NewClientDataId1.Count != 0)
  295. {
  296. intStr = NewClientDataId1;
  297. }
  298. else if (NewClientDataId2.Count != 0)
  299. {
  300. intStr = NewClientDataId2;
  301. }
  302. foreach (var item in intStr)
  303. {
  304. NewClientDataId += item + ",";
  305. }
  306. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  307. {
  308. NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
  309. }
  310. string sqlWhere = string.Empty;
  311. if (!string.IsNullOrWhiteSpace(dto.Contact))
  312. {
  313. sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
  314. }
  315. if (!string.IsNullOrWhiteSpace(dto.Location))
  316. {
  317. sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
  318. }
  319. if (!string.IsNullOrWhiteSpace(dto.Client))
  320. {
  321. sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
  322. }
  323. if (!string.IsNullOrWhiteSpace(NewClientDataId))
  324. {
  325. sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
  326. }
  327. //if (!string.IsNullOrWhiteSpace(dto.Weight.ToString()))
  328. //{
  329. // sqlWhere += string.Format(@" And Weight )", dto.Weight);
  330. //}
  331. if (dto.Lvlid != 0)
  332. {
  333. sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid);
  334. }
  335. sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0);
  336. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  337. {
  338. Regex r = new Regex("And");
  339. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  340. }
  341. int pIndex= dto.PageIndex * dto.PageSize - dto.PageSize + 1;
  342. int pSize = dto.PageIndex * dto.PageSize;
  343. 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()
  344. 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
  345. RowNumber between {1} and {2} ", sqlWhere, pIndex, pSize);
  346. List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
  347. foreach (var item in NewClientDataView)
  348. {
  349. Sys_SetData CategoryStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Category);
  350. item.CategoryStr = CategoryStr.Name;
  351. Sys_SetData lvlStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Lvlid);
  352. item.LvlidStr = lvlStr.Name;
  353. }
  354. int count = NewClientDataView[0].countPage;
  355. float totalPage = (float)count / dto.PageSize;//总页数
  356. if (totalPage == 0) totalPage = 1;
  357. else totalPage = (int)Math.Ceiling((double)totalPage);
  358. result = new Result()
  359. {
  360. Code = 0,
  361. Msg = "查询成功",
  362. Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
  363. };
  364. }
  365. }
  366. catch (Exception ex)
  367. {
  368. result = new Result() { Code = -2, Msg = "未知错误" };
  369. throw;
  370. }
  371. return result;
  372. }
  373. public async Task<Result> QueryUserSelect()
  374. {
  375. Result result = new Result() { Code = -2, Msg = "未知错误" };
  376. try
  377. {
  378. //负责人下拉框
  379. List<dynamic> _Users = new List<dynamic>();
  380. List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
  381. .Where(u => u.CnName == "张海麟" || u.CnName == "安宁" || u.CnName == "李彩娟" || u.CnName == "舒庆" || u.CnName == "李媛媛" && u.IsDel == 0).ToList();
  382. foreach (Sys_Users user in users)
  383. {
  384. var data = new
  385. {
  386. Id = user.Id,
  387. Name = user.CnName
  388. };
  389. _Users.Add(data);
  390. };
  391. if (_Users.Count==0)
  392. {
  393. result = new Result() { Code = -1, Msg = "暂无数据" };
  394. }
  395. result = new Result() { Code = 0, Msg = "查询成功!",Data=_Users };
  396. }
  397. catch (Exception)
  398. {
  399. result = new Result() { Code = -2, Msg = "未知错误" };
  400. throw;
  401. }
  402. return result;
  403. }
  404. public async Task<Result> NewClientOp(NewClientOpDto dto)
  405. {
  406. Result result = new Result() { Code = -2, Msg = "未知错误" };
  407. try
  408. {
  409. if (dto.Status == 1)//添加
  410. {
  411. string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' and Contact='{1}' And IsDel={2}"
  412. , dto.Client,dto.Contact,0);
  413. var NewClientData = await _sqlSugar.SqlQueryable<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
  414. if (NewClientData != null)
  415. {
  416. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  417. }
  418. else//不存在,可添加
  419. {
  420. Crm_NewClientData _NewClientData = _mapper.Map<Crm_NewClientData>(dto);
  421. int id = await AddAsyncReturnId(_NewClientData);
  422. if (id == 0)
  423. {
  424. return result = new Result() { Code = -1, Msg = "添加失败!" };
  425. }
  426. return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  427. }
  428. }
  429. else if (dto.Status == 2)//修改
  430. {
  431. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Crm_NewClientData
  432. {
  433. Number=dto.Number,
  434. Lvlid= dto.Lvlid,
  435. Client= dto.Client,
  436. Weight= dto.Weight,
  437. ClientShort= dto.Clientshort,
  438. Contact= dto.Contact,
  439. Gender= dto.Gender,
  440. Passport= dto.Passport,
  441. PassportDate= dto.PassportDate,
  442. Job= dto.Job,
  443. Telephone= dto.Telephone,
  444. Phone= dto.Phone,
  445. Email= dto.Email,
  446. Location= dto.Location,
  447. Address= dto.Address,
  448. Birthday= dto.Birthday,
  449. OtherInfo= dto.Otherinfo,
  450. Wechat= dto.Wechat,
  451. Category= dto.Category,
  452. PreDele= dto.Predele,
  453. FinlishedDele= dto.FinlishedDele,
  454. Remark= dto.Remark,
  455. });
  456. if (!res)
  457. {
  458. return result = new Result() { Code = -1, Msg = "修改失败!" };
  459. }
  460. return result = new Result() { Code = 0, Msg = "修改成功!" };
  461. }
  462. else
  463. {
  464. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  465. }
  466. }
  467. catch (Exception)
  468. {
  469. result = new Result() { Code = -2, Msg = "未知错误" };
  470. throw;
  471. }
  472. return result;
  473. }
  474. }
  475. }