using JLHHJSvr.Com.Model;
using JLHHJSvr.Helper;
using JLHHJSvr.LJException;
using JLHHJSvr.LJFramework.Tools;
using LJLib.DAL.SQL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
namespace JLHHJSvr.BLL
{
internal abstract class HelperBase
{
///
/// 数据库连接
///
public SqlCommand cmd { get; set; }
///
/// 预留的上下文
///
public Context context { get; set; }
///
/// 数据缓存
///
private CacheHelper _cache;
public static T GetHelper(SqlCommand cmd, Context context = null) where T : HelperBase, new()
{
var rslt = new T();
rslt.cmd = cmd;
rslt.context = context ?? new Context();
return rslt;
}
///
/// 缓存数据库相关信息
///
public CacheHelper Cache
{
get
{
if (_cache == null)
{
_cache = GetHelper(cmd, context);
}
return _cache;
}
}
public JObject DoExecute(string apiName, JObject request)
{
var url = GlobalVar.ERP_API_URL + "/api/common/" + apiName;
if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
url = "https://" + url;
}
var rslt = LJHttpUtil.PostRequest(url, request);
var errMsg = rslt.GetValue("ErrMsg");
if (errMsg != null && !string.IsNullOrEmpty(errMsg.ToString()))
{
throw new LJCommonException(errMsg.ToString());
}
return rslt;
}
///
/// 版本检测
///
///
///
///
///
///
public static void CheckBillVersion(SqlCommand cmd, int billid, int version) where TValue : class, new()
{
var property = typeof(TValue).GetProperty("version", BindingFlags.Public | BindingFlags.Instance);
if (property == null || property.PropertyType != typeof(int))
{
return;
}
var bill = new TValue();
string keycode = string.Empty, empcode = string.Empty;
string keyValue = string.Empty, empValue = string.Empty;
string billName = "单据";
if (bill is u_mattress)
{
billName = "床垫报价";
keycode = "mattresscode";
empcode = "qr_auditingrep";
}
else if (bill is u_bednet)
{
billName = "床网报价";
keycode = "bednetcode";
empcode = "update_emp";
}
else if (bill is u_softbed)
{
billName = "软床报价";
keycode = "softbed_code";
empcode = "update_emp";
}
var outputFields = $"version,{keycode},{empcode}";
DbSqlHelper.SelectOne(cmd, bill, outputFields);
// 通过反射获取属性值
keyValue = typeof(TValue).GetProperty(keycode)?.GetValue(bill)?.ToString() ?? string.Empty;
empValue = typeof(TValue).GetProperty(empcode)?.GetValue(bill)?.ToString() ?? string.Empty;
var billVersion = (int)(typeof(TValue).GetProperty("version")?.GetValue(bill) ?? 0);
if (billVersion != version) throw new LJCommonException($"{billName}【{keyValue}】已被用户【{empValue}】修改,请重新加载最新数据再操作!");
}
public sealed class Context
{
private DateTime _opdate = DateTime.Now;
public DateTime opdate
{
get { return _opdate; }
set { _opdate = value; }
}
///
/// 用户登陆信息
///
public TokenData tokendata { get; set; }
}
}
}