SellRepository.cs 10 KB

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