RecordRepository.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Dapper;
  2. using GCAS.Dto;
  3. using GCAS.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace GCAS.Code
  11. {
  12. public class RecordRepository
  13. {
  14. public async Task<List<SourceChartDto>> GetChartSource(DateTime? startTime, DateTime? endTime)
  15. {
  16. string selectSql = "SELECT id,netweight,time,entrance,team,device from record where 1=1 ";
  17. if (startTime != null && endTime != null)
  18. {
  19. selectSql += " and time>@StartTime and time<@EndTime ";
  20. }
  21. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  22. {
  23. return await Task.Run(() => conn.Query<SourceChartDto>(selectSql, new { StartTime = startTime, EndTime = endTime }).ToList());
  24. }
  25. }
  26. public async Task<List<RecordDto>> GetList(string keyWord, int device, int entrance, int team, DateTime? startTime, DateTime? endTime)
  27. {
  28. string selectSql = @"SELECT record.id,record.number,record.netweight/1000 AS netweight,record.device, device.name as devicename,record.team, team.name as teamname,record.entrance, entrance.name as entrancename,record.time FROM record
  29. LEFT JOIN device ON record.device = device.id
  30. LEFT JOIN team ON record.team = team.id
  31. LEFT JOIN entrance ON record.entrance = entrance.id
  32. WHERE 1=1 ";
  33. if (!string.IsNullOrEmpty(keyWord))
  34. {
  35. selectSql += " and record.number like @KeyWord ";
  36. }
  37. if (startTime != null && endTime != null)
  38. {
  39. selectSql += " and record.time>@StartTime and record.time<@EndTime ";
  40. }
  41. if (device != 0)
  42. {
  43. selectSql += " and record.device=@Device ";
  44. }
  45. if (entrance != 0)
  46. {
  47. selectSql += " and record.entrance=@Entrance ";
  48. }
  49. if (team != 0)
  50. {
  51. selectSql += " and record.team=@Team ";
  52. }
  53. selectSql += " order by record.id desc";
  54. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  55. {
  56. var list = await Task.Run(() => conn.Query<RecordDto>(selectSql, new { KeyWord = "%" + keyWord + "%", StartTime = startTime, Device = device, Entrance = entrance, Team = team, EndTime = endTime }).ToList());
  57. return list;
  58. }
  59. }
  60. public async Task<int> Delete(int id)
  61. {
  62. string deleteSql = "DELETE FROM record WHERE id=@Id";
  63. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  64. {
  65. return await Task.Run(() => conn.ExecuteAsync(deleteSql, new { Id = id }));
  66. }
  67. }
  68. public RecordModel Insert(RecordModel entity)
  69. {
  70. string numberSql = "SELECT CONCAT(DATE_FORMAT(NOW(), '%Y%m%d'),LPAD((COUNT(1) + 1), 5, 0))FROM record WHERE time>DATE(NOW())";
  71. string insertSql = "INSERT INTO record (number,time,grossweight,tareweight,netweight,device,entrance,team)" +
  72. "VALUES(@Number,NOW(),@GrossWeight,@TareWeight,@NetWeight,@Device,@Entrance,@Team)";
  73. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  74. {
  75. var number = conn.QueryFirst<string>(numberSql);
  76. entity.Number = number;
  77. int count = conn.Execute(insertSql, entity);
  78. if (count > 0)
  79. {
  80. return entity;
  81. }
  82. }
  83. return null;
  84. }
  85. public async Task<TotalDto> TotalStatistics()
  86. {
  87. TotalDto output = new TotalDto();
  88. string selectSql = @"SELECT IFNULL(sum(r.netweight),0)AS dailyTotal FROM record AS r WHERE r.time > CURDATE();";
  89. if (Thread.CurrentThread.CurrentUICulture.Name == "zh-Hans")
  90. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS weeklyTotal FROM record AS r WHERE r.time > DATE_FORMAT(SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-1), '%Y-%m-%d 00:00:00');";
  91. else
  92. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS weeklyTotal FROM record AS r WHERE r.time > DATE_FORMAT(SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')), '%Y-%m-%d 00:00:00');";
  93. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS monthlyTotal FROM record AS r WHERE r.time > DATE_FORMAT(CURDATE(), '%Y-%m-01 00:00:00'); ";
  94. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS annualTotal FROM record AS r WHERE r.time > DATE_FORMAT(CURDATE(), '%Y-01-01 00:00:00'); ";
  95. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  96. {
  97. using (var multi = await Task.Run(() => conn.QueryMultiple(selectSql)))
  98. {
  99. output.Daily = multi.ReadFirst<int>();
  100. output.Weekly = multi.ReadFirst<int>();
  101. output.Monthly = multi.ReadFirst<int>();
  102. output.AnnualTotal = multi.ReadFirst<int>();
  103. }
  104. }
  105. return output;
  106. }
  107. public TotalDto TotalStatisticsAB()
  108. {
  109. TotalDto output = new TotalDto();
  110. string selectSql = @"SELECT IFNULL(sum(r.netweight),0)AS dailyTotal FROM record AS r WHERE entrance=1 AND r.time > CURDATE();";
  111. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS dailyTotal FROM record AS r WHERE entrance=2 AND r.time > CURDATE();";
  112. if (Thread.CurrentThread.CurrentUICulture.Name == "zh-Hans")
  113. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS weeklyTotal FROM record AS r WHERE r.time > DATE_FORMAT(SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-1), '%Y-%m-%d 00:00:00');";
  114. else
  115. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS weeklyTotal FROM record AS r WHERE r.time > DATE_FORMAT(SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')), '%Y-%m-%d 00:00:00');";
  116. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS monthlyTotal FROM record AS r WHERE r.time > DATE_FORMAT(CURDATE(), '%Y-%m-01 00:00:00'); ";
  117. selectSql += @"SELECT IFNULL(sum(r.netweight),0)AS annualTotal FROM record AS r WHERE r.time > DATE_FORMAT(CURDATE(), '%Y-01-01 00:00:00'); ";
  118. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  119. {
  120. using (var multi = conn.QueryMultiple(selectSql))
  121. {
  122. output.Daily = multi.ReadFirst<int>();
  123. output.DailyB = multi.ReadFirst<int>();
  124. output.Weekly = multi.ReadFirst<int>();
  125. output.Monthly = multi.ReadFirst<int>();
  126. output.AnnualTotal = multi.ReadFirst<int>();
  127. }
  128. }
  129. return output;
  130. }
  131. public async Task<List<DailyStatisticDto>> DailyStatistics(int teamId, int deviceId)
  132. {
  133. string selectSql = @"SELECT
  134. entrance.id,
  135. entrance.name,
  136. (SELECT
  137. COUNT(1)
  138. FROM
  139. record AS r
  140. WHERE
  141. r.time > CURDATE()
  142. AND r.entrance = record.entrance
  143. AND r.device = @DeviceId) AS dailycount,
  144. SUM( IFNULL( record.netweight, 0 ) ) AS dailytotal,
  145. (SELECT
  146. Count(1)
  147. FROM
  148. record AS r
  149. WHERE
  150. r.time > CURDATE() AND r.team = @TeamId
  151. AND r.entrance = record.entrance
  152. AND r.device = @DeviceId) AS teamcount,
  153. (
  154. SELECT
  155. IFNULL( sum( r.netweight ), 0 )
  156. FROM
  157. record AS r
  158. WHERE
  159. r.time > CURDATE()
  160. AND r.team = @TeamId
  161. AND r.entrance = record.entrance
  162. AND r.device=@DeviceId
  163. ) AS teamtotal
  164. FROM
  165. entrance
  166. LEFT JOIN record ON entrance.id = record.entrance
  167. AND record.time > CURDATE()
  168. AND device = @DeviceId
  169. GROUP BY
  170. entrance.id,
  171. entrance.name";
  172. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  173. {
  174. var result = await Task.Run(() => conn.QueryAsync<DailyStatisticDto>(selectSql, new { TeamId = teamId, DeviceId = deviceId }));
  175. return result.ToList();
  176. }
  177. }
  178. public async Task<List<DailyStatisticDto>> DailyStatistics(int deviceId)
  179. {
  180. string selectSql = @"SELECT
  181. team.id,
  182. team.NAME,
  183. (
  184. SELECT
  185. COUNT( 1 )
  186. FROM
  187. record AS r
  188. WHERE
  189. r.time > CURDATE()
  190. AND r.entrance = record.entrance
  191. AND r.device = @DeviceId
  192. ) AS dailycount,
  193. SUM( IFNULL( record.netweight, 0 ) )/1000 AS dailytotal
  194. FROM
  195. team
  196. LEFT JOIN record ON team.id = record.team
  197. AND record.time > CURDATE()
  198. AND device = @DeviceId
  199. GROUP BY
  200. team.id,
  201. team.NAME";
  202. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  203. {
  204. var result = await Task.Run(() => conn.QueryAsync<DailyStatisticDto>(selectSql, new { DeviceId = deviceId }));
  205. return result.ToList();
  206. }
  207. }
  208. public async Task<List<DailyStatisticDto>> DailyTotalStatistics(int teamId)
  209. {
  210. string selectSql = @"SELECT
  211. entrance.id,
  212. entrance.name,
  213. COUNT(record.id) AS dailycount,
  214. SUM( IFNULL( record.netweight, 0 ) ) AS dailytotal,
  215. (
  216. SELECT
  217. count(1)
  218. FROM
  219. record AS r
  220. WHERE
  221. r.time > CURDATE()
  222. AND r.team = @TeamId
  223. AND r.entrance = record.entrance
  224. ) AS teamcount,
  225. (
  226. SELECT
  227. IFNULL( sum( r.netweight ), 0 )
  228. FROM
  229. record AS r
  230. WHERE
  231. r.time > CURDATE()
  232. AND r.team = @TeamId
  233. AND r.entrance = record.entrance
  234. ) AS teamtotal
  235. FROM
  236. entrance
  237. LEFT JOIN record ON entrance.id = record.entrance
  238. AND record.time > CURDATE()
  239. GROUP BY
  240. entrance.id,
  241. entrance.name";
  242. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  243. {
  244. var result = await Task.Run(() => conn.QueryAsync<DailyStatisticDto>(selectSql, new { TeamId = teamId }));
  245. return result.ToList();
  246. }
  247. }
  248. public async Task<List<RealTimeBarDto>> GetDailyRealTimeData(int deviceId, int teamId)
  249. {
  250. string selectSql = @"SELECT time,FORMAT(netweight/1000,2) as weight FROM record where device=@DeviceId and team=@TeamId and time>CURDATE()";
  251. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  252. {
  253. var result = await Task.Run(() => conn.QueryAsync<RealTimeBarDto>(selectSql, new { DeviceId = deviceId, TeamId = teamId }));
  254. return result.ToList();
  255. }
  256. }
  257. }
  258. }