ProceedsReceivedRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.Dtos.Financial;
  4. using OASystem.Domain.Entities.Financial;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.ViewModels.Financial;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace OASystem.Infrastructure.Repositories.Financial
  13. {
  14. /// <summary>
  15. /// 财务 - 团组已收款项
  16. /// 雷怡 2023.08.16 15:24
  17. /// </summary>
  18. public class ProceedsReceivedRepository : BaseRepository<Fin_ProceedsReceived, Fin_ProceedsReceivedView>
  19. {
  20. private readonly IMapper _mapper;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="sqlSugar"></param>
  25. /// <param name="mapper"></param>
  26. public ProceedsReceivedRepository(SqlSugarClient sqlSugar, IMapper mapper)
  27. : base(sqlSugar)
  28. {
  29. _mapper = mapper;
  30. }
  31. /// <summary>
  32. /// 根据diid查询团组已收款项 已关联应收款项
  33. /// </summary>
  34. /// <param name="diid"></param>
  35. /// <returns></returns>
  36. public async Task<Result> GetGroupReceivedByDiid(int diid)
  37. {
  38. Result result = new() { Code = -2 };
  39. string sql = string.Format(@"Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0}", diid);
  40. var groupReceivablesList = await _sqlSugar.SqlQueryable<Fin_ProceedsReceivedView>(sql).ToListAsync();
  41. result.Code = 0;
  42. result.Msg = "查询成功!";
  43. result.Data = groupReceivablesList;
  44. return result;
  45. }
  46. /// <summary>
  47. /// 根据diid 数组 查询团组已收款项 已关联应收款项
  48. /// </summary>
  49. /// <param name="diid"></param>
  50. /// <returns></returns>
  51. public async Task<Result> GetGroupReceivedByDiids(int[] diids)
  52. {
  53. Result result = new() { Code = -2 };
  54. var groupReceivablesList = await _sqlSugar.Queryable<Fin_ProceedsReceived>()
  55. .Where(fr => fr.IsDel == 0 && diids.Contains(fr.Diid)).ToListAsync();
  56. result.Code = 0;
  57. result.Msg = "查询成功!";
  58. result.Data = groupReceivablesList;
  59. return result;
  60. }
  61. /// <summary>
  62. /// 已收款项 已关联应收款项 删除
  63. /// </summary>
  64. /// <param name="dto"></param>
  65. /// <returns></returns>
  66. public async Task<Result> _Del(ProceedsReceivedDelDto dto)
  67. {
  68. Result result = new Result() { Code = -1, Msg = "程序错误!" };
  69. var res = await _sqlSugar.Updateable<Fin_ProceedsReceived>()
  70. .Where(it => it.Id == dto.Id)
  71. .SetColumns(it => new Fin_ProceedsReceived()
  72. {
  73. IsDel = 1,
  74. DeleteUserId = dto.UserId,
  75. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  76. }
  77. ).ExecuteCommandAsync();
  78. if (res > 0)
  79. {
  80. result.Msg = "删除成功!";
  81. result.Code = 0;
  82. }
  83. else
  84. {
  85. result.Msg = "删除失败!";
  86. }
  87. return result;
  88. }
  89. /// <summary>
  90. /// 已收款项 已关联应收款项
  91. /// Add And Update
  92. /// </summary>
  93. /// <param name="diid"></param>
  94. /// <returns></returns>
  95. public async Task<Result> PostAmountReceivedOperate(ProceedsReceivedDto dto)
  96. {
  97. Result result = new() { Code = -2 };
  98. if (dto._ProceedsReceivedInfos.Count <= 0)
  99. {
  100. result.Msg = "已收款项没有信息,不能进行,添加或修改操作!!!";
  101. return result;
  102. }
  103. int addCount = 0, updateCount = 0;
  104. if (dto.PortType == 1)
  105. {
  106. List<Fin_ProceedsReceived> _ProceedsReceived = new List<Fin_ProceedsReceived>();
  107. foreach (var item in dto._ProceedsReceivedInfos)
  108. {
  109. _ProceedsReceived.Add(new Fin_ProceedsReceived()
  110. {
  111. Diid = dto.DiId,
  112. Id = item.Id,
  113. SectionTime = item.SectionTime,
  114. Price = item.Price,
  115. Currency = item.Currency,
  116. ReceivablesType = item.ReceivablesType,
  117. Client = item.Client,
  118. CustomerName = item.CustomerName,
  119. CustomerTel = item.CustomerTel,
  120. FID = item.FID,
  121. CreateUserId = dto.UserId,
  122. CreateTime = DateTime.Now,
  123. Remark = item.Remark
  124. });
  125. }
  126. if (_ProceedsReceived.Count > 0)
  127. {
  128. var x = _sqlSugar.Storageable(_ProceedsReceived).ToStorage();
  129. addCount = x.AsInsertable.ExecuteCommand(); //不存在插入
  130. updateCount = x.AsUpdateable.ExecuteCommand(); //存在更新
  131. }
  132. result.Code = 0;
  133. result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);
  134. }
  135. return result;
  136. }
  137. /// <summary>
  138. /// 已收款项 已关联应收款项
  139. /// 分配已收款项至 应收项下
  140. /// </summary>
  141. /// <param name="diid"></param>
  142. /// <returns></returns>
  143. public async Task<Result> PostAllocateAmountReceived(AllocateAmountReceivedDto dto)
  144. {
  145. Result result = new() { Code = -2 };
  146. if (dto.SubIds.Count <= 0)
  147. {
  148. result.Msg = "请选择要添加的已收款项!";
  149. return result;
  150. }
  151. if (dto.PortType == 1)
  152. {
  153. List<Fin_ProceedsReceived> _proceedsReceived = new List<Fin_ProceedsReceived>();
  154. foreach (var id in dto.SubIds)
  155. {
  156. _proceedsReceived.Add(new Fin_ProceedsReceived() { Id = id, FID = dto.ParentId });
  157. }
  158. var res = await _sqlSugar.Updateable(_proceedsReceived).
  159. WhereColumns(it => new { it.Id })
  160. .UpdateColumns(it => new { it.FID })
  161. .ExecuteCommandAsync();
  162. result.Code = 0;
  163. }
  164. return result;
  165. }
  166. /// <summary>
  167. /// 根据diid查询团组已收款项
  168. /// 全字段
  169. /// </summary>
  170. /// <param name="diid"></param>
  171. /// <returns></returns>
  172. public async Task<Result> PostAmountReceivedByDiId(int diId)
  173. {
  174. Result result = new() { Code = -2 };
  175. string sql = string.Format(@"Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0}", diId);
  176. var groupReceivablesList = await _sqlSugar.SqlQueryable<Fin_ProceedsReceivedView>(sql).ToListAsync();
  177. result.Code = 0;
  178. result.Msg = "查询成功!";
  179. result.Data = groupReceivablesList;
  180. return result;
  181. }
  182. /// <summary>
  183. /// 根据diid查询团组已收款项
  184. /// </summary>
  185. /// <param name="diid"></param>
  186. /// <returns></returns>
  187. public async Task<Result> PostAmountReceived(int diid)
  188. {
  189. Result result = new() { Code = -2 };
  190. string sql = string.Format(@"Select Id,Diid,SectionTime,Price,Currency,ReceivablesType,Client,CustomerName,CustomerTel, Remark From Fin_ProceedsReceived
  191. Where IsDel=0 And Diid={0}", diid);
  192. var groupReceivablesList = await _sqlSugar.SqlQueryable<ProceedsReceived1View>(sql).ToListAsync();
  193. result.Code = 0;
  194. result.Msg = "查询成功!";
  195. result.Data = groupReceivablesList;
  196. return result;
  197. }
  198. /// <summary>
  199. /// 已收款项
  200. /// Add And Update
  201. /// </summary>
  202. /// <param name="diid"></param>
  203. /// <returns></returns>
  204. public async Task<Result> PostAmountReceivedAddOrEditDto(AmountReceivedAddOrEditDto dto)
  205. {
  206. Result result = new() { Code = -2 };
  207. if (dto._ProceedsReceivedInfos.Count <= 0)
  208. {
  209. result.Msg = "已收款项没有信息,不能进行,添加或修改操作!!!";
  210. return result;
  211. }
  212. int addCount = 0, updateCount = 0;
  213. if (dto.PortType == 1)
  214. {
  215. List<Fin_ProceedsReceived> _ProceedsReceived = new List<Fin_ProceedsReceived>();
  216. foreach (var item in dto._ProceedsReceivedInfos)
  217. {
  218. _ProceedsReceived.Add(new Fin_ProceedsReceived()
  219. {
  220. Diid = dto.DiId,
  221. Id = item.Id,
  222. SectionTime = item.SectionTime,
  223. Price = item.Price,
  224. Currency = item.Currency,
  225. ReceivablesType = item.ReceivablesType,
  226. Client = item.Client,
  227. CustomerName = item.CustomerName,
  228. CustomerTel = item.CustomerTel,
  229. FID = 0,
  230. CreateUserId = dto.UserId,
  231. CreateTime = DateTime.Now,
  232. Remark = item.Remark
  233. });
  234. }
  235. if (_ProceedsReceived.Count > 0)
  236. {
  237. var x = _sqlSugar.Storageable(_ProceedsReceived).ToStorage();
  238. addCount = x.AsInsertable.ExecuteCommand(); //不存在插入
  239. _sqlSugar.BeginTran();
  240. foreach (var item in _ProceedsReceived)
  241. {
  242. bool res = await UpdateAsync<Fin_ProceedsReceived>(s => s.Id == item.Id, s => new Fin_ProceedsReceived
  243. {
  244. SectionTime = item.SectionTime,
  245. Price = item.Price,
  246. Currency = item.Currency,
  247. ReceivablesType = item.ReceivablesType,
  248. Client = item.Client,
  249. CustomerName = item.CustomerName,
  250. CustomerTel = item.CustomerTel,
  251. Remark = item.Remark
  252. });
  253. if (!res)
  254. {
  255. _sqlSugar.RollbackTran();
  256. break;
  257. }
  258. updateCount++;
  259. }
  260. _sqlSugar.CommitTran();
  261. //updateCount = x.AsUpdateable.IgnoreColumns(p => new
  262. //{
  263. // p.SectionTime,
  264. // p.Price,
  265. // p.Currency,
  266. // p.ReceivablesType,
  267. // p.Client,
  268. // p.CustomerName,
  269. // p.CustomerTel,
  270. // p.Remark
  271. //}).ExecuteCommand(); //存在更新
  272. }
  273. result.Code = 0;
  274. result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);
  275. //修改团组状态
  276. int diId = dto.DiId;
  277. decimal sum_fr = 0M;
  278. decimal sum_pr = 0M;
  279. decimal sum_refund = 0M; //收款退还
  280. decimal sum_extra = 0M; //超支费用
  281. //1.应收
  282. string sql_fr = string.Format(@" Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0} ", diId);
  283. List<Fin_ForeignReceivables> list_fr = _sqlSugar.SqlQueryable<Fin_ForeignReceivables>(sql_fr).ToList();
  284. sum_fr = list_fr.Sum(s => s.ItemSumPrice);
  285. //2.已收
  286. string sql_pr = string.Format(@" Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0} ", diId);
  287. List<Fin_ProceedsReceived> list_pr = _sqlSugar.SqlQueryable<Fin_ProceedsReceived>(sql_pr).ToList();
  288. sum_pr = list_pr.Sum(s => s.Price);
  289. //3.收款退还
  290. string sql_other = string.Format(@"Select * From Fin_PaymentRefundAndOtherMoney prao
  291. Inner Join Grp_CreditCardPayment ccp On prao.Id = ccp.CId
  292. Where ccp.CTable = 285 And ccp.IsPay = 1 And prao.IsDel = 0 And ccp.IsDel = 0 And prao.DiId = {0}", diId);
  293. List<Grp_CreditCardPayment> list_other = _sqlSugar.SqlQueryable<Grp_CreditCardPayment>(sql_other).ToList();
  294. sum_refund = list_other.Sum(s => s.RMBPrice);
  295. //4.超支
  296. string sql_extra = string.Format(@" Select c.* From Fin_GroupExtraCost f
  297. Inner join Grp_CreditCardPayment c On f.Id = c.CId
  298. Where c.CTable = 1015 And c.IsPay = 1 And f.IsDel = 0 And c.IsDel = 0 And f.DiId = {0} ", diId);
  299. List<Grp_CreditCardPayment> list_extra = _sqlSugar.SqlQueryable<Grp_CreditCardPayment>(sql_extra).ToList();
  300. sum_extra = list_extra.Sum(s => s.RMBPrice);
  301. decimal balance = ((sum_fr + sum_extra) - (sum_pr - sum_refund));
  302. if (balance <= 0)
  303. {
  304. bool res = await UpdateAsync<Grp_DelegationInfo>(s => s.Id == dto.DiId, s => new Grp_DelegationInfo
  305. {
  306. IsSure = 1
  307. });
  308. }
  309. }
  310. return result;
  311. }
  312. /// <summary>
  313. /// 已收款项 删除
  314. /// </summary>
  315. /// <param name="dto"></param>
  316. /// <returns></returns>
  317. public async Task<Result> PostAmountReceivedDel(AmountReceivedDelDto dto)
  318. {
  319. Result result = new Result() { Code = -1, Msg = "程序错误!" };
  320. var res = await _sqlSugar.Updateable<Fin_ProceedsReceived>()
  321. .Where(it => it.Id == dto.Id)
  322. .SetColumns(it => new Fin_ProceedsReceived()
  323. {
  324. IsDel = 1,
  325. DeleteUserId = dto.UserId,
  326. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  327. }
  328. ).ExecuteCommandAsync();
  329. if (res > 0)
  330. {
  331. result.Msg = "删除成功!";
  332. result.Code = 0;
  333. }
  334. else
  335. {
  336. result.Msg = "删除失败!";
  337. }
  338. return result;
  339. }
  340. }
  341. }