ForeignReceivablesRepository.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using AutoMapper;
  2. using MathNet.Numerics.Statistics.Mcmc;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Dtos.Financial;
  6. using OASystem.Domain.Dtos.Groups;
  7. using OASystem.Domain.Entities.Financial;
  8. using OASystem.Domain.ViewModels.Financial;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Infrastructure.Repositories.Groups;
  11. using OASystem.Infrastructure.Repositories.System;
  12. using SqlSugar;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace OASystem.Infrastructure.Repositories.Financial
  19. {
  20. /// <summary>
  21. /// 财务 - 团组应收款项 仓库
  22. /// 雷怡 2023.08.16 15:03
  23. /// </summary>
  24. public class ForeignReceivablesRepository : BaseRepository<Fin_ForeignReceivables, Fin_ForeignReceivablesView>
  25. {
  26. private readonly IMapper _mapper;
  27. private readonly DelegationInfoRepository _delegationRep;
  28. private readonly SetDataRepository _setDataRep;
  29. public ForeignReceivablesRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository delegationRep, SetDataRepository setDataRep)
  30. : base(sqlSugar)
  31. {
  32. _mapper = mapper;
  33. _delegationRep = delegationRep;
  34. _setDataRep = setDataRep;
  35. }
  36. #region 关联已收款项
  37. /// <summary>
  38. /// 收款账单 数据源
  39. /// </summary>
  40. /// <returns></returns>
  41. public async Task<JsonView> GetDataSource(ForeignReceivablesDataSourcesDto dto)
  42. {
  43. JsonView result = new() { Code = StatusCodes.Status204NoContent };
  44. //已收款项 判断如果是市场部的人员进来的话 只显示自己的 其他的都显示全部的
  45. var userInfos = await _sqlSugar.Queryable<Sys_Users>()
  46. .InnerJoin<Sys_Department>((u, d) => u.DepId == d.Id)
  47. .Where((u, d) => u.IsDel == 0 && d.DepName.Contains("市场部") && u.Id == dto.CurrUserId)
  48. .ToListAsync();
  49. string sqlWhere = "";
  50. if (userInfos.Count > 0) sqlWhere = string.Format(@$" And JietuanOperator={dto.CurrUserId} ");
  51. string sql = string.Format(@$"Select Id,TeamName GroupName From Grp_DelegationInfo
  52. Where TeamName != '' And IsDel = 0 {sqlWhere}
  53. Order By Id Desc");
  54. var _groupNameList = await _sqlSugar.SqlQueryable<GroupNameView>(sql).ToListAsync();
  55. //var groupNameData = await _delegationRep.GetGroupNameList(new GroupNameDto());
  56. var currencyData = await _setDataRep.GetSetDataBySTId(_setDataRep, 66); //币种
  57. var remittanceMethodData = await _setDataRep.GetSetDataBySTId(_setDataRep, 14); //汇款方式
  58. result.Code = StatusCodes.Status200OK;
  59. result.Msg = "成功!";
  60. result.Data = new
  61. {
  62. GroupNameData = _groupNameList,
  63. CurrencyData = currencyData.Data,
  64. RemittanceMethodData = remittanceMethodData.Data
  65. };
  66. return result;
  67. }
  68. /// <summary>
  69. /// 根据diid查询团组应收款项
  70. /// </summary>
  71. /// <param name="diid"></param>
  72. /// <returns></returns>
  73. public async Task<Result> GetGroupReceivablesInfoByDiId(ForForeignReceivablesInfoDto dto)
  74. {
  75. Result result = new() { Code = -2 };
  76. var groupInfoData = await _delegationRep.GetGroupInfo(new GroupInfoDto() { Id = dto.DiId });
  77. //应收款项
  78. string groupReceivedSql = string.Format(@"Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0}", dto.DiId);
  79. var groupReceivedList = await _sqlSugar.SqlQueryable<ForeignReceivablesView>(groupReceivedSql).ToListAsync();
  80. //已收款项
  81. string groupProceedsReceivedSql = string.Format(@"Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0}", dto.DiId);
  82. var groupProceedsReceivedList = await _sqlSugar.SqlQueryable<ProceedsReceivedView>(groupProceedsReceivedSql).ToListAsync();
  83. List<ProceedsReceivedView> NotFIDData = new List<ProceedsReceivedView>();
  84. if (dto.PortType == 1)
  85. {
  86. foreach (var item in groupReceivedList)
  87. {
  88. item._ProceedsReceivedDatas = groupProceedsReceivedList.Where(s => s.FID == item.Id).ToList();
  89. }
  90. NotFIDData = groupProceedsReceivedList.Where(s => !groupReceivedList.Any(e => s.FID == e.Id)).ToList();
  91. }
  92. result.Code = 0;
  93. result.Msg = "查询成功!";
  94. result.Data = new
  95. {
  96. GroupInfo = groupInfoData.Data,
  97. GroupCollectionStatementData = new
  98. {
  99. ReceivedData = groupReceivedList,
  100. UnallocatedData = NotFIDData
  101. }
  102. };
  103. return result;
  104. }
  105. /// <summary>
  106. /// 应收款项 删除
  107. /// </summary>
  108. /// <param name="dto"></param>
  109. /// <returns></returns>
  110. public async Task<Result> _Del(DelForForeignReceivablesInfoDto dto)
  111. {
  112. Result result = new Result() { Code = -1, Msg = "程序错误!" };
  113. _sqlSugar.BeginTran();
  114. try
  115. {
  116. var res = await _sqlSugar.Updateable<Fin_ForeignReceivables>()
  117. .Where(it => it.Id == dto.Id)
  118. .SetColumns(it => new Fin_ForeignReceivables()
  119. {
  120. IsDel = 1,
  121. DeleteUserId = dto.UserId,
  122. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  123. }
  124. ).ExecuteCommandAsync();
  125. if (res > 0)
  126. {
  127. await _sqlSugar.Updateable<Fin_ProceedsReceived>()
  128. .Where(a => a.FID == dto.Id)
  129. .SetColumns(a => new Fin_ProceedsReceived
  130. {
  131. IsDel = 1,
  132. DeleteUserId = dto.UserId,
  133. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  134. }).ExecuteCommandAsync();
  135. _sqlSugar.CommitTran();
  136. result.Msg = "删除成功!";
  137. result.Code = 0;
  138. }
  139. else
  140. {
  141. _sqlSugar.RollbackTran();
  142. result.Msg = "删除失败!";
  143. return result;
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. _sqlSugar.RollbackTran();
  149. result.Msg = ex.Message;
  150. return result;
  151. }
  152. return result;
  153. }
  154. /// <summary>
  155. /// 财务模块
  156. /// 收款账单 Add And Update
  157. /// </summary>
  158. /// <param name="diid"></param>
  159. /// <returns></returns>
  160. public async Task<Result> PostReceivablesOperate(ForeignReceivablesAddAndUpdateDto dto)
  161. {
  162. Result result = new() { Code = -2 };
  163. if (dto.foreignReceivablesInfos.Count <= 0)
  164. {
  165. result.Msg = "收款账单没有信息,不能进行,添加或修改操作!!!";
  166. return result;
  167. }
  168. int addCount = 0, updateCount = 0;
  169. if (dto.PortType == 1)
  170. {
  171. List<Fin_ForeignReceivables> _ForeignReceivables = new List<Fin_ForeignReceivables>();
  172. foreach (var item in dto.foreignReceivablesInfos)
  173. {
  174. _ForeignReceivables.Add(new Fin_ForeignReceivables()
  175. {
  176. Diid = dto.DiId,
  177. Id = item.Id,
  178. PriceName = item.PriceName,
  179. Price = item.Price,
  180. Count = item.Count,
  181. Unit = item.Unit,
  182. ItemSumPrice = item.ItemSumPrice,
  183. Rate = item.Rate,
  184. Currency = item.Currency,
  185. AddingWay = item.AddingWay,
  186. CreateUserId = dto.UserId,
  187. CreateTime = DateTime.Now,
  188. Remark = item.Remark
  189. });
  190. }
  191. if (_ForeignReceivables.Count > 0)
  192. {
  193. var x = _sqlSugar.Storageable(_ForeignReceivables).ToStorage();
  194. addCount = x.AsInsertable.ExecuteCommand(); //不存在插入
  195. updateCount = x.AsUpdateable.ExecuteCommand(); //存在更新
  196. }
  197. result.Code = 0;
  198. result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);
  199. }
  200. return result;
  201. }
  202. /// <summary>
  203. /// 根据diid查询团组应收款项
  204. /// </summary>
  205. /// <param name="diid"></param>
  206. /// <returns></returns>
  207. public async Task<Result> GetGroupReceivablesByDiid(int diid)
  208. {
  209. Result result = new() { Code = -2 };
  210. string sql = string.Format(@"Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0}", diid);
  211. var groupReceivedList = await _sqlSugar.SqlQueryable<Fin_ForeignReceivables>(sql).ToListAsync();
  212. result.Code = 0;
  213. result.Msg = "查询成功!";
  214. result.Data = groupReceivedList;
  215. return result;
  216. }
  217. /// <summary>
  218. /// 根据diid 数组 查询团组应收款项
  219. /// </summary>
  220. /// <param name="diid"></param>
  221. /// <returns></returns>
  222. public async Task<Result> GetGroupReceivablesByDiids(int[] diids)
  223. {
  224. Result result = new() { Code = -2 };
  225. //string sql = string.Format(@"Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0}", diids);
  226. var groupReceivedList = await _sqlSugar.Queryable<Fin_ForeignReceivables>()
  227. .Where(pr => pr.IsDel == 0 && diids.Contains(pr.Diid)).ToListAsync();
  228. result.Code = 0;
  229. result.Msg = "查询成功!";
  230. result.Data = groupReceivedList;
  231. return result;
  232. }
  233. #endregion
  234. #region 未关联已收款项
  235. /// <summary>
  236. /// 收款账单 数据源
  237. /// </summary>
  238. /// <returns></returns>
  239. public async Task<JsonView> PostDataSource(ForeignReceivablesDataSourcesDto dto)
  240. {
  241. JsonView result = new() { Code = StatusCodes.Status204NoContent };
  242. //已收款项 判断如果是市场部的人员进来的话 只显示自己的 其他的都显示全部的
  243. var userInfos = await _sqlSugar.Queryable<Sys_Users>()
  244. .InnerJoin<Sys_Department>((u, d) => u.DepId == d.Id)
  245. .Where((u, d) => u.IsDel == 0 && d.DepName.Contains("市场部") && u.Id == dto.CurrUserId)
  246. .ToListAsync();
  247. string sqlWhere = "";
  248. if (userInfos.Count > 0) sqlWhere = string.Format(@$" And JietuanOperator={dto.CurrUserId} ");
  249. string sql = string.Format(@$"Select Id,TeamName GroupName From Grp_DelegationInfo
  250. Where TeamName != '' And IsDel = 0 {sqlWhere}
  251. Order By Id Desc");
  252. var _groupNameList = await _sqlSugar.SqlQueryable<GroupNameView>(sql).ToListAsync();
  253. //var groupNameData = await _delegationRep.GetGroupNameList(new GroupNameDto());
  254. var currencyData = await _setDataRep.GetSetDataBySTId(_setDataRep, 66); //币种
  255. var remittanceMethodData = await _setDataRep.GetSetDataBySTId(_setDataRep, 14); //汇款方式
  256. result.Code = StatusCodes.Status200OK;
  257. result.Msg = "成功!";
  258. result.Data = new
  259. {
  260. GroupNameData = _groupNameList,
  261. CurrencyData = currencyData.Data,
  262. RemittanceMethodData = remittanceMethodData.Data
  263. };
  264. return result;
  265. }
  266. /// <summary>
  267. /// 根据diid查询团组应收款项
  268. /// </summary>
  269. /// <param name="diid"></param>
  270. /// <returns></returns>
  271. public async Task<Result> PostGroupReceivablesInfoByDiId(ForForeignReceivablesNewDto dto)
  272. {
  273. Result result = new() { Code = -2 };
  274. var groupInfoData = await _delegationRep.GetGroupInfo(new GroupInfoDto() { Id = dto.DiId });
  275. //应收款项
  276. string groupReceivedSql = string.Format(@"Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0}", dto.DiId);
  277. var groupReceivedList = await _sqlSugar.SqlQueryable<ProceedsReceivedNewView>(groupReceivedSql).ToListAsync();
  278. result.Code = 0;
  279. result.Msg = "查询成功!";
  280. result.Data = new
  281. {
  282. GroupInfo = groupInfoData.Data,
  283. GroupCollectionStatementData = groupReceivedList
  284. };
  285. return result;
  286. }
  287. /// <summary>
  288. /// 财务模块
  289. /// 收款账单 Add And Update
  290. /// </summary>
  291. /// <param name="diid"></param>
  292. /// <returns></returns>
  293. public async Task<Result> PostReceivablesSave(ForeignReceivablesSaveDto dto)
  294. {
  295. Result result = new() { Code = -2 };
  296. if (dto.foreignReceivablesInfos.Count <= 0)
  297. {
  298. result.Msg = "收款账单没有信息,不能进行,添加或修改操作!!!";
  299. return result;
  300. }
  301. int addCount = 0, updateCount = 0;
  302. if (dto.PortType == 1)
  303. {
  304. List<Fin_ForeignReceivables> _ForeignReceivables = new List<Fin_ForeignReceivables>();
  305. foreach (var item in dto.foreignReceivablesInfos)
  306. {
  307. _ForeignReceivables.Add(new Fin_ForeignReceivables()
  308. {
  309. Diid = dto.DiId,
  310. Id = item.Id,
  311. PriceName = item.PriceName,
  312. Price = item.Price,
  313. Count = item.Count,
  314. Unit = item.Unit,
  315. ItemSumPrice = item.ItemSumPrice,
  316. Rate = item.Rate,
  317. Currency = item.Currency,
  318. AddingWay = item.AddingWay,
  319. CreateUserId = dto.UserId,
  320. CreateTime = DateTime.Now,
  321. Remark = item.Remark
  322. });
  323. }
  324. if (_ForeignReceivables.Count > 0)
  325. {
  326. var x = _sqlSugar.Storageable(_ForeignReceivables).ToStorage();
  327. addCount = x.AsInsertable.ExecuteCommand(); //不存在插入
  328. updateCount = x.AsUpdateable.ExecuteCommand(); //存在更新
  329. }
  330. result.Code = 0;
  331. result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);
  332. }
  333. return result;
  334. }
  335. /// <summary>
  336. /// 财务模块
  337. /// 收款账单
  338. /// Del
  339. /// </summary>
  340. /// <param name="diid"></param>
  341. /// <returns></returns>
  342. public async Task<Result> PostReceivablesDel(ForeignReceivablesDelDto dto)
  343. {
  344. Result result = new() { Code = -2 };
  345. var delStatus = await _sqlSugar.Updateable<Fin_ForeignReceivables>()
  346. .SetColumns(a => new Fin_ForeignReceivables
  347. {
  348. IsDel = 1,
  349. DeleteUserId = dto.UserId,
  350. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  351. })
  352. .Where(a => a.Id == dto.Id)
  353. .ExecuteCommandAsync();
  354. if (delStatus > 0)
  355. {
  356. result.Msg = "操作成功!";
  357. result.Code = 0;
  358. }
  359. else
  360. {
  361. result.Msg = "操作成功!";
  362. }
  363. return result;
  364. }
  365. #endregion
  366. }
  367. }