NewClientDataRepository.cs 41 KB

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