using SlMesDbIterface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesWork
{
    /// <summary>
    /// 显示当前工位信息
    /// </summary>
    public partial class TagDataEngineStatusShowDialog : Form
    {
        /// <summary>
        /// 当前的工位号
        /// </summary>
        public string opName;
        /// <summary>
        /// 
        /// </summary>
        public TagDataEngineStatusShowDialog()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="opName"></param>
        public TagDataEngineStatusShowDialog(string opName)
        {
            InitializeComponent();

            timer_ReadPLC.Interval = 5000;
            timer_ReadPLC.Enabled = true;

            txt_OpName.Text = this.opName = opName;

            ShowOpNamePic();
            SelectOpToInfoFromSQL();
            FromPLCRead();
        }
        /// <summary>
        /// 显示图片
        /// </summary>
        /// <param name="opName"></param>
        private void ShowOpNamePic()
        {
            string file = "OpNamePic\\"+ opName+ ".jpg";
            if (File.Exists(file))
            {
                pictureBox_OpName.Image = Image.FromFile(file);
            }
            else
            {
                pictureBox_OpName.Image = Image.FromFile("OpNamePic\\404.png");
            }
        }
        /// <summary>
        /// 根据工位号查询详细信息
        /// </summary>
        /// <param name="opName"></param>
        private void SelectOpToInfoFromSQL()
        {
            //获取工位上前信息
            var sql = $"SELECT [Last_工单号],[Last_发动机号],[Last_机型号],[Last_进站时间],[Last_离站时间],[Last_托盘号] FROM [MES_计划BOM_工位与名称] where 工位号 = '{opName}'";
            SqlOperation.ExecuteSql(sql, out DataTable dt_StationInfo, out string error);
            if(dt_StationInfo.Rows.Count>0)
            {
                var Last_工单号=dt_StationInfo.Rows[0]["Last_工单号"].ToString();
                var Last_机型号 = dt_StationInfo.Rows[0]["Last_机型号"].ToString();
                var Last_发动机号 = dt_StationInfo.Rows[0]["Last_发动机号"].ToString();
                var Last_托盘号 = dt_StationInfo.Rows[0]["Last_托盘号"].ToString();
                var Last_进站时间 = dt_StationInfo.Rows[0]["Last_进站时间"].ToString();
                var Last_离站时间 = dt_StationInfo.Rows[0]["Last_离站时间"].ToString();

                var Front_发动机号 = "";
                var Duration_Curr = "";
                var Duration_Front = "";
                if (!string.IsNullOrEmpty(Last_进站时间) && !string.IsNullOrEmpty(Last_离站时间))
                {
                    if(DateTime.Parse(Last_离站时间)> DateTime.Parse(Last_进站时间))
                    Duration_Curr = (DateTime.Parse(Last_离站时间) - DateTime.Parse(Last_进站时间)).TotalSeconds.ToString();
                }

                var sql_Front = $"SELECT top 1 [发动机流水号] FROM [大柴_过点信息] " +
                    $"where 工序编码='{opName}' and [发动机流水号]<>'{Last_发动机号}' and [发动机流水号]<>'' and 过点属性=1 " +
                    $"order by 采集时间 desc";
                SqlOperation.ExecuteSql(sql_Front, out DataTable dt_front, out string err);
                if(dt_front.Rows.Count>0) Front_发动机号=dt_front.Rows[0][0].ToString().Trim();
                if(Front_发动机号!="")
                {
                    var sql_frontInfo = $"SELECT top 2 发动机流水号,[工位编码],max([采集时间]) as [采集时间],[过点属性] " +
                        $"FROM [大柴_过点信息] where [工位编码]='{opName}' and 发动机流水号='{Front_发动机号}' "+
                        $"group by  [工位编码],发动机流水号,[过点属性] order by [采集时间] desc";
                    SqlOperation.ExecuteSql(sql_frontInfo, out DataTable dt_frontEngine, out string err1);
                    if(dt_frontEngine.Rows.Count>0)
                    {
                        var Front_进站时间 = dt_frontEngine.Rows[1]["采集时间"].ToString();
                        var Front_离站时间 = dt_frontEngine.Rows[0]["采集时间"].ToString();
                        if (!string.IsNullOrEmpty(Front_进站时间) && !string.IsNullOrEmpty(Front_离站时间))
                        {
                            if (DateTime.Parse(Front_离站时间) > DateTime.Parse(Front_进站时间))
                                Duration_Front = (DateTime.Parse(Front_离站时间) - DateTime.Parse(Front_进站时间)).TotalSeconds.ToString();
                        }
                    }
                }

                txt_Order.Text = Last_工单号;
                txt_SortNo.Text = Last_机型号;
                txt_EngineNo.Text = Last_发动机号;
                txt_Pollet.Text = Last_托盘号;
                txt_CylinderBlockNo.Text = "";//从扫码结果中读取
                txt_Duration_Curr.Text = "";
                txt_Duration_Front.Text = "";
                txt_Duration_Avg.Text = "";
                txt_Time_PartOn.Text = Last_进站时间;
                txt_Time_PartOff.Text = Last_离站时间;
                txt_Time_PartComplete.Text = Duration_Curr;
                txt_Time_PartComplete_Front.Text = Duration_Front;
            }
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TagDataEngineStatusShowDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer_ReadPLC.Enabled = false;
            MesWorkForm.list_OpenForm.Remove(opName);
        }
        /// <summary>
        /// 刷新 设备数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_ReadPLC_Tick(object sender, EventArgs e)
        {
            timer_ReadPLC.Enabled = false;
            FromPLCRead();
            timer_ReadPLC.Enabled = true;
        }
        /// <summary>
        /// 刷新 设备数据
        /// </summary>
        private void FromPLCRead()
        {
            try
            {

                txt_Order.Text = MesWorkForm.ReadPLC(40, opName).ToString();
                txt_SortNo.Text = MesWorkForm.ReadPLC(11, opName).ToString();
                txt_EngineNo.Text = MesWorkForm.ReadPLC(10, opName).ToString();
                txt_Pollet.Text = MesWorkForm.ReadPLC(80, opName).ToString();


                //txt_Duration_Curr.Text = 当前循环时间;
                //txt_Duration_Front.Text = 上一工件的循环时间;
                //txt_Duration_Avg.Text = 平均循环时间;
                //txt_Count_Group.Text = 班计数;
                //txt_Count_OK.Text = 总合格工件计数;
                //txt_Count_NOK.Text = 总不合格工件计数;

            }
            catch (Exception err)
            {
            }
        }



    }
}
