ProceedsReceivedRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().Where(x => x.IsDel == 0 && x.Id == diid).FirstAsync();
  194. result.Code = 0;
  195. result.Msg = "查询成功!";
  196. result.Data = new
  197. {
  198. UnSettleReason = groupInfo.PrUnSettleReason,
  199. groupReceivablesList
  200. };
  201. return result;
  202. }
  203. /// <summary>
  204. /// 已收款项
  205. /// Add And Update
  206. /// </summary>
  207. /// <param name="diid"></param>
  208. /// <returns></returns>
  209. public async Task<Result> PostAmountReceivedAddOrEdit(AmountReceivedAddOrEditDto dto)
  210. {
  211. Result result = new() { Code = -2 };
  212. //if (dto._ProceedsReceivedInfos.Count <= 0)
  213. //{
  214. // result.Msg = "已收款项没有信息,不能进行,添加或修改操作!!!";
  215. // return result;
  216. //}
  217. int addCount = 0, updateCount = 0;
  218. if (dto.PortType == 1)
  219. {
  220. List<Fin_ProceedsReceived> _ProceedsReceived = new List<Fin_ProceedsReceived>();
  221. foreach (var item in dto._ProceedsReceivedInfos)
  222. {
  223. _ProceedsReceived.Add(new Fin_ProceedsReceived()
  224. {
  225. Diid = dto.DiId,
  226. Id = item.Id,
  227. SectionTime = item.SectionTime,
  228. Price = item.Price,
  229. Currency = item.Currency,
  230. ReceivablesType = item.ReceivablesType,
  231. Client = item.Client,
  232. CustomerName = item.CustomerName,
  233. CustomerTel = item.CustomerTel,
  234. FID = 0,
  235. CreateUserId = dto.UserId,
  236. CreateTime = DateTime.Now,
  237. Remark = item.Remark
  238. });
  239. }
  240. if (_ProceedsReceived.Count > 0)
  241. {
  242. var x = _sqlSugar.Storageable(_ProceedsReceived).ToStorage();
  243. addCount = x.AsInsertable.ExecuteCommand(); //不存在插入
  244. _sqlSugar.BeginTran();
  245. foreach (var item in _ProceedsReceived)
  246. {
  247. bool res = await UpdateAsync<Fin_ProceedsReceived>(s => s.Id == item.Id, s => new Fin_ProceedsReceived
  248. {
  249. SectionTime = item.SectionTime,
  250. Price = item.Price,
  251. Currency = item.Currency,
  252. ReceivablesType = item.ReceivablesType,
  253. Client = item.Client,
  254. CustomerName = item.CustomerName,
  255. CustomerTel = item.CustomerTel,
  256. Remark = item.Remark
  257. });
  258. if (!res)
  259. {
  260. _sqlSugar.RollbackTran();
  261. break;
  262. }
  263. updateCount++;
  264. }
  265. _sqlSugar.CommitTran();
  266. }
  267. result.Code = 0;
  268. result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);
  269. //修改团组状态
  270. int diId = dto.DiId;
  271. decimal sum_fr = 0M;
  272. decimal sum_pr = 0M;
  273. decimal sum_refund = 0M; //收款退还
  274. decimal sum_extra = 0M; //超支费用
  275. //1.应收
  276. string sql_fr = string.Format(@" Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0} ", diId);
  277. List<Fin_ForeignReceivables> list_fr = _sqlSugar.SqlQueryable<Fin_ForeignReceivables>(sql_fr).ToList();
  278. sum_fr = list_fr.Sum(s => s.ItemSumPrice);
  279. //2.已收
  280. string sql_pr = string.Format(@" Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0} ", diId);
  281. List<Fin_ProceedsReceived> list_pr = _sqlSugar.SqlQueryable<Fin_ProceedsReceived>(sql_pr).ToList();
  282. sum_pr = list_pr.Sum(s => s.Price);
  283. //3.收款退还
  284. string sql_other = string.Format(@"Select * From Fin_PaymentRefundAndOtherMoney prao
  285. Inner Join Grp_CreditCardPayment ccp On prao.Id = ccp.CId
  286. Where ccp.CTable = 285 And ccp.IsPay = 1 And prao.IsDel = 0 And ccp.IsDel = 0 And prao.DiId = {0}", diId);
  287. List<Grp_CreditCardPayment> list_other = _sqlSugar.SqlQueryable<Grp_CreditCardPayment>(sql_other).ToList();
  288. sum_refund = list_other.Sum(s => s.RMBPrice);
  289. //4.超支
  290. string sql_extra = string.Format(@" Select c.* From Fin_GroupExtraCost f
  291. Inner join Grp_CreditCardPayment c On f.Id = c.CId
  292. Where c.CTable = 1015 And c.IsPay = 1 And f.IsDel = 0 And c.IsDel = 0 And f.DiId = {0} ", diId);
  293. List<Grp_CreditCardPayment> list_extra = _sqlSugar.SqlQueryable<Grp_CreditCardPayment>(sql_extra).ToList();
  294. sum_extra = list_extra.Sum(s => s.RMBPrice);
  295. decimal balance = ((sum_fr + sum_extra) - (sum_pr - sum_refund));
  296. var updateGroup = await _sqlSugar.Updateable<Grp_DelegationInfo>()
  297. .SetColumnsIF(balance <= 0, x => x.IsSure == 1)
  298. .SetColumns( x => x.PrUnSettleReason == dto.UnSettleReason)
  299. .Where(it => it.Id == dto.DiId)
  300. .ExecuteCommandAsync();
  301. }
  302. return result;
  303. }
  304. /// <summary>
  305. /// 已收款项 删除
  306. /// </summary>
  307. /// <param name="dto"></param>
  308. /// <returns></returns>
  309. public async Task<Result> PostAmountReceivedDel(AmountReceivedDelDto dto)
  310. {
  311. Result result = new Result() { Code = -1, Msg = "程序错误!" };
  312. var res = await _sqlSugar.Updateable<Fin_ProceedsReceived>()
  313. .Where(it => it.Id == dto.Id)
  314. .SetColumns(it => new Fin_ProceedsReceived()
  315. {
  316. IsDel = 1,
  317. DeleteUserId = dto.UserId,
  318. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  319. }
  320. ).ExecuteCommandAsync();
  321. if (res > 0)
  322. {
  323. result.Msg = "删除成功!";
  324. result.Code = 0;
  325. }
  326. else
  327. {
  328. result.Msg = "删除失败!";
  329. }
  330. return result;
  331. }
  332. }
  333. }