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 INDEX_PAN : ADataObject
    {
     
        internal INDEX_PAN(국내지수종류 recvDataType)
        {
            this.mDataType = recvDataType;
        }

        국내지수종류 mDataType;

        #region 쿼리
        string queryKOSPI = @"SELECT '코스피' name, round(f_part_idx/100,2) part_idx, f_chg_type chg_type, round(f_part_chg/100,2) part_chg,
		round((f_part_chg / decode(f_chg_type, '+', f_part_idx - f_part_chg, '-', f_part_idx + f_part_chg, 1)) * 100, 2) rate
		FROM t_index
		WHERE f_part_code = '001'";

        string queryKOSDAQ = @"SELECT '코스닥' name, round(f_part_idx/100,2) part_idx, f_chg_type chg_type, round(f_part_chg/100,2) part_chg,
		round((f_part_chg / decode(f_chg_type, '+', f_part_idx - f_part_chg, '-', f_part_idx + f_part_chg, 1)) * 100, 2) rate
		FROM t_kosdaq_index 
		WHERE f_part_code = '001'";

        string queryKOSPI200 = @"SELECT '코스피200' name,f_part_idx/100 part_idx, f_chg_type chg_type, f_part_chg/100 part_chg, 
		round(f_part_chg / decode(f_chg_type, '+', f_part_idx - f_part_chg, '-', f_part_idx + f_part_chg, 1) * 100, 2) rate 
		FROM T_200_INDEX 
		WHERE f_part_code = '029'";

        //string queryKRX100 = @"SELECT 'KRX100' name, f_part_idx/100 part_idx, f_chg_type chg_type, f_part_chg/100 part_chg, 1
        //round(f_part_chg / decode(f_chg_type, '+', (f_part_idx / 100) - (f_part_chg / 100), '-', (f_part_idx / 100) + (f_part_chg / 100)), 2) rate 
        //FROM t_krx100_index WHERE f_part_code = '043'";
        string queryKRX100 = @"SELECT 'KRX100' name, f_part_idx/100 part_idx, f_chg_type chg_type, f_part_chg/100 part_chg, 
		round(f_part_chg / decode(f_chg_type, '+', (f_part_idx / 100) - (f_part_chg / 100), '-', (f_part_idx / 100) + (f_part_chg / 100)), 2) rate 
		FROM t_krx100_index WHERE f_part_code = '043'";


        //string queryFUTURE = @"SELECT '선물' name, part_idx part_idx, chg_type, ABS(part_chg) part_chg,F_NET_VOL,f_out_vol,  F_NET_TURNOVER, 
        //	ABS(round(part_chg / decode(chg_type, '+', (part_idx / 100) - (part_chg / 100), 
        //		'-', (part_idx / 100) + (part_chg / 100), 1), 2)) rate 
        //	FROM(SELECT f_curr_price / 100 part_idx, F_NET_VOL, f_out_vol, F_NET_TURNOVER, 
        //		(f_curr_price - f_base_price) / 100 part_chg, 
        //		decode(sign(f_curr_price - f_base_price), 1, '+', -1, '-', 0, ' ') chg_type 
        //		FROM t_sunmul_online 
        //		WHERE f_stock_code = (select f_stock_code from t_sunmul_batch 
        //where f_market_date = (select max(f_market_date)
        //	from t_sunmul_batch) 
        //	and substr(f_stock_code, 4, 1) = '1' 
        //	and F_MONTH_GUBUN = '1'))";


        string queryFUTURE = @"SELECT '선물' name, part_idx part_idx, chg_type, ABS(part_chg) part_chg,F_NET_VOL,f_out_vol,  F_NET_TURNOVER, 
        	ABS(round(part_chg / decode(chg_type, '+', (part_idx / 100) - (part_chg / 100), 
        		'-', (part_idx / 100) + (part_chg / 100), 1), 2)) rate 
        	FROM(SELECT decode(a.f_curr_price,0, b.F_JUN_LAST_PRICE/100, a.f_curr_price/100) part_idx, a.F_NET_VOL, a.f_out_vol,  a.F_NET_TURNOVER,
                decode(a.f_curr_price, 0, 0, (a.f_curr_price - b.F_JUN_LAST_PRICE*100)/100) part_chg,
                decode(a.f_curr_price, 0, ' ', decode(sign(a.f_curr_price -b.F_JUN_LAST_PRICE*100),1,'+',-1,'-',0,' ')) chg_type
           FROM t_sunmul_online a, t_sunmul_batch b
          WHERE a.f_stock_code = b.f_stock_code
            AND a.f_stock_seq = 1
            AND b.f_market_date = (select max(OPEN_DAY) from v_open_day)
            AND b.F_MONTH_GUBUN = '1') ";


        #endregion

        public override IDataRequest buildData()
        {

            string bufQuery = "";

            switch (this.mDataType)
            {
                case 국내지수종류.KOSPI:
                    bufQuery = queryKOSPI;
                    break;
                case 국내지수종류.KOSDAQ:
                    bufQuery = queryKOSDAQ;
                    break;
                case 국내지수종류.KOSPI200:
                    bufQuery = queryKOSPI200;
                    break;
                case 국내지수종류.KRX100:
                    bufQuery = queryKRX100;
                    break;
                case 국내지수종류.선물:
                    bufQuery = queryFUTURE;
                    break;
            }

            mRequestQuery.Add("", bufQuery);

            return this;
        }
    }
}
