StatisticsController.cs 14 KB

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