NewClientDataRepository.cs 38 KB

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