using System;
using System.Configuration;

namespace Cmdjy
{
    /// <summary>
    /// 网站配置
    /// </summary>
    public static class WebSettings
    {
        /// <summary>
        /// 获取配置的值
        /// </summary>
        public static string GetValue(String key) {
            var val = ConfigurationManager.AppSettings[key];
            return val;
        }
        /// <summary>
        /// 万仕达加密通信用的的加密key
        /// </summary>
        public static string DesKey {
            get => GetValue("DesKey") ?? throw new Exception("必须在web.config文件appSettings节配置DesKey的值");
        }
        /// <summary>
        /// 获取Redis服务器连接字符串
        /// </summary>
        public static string RedisConnectionString {
            get => GetValue("RedisConnectionString");
        }
        /// <summary>
        /// 万仕达接口一次最大传送处方数
        /// </summary>
        public static int WsdMaxCount {
            get {
                var v = GetValue("wsd:maxCount");
                if(int.TryParse(v,out int iv)) {
                    return iv;
                }
                return 100;
            }
        }
        /// <summary>
        /// 自动升级数据库
        /// </summary>
        public static bool AutoMigrations {
            get {
                var val = GetValue("AutoMigrations");
                if(bool.TryParse(val,out bool v)) {
                    return v;
                }
                return true;
            }
        }
        /// <summary>
        /// 已秒为单位的默认缓冲时间。
        /// </summary>
        public static TimeSpan DefaultCacheTimeSpan {
            get {
                var v = GetValue("DefaultCacheTimeSpan");
                if(v == null || !long.TryParse(v,out var lv)) {
                    return new TimeSpan(0,10,0);
                }
                else {
                    return new TimeSpan(lv * 1000000 / 100);
                }

            }
        }
    }
}