using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using DirectService.Tools;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.Helper;
using JLHHJSvr.LJException;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using Newtonsoft.Json.Linq;

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

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

                var erpHelper = HelperBase.GetHelper<ERPHelper>(null);

                var parameters = new JObject();

                if (request.mtrlid > 0)
                {
                    parameters.Add("mtrlid", request.mtrlid);
                }
                else
                {
                    if (request.mtrlprp != null && request.mtrlprp != -1)
                    {
                        parameters.Add("mtrlprp", request.mtrlprp);
                    }
                    if (request.mtrltypeid != null && request.mtrltypeid != -1)
                    {
                        parameters.Add("mtrltypeid", request.mtrltypeid);
                    }
                    if (request.storageid != null && request.storageid != -1)
                    {
                        parameters.Add("storageid", request.storageid);
                    }
                    if (request.mtrlorigin != null && request.mtrlorigin != -1)
                    {
                        parameters.Add("mtrlorigin", request.mtrlorigin);
                    }
                    if (request.isuse != null && request.isuse != -1)
                    {
                        parameters.Add("isuse", request.isuse);
                    }
                    if (!string.IsNullOrEmpty(request.keyword))
                    {
                        parameters.Add("keyword", request.keyword);
                    }

                    if (!string.IsNullOrEmpty(request.mtrlcode))
                    {
                        parameters.Add("mtrlcode", request.mtrlcode);
                    }

                    if (!string.IsNullOrEmpty(request.mtrlname))
                    {
                        parameters.Add("mtrlname", request.mtrlname);
                    }

                    if (!string.IsNullOrEmpty(request.mtrlmode))
                    {
                        parameters.Add("mtrlmode", request.mtrlmode);
                    }

                    if (!string.IsNullOrEmpty(request.mtrlsectype))
                    {
                        parameters.Add("mtrlsectype", request.mtrlsectype);
                    }

                    if (!string.IsNullOrEmpty(request.zxmtrlmode))
                    {
                        parameters.Add("zxmtrlmode", request.zxmtrlmode);
                    }

                    if (!string.IsNullOrEmpty(request.usermtrlmode))
                    {
                        parameters.Add("usermtrlmode", request.usermtrlmode);
                    }

                    if (!string.IsNullOrEmpty(request.plancode))
                    {
                        parameters.Add("plancode", request.plancode);
                    }

                    if (request.pageindex > 0)
                    {
                        parameters.Add("pageindex", request.pageindex);
                    }

                    if (request.pagesize > 0)
                    {
                        parameters.Add("pagesize", request.pagesize);
                    }
                }

                int total = 0;
                var resultList = erpHelper.GetERPList<erp_mtrldef>("GetL1Mtrldef", ref total, parameters);

                rslt.mtrldefList = resultList;
                rslt.totalcnt = total;
                rslt.pageindex = request.pageindex;
                rslt.pagesize = request.pagesize;
            }
        }
    }
}
