using DC_A95.MES_A95;
using NPOI.OpenXmlFormats.Dml;
using NPOI.Util;
using Opc.Ua;
using OpcBasic;
using Rhino.DocObjects;
using SlMesDbIterface;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemFramework;

/// <summary>
/// 
/// </summary>
namespace MesWork
{
    /// <summary>
    /// 
    /// </summary>
    public class MES_QueryCache
    {
        /// <summary>
        /// 根据发动机编号，获取计划信息
        /// </summary>
        /// <param name="engineNo"></param>
        /// <returns></returns>
        public static void Get_ProductPlan_ByEngineNo(string engineNo, out string engineTypeNo, out string orderNum, out int orderNo, out int orderSum,out int if_codetest,out int if_hottest)
        {
            engineTypeNo = "";
            orderNum = "";
            orderNo = -1;
            orderSum = -1; 
            if_codetest=0; 
            if_hottest=0;
            var sql =
                $" SELECT" +
                $"  [订单类型],[生产日期],[班次],[上线序号],[上线总排序]" +
                $" ,[发动机流水号],[机型号],[托盘号],[订单号],[订单序号],[订单数量]" +
                $" ,[起始流水号],[终止流水号],[创建时间],[执行状态],[状态变更时间],[操作标记],isnull([是否冷试],0) as 是否冷试,isnull([是否热试],0) as 是否热试" +
                $" FROM [潍柴_接口_生产计划_执行计划]" +
                $" WHERE [发动机流水号] = '{engineNo}'";
            SqlOperation.ExecuteSql(sql, out DataTable dt, out string err);
            if (dt.Rows.Count > 0)
            {
                engineTypeNo = dt.Rows[0]["机型号"].ToString();
                orderNum = dt.Rows[0]["订单号"].ToString();
                orderNo = int.Parse(dt.Rows[0]["订单序号"].ToString());
                orderSum = int.Parse(dt.Rows[0]["订单数量"].ToString());
                if_codetest= int.Parse(dt.Rows[0]["是否冷试"].ToString());
                if_hottest = int.Parse(dt.Rows[0]["是否热试"].ToString());
            }
            if (err.Length > 0)
            {
                LogRecord.LogRecord.Insert_Log(Type_Log.Alarm, "", $"Get_ProductPlan_ByEngineNo 报错！，，{err}");
            }
        }
        /// <summary>
        /// 根据工位号,获取工序信息
        /// </summary>
        /// <param name="stationName"></param>
        /// <param name="processNum"></param>
        /// <param name="processName"></param>
        public static void Get_ProductProcess_ByOP(string opCode, out string processCode, out string processName, out string opName, out string AreaCode)
        {
            processCode = "";
            processName = "";
            opName = "";
            AreaCode = "";
            var sql = $"SELECT [工序号],[工序名称],[工位名称],[区域编码] FROM [MES_计划BOM_工位与名称] WHERE [工位号]='{opCode}'";
            SqlOperation.ExecuteSql(sql, out DataTable dt, out string err);
            if (dt.Rows.Count > 0)
            {
                processCode = dt.Rows[0]["工序号"].ToString();
                processName = dt.Rows[0]["工序名称"].ToString();
                opName = dt.Rows[0]["工位名称"].ToString();
                AreaCode = dt.Rows[0]["区域编码"].ToString();
            }
            if (err.Length > 0)
            {
                LogRecord.LogRecord.Insert_Log(Type_Log.Alarm, "", $"Get_ProductProcess_ByOP报错！，，{err}");
            }
        }
        //public static DataTable Get_QCData_Configure(string opCode)
        //{
        //    var sql = $"SELECT [修改时间],[机型号],[工序编号],[工序名称],[工位编号],[工位名称],[数据编码],[组顺序号],[列位置],[变量排序]," +
        //        $"[测量位置],[测量项目],[数据单位],[理论值],[下限值],[上限值],[数据说明]" +
        //        $" FROM [大柴_质量数据_配置信息]" +
        //        $" WHERE 工位编号='{opCode}'";
        //    SqlOperation.ExecuteSql(sql, out DataTable dt, out string err);
        //    return dt;
        //}
        public static int Get_EngineTypeID(string EngineType)
        {
            var sql = $"SELECT [AID] FROM [配方_大柴_机型] WHERE [机型号]='{EngineType}'";
            SqlOperation.ExecuteSql(sql, out DataTable dt, out string err);
            if(dt.Rows.Count>0)return int.Parse(dt.Rows[0][0].ToString());
            else return 0;
        }
        public static void Get_StationInfo(string OPName, out string PLC, out string StartDB)
        {
            var rows = MesWorkForm.dt_OPNameInfo.Select($"工位号='{OPName}'");
            if (rows.Length > 0)
            {
                PLC = rows[0]["PLC"].ToString();
                StartDB = "DB" + rows[0]["工位起始DB号"].ToString();
            }
            else
            {
                PLC = "";
                StartDB = "";
            }
        }
        public static void Get_OPCode_ByAGVCode(string agvcode, out string OPCode)
        {
            string sql = $"SELECT [操作工位] FROM [大柴_上线记录_外装线] WHERE AGV号={agvcode}";
            SqlOperation.ExecuteSql(sql, out DataTable dt, out string err);
            if (dt.Rows.Count > 0)
            {
                OPCode = dt.Rows[0][0].ToString();
            }
            else OPCode = "";
        }

    }
}
