NewClientDataRepository.cs 33 KB

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