using FactorySystemCommon;
using FactorySystemModel.BusinessModel;
using FactorySystemModel.SqlSugarModel;
using SqlSugar;
using System.Collections.Generic;

namespace FactorySystemBll
{
    public class CommonBll
    {
        /// <summary>
        /// 获取配置信息
        /// </summary>
        public object GetBasicList(int type)
        {
            if (type == -1) return AppSettingsHelper.GetSqlSugar().Queryable<TBasicCode>().Where(s => s.FState == 1).OrderBy("FType,FOrder").ToList();
            return AppSettingsHelper.GetSqlSugar().Queryable<TBasicCode>().Where(s => s.FType == type && s.FState == 1).ToList();
        }

        /// <summary>
        /// 获取用户某类型权限
        /// </summary>
        public List<int> CheckIsHasPower(int roleType, int userId)
        {
            return AppSettingsHelper.GetSqlSugar().Queryable<TRole_Right>().Where(string.Format("FRoleID in(select FRoleID from TUser where FID={0})", userId))
                .Where(s => s.FType == roleType).Select<int>("FFunctionID").ToList();
        }

        /// <summary>
        /// 获取数据选择值
        /// </summary>
        public List<TDataCode> GetDataCodeList(string[] types)
        {
            if (types == null || types.Length == 0)
                return AppSettingsHelper.GetSqlSugar().Queryable<TDataCode>().OrderBy("FType,FOrder").ToList();
            return AppSettingsHelper.GetSqlSugar().Queryable<TDataCode>().Where(s => s.FState == 1
            && SqlFunc.ContainsArray(types, s.FType)).OrderBy(s => s.FOrder).ToList();
        }

        /// <summary>
        /// 获取系统配置
        /// </summary>
        public object GetConfigList(string key = "", int dataId = -1)
        {
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            if (string.IsNullOrEmpty(key) && dataId <= 0)
                return db.Queryable<TFS_Config>().Select<dynamic>("FID,FName,FKey").ToList();
            return db.Queryable<TFS_Config>().WhereIF(!string.IsNullOrEmpty(key), s => s.FKey == key)
                .WhereIF(!string.IsNullOrEmpty(key), s => s.FKey == key)
                .WhereIF(dataId > 0, s => s.FID == dataId).First();
        }

        /// <summary>
        /// 获取系统配置值
        /// </summary>
        public string GetConfigValue(int dataId)
        {
            string result = "";
            TFS_Config config = GetConfigList("", dataId) as TFS_Config;
            if (config != null)
            {
                BaseClass bc = JWTHelper.Decrypt<BaseClass>(config.FValue, config.FKey);
                if (bc != null) result = bc.value;
            }
            return result;
        }

        /// <summary>
        /// 修改数据值状态
        /// </summary>
        public int StateDataCode(int dataId, int userId)
        {
            string sqlStr = string.Format(@"update TDataCode set FUserOpt={1},FDateOpt=getdate(),
            FState=(case FState when '1' then '0' else '1' end) where FID={0}", dataId, userId);
            return AppSettingsHelper.GetSqlSugar().Ado.ExecuteCommand(sqlStr);
        }
    }
}
