NewClientDataRepository.cs 30 KB

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