using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DirectService.Tools;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.DBA.DBModle;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetOptionListExcutor : ExcutorBase<GetOptionListRequest, GetOptionListResponse>
    {
        protected override void ExcuteInternal(GetOptionListRequest request, object state, GetOptionListResponse rslt)
        {
            if (!request.token.Equals("FROM PUBLIC WEBAPP"))
            {
                var tokendata = BllHelper.GetToken(request.token);
                if (tokendata == null)
                {
                    rslt.ErrMsg = "会话已经中断，请重新登录";
                    return;
                }
            }
            if (request.optionid != null && request.optionid != 0)
            {
                if (request.optionidList == null)
                {
                    request.optionidList = new List<int>();
                }
                request.optionidList.Add(request.optionid.Value);
            }
            
            using (var con = new SqlConnection(GlobalVar.ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                Dictionary<int, st_option> defaultOptionDic;
                cmd.CommandText = @"SELECT optionid, optionvalue FROM st_option";
                if (request.optionidList != null && request.optionidList.Count > 0)
                {
                    cmd.CommandText += " WHERE optionid IN " + ListEx.getString(request.optionidList);
                    defaultOptionDic = new Dictionary<int, st_option>();
                    var optionHelper = new st_option();
                    foreach (var optionItem in request.optionidList)
                    {
                        defaultOptionDic[optionItem] = optionHelper.GetDefaultOption(optionItem);
                    }
                }
                else
                {
                    defaultOptionDic = new st_option().GetDefaultOptions();
                }
                using (var reader = cmd.ExecuteReader())
                {
                    var optionId = 0;
                    var optionValue = string.Empty;
                    while (reader.Read())
                    {
                        optionId = Convert.ToInt32(reader["optionid"]);
                        optionValue = Convert.ToString(reader["optionvalue"]);
                        if (!defaultOptionDic.ContainsKey(optionId))
                        {
                            rslt.ErrMsg = string.Format("选项[{0}]未定义",optionId);
                            return;
                        }
                        else
                        {
                            defaultOptionDic[optionId].optionvalue = optionValue;
                        }
                    }
                }
                rslt.optionList = new List<Option>();
                foreach (var stOption in defaultOptionDic.Values)
                {
                    rslt.optionList.Add(new Option
                    {
                        optionid = stOption.optionid,
                        optionname = stOption.optionname,
                        optionvalue = stOption.optionvalue,
                        optiondscrp = stOption.optiondscrp
                    });
                }
            }
        }
    }
}
