using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using Newtonsoft.Json.Linq;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetFormulaVarListExcutor : ExcutorBase<GetFormulaVarListRequest, GetFormulaVarListResponse>
    {
        protected override void ExcuteInternal(GetFormulaVarListRequest request, object state, GetFormulaVarListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

            rslt.recursionList = new List<Recursion2>();
            // 变量定义
            var varRecusionChilds = new List<Recursion2>();
            rslt.recursionList.Add(new Recursion2() { text = "变量定义", value = 0, children = varRecusionChilds });
            // 工艺加点设置
            var workShipChilds = new List<Recursion2>();
            rslt.recursionList.Add(new Recursion2() { text = "工艺加点设置", value = 0, children = workShipChilds });
            // 固定参数
            var fixedVarChilds = new List<Recursion2>();
            rslt.recursionList.Add(new Recursion2() { text = "固定变量", value = 0, children = fixedVarChilds });

            using (var con = new SqlConnection(GlobalVar.ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var selectStr = string.Empty;
                var param = new Dictionary<string, object>();
                var outputFields = string.Empty;

                // 变量定义
                selectStr = "SELECT varname FROM u_bednet_var";
                outputFields = "varname";
                var bednetVarList = new List<u_bednet_var>();
                DbSqlHelper.SelectJoin(cmd, selectStr, null, param, "varid", outputFields, 0, 0, bednetVarList);
                foreach(var child in bednetVarList)
                {
                    varRecusionChilds.Add(new Recursion2()
                    {
                        text = child.varname,
                        value = 1
                    });
                }

                // 加点工艺
                selectStr = "SELECT workmanshipname FROM u_workmanship_add";
                outputFields = "workmanshipname";
                var workshipList = new List<u_workmanship_add>();
                DbSqlHelper.SelectJoin(cmd, selectStr, null, param, "workmanshipid", outputFields, 0, 0, workshipList);
                foreach (var child in workshipList)
                {
                    workShipChilds.Add(new Recursion2()
                    {
                        text = child.workmanshipname,
                        value = 1
                    });
                }
            }

            var queryparams = new JObject();
            var commonExcutor = new CommonDynamicSelectExcutor();
            var commonRequest = new CommonDynamicSelectRequest()
            {
                token = request.token,
                dsname = "_Mapper_fixedParamters",
                queryparams = queryparams
            };

            var billResponse = commonExcutor.Excute(commonRequest, state) as CommonDynamicSelectResponse;
            if (billResponse != null && billResponse.datatable != null && billResponse.datatable.Any())
            {
                var fiexdList = billResponse.datatable;
                foreach(var item in fiexdList)
                {
                    fixedVarChilds.Add(new Recursion2() { text = Convert.ToString(item["label"]),value = Convert.ToInt32(item["value"]) });
                }
            }
        }
    }
}
