You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
10 KiB

  1. using HandyControl.Controls;
  2. using HKCardOUT.Helper;
  3. using HKCardOUT.Logic.Model;
  4. using HKControl;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using UHFHelper;
  11. using XExten.Advance.LinqFramework;
  12. namespace HKCardOUT.Logic.Service
  13. {
  14. public class HKCore
  15. {
  16. List<SaleLog> Commom(SaleLog input)
  17. {
  18. if (DataBus.StartDevice)
  19. if (!Main.GetInstance.GetIsSwipe(input.Location.AsInt())) return null;
  20. var Old = DbContext.Context.Queryable<SaleLog>()
  21. .Where(t => t.CardNo == input.CardNo)
  22. .Where(t => t.CreateTime.Year == DateTime.Now.Year)
  23. .Where(t => t.CreateTime.Month == DateTime.Now.Month)
  24. .Where(t => t.CreateTime.Day == DateTime.Now.Day).ToList();
  25. if (DataBus.Cancel)
  26. if (Old.Count > 3) return null;
  27. return Old;
  28. }
  29. bool GetCardStutas(string CardNo)
  30. {
  31. var data = DbContext.Context.Queryable<CardStutas>().First(t => t.CardNo == CardNo);
  32. if (data == null) return false;
  33. if (data.Stutas != 1) return false;
  34. return true;
  35. }
  36. public static void AddCard(CardStutas input)
  37. {
  38. DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteCommand();
  39. }
  40. /// <summary>
  41. /// 早上消费
  42. /// </summary>
  43. /// <param name="input"></param>
  44. /// <returns></returns>
  45. public bool DeviceSaleAM(SaleLog input)
  46. {
  47. try
  48. {
  49. if(!GetCardStutas(input.CardNo)) return false;
  50. var Old = Commom(input);
  51. if (Old == null) return false;
  52. var AM = DataBus.Times.AM.Split(",");
  53. var S = DateTime.Parse(AM[0]);
  54. var E = DateTime.Parse(AM[1]);
  55. var AMDay = Old.Where(t => t.CreateTime >=S && t.CreateTime < E)
  56. .Where(t => t.CreateTime.Year == DateTime.Now.Year)
  57. .Where(t => t.CreateTime.Month == DateTime.Now.Month)
  58. .Where(t => t.CreateTime.Day == DateTime.Now.Day)
  59. .Count();
  60. HKLog.HKLogImport.WriteInfo($"卡号{input.CardNo}上午消费次数:{AMDay}");
  61. if (AMDay < DataBus.Count)
  62. {
  63. var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity();
  64. if (entity.Id != Guid.Empty)
  65. {
  66. ////有网络直接同步
  67. //if (DataBus.NetWordState)
  68. //{
  69. // //先出餐后推数据
  70. // if (DataBus.StartDevice)
  71. // {
  72. // Main.GetInstance.Start(input.Location.AsInt());
  73. // UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
  74. // }
  75. // var res = RemoteService.SyncSaleLog(input.CardNo, input.Location);
  76. // if (res)
  77. // DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand();
  78. // return res;
  79. //}
  80. //else
  81. //{
  82. //}
  83. if (DataBus.StartDevice)
  84. {
  85. Main.GetInstance.Start(input.Location.AsInt());
  86. UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
  87. }
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. catch (Exception ex)
  94. {
  95. HKLog.HKLogImport.WriteError(ex);
  96. return false;
  97. }
  98. }
  99. /// <summary>
  100. /// 中午消费
  101. /// </summary>
  102. /// <param name="input"></param>
  103. /// <param name="Old"></param>
  104. /// <returns></returns>
  105. public bool DeviceSalePM(SaleLog input)
  106. {
  107. try
  108. {
  109. if (!GetCardStutas(input.CardNo)) return false;
  110. var Old = Commom(input);
  111. if (Old == null) return false;
  112. var PM = DataBus.Times.PM.Split(",");
  113. var S = DateTime.Parse(PM[0]);
  114. var E = DateTime.Parse(PM[1]);
  115. var PMDay = Old.Where(t => t.CreateTime >= S && t.CreateTime < E).
  116. Where(t => t.CreateTime.Year == DateTime.Now.Year)
  117. .Where(t => t.CreateTime.Month == DateTime.Now.Month)
  118. .Where(t => t.CreateTime.Day == DateTime.Now.Day)
  119. .Count();
  120. HKLog.HKLogImport.WriteInfo($"卡号{input.CardNo}中午消费次数:{PMDay}");
  121. if (PMDay < DataBus.Count)
  122. {
  123. var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity();
  124. if (entity.Id != Guid.Empty)
  125. {
  126. ////有网络直接同步
  127. //if (DataBus.NetWordState)
  128. //{
  129. // //先出餐后推数据
  130. // if (DataBus.StartDevice)
  131. // {
  132. // Main.GetInstance.Start(input.Location.AsInt());
  133. // UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
  134. // }
  135. // var res = RemoteService.SyncSaleLog(input.CardNo, input.Location);
  136. // if (res)
  137. // DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand();
  138. // return res;
  139. //}
  140. //else
  141. //{
  142. if (DataBus.StartDevice)
  143. {
  144. Main.GetInstance.Start(input.Location.AsInt());
  145. UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
  146. }
  147. return true;
  148. //}
  149. }
  150. }
  151. return false;
  152. }
  153. catch (Exception ex)
  154. {
  155. HKLog.HKLogImport.WriteError(ex);
  156. return false;
  157. }
  158. }
  159. /// <summary>
  160. /// 晚上
  161. /// </summary>
  162. /// <param name="input"></param>
  163. /// <param name="Old"></param>
  164. /// <returns></returns>
  165. public bool DeviceSaleAT(SaleLog input)
  166. {
  167. try
  168. {
  169. if (!GetCardStutas(input.CardNo)) return false;
  170. var Old = Commom(input);
  171. if (Old == null) return false;
  172. var AT = DataBus.Times.AT.Split(",");
  173. var S = DateTime.Parse(AT[0]);
  174. var E = DateTime.Parse(AT[1]);
  175. var ATDay = Old.Where(t => t.CreateTime >= S && t.CreateTime <E)
  176. .Where(t => t.CreateTime.Year == DateTime.Now.Year)
  177. .Where(t => t.CreateTime.Month == DateTime.Now.Month)
  178. .Where(t => t.CreateTime.Day == DateTime.Now.Day)
  179. .Count();
  180. HKLog.HKLogImport.WriteInfo($"卡号{input.CardNo}下午消费次数:{ATDay}");
  181. if (ATDay < DataBus.Count)
  182. {
  183. var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity();
  184. if (entity.Id != Guid.Empty)
  185. {
  186. ////有网络直接同步
  187. //if (DataBus.NetWordState)
  188. //{
  189. // //先出餐后推数据
  190. // if (DataBus.StartDevice)
  191. // {
  192. // Main.GetInstance.Start(input.Location.AsInt());
  193. // UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
  194. // }
  195. // var res = RemoteService.SyncSaleLog(input.CardNo, input.Location);
  196. // if (res)
  197. // DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand();
  198. // return res;
  199. //}
  200. //else
  201. //{
  202. if (DataBus.StartDevice)
  203. {
  204. Main.GetInstance.Start(input.Location.AsInt());
  205. UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt());
  206. }
  207. return true;
  208. //}
  209. }
  210. }
  211. return false;
  212. }
  213. catch (Exception ex)
  214. {
  215. HKLog.HKLogImport.WriteError(ex);
  216. return false;
  217. }
  218. }
  219. /// <summary>
  220. /// 获取当天消费前100条展示
  221. /// </summary>
  222. /// <returns></returns>
  223. public List<SaleLog> PullDaySaleLog()
  224. {
  225. var Begin = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
  226. var End = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
  227. var data = DbContext.Context.Queryable<SaleLog>()
  228. .Where(t => t.CreateTime >= Begin)
  229. .Where(t => t.CreateTime < End)
  230. .OrderByDescending(t => t.CreateTime).ToPageList(0, 100);
  231. return data.GroupBy(t => t.CardNo).Select(t => new SaleLog
  232. {
  233. CardNo = t.Key,
  234. Location = String.Join(",", t.Select(m => m.Location)),
  235. Count = t.Count()
  236. }).ToList();
  237. }
  238. }
  239. }