NewClientDataRepository.cs 41 KB

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