NewClientDataRepository.cs 42 KB

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