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

        internal ETF(국내시장종류 recvDataType)
        {
            this.mDataType = recvDataType;

        }

        국내시장종류 mDataType;

        #region 쿼리

        string queryKOSPI = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_WANNAME wanname, 
               a.f_curr_price curr_price,
               a.f_net_chg net_chg, a.f_chg_type chg_type,
               b.f_list_num * a.f_curr_price sigachong,
               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_online1 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  f_sect_code = 'EF'
         ORDER BY sigachong DESC";

        string queryKOSDAQ = @"select 
                b.f_stock_code, 
                b.f_stock_wanname, 
                d.f_curr_price, 
                d.f_chg_type, 
                d.f_net_chg, 
                round((d.f_net_chg / decode(d.f_chg_type, '1', d.f_curr_price - d.f_net_chg, 
                                                        '2', d.f_curr_price - d.f_net_chg, 
                                                        '3', d.f_curr_price, 
                                                        '4', -1*(d.f_curr_price + d.f_net_chg), 
                                                        '5', -1*(d.f_curr_price + d.f_net_chg))) * 100, 2) rate 
            from ( 
                select f_stock_code, max(f_low_price) f_low_price  
                from t_kosdaq_candle_history 
                where f_data_date between '{0}' and '{1}' 
                group by f_stock_code) a, 
                t_kosdaq_stock b, t_kosdaq_online1 d 
            where a.f_stock_code = b.f_stock_code 
                and d.f_mkt_halt = 'N' 
                and d.f_curr_price > 0 
                and a.f_stock_code = d.f_stock_code 
                and a.f_low_price <= d.f_low_price order by rate desc";




        #endregion

        public override IDataRequest buildData()
        {


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


            return this;
        }
    }
}
