using MMoneyCoderSharp.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MMoneyCoderSharp.DBDefine;

namespace MMoneyCoderSharp.Data.Request
{
    internal class IndexIndustryYield : ADataObject
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="recvStockName"></param>
        /// <param name="recvDataLength"></param>
        public IndexIndustryYield(string recvIndustryCode, 데이터기간 recvDataLength, 국내시장종류 recvMarketType)
        {
            this.mIndustryCode = recvIndustryCode;
            this.mDataLength = recvDataLength;
            this.mMarketType = recvMarketType;
        }

        private string mIndustryCode = "";

        private 데이터기간 mDataLength;

        private 국내시장종류 mMarketType;

        const string queryKOSPI = @"select 
                substr(f_data_time, 1,8) data_day, 
                f_part_idx/100 curr_price, 
                f_chg_type chg_type, 
                F_PART_CHG/100 net_chg, 
                round((f_part_chg / decode(f_chg_type, '+', (f_part_idx - f_part_chg), '-', (f_part_idx + f_part_chg))) * 100, 2) rate 
            from t_index_his 
            where f_part_code = '{0}' 
                and Rtrim(f_data_time) in ( 
                    select open_day || '1535' 
                    from ( 
                        select open_day from v_open_day 
                        where open_day <> to_char(sysdate, 'yyyymmdd') 
                        minus 
                        select substr(f_data_time, 1, 8) data_day 
                        from t_index 
                        where f_part_code = '001' 
                        order by open_day desc 
                    ) 
                    where rownum < {1} 
            ) 
            UNION 
            select
                substr(f_data_time, 1, 8) data_day, 
                f_part_idx / 100 curr_price, 
                f_chg_type chg_type, 
                F_PART_CHG / 100 net_chg, 
                round((f_part_chg / decode(f_chg_type, '+', (f_part_idx - f_part_chg), '-', (f_part_idx + f_part_chg))) * 100, 2) rate 
            from t_index 
            where f_part_code = '{0}' 
            order by data_day desc";



        const string queryKOSDAQ = @"select 
                substr(f_data_time, 1,8) data_day, 
                f_part_idx/100 curr_price, 
                f_chg_type chg_type, 
                F_PART_CHG/100 net_chg, 
                round((f_part_chg / decode(f_chg_type, '+', (f_part_idx - f_part_chg), '-', (f_part_idx + f_part_chg))) * 100, 2) rate 
            from t_kosdaq_index_his 
            where f_part_code = '{0}' 
                and Rtrim(f_data_time) in ( 
                    select open_day || '1535' 
                    from ( 
                        select open_day from v_open_day 
                        where open_day <> to_char(sysdate, 'yyyymmdd') 
                        minus 
                        select substr(f_data_time, 1, 8) data_day 
                        from t_kosdaq_index 
                        where f_part_code = '001' 
                        order by open_day desc 
                    ) 
                    where rownum < {1}
            ) 
            UNION 
            select 
                substr(f_data_time, 1, 8) data_day, 
                f_part_idx / 100 curr_price, 
                f_chg_type chg_type, 
                F_PART_CHG / 100 net_chg, 
                round((f_part_chg / decode(f_chg_type, '+', (f_part_idx - f_part_chg), '-', (f_part_idx + f_part_chg))) * 100, 2) rate 
            from t_kosdaq_index 
            where f_part_code = '{0}' 
            order by data_day desc";

        public override IDataRequest buildData()
        {
            string bufQuery = string.Empty;

            if (mMarketType == 국내시장종류.KOSPI)
            {
                bufQuery = String.Format(queryKOSPI, this.mIndustryCode, this.mDataLength.GetStringValue());
            }
            else
            {
                bufQuery = String.Format(queryKOSDAQ, this.mIndustryCode, this.mDataLength.GetStringValue());
            }

            mRequestQuery.Add("", bufQuery);

            return this;
        }
    }

}
