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 Quote_Stocks : ADataObject
    {
     
        internal Quote_Stocks(국내시장종류 recvDataType, string recvStockCode)
        {
            this.mDataType = recvDataType;

            this.mStockCode = recvStockCode;
        }

        국내시장종류 mDataType;

        string mStockCode = "";

        #region 쿼리
        string queryKOSPI = @"select a.f_stock_code ,  b.f_stock_wanname,  a.f_curr_price,
		a.f_net_chg NET_CHG,
		decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') CHG_TYPE,
		round((a.f_net_chg / decode(a.f_chg_type, '1', a.f_curr_price - a.f_net_chg,
			'2', a.f_curr_price - a.f_net_chg,
			'3', a.f_curr_price,
			'4', -1*(a.f_curr_price + a.f_net_chg),
			'5', -1*(a.f_curr_price + a.f_net_chg))) * 100, 2) rate,
		c.f_call_sell_1,
		c.f_call_sell_2,
		c.f_call_sell_3,
		c.f_call_sell_jan_1,
		c.f_call_sell_jan_2,
		c.f_call_sell_jan_3,
		c.f_call_buy_1,
		c.f_call_buy_2,
		c.f_call_buy_3,
		c.f_call_buy_jan_1,
		c.f_call_buy_jan_2,
		c.f_call_buy_jan_3,
		c.f_t_call_sell_jan,
		c.f_t_call_buy_jan 
		from t_online1 a, t_stock b , t_online1_CALL c
	where a.f_stock_code = b.f_stock_code 
    and a.f_stock_code = c.f_stock_code
	and a.f_mkt_halt = 'N' 
		and a.f_curr_price > 0 
		and a.f_stock_code = '{0}'";

        string queryKOSDAQ = @"select a.f_stock_code ,  b.f_stock_wanname,  a.f_curr_price, 
		a.f_net_chg NET_CHG, 
		decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') CHG_TYPE, 
		round((a.f_net_chg / decode(a.f_chg_type, '1', a.f_curr_price - a.f_net_chg, 
			'2', a.f_curr_price - a.f_net_chg, 
			'3', a.f_curr_price, 
			'4', -1*(a.f_curr_price + a.f_net_chg), 
			'5', -1*(a.f_curr_price + a.f_net_chg))) * 100, 2) rate, 
		c.f_call_sell_1, 
		c.f_call_sell_2, 
		c.f_call_sell_3, 
		c.f_call_sell_jan_1, 
		c.f_call_sell_jan_2, 
		c.f_call_sell_jan_3, 
		c.f_call_buy_1, 
		c.f_call_buy_2, 
		c.f_call_buy_3, 
		c.f_call_buy_jan_1, 
		c.f_call_buy_jan_2, 
		c.f_call_buy_jan_3, 
		c.f_t_call_sell_jan,
		c.f_t_call_buy_jan 
		FROM t_kosdaq_online1 a, t_kosdaq_stock b , t_kosdaq_online1_CALL c
	where a.f_stock_code = b.f_stock_code 
      and a.f_stock_code = c.f_stock_code
		and a.f_mkt_halt = 'N'
		and a.f_curr_price > 0
		and a.f_stock_code = '{0}'";

        
        #endregion

        public override IDataRequest buildData()
        {

            string bufQuery = "";

            switch (this.mDataType)
            {
                case 국내시장종류.KOSPI:
                    bufQuery = queryKOSPI;
                    break;
                case 국내시장종류.KOSDAQ:
                    bufQuery = queryKOSDAQ;
                    break;
            }

            mRequestQuery.Add("", String.Format(bufQuery, this.mStockCode));

            return this;
        }
    }
}
