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 LJLib.SQLEX;
using Newtonsoft.Json.Linq;

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

            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();
                var baseHelper = HelperBase.GetHelper<BasicInfoHelper>(cmd);

                var whereList = new List<string>();
                var param = new Dictionary<string, object>();

                //
                whereList.Add("u_mtrldef.flag = 2");
                //
                if (request.mtrltypeids != null && request.mtrltypeids.Count > 0)
                {
                    whereList.Add($"u_mtrldef.mtrltypeid IN {ListEx.getString(request.mtrltypeids)}");
                }
                if (!string.IsNullOrEmpty(request.keyword))
                {
                    whereList.Add("u_mtrldef.mtrlcode LIKE @keyword OR u_mtrldef.mtrlname LIKE @keyword");
                    param.Add("@keyword", $"%{request.keyword}%");
                }
                if (!string.IsNullOrEmpty(request.mtrlcode))
                {
                    whereList.Add("u_mtrldef.mtrlcode LIKE @mtrlcode");
                    param.Add("@mtrlcode", $"%{request.mtrlcode}%");
                }
                if (!string.IsNullOrEmpty(request.mtrlname))
                {
                    whereList.Add("u_mtrldef.mtrlname LIKE @mtrlname");
                    param.Add("@mtrlname", $"%{request.mtrlname}%");
                }
                if (!string.IsNullOrEmpty(request.mtrlmode))
                {
                    whereList.Add("u_mtrldef.mtrlmode LIKE @mtrlmode");
                    param.Add("@mtrlmode", $"%{request.mtrlmode}%");
                }
                if (!string.IsNullOrEmpty(request.mtrlsectype))
                {
                    whereList.Add("u_mtrldef.mtrlsectype LIKE @mtrlsectype");
                    param.Add("@mtrlsectype", $"%{request.mtrlsectype}%");
                }
                if (!string.IsNullOrEmpty(request.zxmtrlmode))
                {
                    whereList.Add("u_mtrldef.zxmtrlmode LIKE @zxmtrlmode");
                    param.Add("@zxmtrlmode", $"%{request.zxmtrlmode}%");
                }
                if (!string.IsNullOrEmpty(request.usermtrlmode))
                {
                    whereList.Add("u_mtrldef.usermtrlmode LIKE @usermtrlmode");
                    param.Add("@usermtrlmode", $"%{request.usermtrlmode}%");
                }
                if (!string.IsNullOrEmpty(request.plancode))
                {
                    whereList.Add("u_mtrldef.plancode LIKE @plancode");
                    param.Add("@plancode", $"%{request.plancode}%");
                }
                if (!string.IsNullOrEmpty(request.mtrlengname))
                {
                    whereList.Add("u_mtrldef.mtrlengname LIKE @mtrlengname");
                    param.Add("@mtrlengname", $"%{request.mtrlengname}%");
                }

                rslt.mtrldefList = baseHelper.GetErpMtrlPriceList(whereList, param, request.pageindex, request.pagesize, out int total);
                rslt.totalcnt = total;
                rslt.pageindex = request.pageindex;
                rslt.pagesize = request.pagesize;
            }
        }
    }
}
