StatisticsController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using Microsoft.AspNetCore.Mvc;
  2. using NPOI.POIFS.Properties;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.API.OAMethodLib;
  5. using OASystem.Domain.Dtos.Groups;
  6. using OASystem.Domain.Dtos.Statistics;
  7. using OASystem.Domain.Entities.Financial;
  8. using OASystem.Domain.ViewModels.Financial;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Domain.ViewModels.Statistics;
  11. using OASystem.Infrastructure.Repositories.Groups;
  12. namespace OASystem.API.Controllers
  13. {
  14. /// <summary>
  15. /// 统计模块
  16. /// </summary>
  17. [Route("api/[controller]")]
  18. [ApiController]
  19. public class StatisticsController : ControllerBase
  20. {
  21. private readonly IMapper _mapper;
  22. private readonly SqlSugarClient _sqlSugar;
  23. private readonly DelegationInfoRepository _groupRep;
  24. private readonly SetDataRepository _setDataRep;
  25. /// <summary>
  26. /// Init
  27. /// </summary>
  28. /// <param name="mapper"></param>
  29. /// <param name="sqlSugar"></param>
  30. /// <param name="groupRep"></param>
  31. /// <param name="setDataRep"></param>
  32. public StatisticsController(IMapper mapper, SqlSugarClient sqlSugar, DelegationInfoRepository groupRep, SetDataRepository setDataRep)
  33. {
  34. _mapper = mapper;
  35. _groupRep = groupRep;
  36. _setDataRep = setDataRep;
  37. _sqlSugar = sqlSugar;
  38. }
  39. #region 团组报表
  40. /// <summary>
  41. /// 团组报表
  42. /// Items
  43. /// </summary>
  44. /// <param name="_dto">团组列表请求dto</param>
  45. /// <returns></returns>
  46. [HttpPost("PostGroupStatementItems")]
  47. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  48. public async Task<IActionResult> PostGroupStatementItems(GroupStatementItemsDto _dto)
  49. {
  50. #region 参数验证
  51. if (_dto.UserId < 1) return Ok(JsonView(false, "员工Id为空"));
  52. if (_dto.PageId < 1) return Ok(JsonView(false, "页面Id为空"));
  53. PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
  54. #region 页面操作权限验证
  55. pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(_dto.UserId, _dto.PageId);
  56. if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限"));
  57. #endregion
  58. #endregion
  59. if (_dto.PortType == 1 || _dto.PortType == 2 || _dto.PortType == 3) // web/Android/IOS
  60. {
  61. string sqlWhere = string.Empty;
  62. if (_dto.IsSure == 0) //未完成
  63. {
  64. sqlWhere += string.Format(@" And IsSure = 0");
  65. }
  66. else if (_dto.IsSure == 1) //已完成
  67. {
  68. sqlWhere += string.Format(@" And IsSure = 1");
  69. }
  70. if (!string.IsNullOrEmpty(_dto.SearchCriteria))
  71. {
  72. string tj = _dto.SearchCriteria;
  73. sqlWhere += string.Format(@"And (ssd.Name Like '%{0}%' Or TeamName Like '%{1}%' Or ClientName Like '%{2}%' Or ClientName Like '%{3}%' Or su.CnName Like '%{4}%')",
  74. tj, tj, tj, tj, tj);
  75. }
  76. string sql = string.Format(@"Select row_number() over(order by gdi.VisitDate Desc) as Row_Number,
  77. gdi.Id,TourCode,ssd1.Id TeamLevId,ssd1.Name TeamLev,TeamName,
  78. ClientName,ClientUnit,VisitDate,ssd.Id TeamTypeId, ssd.Name TeamType,
  79. VisitDays,VisitPNumber,su.CnName JietuanOperator,IsSure,gdi.CreateTime,
  80. pr.LastCollectionTime
  81. From Grp_DelegationInfo gdi
  82. Left Join Sys_SetData ssd On gdi.TeamDid = ssd.Id
  83. Left Join Sys_SetData ssd1 On gdi.TeamLevSId = ssd1.Id
  84. Left Join Sys_Users su On gdi.JietuanOperator = su.Id
  85. Left Join (
  86. SELECT Diid, MAX(CreateTime) LastCollectionTime
  87. FROM Fin_ProceedsReceived
  88. Where IsDel = 0
  89. GROUP BY Diid
  90. ) pr On gdi.Id = pr.Diid
  91. Where gdi.IsDel = 0 {0} ", sqlWhere);
  92. RefAsync<int> total = 0;//REF和OUT不支持异步,想要真的异步这是最优解
  93. var _DelegationList = await _sqlSugar.SqlQueryable<GroupStatementItemView>(sql).ToPageListAsync(_dto.PageIndex, _dto.PageSize, total);//ToPageAsync
  94. var _view = new
  95. {
  96. PageFuncAuth = pageFunAuthView,
  97. Data = _DelegationList
  98. };
  99. return Ok(JsonView(true, "查询成功!", _view, total));
  100. }
  101. else
  102. {
  103. return Ok(JsonView(false, "查询失败"));
  104. }
  105. }
  106. /// <summary>
  107. /// 团组报表
  108. /// Details
  109. /// </summary>
  110. /// <param name="_dto">团组列表请求dto</param>
  111. /// <returns></returns>
  112. [HttpPost("PostGroupStatementDetails")]
  113. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  114. public async Task<IActionResult> PostGroupStatementDetails(GroupStatementDetailsDto _dto)
  115. {
  116. #region 参数验证
  117. if (_dto.UserId < 1) return Ok(JsonView(false, "员工Id为空"));
  118. if (_dto.PageId < 1) return Ok(JsonView(false, "页面Id为空"));
  119. if (_dto.DiId < 1) return Ok(JsonView(false, "团组Id为空"));
  120. PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
  121. #region 页面操作权限验证
  122. pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(_dto.UserId, _dto.PageId);
  123. if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限"));
  124. #endregion
  125. #endregion
  126. if (_dto.PortType == 1 || _dto.PortType == 2 || _dto.PortType == 3) // web/Android/IOS
  127. {
  128. GroupStatementDetailsView _view = new GroupStatementDetailsView();
  129. #region 费用类型 币种,转账
  130. List<Sys_SetData> _setDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToListAsync();
  131. #endregion
  132. #region 团组收入
  133. GroupIncomeView _giView = new GroupIncomeView();
  134. /*
  135. * 应收报表
  136. */
  137. decimal frTotalAmount = 0.00M;//应收总金额
  138. string _frSql = string.Format(@"Select fr.Id,fr.Diid,fr.PriceName,fr.Price,fr.Count,fr.Unit,fr.Currency,
  139. sd.Name As CurrencyCode,sd.Remark As CurrencyName,fr.Rate,fr.ItemSumPrice
  140. From Fin_ForeignReceivables fr
  141. Left Join Sys_SetData sd On fr.Currency = sd.Id
  142. Where fr.IsDel = 0 And fr.Diid = {0}",_dto.DiId);
  143. List<Gsd_ForeignReceivablesView> _frViews = await _sqlSugar.SqlQueryable<Gsd_ForeignReceivablesView>(_frSql).ToListAsync();
  144. frTotalAmount = _frViews.Sum(it => it.ItemSumPrice);
  145. _giView.Receivables = _frViews;
  146. _giView.ReceivableStr = string.Format(@"应收款合计:{0} CNY(人名币)", frTotalAmount.ConvertToDecimal1().ToString("#0.00"));
  147. /*
  148. * 已收报表
  149. */
  150. decimal prTotalAmount = 0.00M;//已收总金额
  151. string _prSql = string.Format(@"Select pr.Id,pr.Diid,pr.SectionTime,pr.Price,pr.Currency,
  152. sd1.Name As CurrencyCode,sd1.Remark As CurrencyName,pr.Client,
  153. pr.ReceivablesType,sd2.Name As ReceivablesTypeName,pr.Remark
  154. From Fin_ProceedsReceived pr
  155. Left Join Sys_SetData sd1 On pr.Currency = sd1.Id
  156. Left Join Sys_SetData sd2 On pr.ReceivablesType = sd2.Id
  157. Where pr.IsDel = 0 and pr.Diid = {0}", _dto.DiId);
  158. List<Gsd_ProceedsReceivedView> _prViews = await _sqlSugar.SqlQueryable<Gsd_ProceedsReceivedView>(_prSql).ToListAsync();
  159. prTotalAmount = _prViews.Sum(it => it.Price);
  160. _giView.ProceedsReceiveds = _prViews;
  161. _giView.ProceedsReceivedStr = string.Format(@"已收款合计:{0} CNY(人名币)", prTotalAmount.ConvertToDecimal1().ToString("#0.00"));
  162. /*
  163. * 超支费用
  164. * 未挪表
  165. */
  166. /*
  167. * 收款退还
  168. */
  169. decimal promTotalAmount = 0.00M;// 收款退还总金额
  170. List<Gsd_PaymentRefundAndOtherMoneyView> _promView = new List<Gsd_PaymentRefundAndOtherMoneyView>();
  171. string _ropSql = string.Format(@"Select u.CnName As Appliction,prom.Id As PrId,prom.DiId As PrDiId,prom.Price As PrPrice,
  172. prom.PriceName AS PrPriceName,prom.CurrencyId As PrCurrencyId,
  173. prom.PayType As PrPayType,prom.PriceType As PrPriceType,ccp.*
  174. From Fin_PaymentRefundAndOtherMoney prom
  175. Left Join Grp_CreditCardPayment ccp On prom.DiId = ccp.DIId And prom.Id = ccp.CId
  176. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  177. Where prom.IsDel = 0 And prom.PayType = 1 And prom.PriceType = 1 And ccp.CTable = 285
  178. And ccp.IsAuditGM = 1 And ccp.IsPay = 1
  179. And prom.DiId = {0}", _dto.DiId);
  180. var _promDatas = await _sqlSugar.SqlQueryable<Gsd_PaymentRefundAndOtherMoneyDataSource1View>(_ropSql).ToListAsync();
  181. foreach (var ropItem in _promDatas)
  182. {
  183. string thisCueencyCode = "Unknown";
  184. string thisCueencyName = "Unknown";
  185. var currency = _setDatas.Where(it => it.Id == ropItem.PaymentCurrency).FirstOrDefault();
  186. if (currency != null)
  187. {
  188. thisCueencyCode = currency.Name;
  189. thisCueencyName = currency.Remark;
  190. }
  191. string orbitalPrivateTransferStr = "Unknown";
  192. var orbitalPrivateTransfer = _setDatas.Where(it => it.Id == ropItem.OrbitalPrivateTransfer).FirstOrDefault();
  193. if (orbitalPrivateTransfer != null)
  194. {
  195. orbitalPrivateTransferStr = orbitalPrivateTransfer.Name;
  196. }
  197. string payStr = "Unknown";
  198. var pay = _setDatas.Where(it => it.Id == ropItem.PayDId).FirstOrDefault();
  199. if (pay != null)
  200. {
  201. payStr = pay.Name;
  202. }
  203. Gsd_PaymentRefundAndOtherMoneyView gsd_PaymentRefund = new Gsd_PaymentRefundAndOtherMoneyView()
  204. {
  205. Id = ropItem.Id,
  206. DiId = ropItem.DIId,
  207. PriceName = ropItem.PrPriceName,
  208. PayCurrencyCode = thisCueencyCode,
  209. PayCurrencyName = thisCueencyName,
  210. Price = ropItem.PrPrice,
  211. CNYPrice = ropItem.RMBPrice,
  212. ThisRate = ropItem.DayRate,
  213. Payee = ropItem.Payee,
  214. PayTime = ropItem.AuditGMDate,
  215. FeeType = orbitalPrivateTransferStr,
  216. PayType = payStr,
  217. PayStatus = ropItem.IsPay == 0 ? "未付款" : "已付款",
  218. Applicant = ropItem.Appliction
  219. };
  220. _promView.Add(gsd_PaymentRefund);
  221. }
  222. promTotalAmount = _promView.Sum(it => it.CNYPrice);
  223. _giView.PaymentRefundAndOtherMoneys = _promView;
  224. _giView.PaymentRefundAndOtherMoneyStr = string.Format(@"人名币总费用:{0} CNY", promTotalAmount.ConvertToDecimal1().ToString("#0.00"));
  225. decimal BalancePayment = frTotalAmount - prTotalAmount + promTotalAmount;
  226. _view.GroupIncome = _giView;
  227. _view.GroupIncomeStr = string.Format(@"剩余尾款:{0} (包含了收款退还费用数据)", BalancePayment.ConvertToDecimal1().ToString("#0.00"));
  228. #endregion
  229. return Ok(JsonView(true, "查询成功!", _view));
  230. }
  231. else
  232. {
  233. return Ok(JsonView(false, "查询成功"));
  234. }
  235. }
  236. #endregion
  237. }
  238. }