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 STOCK_OVERTIME_PAN : ADataObject
    {


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

        국내시장종류 mDataType;

        string mDataCode = String.Empty;

        #region "쿼리"

        //      string queryKOSPI = @"SELECT DISTINCT b.f_stock_code stock_code, b.f_stock_nickname nickname, b.F_STOCK_WANNAME wanname, a.f_fore_price curr_price, 
        //a.f_fore_vol net_vol, c.f_final_price final_price, 
        //ABS(a.f_fore_price - c.f_final_price) net_chg, '' f_net_chg, 
        //case when(a.f_fore_price - c.f_final_price) > 0 then '+' 
        //when(a.f_fore_price - c.f_final_price) = 0 then '' 
        //when(a.f_fore_price - c.f_final_price) < 0 then '-' end chg_type, 
        //round(((a.f_fore_price - c.f_final_price) / c.f_final_price) * 100, 2)  rate 
        //FROM t_online1_call a, t_stock b, t_batch_day c 
        //WHERE b.f_stock_code = a.f_stock_code 
        //AND c.f_stock_code = a.f_stock_code 
        //AND b.f_mkt_halt = 'N' 
        //and b.f_STOCK_wanname = '{0}' 
        //      and a.f_fore_price <> 0";
        string queryKOSPI = @"SELECT DISTINCT 
                b.f_stock_code stock_code, 
                b.F_STOCK_WANNAME wanname, 
                a.f_net_chg net_chg, 
                decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') chg_type, 
                a.f_curr_price, 
			    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 
            FROM t_overtime_sise a, t_stock b 
            WHERE b.f_mkt_halt = 'N' 
                AND a.f_stock_code = b.f_stock_code 
                AND a.f_curr_price < > 0 
                AND b.f_sect_code = 'ST'
                AND b.f_STOCK_wanname = '{0}' 
            ORDER BY rate DESC";

        //      string queryKOSDAQ = @"SELECT DISTINCT b.f_stock_code stock_code, b.f_stock_nickname nickname, b.F_STOCK_WANNAME wanname, a.f_fore_price curr_price, 
        //a.f_fore_vol net_vol, c.f_final_price final_price, 
        //ABS(a.f_fore_price - c.f_final_price) net_chg, '' f_net_chg, 
        //case when(a.f_fore_price - c.f_final_price) > 0 then '+' 
        //when(a.f_fore_price - c.f_final_price) = 0 then '' 
        //when(a.f_fore_price - c.f_final_price) < 0 then '-' end chg_type, 
        //round(((a.f_fore_price - c.f_final_price) / c.f_final_price) * 100, 2)  rate 
        //FROM t_kosdaq_online1_call a, t_kosdaq_stock b, t_kosdaq_batch_day c 
        //WHERE a.f_stock_code = b.f_stock_code 
        //AND b.f_mkt_halt = 'N' 
        //AND c.f_stock_code = b.f_stock_code 
        //and b.f_stock_wanname = '{0}' 
        //      and a.f_fore_price <> 0";
        string queryKOSDAQ = @"SELECT DISTINCT 
			    b.f_stock_code stock_code, 
			    b.F_STOCK_WANNAME wanname, 
			    a.f_net_chg net_chg, 
			    decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') chg_type, 
			    a.f_curr_price, 
			    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 
			FROM t_kosdaq_overtime_sise a, t_kosdaq_stock b 
			WHERE b.f_mkt_halt = 'N' 
			AND a.f_stock_code = b.f_stock_code 
			AND a.f_curr_price < > 0 
            AND b.f_sect_code = 'ST'
            AND b.f_STOCK_wanname = '{0}' 
			ORDER BY rate DESC";

        #endregion


        public override IDataRequest buildData()
        {

            string bufQuery = "";

            switch (mDataType)
            {
                case 국내시장종류.KOSPI:
                    bufQuery = string.Format(queryKOSPI, mDataCode);
                    break;
                case 국내시장종류.KOSDAQ:
                    bufQuery = string.Format(queryKOSDAQ, mDataCode);
                    break;
            }
            
            mRequestQuery.Add("", bufQuery);

            return this;
        }
    }
}
