NewClientDataRepository.cs 40 KB

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