EnterExitCostQuoteRepository.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. using AutoMapper;
  2. using EyeSoft.Collections.Generic;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.ViewModels.Groups;
  7. using OASystem.Infrastructure.Tools;
  8. using Org.BouncyCastle.Utilities;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace OASystem.Infrastructure.Repositories.Groups
  15. {
  16. /// <summary>
  17. /// 团组-出入境费用报价表 仓储
  18. /// </summary>
  19. public class EnterExitCostQuoteRepository : BaseRepository<Grp_EnterExitCostQuoteItem, EnterExitCostQuoteView>
  20. {
  21. private readonly IMapper _mapper;
  22. public EnterExitCostQuoteRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  23. {
  24. _mapper = mapper;
  25. }
  26. /// <summary>
  27. /// 初始化基础项
  28. /// </summary>
  29. /// <param name="removeNl">是否移除换行符</param>
  30. /// <returns></returns>
  31. public async Task<List<InitBasicItemView>> InitBasicItemAsync(bool removeNl)
  32. {
  33. var origList = await _sqlSugar.Queryable<Sys_SetData>()
  34. .Where(x => x.IsDel == 0 && x.STid == 105)
  35. .Select(x => new
  36. {
  37. Id = x.Id,
  38. Name = x.Name,
  39. Index = x.Remark ?? "-1"
  40. }).ToListAsync();
  41. var newList = origList.Select(x => new InitBasicItemView
  42. {
  43. Id = x.Id,
  44. Name = removeNl ? x.Name.Replace("\\r\\n", "") : x.Name,
  45. Index = int.TryParse(x.Index, out int index) ? index : -1
  46. })
  47. .OrderBy(x => x.Index)
  48. .ToList();
  49. var onlyItems = new List<int>() {
  50. 1358, //邀请函发放对象
  51. 1360, //邀请函发放时间
  52. };
  53. newList.ForEach(x =>
  54. {
  55. if (onlyItems.Contains(x.Id)) x.IsOnlyRemark = true;
  56. });
  57. return newList;
  58. }
  59. /// <summary>
  60. /// 获取报价名称列表
  61. /// </summary>
  62. /// <param name="name"></param>
  63. /// <param name="pageIndex"></param>
  64. /// <param name="pageSize"></param>
  65. /// <returns></returns>
  66. public async Task<JsonView> QuoteNameListAsync(EnterExitCostQuoteNameListDto dto)
  67. {
  68. int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
  69. string name = dto.Name;
  70. if (pageIndex < 1 || pageSize < 1)
  71. {
  72. var noPageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  73. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))
  74. .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))
  75. .Select(x => new EnterExitCostQuoteNameView
  76. {
  77. Id = x.Id,
  78. Name = x.Name
  79. }).ToListAsync();
  80. return new JsonView
  81. {
  82. Code = StatusCodes.Status200OK,
  83. Msg = "获取成功",
  84. Count = noPageList.Count,
  85. Data = noPageList
  86. };
  87. }
  88. RefAsync<int> total = 0;
  89. var pageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  90. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))
  91. .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))
  92. .Select(x => new EnterExitCostQuoteNameView
  93. {
  94. Id = x.Id,
  95. Name = x.Name
  96. }).ToPageListAsync(pageIndex, pageSize, total);
  97. return new JsonView
  98. {
  99. Code = StatusCodes.Status200OK,
  100. Msg = "获取成功",
  101. Count = total,
  102. Data = pageList
  103. };
  104. }
  105. /// <summary>
  106. /// 获取团组名称列表
  107. /// </summary>
  108. /// <param name="name"></param>
  109. /// <param name="pageIndex"></param>
  110. /// <param name="pageSize"></param>
  111. /// <returns></returns>
  112. public async Task<JsonView> GroupNameListAsync(EnterExitCostQuoteGroupNameListDto dto)
  113. {
  114. int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
  115. string name = dto.Name;
  116. if (pageIndex < 1 || pageSize < 1)
  117. {
  118. var noPageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  119. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))
  120. .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))
  121. .OrderByDescending(x => x.CreateTime)
  122. .Select(x => new EnterExitCostQuoteGroupNameView
  123. {
  124. Id = x.Id,
  125. Name = x.TeamName
  126. }).ToListAsync();
  127. return new JsonView
  128. {
  129. Code = StatusCodes.Status200OK,
  130. Msg = "获取成功",
  131. Count = noPageList.Count,
  132. Data = noPageList
  133. };
  134. }
  135. RefAsync<int> total = 0;
  136. var pageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  137. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))
  138. .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))
  139. .OrderByDescending(x => x.CreateTime)
  140. .Select(x => new EnterExitCostQuoteGroupNameView
  141. {
  142. Id = x.Id,
  143. Name = x.TeamName
  144. }).ToPageListAsync(pageIndex, pageSize, total);
  145. return new JsonView
  146. {
  147. Code = StatusCodes.Status200OK,
  148. Msg = "获取成功",
  149. Count = total,
  150. Data = pageList
  151. };
  152. }
  153. /// <summary>
  154. /// 获取报价详情
  155. /// </summary>
  156. /// <param name="name"></param>
  157. /// <param name="pageIndex"></param>
  158. /// <param name="pageSize"></param>
  159. /// <returns></returns>
  160. public async Task<EnterExitCostQuoteInfoView> InfoAsync(EnterExitCostQuoteInfoDto dto)
  161. {
  162. int quoteId = dto.Id;
  163. var viewInfo = new EnterExitCostQuoteInfoView();
  164. viewInfo.Id = quoteId;
  165. var basicItems = await InitBasicItemAsync(true);
  166. if (basicItems.Any())
  167. {
  168. var quoteItemInfos = new List<QuoteItemInfo>();
  169. basicItems.ForEach(x =>
  170. {
  171. var quoteSubItemInfos = new List<QuoteSubItemInfo>();
  172. if (x.IsOnlyRemark)
  173. {
  174. quoteSubItemInfos.Add(new QuoteSubItemInfo
  175. {
  176. Id = 0,
  177. ItemId = x.Id,
  178. Index = 1,
  179. FeeName = "",
  180. UnitPrice = 0.00M,
  181. Currency = "CNY",
  182. Quantity = 1.00M,
  183. PplNum = 1,
  184. TotalAmt = 0.00M,
  185. Remark = $"-"
  186. });
  187. }
  188. quoteItemInfos.Add(new QuoteItemInfo
  189. {
  190. QuoteId = quoteId,
  191. ItemId = x.Id,
  192. ItemName = x.Name,
  193. IsOnlyRemark = x.IsOnlyRemark,
  194. Infos = quoteSubItemInfos.ToArray(),
  195. Index = x.Index
  196. });
  197. });
  198. viewInfo.FeeItems = quoteItemInfos.OrderBy(x => x.Index).ToArray();
  199. }
  200. var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  201. .Where(x => x.IsDel == 0 && x.Id == quoteId)
  202. .FirstAsync();
  203. if (quoteInfo != null)
  204. {
  205. viewInfo.Id = quoteInfo.Id;
  206. viewInfo.Name = quoteInfo.Name;
  207. viewInfo.GroupId = quoteInfo.GroupId;
  208. viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();
  209. var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()
  210. .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)
  211. .Select(x => new QuoteSubItemInfo
  212. {
  213. Id = x.Id,
  214. ItemId = x.ItemId,
  215. FeeName = x.FeeName,
  216. UnitPrice = x.UnitPrice,
  217. Quantity = x.Quantity,
  218. PplNum = x.PplNum,
  219. Currency = x.Currency,
  220. TotalAmt = x.TotalAmt,
  221. Index = x.Index,
  222. Remark = x.Remark
  223. }).ToListAsync();
  224. if (quoteItems.Any())
  225. {
  226. foreach (var x in viewInfo.FeeItems)
  227. {
  228. var currQuoteInfos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray();
  229. if (x.IsOnlyRemark)
  230. {
  231. if (currQuoteInfos.Length > 0)
  232. {
  233. x.Infos = currQuoteInfos;
  234. }
  235. }
  236. else x.Infos = currQuoteInfos;
  237. }
  238. }
  239. }
  240. return viewInfo;
  241. }
  242. /// <summary>
  243. /// 获取报价详情 By GroupId
  244. /// </summary>
  245. /// <param name="name"></param>
  246. /// <param name="pageIndex"></param>
  247. /// <param name="pageSize"></param>
  248. /// <returns></returns>
  249. public async Task<EnterExitCostQuoteInfoView> InfoByGroupIdAsync(int groupId)
  250. {
  251. var viewInfo = new EnterExitCostQuoteInfoView();
  252. var basicItems = await InitBasicItemAsync(true);
  253. if (basicItems.Any())
  254. {
  255. var quoteItemInfos = new List<QuoteItemInfo>();
  256. basicItems.ForEach(x =>
  257. {
  258. var quoteSubItemInfos = new List<QuoteSubItemInfo>();
  259. if (x.IsOnlyRemark)
  260. {
  261. quoteSubItemInfos.Add(new QuoteSubItemInfo
  262. {
  263. Id = 0,
  264. ItemId = x.Id,
  265. Index = 1,
  266. FeeName = "",
  267. UnitPrice = 0.00M,
  268. Currency = "CNY",
  269. Quantity = 1.00M,
  270. PplNum = 1,
  271. TotalAmt = 0.00M,
  272. Remark = $"-"
  273. });
  274. }
  275. quoteItemInfos.Add(new QuoteItemInfo
  276. {
  277. QuoteId = 0,
  278. ItemId = x.Id,
  279. ItemName = x.Name,
  280. IsOnlyRemark = x.IsOnlyRemark,
  281. Infos = quoteSubItemInfos.ToArray(),
  282. Index = x.Index
  283. });
  284. });
  285. viewInfo.FeeItems = quoteItemInfos.OrderBy(x => x.Index).ToArray();
  286. }
  287. var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  288. .Where(x => x.IsDel == 0 && x.GroupId == groupId)
  289. .FirstAsync();
  290. if (quoteInfo != null)
  291. {
  292. viewInfo.Id = quoteInfo.Id;
  293. viewInfo.Name = quoteInfo.Name;
  294. viewInfo.GroupId = quoteInfo.GroupId;
  295. viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();
  296. var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()
  297. .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)
  298. .Select(x => new QuoteSubItemInfo
  299. {
  300. Id = x.Id,
  301. ItemId = x.ItemId,
  302. FeeName = x.FeeName,
  303. UnitPrice = x.UnitPrice,
  304. Quantity = x.Quantity,
  305. PplNum = x.PplNum,
  306. Currency = x.Currency,
  307. TotalAmt = x.TotalAmt,
  308. Index = x.Index
  309. }).ToListAsync();
  310. if (quoteItems.Any())
  311. {
  312. //viewInfo.FeeItems = viewInfo.FeeItems.Select(x => new QuoteItemInfo { Index = x.Index, QuoteId = quoteInfo.Id, ItemId = x.ItemId, ItemName = x.ItemName, Infos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray() }).ToArray();
  313. viewInfo.FeeItems.ForEach(x =>
  314. {
  315. x.QuoteId = quoteInfo.Id;
  316. x.Infos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray(); ;
  317. });
  318. }
  319. }
  320. return viewInfo;
  321. }
  322. /// <summary>
  323. /// Add Or Edit
  324. /// </summary>
  325. /// <param name="name"></param>
  326. /// <param name="pageIndex"></param>
  327. /// <param name="pageSize"></param>
  328. /// <returns></returns>
  329. public async Task<JsonView> OpAsync(EnterExitCostQuoteOpDto dto)
  330. {
  331. var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };
  332. string quoteName = dto.Name;
  333. int quoteId = dto.Id;
  334. if (string.IsNullOrEmpty(dto.Name))
  335. {
  336. jw.Msg = "报价名称不能为空";
  337. return jw;
  338. }
  339. var quoteInfo = new Grp_EnterExitCostQuote
  340. {
  341. Id = quoteId,
  342. Name = quoteName,
  343. GroupId = dto.GroupId,
  344. CurrencyRemark = CommonFun.GetCurrencyChinaToString(dto.Rates.ToList()),
  345. CreateUserId = dto.CurrUserId
  346. };
  347. var quoteItemInfos = new List<Grp_EnterExitCostQuoteItem>();
  348. if (dto.FeeItems.Any())
  349. {
  350. foreach (var item in dto.FeeItems)
  351. {
  352. if (item.Infos.Any())
  353. {
  354. quoteItemInfos.AddRange(item.Infos.Select(x => new Grp_EnterExitCostQuoteItem
  355. {
  356. Id = x.Id,
  357. QuoteId = quoteId,
  358. ItemId = x.ItemId,
  359. Index = x.Index,
  360. FeeName = x.FeeName,
  361. UnitPrice = x.UnitPrice,
  362. Currency = x.Currency,
  363. Quantity = x.Quantity,
  364. PplNum = x.PplNum,
  365. TotalAmt = x.TotalAmt,
  366. Remark = x.Remark,
  367. CreateUserId = dto.CurrUserId
  368. }));
  369. }
  370. }
  371. }
  372. var isNull = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>().FirstAsync(x => x.IsDel == 0 && x.Id == quoteId) == null ? true : false;
  373. _sqlSugar.BeginTran();
  374. if (isNull) //新增
  375. {
  376. quoteId = await _sqlSugar.Insertable(quoteInfo).ExecuteReturnIdentityAsync();
  377. if (quoteId < 1)
  378. {
  379. jw.Msg = "新增失败!";
  380. _sqlSugar.RollbackTran();
  381. return jw;
  382. }
  383. quoteItemInfos.ForEach(x => x.QuoteId = quoteId);
  384. var quoteItemIds = await _sqlSugar.Insertable(quoteItemInfos).ExecuteReturnIdentityAsync();
  385. if (quoteItemIds < 1)
  386. {
  387. jw.Msg = "新增失败";
  388. _sqlSugar.RollbackTran();
  389. return jw;
  390. }
  391. jw.Msg = "新增成功!";
  392. }
  393. else if (!isNull) //编辑
  394. {
  395. var quoteUpd = await _sqlSugar.Updateable(quoteInfo)
  396. .IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel })
  397. .ExecuteCommandAsync();
  398. if (quoteUpd < 1)
  399. {
  400. jw.Msg = "编辑失败!";
  401. _sqlSugar.RollbackTran();
  402. return jw;
  403. }
  404. var insertItems = quoteItemInfos.Where(x => x.Id < 1).ToList();
  405. var updItems = quoteItemInfos.Where(x => x.Id > 0).ToList();
  406. if (insertItems.Any())
  407. {
  408. foreach (var item in insertItems) {
  409. var insertInfo = await _sqlSugar.Insertable(item).ExecuteReturnEntityAsync();
  410. if (insertInfo == null)
  411. {
  412. jw.Msg = "编辑失败!";
  413. _sqlSugar.RollbackTran();
  414. return jw;
  415. }
  416. quoteItemInfos.Add(insertInfo);
  417. }
  418. }
  419. if (updItems.Any())
  420. {
  421. var updItem = await _sqlSugar.Updateable(updItems).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
  422. if (updItem < 1)
  423. {
  424. jw.Msg = "编辑失败!";
  425. _sqlSugar.RollbackTran();
  426. return jw;
  427. }
  428. }
  429. //验证及处理前台删除项数据
  430. var perDelItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()
  431. .Where(x => x.IsDel == 0 && x.QuoteId == quoteId)
  432. .ToListAsync();
  433. if (perDelItems.Any())
  434. {
  435. var delItems = perDelItems.Where(x => !quoteItemInfos.Select(y => y.Id).Contains(x.Id)).ToList();
  436. if (delItems.Any())
  437. {
  438. var newDelItems = delItems.Select(x =>
  439. new Grp_EnterExitCostQuoteItem
  440. {
  441. Id = x.Id,
  442. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  443. DeleteUserId = dto.CurrUserId,
  444. IsDel = 1
  445. }).ToList();
  446. var delItemStatus = await _sqlSugar.Updateable(newDelItems)
  447. .UpdateColumns(x => new { x.DeleteTime, x.DeleteUserId, x.IsDel })
  448. .ExecuteCommandAsync();
  449. if (delItemStatus < 1)
  450. {
  451. jw.Msg = "移除项费用失败!";
  452. _sqlSugar.RollbackTran();
  453. return jw;
  454. }
  455. }
  456. }
  457. jw.Msg = "编辑成功!";
  458. }
  459. _sqlSugar.CommitTran();
  460. jw.Code = StatusCodes.Status200OK;
  461. jw.Data = quoteId;
  462. return jw;
  463. }
  464. /// <summary>
  465. /// ItemDel
  466. /// </summary>
  467. /// <param name="name"></param>
  468. /// <param name="pageIndex"></param>
  469. /// <param name="pageSize"></param>
  470. /// <returns></returns>
  471. public async Task<JsonView> ItemDelAsync(EnterExitCostQuoteItemDelDto dto)
  472. {
  473. var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };
  474. int currUserId = dto.CurrUserId,
  475. id = dto.Id;
  476. if (currUserId < 1)
  477. {
  478. jw.Msg = MsgTips.UserId;
  479. return jw;
  480. }
  481. if (id < 1)
  482. {
  483. jw.Msg = MsgTips.Id;
  484. return jw;
  485. }
  486. var info = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().FirstAsync(x => x.IsDel == 0 && x.Id == id);
  487. if (info == null)
  488. {
  489. jw.Msg = $"操作成功!";
  490. jw.Code = StatusCodes.Status200OK;
  491. return jw;
  492. }
  493. var itemInfos = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().Where(x => x.IsDel == 0 && x.QuoteId == info.QuoteId && x.ItemId == info.ItemId).ToListAsync();
  494. _sqlSugar.BeginTran();
  495. var delInfo = new Grp_EnterExitCostQuoteItem { DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DeleteUserId = currUserId, IsDel = 1 };
  496. var del = await _sqlSugar.Updateable(delInfo)
  497. .UpdateColumns(x => new { x.DeleteTime, x.DeleteUserId, x.IsDel })
  498. .Where(x => x.Id == id)
  499. .ExecuteCommandAsync();
  500. if (del < 1)
  501. {
  502. jw.Msg = $"删除失败!";
  503. _sqlSugar.RollbackTran();
  504. return jw;
  505. }
  506. if (itemInfos.Any())
  507. {
  508. // 更新剩余项的索引
  509. var itemsToUpdate = itemInfos.Where(i => i.Index > itemInfos.First(i => i.Id == id).Index).ToList();
  510. foreach (var item in itemsToUpdate)
  511. {
  512. item.Index -= 1;
  513. }
  514. await _sqlSugar.Updateable(itemsToUpdate).ExecuteCommandAsync();
  515. }
  516. _sqlSugar.CommitTran();
  517. jw.Msg = "操作成功!";
  518. jw.Code = StatusCodes.Status200OK;
  519. return jw;
  520. }
  521. }
  522. }