using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using LJLib.DAL.SQL;
using JLHHJSvr.Com.Model;

namespace JLHHJSvr.DBA.DBModle
{
    /// <summary>
    /// 1-系统收款二维码
    /// 2-默认违停收款金额
    /// 3-每月备案次数上限
    /// 4-即将过期判断天数
    /// </summary>
    [PK(new[] { "optionid" })]
    public sealed class st_option
    {
        /// <summary>
        /// ID
        /// </summary>
        public int? optionid { get; set; }
        /// <summary>
        /// 参数名
        /// </summary>
        public string optionname { get; set; }
        /// <summary>
        /// 参数值
        /// </summary>
        public string optionvalue { get; set; }
        /// <summary>
        /// 参数说明
        /// </summary>
        public string optiondscrp { get; set; }
        /// <summary>
        /// 修改人
        /// </summary>
        public string modemp { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? moddate { get; set; }
        /// <summary>
        /// key:optionid
        /// value:默认值
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, st_option> GetDefaultOptions()
        {
            return new Dictionary<int, st_option>
            {
                {
                    1, new st_option
                    {
                        optionid = 1,
                        optionname = "系统收款二维码",
                        optionvalue = string.Empty,
                        optiondscrp = "系统收款二维码"
                    }
                },
                {
                    2, new st_option
                    {
                        optionid = 2,
                        optionname = "默认违停收款金额(元/天)",
                        optionvalue = "50",
                        optiondscrp = "用于自动计算违停处理金额"
                    }
                },
                {
                    3, new st_option
                    {
                        optionid = 3,
                        optionname = "每月备案次数上限",
                        optionvalue = "4",
                        optiondscrp = "每月备案超过指定次数的会提示车主"
                    }
                },
                {
                    4, new st_option
                    {
                        optionid = 4,
                        optionname = "即将过期判断天数",
                        optionvalue = "30",
                        optiondscrp = "有效期结束前指定天数之后视为即将过期"
                    }
                },
                {
                    5, new st_option
                    {
                        optionid = 5,
                        optionname = "备案截止时间",
                        optionvalue = "08:00",
                        optiondscrp = "自助备案时，允许停放到次日的时间，格式hh:mm"
                    }
                },
                {
                    6, new st_option
                    {
                        optionid = 6,
                        optionname = "图片上传压缩后的宽度",
                        optionvalue = "800",
                        optiondscrp = "违停图片上传压缩后的宽度(数值越小图片占用空间越小)"
                    }
                },
                {
                    7, new st_option
                    {
                        optionid = 7,
                        optionname = "报备开放时间",
                        optionvalue = "08:00",
                        optiondscrp = "每天开始接受报备申请的时间点"
                    }
                },
                {
                    8, new st_option
                    {
                        optionid = 8,
                        optionname = "报备网页顶部通知",
                        optionvalue = "",
                        optiondscrp = "报备网页顶部显示的通知信息"
                    }
                },
                {
                    9, new st_option
                    {
                        optionid = 9,
                        optionname = "报备成功通知",
                        optionvalue = "",
                        optiondscrp = "报备网页成功后的弹框通知信息"
                    }
                },
                {
                    10, new st_option
                    {
                        optionid = 10,
                        optionname = "报备不在时间段的通知",
                        optionvalue = "当前时间段未开放报备",
                        optiondscrp = "当报备时间不在开放时间段时，报备页面显示的信息"
                    }
                },
            };
        }

        public void SetValue(DbCommand cmd, int optionid, string value, string modemp)
        {
            if (optionid == 0)
            {
                throw new Exception("optionid can't be 0");
            }
            if (value == null)
            {
                throw new Exception("value can't be null");
            }
            switch (optionid)
            {
                case 2:
                case 3:
                case 4:
                case 6:
                    try
                    {
                        var parsedValue = int.Parse(value);
                    }
                    catch (Exception)
                    {
                        throw new Exception("非法的系统参数值：只能为数字");
                    }
                    break;
                case 5:
                    try
                    {
                        if (value.Length != 5 || !value.Contains(':'))
                        {
                            throw new Exception();
                        }
                        var parsedValue = DateTime.Parse(value);
                    }
                    catch (Exception)
                    {
                        throw new Exception("非法的系统参数值：格式只能为hh:mm");
                    }
                    break;
            }
            var defOption = GetDefaultOption(optionid);
            
            this.optionid = optionid;
            this.optionname = defOption.optionname;
            this.optionvalue = value;
            this.optiondscrp = defOption.optiondscrp;
            this.modemp = modemp;
            this.moddate = DateTime.Now;
            DbSqlHelper.InsertOrUpdate(cmd, this, "optionname, optionvalue,optiondscrp, modemp, moddate");
        }

        public String GetValue(DbCommand cmd)
        {
            return GetValue(cmd, optionid);
        }

        public String GetValue(DbCommand cmd, int? optionid)
        {
            if (optionid == null || optionid == 0)
            {
                throw new Exception("未定义的选项ID");
            }
            cmd.CommandText = "SELECT optionvalue FROM st_option WHERE optionid = " + optionid;
            cmd.Parameters.Clear();
            var rslt = cmd.ExecuteScalar();
            if (rslt != null && rslt != DBNull.Value)
            {
                return rslt.ToString();
            }
            //返回默认值
            var defaultOption = GetDefaultOption(optionid.Value);
            if (defaultOption == null)
            {
                return null;
            }
            else
            {
                return defaultOption.optionvalue;
            }
        }

        public st_option GetDefaultOption(int optionid)
        {
            if (optionid == 0)
            {
                throw new Exception("Unknown Exception: optionid[0]");
            }
            var defaultDic = GetDefaultOptions();
            if (defaultDic.ContainsKey(optionid))
            {
                return defaultDic[optionid];
            }
            return null;
        }
        
    }
}
