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_PAN : ADataObject
    {
     
        internal STOCK_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_WANNAME stock_name , a.f_curr_price curr_price, 
		ABS(a.f_net_chg) net_chg, 
		decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') chg_type, a.f_net_vol net_vol, 
		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 , 'KOSPI' market
		FROM t_online1 a, t_stock b 
		WHERE b.f_mkt_halt = 'N'    
		AND a.f_curr_price < > 0     
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_wanname = '{0}'";

        string queryKOSDAQ = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_WANNAME stock_name , a.f_curr_price curr_price, a.f_net_chg net_chg, 
		decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') chg_type, a.f_net_vol net_vol, 
		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 , 'KOSDAQ' market
		FROM t_kosdaq_online1 a, t_kosdaq_stock b 
		WHERE b.f_mkt_halt = 'N' 
		AND a.f_curr_price < > 0 
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_wanname = '{0}'";

        string queryNXTKOSPI = @"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 , 'KOSPI' market
		FROM n_online a, n_stock b 
		WHERE b.f_mkt_halt = 'N'    
		AND a.f_curr_price != 0     
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_name = '{0}'";

        string queryNXTKOSDAQ = @"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 , 'KOSDAQ' market
		FROM n_kosdaq_online a, n_kosdaq_stock b 
		WHERE b.f_mkt_halt = 'N' 
		AND a.f_curr_price != 0 
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_name = '{0}'";

        string queryDis = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_WANNAME stock_name , a.f_curr_price curr_price, 
		ABS(a.f_net_chg) net_chg, 
		decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') chg_type, a.f_net_vol net_vol, 
		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_stop_online1 a, t_stock b 
		WHERE b.f_mkt_halt = 'Y' 
		AND a.f_curr_price < > 0 
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_wanname = '{0}'";

        string queryDisk = @"SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_WANNAME stock_name , a.f_curr_price curr_price, 
		ABS(a.f_net_chg) net_chg, 
		decode(a.f_chg_type, '1', '상한', '2', '상승', '3', '보합', '4', '하한', '5', '하락') chg_type, a.f_net_vol net_vol, 
		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_stop_kosdaq_online1 a, t_kosdaq_stock b 
		WHERE b.f_mkt_halt = 'Y' 
		AND a.f_curr_price < > 0 
		AND a.f_stock_code = b.f_stock_code 
		and b.f_STOCK_wanname = '{0}'";


   //     SELECT DISTINCT b.f_stock_code stock_code, b.F_STOCK_WANNAME stock_name, a.f_curr_price curr_price, \

   //         ABS(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 \
			//FROM t_stop_kosdaq_online1 a, t_kosdaq_stock b \

   //         WHERE b.f_mkt_halt = 'Y' \
			//AND a.f_curr_price<> 0  \

   //         AND a.f_stock_code = b.f_stock_code \

   //         AND b.f_stock_wanname = '%s'"
        #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;
                case 국내시장종류.거래정지:
                    bufQuery = string.Format(queryDis, mDataCode);
                    break;
                case 국내시장종류.거래정지K:
                    bufQuery = string.Format(queryDisk, mDataCode);
                    break;
            }

            mRequestQuery.Add("", bufQuery);
			

            return this;
        }
    }
}
