using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
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 GetPriceListExcutor : ExcutorBase<GetPriceListRequest, GetPriceListResponse>
    {
        protected override void ExcuteInternal(GetPriceListRequest request, object state, GetPriceListResponse 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 whereStr = "";

                if (request.inuse != null)
                {
                    whereStr += "inuse = @inuse";
                }

                if (!string.IsNullOrEmpty(whereStr))
                {
                    whereStr = " WHERE " + whereStr;
                }

                cmd.CommandText = @"SELECT ROW_NUMBER() OVER (ORDER BY u_pricelist.pricelistid) AS RowNumber,   
                                         u_pricelist.pricelistid,   
                                         u_pricelist.pricelistname,   
                                         u_pricelist.createtime,   
                                         u_pricelist.dscrp,
                                         flag,
                                         moddate,
                                         modemp,
                                         auditemp,
                                         auditdate,
                                         inuse
                                    FROM u_pricelist" + whereStr;
                cmd.Parameters.Clear();
                if (request.inuse != null) cmd.Parameters.AddWithValue("@inuse", request.inuse);
                rslt.list = new List<u_pricelist>();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var item = new u_pricelist();
                        item.RowNumber = Convert.ToInt32(reader["RowNumber"]);
                        item.pricelistid = Convert.ToInt32(reader["pricelistid"]);
                        item.pricelistname = Convert.ToString(reader["pricelistname"]);
                        item.createtime = Convert.ToDateTime(reader["createtime"]);
                        item.dscrp = Convert.ToString(reader["dscrp"]);
                        item.flag = Convert.ToByte(reader["flag"]);
                        item.moddate = reader["moddate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(reader["moddate"]);
                        item.modemp = Convert.ToString(reader["modemp"]);
                        item.auditemp = Convert.ToString(reader["auditemp"]);
                        item.auditdate = reader["auditdate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(reader["auditdate"]);
                        item.inuse = Convert.ToByte(reader["inuse"]);
                        rslt.list.Add(item);
                    }
                }

            }
        }

    }
}
