EnterExitCostQuoteRepository.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. return newList;
  50. }
  51. /// <summary>
  52. /// 获取报价名称列表
  53. /// </summary>
  54. /// <param name="name"></param>
  55. /// <param name="pageIndex"></param>
  56. /// <param name="pageSize"></param>
  57. /// <returns></returns>
  58. public async Task<JsonView> QuoteNameListAsync(EnterExitCostQuoteNameListDto dto)
  59. {
  60. int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
  61. string name = dto.Name;
  62. if (pageIndex < 1 || pageSize < 1)
  63. {
  64. var noPageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  65. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))
  66. .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))
  67. .Select(x => new EnterExitCostQuoteNameView
  68. {
  69. Id = x.Id,
  70. Name = x.Name
  71. }).ToListAsync();
  72. return new JsonView
  73. {
  74. Code = StatusCodes.Status200OK,
  75. Msg = "获取成功",
  76. Count = noPageList.Count,
  77. Data = noPageList
  78. };
  79. }
  80. RefAsync<int> total = 0;
  81. var pageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  82. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))
  83. .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))
  84. .Select(x => new EnterExitCostQuoteNameView
  85. {
  86. Id = x.Id,
  87. Name = x.Name
  88. }).ToPageListAsync(pageIndex, pageSize, total);
  89. return new JsonView
  90. {
  91. Code = StatusCodes.Status200OK,
  92. Msg = "获取成功",
  93. Count = total,
  94. Data = pageList
  95. };
  96. }
  97. /// <summary>
  98. /// 获取团组名称列表
  99. /// </summary>
  100. /// <param name="name"></param>
  101. /// <param name="pageIndex"></param>
  102. /// <param name="pageSize"></param>
  103. /// <returns></returns>
  104. public async Task<JsonView> GroupNameListAsync(EnterExitCostQuoteGroupNameListDto dto)
  105. {
  106. int pageIndex = dto.PageIndex, pageSize = dto.PageSize;
  107. string name = dto.Name;
  108. if (pageIndex < 1 || pageSize < 1)
  109. {
  110. var noPageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  111. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))
  112. .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))
  113. .OrderByDescending(x => x.CreateTime)
  114. .Select(x => new EnterExitCostQuoteGroupNameView
  115. {
  116. Id = x.Id,
  117. Name = x.TeamName
  118. }).ToListAsync();
  119. return new JsonView
  120. {
  121. Code = StatusCodes.Status200OK,
  122. Msg = "获取成功",
  123. Count = noPageList.Count,
  124. Data = noPageList
  125. };
  126. }
  127. RefAsync<int> total = 0;
  128. var pageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  129. .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))
  130. .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))
  131. .OrderByDescending(x => x.CreateTime)
  132. .Select(x => new EnterExitCostQuoteGroupNameView
  133. {
  134. Id = x.Id,
  135. Name = x.TeamName
  136. }).ToPageListAsync(pageIndex, pageSize, total);
  137. return new JsonView
  138. {
  139. Code = StatusCodes.Status200OK,
  140. Msg = "获取成功",
  141. Count = total,
  142. Data = pageList
  143. };
  144. }
  145. /// <summary>
  146. /// 获取报价详情
  147. /// </summary>
  148. /// <param name="name"></param>
  149. /// <param name="pageIndex"></param>
  150. /// <param name="pageSize"></param>
  151. /// <returns></returns>
  152. public async Task<EnterExitCostQuoteInfoView> InfoAsync(EnterExitCostQuoteInfoDto dto)
  153. {
  154. int quoteId = dto.Id;
  155. var viewInfo = new EnterExitCostQuoteInfoView();
  156. viewInfo.Id = quoteId;
  157. var basicItems = await InitBasicItemAsync(true);
  158. if (basicItems.Any())
  159. {
  160. viewInfo.FeeItems = basicItems.Select(x => new QuoteItemInfo { ItemId = x.Id, ItemName = x.Name, Index = x.Index }).OrderBy(x => x.Index).ToArray();
  161. }
  162. var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  163. .Where(x => x.IsDel == 0 && x.Id == quoteId)
  164. .FirstAsync();
  165. if (quoteInfo != null)
  166. {
  167. viewInfo.Id = quoteInfo.Id;
  168. viewInfo.Name = quoteInfo.Name;
  169. viewInfo.GroupId = quoteInfo.GroupId;
  170. viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();
  171. var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()
  172. .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)
  173. .Select(x => new QuoteSubItemInfo
  174. {
  175. Id = x.Id,
  176. ItemId = x.ItemId,
  177. FeeName = x.FeeName,
  178. UnitPrice = x.UnitPrice,
  179. Quantity = x.Quantity,
  180. PplNum = x.PplNum,
  181. Currency = x.Currency,
  182. TotalAmt = x.TotalAmt,
  183. Index = x.Index
  184. }).ToListAsync();
  185. if (quoteItems.Any())
  186. {
  187. viewInfo.FeeItems = viewInfo.FeeItems.Select(x => new QuoteItemInfo { Index = x.Index, QuoteId = dto.Id, ItemId = x.ItemId, ItemName = x.ItemName, Infos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray() }).ToArray();
  188. }
  189. }
  190. return viewInfo;
  191. }
  192. /// <summary>
  193. /// 获取报价详情 By GroupId
  194. /// </summary>
  195. /// <param name="name"></param>
  196. /// <param name="pageIndex"></param>
  197. /// <param name="pageSize"></param>
  198. /// <returns></returns>
  199. public async Task<EnterExitCostQuoteInfoView> InfoByGroupIdAsync(int groupId)
  200. {
  201. var viewInfo = new EnterExitCostQuoteInfoView();
  202. var basicItems = await InitBasicItemAsync(true);
  203. if (basicItems.Any())
  204. {
  205. viewInfo.FeeItems = basicItems.Select(x => new QuoteItemInfo { ItemId = x.Id, ItemName = x.Name, Index = x.Index }).OrderBy(x => x.Index).ToArray();
  206. }
  207. var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  208. .Where(x => x.IsDel == 0 && x.GroupId == groupId)
  209. .FirstAsync();
  210. if (quoteInfo != null)
  211. {
  212. viewInfo.Id = quoteInfo.Id;
  213. viewInfo.Name = quoteInfo.Name;
  214. viewInfo.GroupId = quoteInfo.GroupId;
  215. viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();
  216. var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()
  217. .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)
  218. .Select(x => new QuoteSubItemInfo
  219. {
  220. Id = x.Id,
  221. ItemId = x.ItemId,
  222. FeeName = x.FeeName,
  223. UnitPrice = x.UnitPrice,
  224. Quantity = x.Quantity,
  225. PplNum = x.PplNum,
  226. Currency = x.Currency,
  227. TotalAmt = x.TotalAmt,
  228. Index = x.Index
  229. }).ToListAsync();
  230. if (quoteItems.Any())
  231. {
  232. 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();
  233. }
  234. }
  235. return viewInfo;
  236. }
  237. /// <summary>
  238. /// Add Or Edit
  239. /// </summary>
  240. /// <param name="name"></param>
  241. /// <param name="pageIndex"></param>
  242. /// <param name="pageSize"></param>
  243. /// <returns></returns>
  244. public async Task<JsonView> OpAsync(EnterExitCostQuoteOpDto dto)
  245. {
  246. var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };
  247. string quoteName = dto.Name;
  248. int quoteId = dto.Id;
  249. if (string.IsNullOrEmpty(dto.Name))
  250. {
  251. jw.Msg = "报价名称不能为空";
  252. return jw;
  253. }
  254. var quoteInfo = new Grp_EnterExitCostQuote
  255. {
  256. Id = quoteId,
  257. Name = quoteName,
  258. GroupId = dto.GroupId,
  259. CurrencyRemark = CommonFun.GetCurrencyChinaToString(dto.Rates.ToList()),
  260. CreateUserId = dto.CurrUserId
  261. };
  262. var quoteItemInfos = new List<Grp_EnterExitCostQuoteItem>();
  263. if (dto.FeeItems.Any())
  264. {
  265. foreach (var item in dto.FeeItems)
  266. {
  267. if (item.Infos.Any())
  268. {
  269. quoteItemInfos.AddRange(item.Infos.Select(x => new Grp_EnterExitCostQuoteItem
  270. {
  271. Id = x.Id,
  272. QuoteId = quoteId,
  273. ItemId = x.ItemId,
  274. Index = x.Index,
  275. FeeName = x.FeeName,
  276. UnitPrice = x.UnitPrice,
  277. Currency = x.Currency,
  278. Quantity = x.Quantity,
  279. PplNum = x.PplNum,
  280. TotalAmt = x.TotalAmt,
  281. Remark = x.Remark,
  282. CreateUserId = dto.CurrUserId
  283. }));
  284. }
  285. }
  286. }
  287. var isNull = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>().FirstAsync(x => x.IsDel == 0 && x.Name.Equals(quoteName)) == null ? true : false;
  288. _sqlSugar.BeginTran();
  289. if (isNull) //新增
  290. {
  291. quoteId = await _sqlSugar.Insertable(quoteInfo).ExecuteReturnIdentityAsync();
  292. if (quoteId < 1)
  293. {
  294. jw.Msg = "新增失败!";
  295. _sqlSugar.RollbackTran();
  296. return jw;
  297. }
  298. quoteItemInfos.ForEach(x => x.QuoteId = quoteId);
  299. var quoteItemIds = await _sqlSugar.Insertable(quoteItemInfos).ExecuteReturnIdentityAsync();
  300. if (quoteItemIds < 1)
  301. {
  302. jw.Msg = "新增失败";
  303. _sqlSugar.RollbackTran();
  304. return jw;
  305. }
  306. jw.Msg = "新增成功!";
  307. }
  308. else if (!isNull) //编辑
  309. {
  310. var quoteUpd = await _sqlSugar.Updateable(quoteInfo).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
  311. if (quoteUpd < 1)
  312. {
  313. jw.Msg = "编辑失败!";
  314. _sqlSugar.RollbackTran();
  315. return jw;
  316. }
  317. var addItems = quoteItemInfos.Where(x => x.Id < 1).ToList();
  318. var updItems = quoteItemInfos.Where(x => x.Id > 0).ToList();
  319. if (addItems.Any())
  320. {
  321. var addItem = await _sqlSugar.Insertable(addItems).ExecuteCommandAsync();
  322. if (addItem < 1)
  323. {
  324. jw.Msg = "编辑失败!";
  325. _sqlSugar.RollbackTran();
  326. return jw;
  327. }
  328. }
  329. if (updItems.Any())
  330. {
  331. var updItem = await _sqlSugar.Updateable(updItems).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
  332. if (updItem < 1)
  333. {
  334. jw.Msg = "编辑失败!";
  335. _sqlSugar.RollbackTran();
  336. return jw;
  337. }
  338. }
  339. jw.Msg = "编辑成功!";
  340. }
  341. _sqlSugar.CommitTran();
  342. jw.Code = StatusCodes.Status200OK;
  343. jw.Data = quoteId;
  344. return jw;
  345. }
  346. /// <summary>
  347. /// ItemDel
  348. /// </summary>
  349. /// <param name="name"></param>
  350. /// <param name="pageIndex"></param>
  351. /// <param name="pageSize"></param>
  352. /// <returns></returns>
  353. public async Task<JsonView> ItemDelAsync(EnterExitCostQuoteItemDelDto dto)
  354. {
  355. var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };
  356. int currUserId = dto.CurrUserId,
  357. id = dto.Id;
  358. if (currUserId < 1)
  359. {
  360. jw.Msg = MsgTips.UserId;
  361. return jw;
  362. }
  363. if (id < 1)
  364. {
  365. jw.Msg = MsgTips.Id;
  366. return jw;
  367. }
  368. var info = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().FirstAsync(x => x.IsDel == 0 && x.Id == id);
  369. if (info == null)
  370. {
  371. jw.Msg = $"操作成功!";
  372. jw.Code = StatusCodes.Status200OK;
  373. return jw;
  374. }
  375. var itemInfos = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().Where(x => x.IsDel == 0 && x.QuoteId == info.QuoteId && x.ItemId == info.ItemId).ToListAsync();
  376. _sqlSugar.BeginTran();
  377. var delInfo = new Grp_EnterExitCostQuoteItem { DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DeleteUserId = currUserId, IsDel = 1 };
  378. var del = await _sqlSugar.Updateable(delInfo)
  379. .UpdateColumns(x => new { x.DeleteTime, x.DeleteUserId, x.IsDel })
  380. .Where(x => x.Id == id)
  381. .ExecuteCommandAsync();
  382. if (del < 1)
  383. {
  384. jw.Msg = $"删除失败!";
  385. _sqlSugar.RollbackTran();
  386. return jw;
  387. }
  388. if (itemInfos.Any())
  389. {
  390. // 更新剩余项的索引
  391. var itemsToUpdate = itemInfos.Where(i => i.Index > itemInfos.First(i => i.Id == id).Index).ToList();
  392. foreach (var item in itemsToUpdate)
  393. {
  394. item.Index -= 1;
  395. }
  396. await _sqlSugar.Updateable(itemsToUpdate).ExecuteCommandAsync();
  397. }
  398. _sqlSugar.CommitTran();
  399. jw.Msg = "操作成功!";
  400. jw.Code = StatusCodes.Status200OK;
  401. return jw;
  402. }
  403. }
  404. }