EnterExitCostQuoteRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. var viewInfo = new EnterExitCostQuoteInfoView();
  155. viewInfo.Id = dto.Id;
  156. var basicItems = await InitBasicItemAsync(true);
  157. if (basicItems.Any())
  158. {
  159. viewInfo.FeeItems = basicItems.Select(x => new QuoteItemInfo { ItemId = x.Id, ItemName = x.Name,Index = x.Index }).OrderBy(x => x.Index).ToArray();
  160. }
  161. var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()
  162. .Where(x => x.IsDel == 0 && x.Id == x.Id)
  163. .FirstAsync();
  164. if (quoteInfo != null)
  165. {
  166. viewInfo.Name = quoteInfo.Name;
  167. viewInfo.GroupId = quoteInfo.GroupId;
  168. viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();
  169. var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()
  170. .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)
  171. .Select(x => new QuoteSubItemInfo
  172. {
  173. Id = x.Id,
  174. ItemId = x.ItemId,
  175. FeeName = x.FeeName,
  176. UnitPrice = x.UnitPrice,
  177. Quantity = x.Quantity,
  178. PplNum = x.PplNum,
  179. Currency = x.Currency,
  180. TotalAmt = x.TotalAmt,
  181. Index = x.Index
  182. }).ToListAsync();
  183. if (quoteItems.Any())
  184. {
  185. 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();
  186. }
  187. }
  188. return viewInfo;
  189. }
  190. /// <summary>
  191. /// Add Or Edit
  192. /// </summary>
  193. /// <param name="name"></param>
  194. /// <param name="pageIndex"></param>
  195. /// <param name="pageSize"></param>
  196. /// <returns></returns>
  197. public async Task<JsonView> OpAsync(EnterExitCostQuoteOpDto dto)
  198. {
  199. var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };
  200. string quoteName = dto.Name;
  201. int quoteId = dto.Id;
  202. if (string.IsNullOrEmpty(dto.Name))
  203. {
  204. jw.Msg = "报价名称不能为空";
  205. return jw;
  206. }
  207. var quoteInfo = new Grp_EnterExitCostQuote
  208. {
  209. Id = quoteId,
  210. Name = quoteName,
  211. GroupId = dto.GroupId,
  212. CurrencyRemark = CommonFun.GetCurrencyChinaToString(dto.Rates.ToList()),
  213. CreateUserId = dto.CurrUserId
  214. };
  215. var quoteItemInfos = new List<Grp_EnterExitCostQuoteItem>();
  216. if (dto.FeeItems.Any())
  217. {
  218. foreach (var item in dto.FeeItems)
  219. {
  220. if (item.Infos.Any())
  221. {
  222. quoteItemInfos.AddRange(item.Infos.Select(x => new Grp_EnterExitCostQuoteItem
  223. {
  224. Id = x.Id,
  225. QuoteId = quoteId,
  226. ItemId = x.ItemId,
  227. Index = x.Index,
  228. FeeName = x.FeeName,
  229. UnitPrice = x.UnitPrice,
  230. Currency = x.Currency,
  231. Quantity = x.Quantity,
  232. PplNum = x.PplNum,
  233. TotalAmt = x.TotalAmt,
  234. Remark = x.Remark,
  235. CreateUserId = dto.CurrUserId
  236. }));
  237. }
  238. }
  239. }
  240. var isNull = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>().FirstAsync(x => x.IsDel == 0 && x.Name.Equals(quoteName)) == null ? true : false;
  241. _sqlSugar.BeginTran();
  242. if (isNull) //新增
  243. {
  244. quoteId = await _sqlSugar.Insertable(quoteInfo).ExecuteReturnIdentityAsync();
  245. if (quoteId < 1)
  246. {
  247. jw.Msg = "新增失败!";
  248. _sqlSugar.RollbackTran();
  249. return jw;
  250. }
  251. quoteItemInfos.ForEach(x => x.QuoteId = quoteId);
  252. var quoteItemIds = await _sqlSugar.Insertable(quoteItemInfos).ExecuteReturnIdentityAsync();
  253. if (quoteItemIds < 1)
  254. {
  255. jw.Msg = "新增失败";
  256. _sqlSugar.RollbackTran();
  257. return jw;
  258. }
  259. jw.Msg = "新增成功!";
  260. }
  261. else if (!isNull) //编辑
  262. {
  263. var quoteUpd = await _sqlSugar.Updateable(quoteInfo).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
  264. if (quoteUpd < 1)
  265. {
  266. jw.Msg = "编辑失败!";
  267. _sqlSugar.RollbackTran();
  268. return jw;
  269. }
  270. var addItems = quoteItemInfos.Where(x => x.Id < 1).ToList();
  271. var updItems = quoteItemInfos.Where(x => x.Id > 0).ToList();
  272. if (addItems.Any())
  273. {
  274. var addItem = await _sqlSugar.Insertable(addItems).ExecuteCommandAsync();
  275. if (addItem < 1)
  276. {
  277. jw.Msg = "编辑失败!";
  278. _sqlSugar.RollbackTran();
  279. return jw;
  280. }
  281. }
  282. if (updItems.Any())
  283. {
  284. var updItem = await _sqlSugar.Updateable(updItems).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();
  285. if (updItem < 1)
  286. {
  287. jw.Msg = "编辑失败!";
  288. _sqlSugar.RollbackTran();
  289. return jw;
  290. }
  291. }
  292. jw.Msg = "编辑成功!";
  293. }
  294. _sqlSugar.CommitTran();
  295. jw.Code = StatusCodes.Status200OK;
  296. jw.Data = quoteId;
  297. return jw;
  298. }
  299. /// <summary>
  300. /// ItemDel
  301. /// </summary>
  302. /// <param name="name"></param>
  303. /// <param name="pageIndex"></param>
  304. /// <param name="pageSize"></param>
  305. /// <returns></returns>
  306. public async Task<JsonView> ItemDelAsync(EnterExitCostQuoteItemDelDto dto)
  307. {
  308. var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };
  309. int currUserId = dto.CurrUserId,
  310. id = dto.Id;
  311. if (currUserId < 1)
  312. {
  313. jw.Msg = MsgTips.UserId;
  314. return jw;
  315. }
  316. if (id < 1)
  317. {
  318. jw.Msg = MsgTips.Id;
  319. return jw;
  320. }
  321. var info = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().FirstAsync(x => x.IsDel == 0 && x.Id == id);
  322. if (info == null)
  323. {
  324. jw.Msg = $"操作成功!";
  325. jw.Code = StatusCodes.Status200OK;
  326. return jw;
  327. }
  328. var itemInfos = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().Where(x => x.IsDel == 0 && x.QuoteId == info.QuoteId && x.ItemId == info.ItemId).ToListAsync();
  329. _sqlSugar.BeginTran();
  330. var delInfo = new Grp_EnterExitCostQuoteItem { DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DeleteUserId = currUserId, IsDel = 1 };
  331. var del = await _sqlSugar.Updateable(delInfo)
  332. .UpdateColumns(x => new { x.DeleteTime, x.DeleteUserId, x.IsDel })
  333. .Where(x => x.Id == id)
  334. .ExecuteCommandAsync();
  335. if (del < 1)
  336. {
  337. jw.Msg = $"删除失败!";
  338. _sqlSugar.RollbackTran();
  339. return jw;
  340. }
  341. if (itemInfos.Any())
  342. {
  343. // 更新剩余项的索引
  344. var itemsToUpdate = itemInfos.Where(i => i.Index > itemInfos.First(i => i.Id == id).Index).ToList();
  345. foreach (var item in itemsToUpdate)
  346. {
  347. item.Index -= 1;
  348. }
  349. await _sqlSugar.Updateable(itemsToUpdate).ExecuteCommandAsync();
  350. }
  351. _sqlSugar.CommitTran();
  352. jw.Msg = "操作成功!";
  353. jw.Code = StatusCodes.Status200OK;
  354. return jw;
  355. }
  356. }
  357. }