NewClientDataRepository.cs 42 KB

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