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 mtrldef = new L1Mtrldef() {
                    flag = 2,
                };

                if (request.mtrlid > 0)
                {
                    mtrldef.mtrlid = request.mtrlid;
                }
                else
                {
                    if (request.mtrlprp != null && request.mtrlprp != -1)
                    {
                        mtrldef.mtrlprp = request.mtrlprp;
                    }
                    if (request.mtrltypeid != null && request.mtrltypeid != -1)
                    {
                        mtrldef.mtrltypeid = request.mtrltypeid;
                    }
                    if (request.storageid != null && request.storageid != -1)
                    {
                        mtrldef.storageid = request.storageid;
                    }
                    if (request.mtrlorigin != null && request.mtrlorigin != -1)
                    {
                        mtrldef.mtrlorigin = request.mtrlorigin;
                    }
                    if (request.isuse != null && request.isuse != -1)
                    {
                        mtrldef.isuse = request.isuse;
                    }

                    if (!string.IsNullOrEmpty(request.mtrlcode))
                    {
                        mtrldef.mtrlcode = request.mtrlcode;
                    }

                    if (!string.IsNullOrEmpty(request.mtrlname))
                    {
                        mtrldef.mtrlname = request.mtrlname;
                    }

                    if (!string.IsNullOrEmpty(request.mtrlmode))
                    {
                        mtrldef.mtrlmode = request.mtrlmode;
                    }

                    if (!string.IsNullOrEmpty(request.mtrlsectype))
                    {
                        mtrldef.mtrlsectype = request.mtrlsectype;
                    }

                    if (!string.IsNullOrEmpty(request.zxmtrlmode))
                    {
                        mtrldef.zxmtrlmode = request.zxmtrlmode;
                    }

                    if (!string.IsNullOrEmpty(request.usermtrlmode))
                    {
                        mtrldef.usermtrlmode = request.usermtrlmode;
                    }

                    if (!string.IsNullOrEmpty(request.plancode))
                    {
                        mtrldef.plancode = request.plancode;
                    }
                    if (!string.IsNullOrEmpty(request.mtrlengname))
                    {
                        mtrldef.mtrlengname = request.mtrlengname;
                    }
                }

                int total = 0;
                var resultList = erpHelper.GetERPMtrldef(cmd, ref total, request.keyword, mtrldef, request.pagesize, request.pageindex);

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