ForeignReceivablesRepository.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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<JsonView> PostGroupReceivablesInfoByDiId(ForForeignReceivablesNewDto dto)
  272. {
  273. JsonView result = new() { Code = 400,Msg="" };
  274. var groupInfoData = await _delegationRep.GetGroupInfo(new GroupInfoDto() { Id = dto.DiId });
  275. //应收款项
  276. string groupReceivedSql = string.Format(@"Select *,su.CnName As AuditorName From Fin_ForeignReceivables ffr
  277. Left Join Sys_Users su On ffr.Auditor = su.Id
  278. Where ffr.IsDel=0 And ffr.Diid={0}", dto.DiId);
  279. var groupReceivedList = await _sqlSugar.SqlQueryable<ProceedsReceivedNewView>(groupReceivedSql).ToListAsync();
  280. result.Code = 200;
  281. result.Msg = "查询成功!";
  282. result.Data = new
  283. {
  284. GroupInfo = groupInfoData.Data,
  285. GroupCollectionStatementData = groupReceivedList
  286. };
  287. return result;
  288. }
  289. /// <summary>
  290. /// 财务模块
  291. /// 收款账单 Add And Update
  292. /// </summary>
  293. /// <param name="diid"></param>
  294. /// <returns></returns>
  295. public async Task<JsonView> PostReceivablesSave(ForeignReceivablesSaveDto dto)
  296. {
  297. JsonView result = new() { Code = 4002 };
  298. if (dto.foreignReceivablesInfos.Count <= 0)
  299. {
  300. result.Msg = "收款账单没有信息,不能进行,添加或修改操作!!!";
  301. return result;
  302. }
  303. int addCount = 0, updateCount = 0;
  304. if (dto.PortType == 1)
  305. {
  306. List<Fin_ForeignReceivables> _ForeignReceivables = new List<Fin_ForeignReceivables>();
  307. foreach (var item in dto.foreignReceivablesInfos)
  308. {
  309. _ForeignReceivables.Add(new Fin_ForeignReceivables()
  310. {
  311. Diid = dto.DiId,
  312. Id = item.Id,
  313. PriceName = item.PriceName,
  314. Price = item.Price,
  315. Count = item.Count,
  316. Unit = item.Unit,
  317. ItemSumPrice = item.ItemSumPrice,
  318. Rate = item.Rate,
  319. Currency = item.Currency,
  320. AddingWay = item.AddingWay,
  321. IsAudit = item.AddingWay == 2 ? 1 : 0,
  322. CreateUserId = dto.UserId,
  323. CreateTime = DateTime.Now,
  324. Remark = item.Remark
  325. });
  326. }
  327. if (_ForeignReceivables.Count > 0)
  328. {
  329. var x = _sqlSugar.Storageable(_ForeignReceivables).ToStorage();
  330. addCount = x.AsInsertable.ExecuteCommand(); //不存在插入
  331. updateCount = x.AsUpdateable.ExecuteCommand(); //存在更新
  332. }
  333. result.Code = 200;
  334. result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);
  335. }
  336. return result;
  337. }
  338. /// <summary>
  339. /// 财务模块
  340. /// 收款账单
  341. /// Audit
  342. /// </summary>
  343. /// <param name="diid"></param>
  344. /// <returns></returns>
  345. public async Task<JsonView> FeeAudit(FeeAuditDto dto)
  346. {
  347. JsonView result = new() { Code = 400 };
  348. //验证
  349. var info = await _sqlSugar.Queryable<Fin_ForeignReceivables>().FirstAsync(x => x.Id == dto.Id);
  350. if (info == null)
  351. {
  352. result.Msg = "数据不存在";
  353. return result;
  354. }
  355. if (info.AddingWay != 2)
  356. {
  357. result.Msg = "该条数据类型不是“实际报价”类型不可审核!";
  358. return result;
  359. }
  360. if (info.IsAudit == 0)
  361. {
  362. result.Msg = "该条数据类型不可审核!";
  363. return result;
  364. }
  365. var AuditStatus = await _sqlSugar.Updateable<Fin_ForeignReceivables>()
  366. .SetColumns(a => new Fin_ForeignReceivables
  367. {
  368. Status = dto.Status,
  369. Auditor = dto.UserId,
  370. AuditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  371. })
  372. .Where(a => a.Id == dto.Id)
  373. .ExecuteCommandAsync();
  374. if (AuditStatus > 0)
  375. {
  376. result.Msg = "操作成功!";
  377. result.Code = 0;
  378. }
  379. else
  380. {
  381. result.Msg = "操作成功!";
  382. }
  383. return result;
  384. }
  385. /// <summary>
  386. /// 财务模块
  387. /// 收款账单
  388. /// Del
  389. /// </summary>
  390. /// <param name="diid"></param>
  391. /// <returns></returns>
  392. public async Task<Result> PostReceivablesDel(ForeignReceivablesDelDto dto)
  393. {
  394. Result result = new() { Code = -2 };
  395. var delStatus = await _sqlSugar.Updateable<Fin_ForeignReceivables>()
  396. .SetColumns(a => new Fin_ForeignReceivables
  397. {
  398. IsDel = 1,
  399. DeleteUserId = dto.UserId,
  400. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  401. })
  402. .Where(a => a.Id == dto.Id)
  403. .ExecuteCommandAsync();
  404. if (delStatus > 0)
  405. {
  406. result.Msg = "操作成功!";
  407. result.Code = 0;
  408. }
  409. else
  410. {
  411. result.Msg = "操作成功!";
  412. }
  413. return result;
  414. }
  415. #endregion
  416. }
  417. }