using System;
using System.Data;
using System.IO;
using static iMESCore.DataBase.iMESSql;

namespace AutoLoaderLib_C
{

    public class ParsePL : EPIParseTemplate
    {


        // 呼叫父類別的建構子
        public ParsePL(string paramSourcePath, string paramDestinationPath, string paramQueuePath, string paramFailPath) : base(paramSourcePath, paramDestinationPath, paramQueuePath, paramFailPath)
        {

        }

        /// <summary>
    /// 設定暫存資料表的欄位
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    /// <remarks></remarks>
        internal override bool AddColumns(DataTable dt)
        {

            bool blnResult = false;

            try
            {

                dt.Columns.Add("WaferID", Type.GetType("System.String"));
                dt.Columns.Add("MeasurementTime", Type.GetType("System.String"));
                dt.Columns.Add("NG", Type.GetType("System.Decimal"));
                dt.Columns.Add("OK", Type.GetType("System.Decimal"));
                dt.Columns.Add("Yield", Type.GetType("System.String"));
                dt.Columns.Add("Setup", Type.GetType("System.String"));
                dt.Columns.Add("LaserOutput", Type.GetType("System.String"));
                dt.Columns.Add("WPMIN", Type.GetType("System.Decimal"));
                dt.Columns.Add("WPMAX", Type.GetType("System.Decimal"));
                dt.Columns.Add("WPAVG", Type.GetType("System.Decimal"));
                dt.Columns.Add("WPDEV", Type.GetType("System.Decimal"));
                dt.Columns.Add("FWHMMIN", Type.GetType("System.Decimal"));
                dt.Columns.Add("FWHMMAX", Type.GetType("System.Decimal"));
                dt.Columns.Add("FWHMAVG", Type.GetType("System.Decimal"));
                dt.Columns.Add("FWHMDEV", Type.GetType("System.Decimal"));
                dt.Columns.Add("WDMIN", Type.GetType("System.Decimal"));
                dt.Columns.Add("WDMAX", Type.GetType("System.Decimal"));
                dt.Columns.Add("WDAVG", Type.GetType("System.Decimal"));
                dt.Columns.Add("WDDEV", Type.GetType("System.Decimal"));

                blnResult = true;
            }

            catch (Exception ex)
            {

                throw;

            }

            return blnResult;

        }

        /// <summary>
    /// 將暫存資料表寫入資料庫
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    /// <remarks></remarks>
        internal override bool InsertTable(DataTable dt)
        {

            bool blnResult = false;
            string strSQL;

            strSQL = " INSERT INTO TBL_LEDLD_PL " + "             (WaferID, CreateDate, MeasurementTime, NG, OK, Yield, Setup, LaserOutput, " + "              WPMIN, WPMAX, WPAVG, WPDEV, " + "              FWHMMIN, FWHMMAX, FWHMAVG, FWHMDEV, " + "              WDMIN, WDMAX, WDAVG, WDDEV) " + " VALUES      ('{0}', SYSDATE, '{1}', {2}, {3}, '{4}', '{5}', '{6}', " + "              {7}, {8}, {9}, {10}, " + "              {11}, {12}, {13}, {14}, " + "              {15}, {16}, {17}, {18}) ";








            try
            {

                using (var oTran = objConnection.BeginTransaction())
                {

                    try
                    {

                        using (var cmd = CreateCommand())
                        {

                            cmd.Connection = objConnection;
                            cmd.Transaction = oTran;

                            for (int i = 0, loopTo = dt.Rows.Count - 1; i <= loopTo; i++)
                            {

                                cmd.CommandText = string.Format(strSQL, dt.Rows[i]["WaferID"], dt.Rows[i]["MeasurementTime"], dt.Rows[i]["NG"], dt.Rows[i]["OK"], dt.Rows[i]["Yield"], dt.Rows[i]["Setup"], dt.Rows[i]["LaserOutput"], dt.Rows[i]["WPMIN"], dt.Rows[i]["WPMAX"], dt.Rows[i]["WPAVG"], dt.Rows[i]["WPDEV"], dt.Rows[i]["FWHMMIN"], dt.Rows[i]["FWHMMAX"], dt.Rows[i]["FWHMAVG"], dt.Rows[i]["FWHMDEV"], dt.Rows[i]["WDMIN"], dt.Rows[i]["WDMAX"], dt.Rows[i]["WDAVG"], dt.Rows[i]["WDDEV"]);










                                cmd.ExecuteNonQuery();

                            }

                        }

                        oTran.Commit();

                        blnResult = true;
                    }

                    catch (Exception ex)
                    {

                        oTran.Rollback();
                        throw;

                    }

                }
            }

            catch (Exception ex)
            {

                throw;

            }

            return blnResult;

        }

        /// <summary>
    /// 解析資料, 並寫入暫存資料表
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="file"></param>
    /// <returns></returns>
    /// <remarks></remarks>
        internal override bool ParsingFile(DataTable dt, string file)
        {

            bool blnResult = false;
            DataRow dr = null;
            string line;
            string[] tmpData;
            string LotNo;

            try
            {

                // 檔名即為 LotNo
                var fi = new FileInfo(file);
                LotNo = fi.Name.Replace(fi.Extension, "");
                fi = null;

                using (var sr = new StreamReader(file))
                {

                    // 第一行是欄位抬頭
                    line = sr.ReadLine();

                    do
                    {

                        line = sr.ReadLine();

                        if (!string.IsNullOrEmpty(line))
                        {

                            tmpData = line.Split(',');

                            dr = dt.NewRow();

                            dr["WaferID"] = tmpData[4].Trim() + tmpData[5].Trim().Substring(tmpData[5].Trim().Length - 2);
                            dr["MeasurementTime"] = tmpData[3].Trim();
                            dr["NG"] = Convert.ToDecimal(tmpData[11].Trim());
                            dr["OK"] = Convert.ToDecimal(tmpData[12].Trim());
                            dr["Yield"] = tmpData[13].Trim();
                            dr["Setup"] = tmpData[14].Trim();
                            dr["LaserOutput"] = tmpData[15].Trim();
                            dr["WPMIN"] = Convert.ToDecimal(tmpData[16].Trim());
                            dr["WPMAX"] = Convert.ToDecimal(tmpData[17].Trim());
                            dr["WPAVG"] = Convert.ToDecimal(tmpData[18].Trim());
                            dr["WPDEV"] = Convert.ToDecimal(tmpData[20].Trim());
                            dr["FWHMMIN"] = Convert.ToDecimal(tmpData[28].Trim());
                            dr["FWHMMAX"] = Convert.ToDecimal(tmpData[29].Trim());
                            dr["FWHMAVG"] = Convert.ToDecimal(tmpData[30].Trim());
                            dr["FWHMDEV"] = Convert.ToDecimal(tmpData[32].Trim());
                            dr["WDMIN"] = Convert.ToDecimal(tmpData[40].Trim());
                            dr["WDMAX"] = Convert.ToDecimal(tmpData[41].Trim());
                            dr["WDAVG"] = Convert.ToDecimal(tmpData[42].Trim());
                            dr["WDDEV"] = Convert.ToDecimal(tmpData[44].Trim());

                            dt.Rows.Add(dr);

                        }
                    }

                    while (!string.IsNullOrEmpty(line));

                    blnResult = true;

                }
            }

            catch (Exception ex)
            {

                throw;
            }

            finally
            {

                if (dr != null)
                {
                    dr = null;
                }

            }

            return blnResult;

        }

    }
}