using Aitex.Core.RT.DBCore;
using Aitex.Sorter.Common;
using MECF.Framework.Common.ControlDataContext;
using MECF.Framework.Common.Equipment;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CyberX8_Core;

namespace MECF.Framework.Common.DBCore
{
    public class OffsetDataRecorder
    {

        //数据库结构
        //"\"guid\",",
        //"\"source_module\",",
        //"\"source_slot\",",
        //"\"destination_module\",",
        //"\"destination_slot\",",
        //"\"origin_module\",",
        //"\"origin_slot\",",
        //"\"arm_position\",",
        //"\"arm_pan\",",
        //"\"offset_x\",",
        //"\"offset_y\",",
        //"\"offset_d\",",
        //"\"start_time\",",
        //"\"end_time\",",
        public static void RecordOffsetData(
            string guid,
            ModuleName source_module, int source_slot,
            ModuleName destination_module, int destination_slot,
            string origin_module, int origin_slot,
            Hand arm_position, RobotArmPan arm_pan,
            double offset_x, double offset_y, double offset_d,
            DateTime start_time, DateTime end_time)
        {

            string sql = string.Format(
                "INSERT INTO \"offset_data\"(" +
                "\"guid\"," +
                "\"source_module\"," +
                "\"source_slot\"," +
                "\"destination_module\"," +
                "\"destination_slot\"," +
                "\"origin_module\"," +
                "\"origin_slot\"," +
                "\"arm_position\"," +
                "\"arm_pan\"," +
                "\"offset_x\"," +
                "\"offset_y\"," +
                "\"offset_d\"," +
                "\"start_time\"," +
                "\"end_time\"" +
                ")VALUES (" +
                $"'{guid}'," +
                $"'{source_module}'," +
                $"'{source_slot + 1}'," +
                $"'{destination_module}'," +
                $"'{destination_slot + 1}'," +
                $"'{origin_module}'," +
                $"'{origin_slot + 1}'," +
                $"'{arm_position}'," +
                $"'{arm_pan}'," +
                $"'{offset_x}'," +
                $"'{offset_y}'," +
                $"'{offset_d}'," +
                $"'{start_time.ToString("yyyy/MM/dd HH:mm:ss.fff")}'," +
                $"'{end_time.ToString("yyyy/MM/dd HH:mm:ss.fff")}'" +
                ");");
            DB.Insert(sql);
        }

        public static List<OffsetItem> QueryOffsetDataByTime(string moduleName, DateTime from_time, DateTime to_time)
        {
            List<OffsetItem> result = new List<OffsetItem>();
            string sql = string.Format($"SELECT * FROM \"offset_data\" WHERE \"start_time\" >= '{from_time.ToString("yyyy/MM/dd HH:mm:ss.fff")}' AND \"start_time\" <= '{to_time.ToString("yyyy/MM/dd HH:mm:ss.fff")}' AND \"destination_module\" = '{moduleName}' ");
            DataSet ds = DB.ExecuteDataset(sql);
            if (ds == null || ds.Tables.Count == 0)
                return result;
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                result.Add(new OffsetItem()
                {
                    Guid = ds.Tables[0].Rows[i]["guid"].ToString(),
                    SourceModule = ds.Tables[0].Rows[i]["source_module"].ToString(),
                    DestinationModule = ds.Tables[0].Rows[i]["destination_module"].ToString(),
                    OriginModule = ds.Tables[0].Rows[i]["origin_module"].ToString(),
                    SourceSlot = Convert.ToInt16(ds.Tables[0].Rows[i]["source_slot"]),
                    DestinationSlot = Convert.ToInt16(ds.Tables[0].Rows[i]["destination_slot"]),
                    OriginSlot = Convert.ToInt16(ds.Tables[0].Rows[i]["origin_slot"]),
                    ArmPosition = ds.Tables[0].Rows[i]["arm_position"].ToString(),
                    ArmPan = ds.Tables[0].Rows[i]["arm_pan"].ToString(),
                    OffsetX = Convert.ToDouble(ds.Tables[0].Rows[i]["offset_x"]),
                    OffsetY = Convert.ToDouble(ds.Tables[0].Rows[i]["offset_y"]),
                    OffsetD = Convert.ToDouble(ds.Tables[0].Rows[i]["offset_d"]),
                    StartTime = (DateTime)ds.Tables[0].Rows[i]["start_time"],
                    EndTime = (DateTime)ds.Tables[0].Rows[i]["end_time"],
                });
            }

            ds.Clear();

            return result;
        }
    }
}