using MMoneyCoderSharp.Data.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MMoneyCoderSharp.DBDefine;

namespace MMoneyCoderSharp.DB
{
    /// <summary>
    /// </summary>
    internal class Nxt_STOCK_PAN : Nxt_ADataObject
    {

        internal Nxt_STOCK_PAN(국내시장종류 recvDataType, string recvStockCode)
        {
            this.mDataType = recvDataType;
            this.mDataCode = recvStockCode;
        }

        국내시장종류 mDataType;

        string mDataCode = String.Empty;

        #region "쿼리"

        string queryNXT_KOSPI = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_NAME stock_name , a.f_curr_price curr_price, 
		ABS(a.f_net_chg) net_chg, 
		case when a.f_chg_type = '1' then '상한' when a.f_chg_type = '2' then '상승' when a.f_chg_type = '3' then '보합' 
             when a.f_chg_type = '4' then '하한' when a.f_chg_type = '5' then '하락' end chg_type, a.f_net_vol net_vol, 
		round((a.f_net_chg / 
		    case when a.f_chg_type = '1' then (a.f_curr_price - a.f_net_chg) 
			     when a.f_chg_type = '2' then (a.f_curr_price - a.f_net_chg) 
 			     when a.f_chg_type = '3' then (a.f_curr_price) 
			     when a.f_chg_type = '4' then -1*(a.f_curr_price + a.f_net_chg) 
			     when a.f_chg_type = '5' then -1*(a.f_curr_price + a.f_net_chg) end) * 100, 2) rate , 'NXT_KOSPI' market
		FROM n_online a, n_stock b 
		WHERE b.f_stop_gubun = 'N'    
		AND a.f_curr_price != 0     
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_name = '{0}'";

        string queryNXT_KOSDAQ = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_NAME stock_name , a.f_curr_price curr_price, a.f_net_chg net_chg, 
		case when a.f_chg_type = '1' then '상한' when a.f_chg_type = '2' then '상승' when a.f_chg_type = '3' then '보합' 
             when a.f_chg_type = '4' then '하한' when a.f_chg_type = '5' then '하락' end chg_type, a.f_net_vol net_vol, 
		round((a.f_net_chg / 
		    case when a.f_chg_type = '1' then (a.f_curr_price - a.f_net_chg) 
			     when a.f_chg_type = '2' then (a.f_curr_price - a.f_net_chg) 
 			     when a.f_chg_type = '3' then (a.f_curr_price) 
			     when a.f_chg_type = '4' then -1*(a.f_curr_price + a.f_net_chg) 
			     when a.f_chg_type = '5' then -1*(a.f_curr_price + a.f_net_chg) end) * 100, 2) rate , 'NXT_KOSDAQ' market
		FROM n_kosdaq_online a, n_kosdaq_stock b 
		WHERE b.f_stop_gubun = 'N'    
		AND a.f_curr_price != 0     
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_name = '{0}'";

  //      string queryNXT_Dis = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_NAME stock_name , a.f_curr_price curr_price, 
		//ABS(a.f_net_chg) net_chg, 
		//case when a.f_chg_type = '1' then '상한' when a.f_chg_type = '2' then '상승' when a.f_chg_type = '3' then '보합' 
  //           when a.f_chg_type = '4' then '하한' when a.f_chg_type = '5' then '하락' end chg_type, a.f_net_vol net_vol, 
		//round((a.f_net_chg / 
		//    case when a.f_chg_type = '1' then (a.f_curr_price - a.f_net_chg) 
		//	     when a.f_chg_type = '2' then (a.f_curr_price - a.f_net_chg) 
 	//		     when a.f_chg_type = '3' then (a.f_curr_price) 
		//	     when a.f_chg_type = '4' then -1*(a.f_curr_price + a.f_net_chg) 
		//	     when a.f_chg_type = '5' then -1*(a.f_curr_price + a.f_net_chg) end) * 100, 2) rate , , 'NXT_KOSPI' market
		//FROM n_stop_online a, n_stock b 
		//WHERE b.f_stop_gubun = 'Y' 
		//AND a.f_curr_price != 0     
		//AND a.f_stock_code = b.f_stock_code 
		//and b.f_STOCK_name = '{0}'";

  //      string queryNXT_Disk = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_NAME stock_name , a.f_curr_price curr_price, 
		//ABS(a.f_net_chg) net_chg, 
		//case when a.f_chg_type = '1' then '상한' when a.f_chg_type = '2' then '상승' when a.f_chg_type = '3' then '보합' 
  //           when a.f_chg_type = '4' then '하한' when a.f_chg_type = '5' then '하락' end chg_type, a.f_net_vol net_vol, 
		//round((a.f_net_chg / 
		//    case when a.f_chg_type = '1' then (a.f_curr_price - a.f_net_chg) 
		//	     when a.f_chg_type = '2' then (a.f_curr_price - a.f_net_chg) 
 	//		     when a.f_chg_type = '3' then (a.f_curr_price) 
		//	     when a.f_chg_type = '4' then -1*(a.f_curr_price + a.f_net_chg) 
		//	     when a.f_chg_type = '5' then -1*(a.f_curr_price + a.f_net_chg) end) * 100, 2) rate , 'NXT_KOSDAQ' market
		//FROM n_stop_kosdaq_online a, t_kosdaq_stock b 
		//WHERE b.f_stop_gubun = 'Y' 
		//AND a.f_curr_price != 0     
		//AND a.f_stock_code = b.f_stock_code 
		//and b.f_STOCK_name = '{0}'";


        #endregion


        public override Nxt_IDataRequest Nxt_buildData()
        {

            string bufQuery = "";

            switch (mDataType)
            {
                case 국내시장종류.NXT_KOSPI:
                    bufQuery = string.Format(queryNXT_KOSPI, mDataCode);
                    break;
                case 국내시장종류.NXT_KOSDAQ:
                    bufQuery = string.Format(queryNXT_KOSDAQ, mDataCode);
                    break;
                //case 국내시장종류.거래정지:
                //    bufQuery = string.Format(queryNXT_Dis, mDataCode);
                //    break;
                //case 국내시장종류.거래정지K:
                //    bufQuery = string.Format(queryNXT_Disk, mDataCode);
                //    break;
            }

            Nxt_mRequestQuery.Add("", bufQuery);


            return this;
        }
    }
}
