using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.Model;

namespace MESClassLibrary.DAL.Injection
{
    public class InjectionDownRecordDAL
    {
        public static string TableName = "tb_Injection_DownRecord";
        public bool AddInfo(InjectionDownRecordModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = " IF NOT EXISTS (SELECT * FROM tb_Injection_DownRecord WHERE StationID=@StationID AND EndTime IS  NULL ) INSERT INTO " + TableName + " ([ID] ,[StationID] ,[DownType],[DownReason],[Des],[StartTime]";
                sql += ") VALUES (";
                sql += "@ID,";
                sql += "@StationID,";
                sql += "@DownType,";
                sql += "@DownReason,";
                sql += "@Des,";
                sql += "@StartTime)";

                #region 添加参数
                param = new SqlParameter[6];
                param[0] = new SqlParameter("@ID", SqlDbType.VarChar);
                param[0].Value = md.ID;

                param[1] = new SqlParameter("@StationID", SqlDbType.VarChar);
                param[1].Value = md.StationID;

                param[2] = new SqlParameter("@DownType", SqlDbType.VarChar);
                param[2].Value = md.DownType;

                param[3] = new SqlParameter("@DownReason", SqlDbType.VarChar);
                param[3].Value = md.DownReason;

                param[4] = new SqlParameter("@Des", SqlDbType.VarChar);
                param[4].Value = md.Des;

                param[5] = new SqlParameter("@StartTime", SqlDbType.DateTime);
                param[5].Value = md.StartTime;

                #endregion

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                #endregion
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public bool UpdateInfo(InjectionDownRecordModel md)
        {
            try
            {
                #region 添加数据
                string sql = @"update " + TableName + " set EndTime=@EndTime  WHERE [StationID]=@StationID AND EndTime IS NULL ";
                SqlParameter[] param = null;

                #region 添加参数
                param = new SqlParameter[2];
                param[0] = new SqlParameter("@StationID", SqlDbType.VarChar);
                param[0].Value = md.StationID;

                param[1] = new SqlParameter("@EndTime", SqlDbType.DateTime);
                param[1].Value = md.EndTime;
                #endregion

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                #endregion
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }


        public DataTable SearchByTime(string StartTime, string EndTime)
        {
            try
            {
                string sql = @" SELECT * FROM dbo.tb_Injection_DownRecord ";
                sql += " where StationID is not null and StartTime>='" + StartTime + "' and EndTime<='" + EndTime + "'";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }


        public DataTable SearchChangeRecord(string StartTime, string EndTime, string ModelId)
        {
            try
            {
                string sql = @" SELECT s.StationNo,m.PrintDate,d.StartTime,d.DownTime,d.EndTime,m.ModelSumCount,m.ModelPrintCount,d.remark1 FROM (SELECT * FROM [dbo].[tb_ModelCount]  WHERE ";
                sql += "ModelID = '" + ModelId + "' and  PrintDate>= '"+ StartTime + "' and PrintDate<= '"+ EndTime + "') as m ";
                sql += "left join (SELECT* FROM [dbo].[tb_Injection_DownRecord] where StationID is not null and StartTime>='" + StartTime + "' and EndTime<='" + EndTime + "') as d on m.StationID = d.StationID left join tb_Station s on d.StationID = s.StationID";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        //故障停机查询
        public DataTable SearchRepaorByTime(string StartTime, string EndTime)
        {
            try
            {
                string sql = @" SELECT d.StationID,s.StationNo,d.StartTime,d.EndTime,d.DownTime,d.Des,d.Remark1,d.Remark2,d.Remark3 FROM dbo.tb_Injection_DownRecord d ";
                sql += " left join tb_Station s on d.StationID = s.StationID ";
                sql += " where d.StationID is not null and d.StartTime>='" + StartTime + "' and d.EndTime<='" + EndTime + "' and d.DownType='F1F851FB-8913-40B7-A992-F28A3FD25054' order by d.StartTime";

                 return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }


    }
}