HelperBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using JLHHJSvr.Com.Model;
  2. using JLHHJSvr.Helper;
  3. using JLHHJSvr.LJException;
  4. using JLHHJSvr.LJFramework.Tools;
  5. using LJLib.DAL.SQL;
  6. using Newtonsoft.Json.Linq;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Data.SqlClient;
  12. using System.Linq;
  13. using System.Reflection;
  14. namespace JLHHJSvr.BLL
  15. {
  16. internal abstract class HelperBase
  17. {
  18. /// <summary>
  19. /// 数据库连接
  20. /// </summary>
  21. public SqlCommand cmd { get; set; }
  22. /// <summary>
  23. /// 预留的上下文
  24. /// </summary>
  25. public Context context { get; set; }
  26. /// <summary>
  27. /// 数据缓存
  28. /// </summary>
  29. private CacheHelper _cache;
  30. public static T GetHelper<T>(SqlCommand cmd, Context context = null) where T : HelperBase, new()
  31. {
  32. var rslt = new T();
  33. rslt.cmd = cmd;
  34. rslt.context = context ?? new Context();
  35. return rslt;
  36. }
  37. /// <summary>
  38. /// 缓存数据库相关信息
  39. /// </summary>
  40. public CacheHelper Cache
  41. {
  42. get
  43. {
  44. if (_cache == null)
  45. {
  46. _cache = GetHelper<CacheHelper>(cmd, context);
  47. }
  48. return _cache;
  49. }
  50. }
  51. public JObject DoExecute(string apiName, JObject request)
  52. {
  53. var url = GlobalVar.ERP_API_URL + "/api/common/" + apiName;
  54. if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  55. {
  56. url = "https://" + url;
  57. }
  58. var rslt = LJHttpUtil.PostRequest(url, request);
  59. var errMsg = rslt.GetValue("ErrMsg");
  60. if (errMsg != null && !string.IsNullOrEmpty(errMsg.ToString()))
  61. {
  62. throw new LJCommonException(errMsg.ToString());
  63. }
  64. return rslt;
  65. }
  66. /// <summary>
  67. /// 版本检测
  68. /// </summary>
  69. /// <typeparam name="TValue"></typeparam>
  70. /// <param name="cmd"></param>
  71. /// <param name="keyword"></param>
  72. /// <param name="billid"></param>
  73. /// <exception cref="InvalidOperationException"></exception>
  74. public static void CheckBillVersion<TValue>(SqlCommand cmd, int billid, int version) where TValue : class, new()
  75. {
  76. return;
  77. var property = typeof(TValue).GetProperty("version", BindingFlags.Public | BindingFlags.Instance);
  78. if (property == null || property.PropertyType != typeof(int))
  79. {
  80. return;
  81. }
  82. var bill = new TValue();
  83. string keycode = string.Empty, empcode = string.Empty;
  84. string keyValue = string.Empty, empValue = string.Empty;
  85. string billName = "单据";
  86. if (bill is u_mattress)
  87. {
  88. billName = "床垫报价";
  89. keycode = "mattresscode";
  90. empcode = "qr_auditingrep";
  91. }
  92. else if (bill is u_bednet)
  93. {
  94. billName = "床网报价";
  95. keycode = "bednetcode";
  96. empcode = "update_emp";
  97. }
  98. else if (bill is u_softbed)
  99. {
  100. billName = "软床报价";
  101. keycode = "softbed_code";
  102. empcode = "update_emp";
  103. }
  104. var outputFields = $"version,{keycode},{empcode}";
  105. DbSqlHelper.SelectOne(cmd, bill, outputFields);
  106. // 通过反射获取属性值
  107. keyValue = typeof(TValue).GetProperty(keycode)?.GetValue(bill)?.ToString() ?? string.Empty;
  108. empValue = typeof(TValue).GetProperty(empcode)?.GetValue(bill)?.ToString() ?? string.Empty;
  109. var billVersion = (int)(typeof(TValue).GetProperty("version")?.GetValue(bill) ?? 0);
  110. if (billVersion != version) throw new LJCommonException($"{billName}【{keyValue}】已被用户【{empValue}】修改,请重新加载最新数据再操作!");
  111. }
  112. public sealed class Context
  113. {
  114. private DateTime _opdate = DateTime.Now;
  115. public DateTime opdate
  116. {
  117. get { return _opdate; }
  118. set { _opdate = value; }
  119. }
  120. /// <summary>
  121. /// 用户登陆信息
  122. /// </summary>
  123. public TokenData tokendata { get; set; }
  124. }
  125. }
  126. }