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
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public SqlCommand cmd { get; set; }
        /// <summary>
        /// 预留的上下文
        /// </summary>
        public Context context { get; set; }
        /// <summary>
        /// 数据缓存
        /// </summary>
        private CacheHelper _cache;

        public static T GetHelper<T>(SqlCommand cmd, Context context = null) where T : HelperBase, new()
        {
            var rslt = new T();
            rslt.cmd = cmd;
            rslt.context = context ?? new Context();
            return rslt;
        }

        /// <summary>
        /// 缓存数据库相关信息
        /// </summary>
        public CacheHelper Cache
        {
            get
            {
                if (_cache == null)
                {
                    _cache = GetHelper<CacheHelper>(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;
        }
        /// <summary>
        /// 版本检测
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="keyword"></param>
        /// <param name="billid"></param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void CheckBillVersion<TValue>(SqlCommand cmd, int billid, int version) where TValue : class, new()
        {
            return;
            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; }
            }
            /// <summary>
            /// 用户登陆信息
            /// </summary>
            public TokenData tokendata { get; set; }
        }
    }
}
