FinanceRepository.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using AutoMapper;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Exceptions;
  9. using GxPress.Common.Page;
  10. using GxPress.Common.Tools;
  11. using GxPress.Entity;
  12. using GxPress.Repository.Interface;
  13. using GxPress.Request.Finance;
  14. using GxPress.Result.Finance;
  15. using GxPress.Result.HumanAffairs;
  16. using Microsoft.Extensions.Options;
  17. using Datory;
  18. namespace GxPress.Repository.Implement
  19. {
  20. /// <summary>
  21. /// 财务数据
  22. /// </summary>
  23. public class FinanceRepository : IFinanceRepository
  24. {
  25. private readonly Repository<Finance> _repository;
  26. public FinanceRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  27. {
  28. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<Finance>(database);
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. /// <summary>
  36. /// 添加财务数据
  37. /// </summary>
  38. /// <param name="finances"></param>
  39. /// <returns></returns>
  40. public async Task<bool> InsertAsync(IEnumerable<Finance> finances)
  41. {
  42. try
  43. {
  44. using (TransactionScope transactionScope = new TransactionScope())
  45. {
  46. foreach (var finance in finances)
  47. {
  48. await _repository.InsertAsync(finance);
  49. }
  50. transactionScope.Complete();
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. throw new BusinessException(e.Message);
  56. }
  57. return true;
  58. }
  59. /// <summary>
  60. /// 分页显示
  61. /// </summary>
  62. /// <returns></returns>
  63. public async Task<PagedList<Finance>> GetPageListAsync(PageParameter request)
  64. {
  65. var result = new PagedList<Finance>
  66. {
  67. Items = await _repository.GetAllAsync(Q.ForPage(request.Page, request.PerPage)),
  68. Total = await _repository.CountAsync()
  69. };
  70. return result;
  71. }
  72. /// <summary>
  73. /// 获取财务图像
  74. /// </summary>
  75. /// <param name="request"></param>
  76. /// <returns></returns>
  77. public async Task<FinanceResult> GetFinanceChartAsync(FinanceRequest request)
  78. {
  79. var result = new FinanceResult();
  80. //获取当前时间
  81. DateTime dt = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
  82. DateTime dtNow = DateTime.Now;
  83. //当前星期几
  84. int weekNow = Convert.ToInt32(DateTime.Now.DayOfWeek);
  85. result.HumanAffairsChartDataResult = new List<HumanAffairsChartData>();
  86. //上月
  87. var a = await _repository.GetAllAsync(Q.Where(nameof(Finance.FinanceType), request.DataType)
  88. .WhereDate(nameof(Finance.EnteringDateTime), "<=", dt.AddDays(-dt.Day))
  89. .WhereDate(nameof(Finance.EnteringDateTime), ">=",
  90. dt.AddDays(-dt.Day).AddMonths(1)));
  91. //本月
  92. var b = await _repository.GetAllAsync(Q.Where(nameof(Finance.FinanceType), request.DataType)
  93. .WhereDate(nameof(Finance.EnteringDateTime), "<=", dtNow)
  94. .WhereDate(nameof(Finance.EnteringDateTime), ">=",
  95. dt.AddDays(-dt.Day)));
  96. //今天
  97. var c = await _repository.GetAllAsync(Q.Where(nameof(Finance.FinanceType), request.DataType)
  98. .WhereDate(nameof(Finance.EnteringDateTime), "<=", dtNow)
  99. .WhereDate(nameof(Finance.EnteringDateTime), ">=", dt));
  100. //同比增长
  101. result.ComparedMonthAmount = b.Sum(n => n.Amount) - a.Sum(n => n.Amount);
  102. if (request.DateType == 1)
  103. //今天收入
  104. result.ToDayIncome = c.Sum(n => n.Amount);
  105. else if (request.DateType == 2)
  106. {
  107. var weekDt = dt;
  108. //本周
  109. if (dtNow.DayOfWeek.GetHashCode() == 0)
  110. weekDt = dt.AddDays(-7);
  111. else
  112. weekDt = dt.AddDays(-dtNow.DayOfWeek.GetHashCode());
  113. c = await _repository.GetAllAsync(Q.Where(nameof(Finance.FinanceType), request.DataType)
  114. .WhereDate(nameof(Finance.EnteringDateTime), "<=", dtNow)
  115. .WhereDate(nameof(Finance.EnteringDateTime), ">=", weekDt));
  116. //本周收入
  117. result.ToDayIncome = c.Sum(n => n.Amount);
  118. }
  119. //月
  120. else if (request.DateType == 3)
  121. {
  122. var mouthDt = dt;
  123. mouthDt = dt.AddDays(-dt.Day);
  124. c = await _repository.GetAllAsync(Q.Where(nameof(Finance.FinanceType), request.DataType)
  125. .WhereDate(nameof(Finance.EnteringDateTime), "<=", dtNow)
  126. .WhereDate(nameof(Finance.EnteringDateTime), ">=", mouthDt));
  127. //本月收入
  128. result.ToDayIncome = c.Sum(n => n.Amount);
  129. }
  130. else if (request.DateType == 4)
  131. {
  132. var yearDt = dt;
  133. yearDt = dt.AddMonths(-dt.Month).AddDays(-dt.Day);
  134. c = await _repository.GetAllAsync(Q.Where(nameof(Finance.FinanceType), request.DataType)
  135. .WhereDate(nameof(Finance.EnteringDateTime), "<=", dtNow)
  136. .WhereDate(nameof(Finance.EnteringDateTime), ">=", yearDt));
  137. //本年收入
  138. result.ToDayIncome = c.Sum(n => n.Amount);
  139. }
  140. //计算周
  141. if (request.DateType == 2)
  142. {
  143. var beginDat = dt;
  144. for (int i = 1; i <= 7; i++)
  145. {
  146. var week = (i - 1) * -7;
  147. var endDay = beginDat.AddDays(-weekNow + week + 1);
  148. var query = Q.NewQuery();
  149. var humanAffairsWeekList = await _repository.GetAllAsync(query.Where(nameof(Finance.FinanceType), request.DataType)
  150. .WhereDate(nameof(Finance.EnteringDateTime), "<=", beginDat)
  151. .WhereDate(nameof(Finance.EnteringDateTime), ">=", endDay));
  152. var humanAffairsChartData = new HumanAffairsChartData
  153. {
  154. NavigationNumber = i,
  155. ResultParameter = humanAffairsWeekList.Sum(n => n.Amount)
  156. };
  157. result.HumanAffairsChartDataResult.Add(humanAffairsChartData);
  158. beginDat = endDay;
  159. }
  160. }
  161. //天数
  162. if (request.DateType == 1)
  163. {
  164. var beginDat = dt;
  165. for (int i = 1; i <= 7; i++)
  166. {
  167. var endDay = beginDat.AddDays(-i);
  168. var query = Q.NewQuery();
  169. var humanAffairsDayList = await _repository.GetAllAsync(query.Where(nameof(Finance.FinanceType), request.DataType)
  170. .WhereDate(nameof(HumanAffairs.EnteringDateTime), "<=", beginDat)
  171. .WhereDate(nameof(HumanAffairs.EnteringDateTime), ">=", endDay));
  172. var humanAffairsChartData = new HumanAffairsChartData
  173. {
  174. NavigationNumber = i,
  175. ResultParameter = humanAffairsDayList.Sum(n => n.Amount)
  176. };
  177. result.HumanAffairsChartDataResult.Add(humanAffairsChartData);
  178. beginDat = endDay;
  179. }
  180. }
  181. //月
  182. if (request.DateType == 3)
  183. {
  184. var beginDat = dt;
  185. for (int i = 1; i <= 7; i++)
  186. {
  187. var days = beginDat.Day;
  188. var endDay = dt.AddDays(-days).AddMonths(-i);
  189. var query = Q.NewQuery();
  190. var humanAffairsMonthList = await _repository.GetAllAsync(query.Where(nameof(Finance.FinanceType), request.DataType)
  191. .WhereDate(nameof(HumanAffairs.EnteringDateTime), "<=", beginDat)
  192. .WhereDate(nameof(HumanAffairs.EnteringDateTime), ">=", endDay));
  193. var humanAffairsChartData = new HumanAffairsChartData
  194. {
  195. NavigationNumber = i,
  196. ResultParameter = humanAffairsMonthList.Sum(n => n.Amount)
  197. };
  198. result.HumanAffairsChartDataResult.Add(humanAffairsChartData);
  199. beginDat = endDay;
  200. }
  201. }
  202. //年
  203. if (request.DateType == 4)
  204. {
  205. var beginDat = dt;
  206. for (int i = 1; i <= 7; i++)
  207. {
  208. var days = beginDat.DayOfYear;
  209. var endDay = dt.AddDays(-days).AddYears(-i);
  210. var query = Q.NewQuery();
  211. var humanAffairsYearList = await _repository.GetAllAsync(query.Where(nameof(Finance.FinanceType), request.DataType)
  212. .WhereDate(nameof(HumanAffairs.EnteringDateTime), "<=", beginDat)
  213. .WhereDate(nameof(HumanAffairs.EnteringDateTime), ">=", endDay));
  214. var humanAffairsChartData = new HumanAffairsChartData
  215. {
  216. NavigationNumber = i,
  217. ResultParameter = humanAffairsYearList.Sum(n => n.Amount)
  218. };
  219. result.HumanAffairsChartDataResult.Add(humanAffairsChartData);
  220. beginDat = endDay;
  221. }
  222. }
  223. return result;
  224. }
  225. }
  226. }