using DeviceRepair.DataAccess.Data;
using DeviceRepair.DataAccess.Utils;
using DeviceRepair.Models.Enum;
using DeviceRepair.Models.OperationHistory.Device;
using DeviceRepair.Models.OperationHistory.Field;
using DeviceRepair.Models.OperationHistory.Tag;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DeviceRepair.DataAccess
{
    public class HistoryDa : BaseDa
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        public HistoryDa(IDictionary<string, string> apiParams) : base(apiParams)
        {
        }

        public DataSet HistoryGet(HistoryType vType)
        {
            try
            {
                switch (vType)
                {
                    case HistoryType.Tag_Data:
                        return GetTagDataHistory();
                    case HistoryType.Tag_Operation:
                        return GetTagOperationHistory();
                    case HistoryType.Field_Data:
                        return GetFieldDataHistory();
                    case HistoryType.Field_Operation:
                        return GetFieldOpsHistory();
                    case HistoryType.Dev_Data:
                        return GetDevDataHistory();
                    case HistoryType.Dev_Ops:
                        return GetDevOpsHistory();
                    default:
                        break;
                }

                return null;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }

        #region 红卡片/Tag
        /// <summary>
        /// 红卡片数据历史
        /// </summary>
        /// <returns></returns>
        public DataSet GetTagDataHistory()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                int TagNo = 0;
                if (ApiParameters.ContainsKey("TagNo") && !string.IsNullOrEmpty(ApiParameters["TagNo"] + ""))
                {
                    int.TryParse((ApiParameters["TagNo"] + ""), out TagNo);
                }

                string EquipNo = "";
                if (ApiParameters.ContainsKey("EquipNo"))
                {
                    EquipNo = ApiParameters["EquipNo"];
                }

                string StartTime = "";
                if (ApiParameters.ContainsKey("LogStartDateTime"))
                {
                    StartTime = ApiParameters["LogStartDateTime"];
                }

                string EndTime = "";
                if (ApiParameters.ContainsKey("LogEndDateTime"))
                {
                    EndTime = ApiParameters["LogEndDateTime"];
                }

                dsDatas = devLog.Ado.UseStoredProcedure().GetDataSetAll(SQLConstants.SP_GET_TAG_DATA_HISTORY, new
                {
                    TagNo = TagNo,
                    EquipNo = EquipNo,
                    @OperationBy = 0,
                    @StartTime = StartTime,
                    @EndTime = EndTime
                });

                // dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }

        /// <summary>
        /// 红卡片 操作历史
        /// </summary>
        /// <returns></returns>
        public DataSet GetTagOperationHistory()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                int TagNo = 0;
                if (ApiParameters.ContainsKey("TagNo"))
                {
                    TagNo = base.GetParamInt("TagNo", "Tag编号");
                }

                string OperationType = string.Empty;
                if (ApiParameters.ContainsKey("OperationType"))
                {
                    OperationType = ApiParameters["OperationType"];
                }

                string StartTime = "";
                if (ApiParameters.ContainsKey("LogStartDateTime"))
                {
                    StartTime = ApiParameters["LogStartDateTime"];
                }

                string EndTime = "";
                if (ApiParameters.ContainsKey("LogEndDateTime"))
                {
                    EndTime = ApiParameters["LogEndDateTime"];
                }

                var exp = Expressionable.Create<v_TagRecordLog>()
                      .AndIF(TagNo != 0, x => x.HisAutoID == TagNo)
                      .AndIF(!string.IsNullOrEmpty(OperationType), x => x.OperationType == OperationType)
                      .AndIF(!string.IsNullOrEmpty(StartTime), x => x.OperationDate >= SqlFunc.ToDate(StartTime))
                      .AndIF(!string.IsNullOrEmpty(EndTime), x => x.OperationDate <= SqlFunc.ToDate(EndTime))
                      .ToExpression();

                var table = devLog.Queryable<v_TagRecordLog>().Where(exp).ToDataTable();
                table.DataSet?.Tables?.Remove(table);
                dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }
        #endregion

        #region 主数据字段
        public DataSet GetFieldDataHistory()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                int vEnumType = -1;
                if (ApiParameters.ContainsKey("EnumType") && !string.IsNullOrEmpty(ApiParameters["EnumType"] + ""))
                {
                    int.TryParse((ApiParameters["EnumType"] + ""), out vEnumType);
                }

                if (vEnumType == -1)
                {
                    throw new Exception("请选择字段类型！");
                }

                string FieldText = "";
                if (ApiParameters.ContainsKey("FieldText"))
                {
                    FieldText = ApiParameters["FieldText"];
                }

                string StartTime = "";
                if (ApiParameters.ContainsKey("LogStartDateTime"))
                {
                    StartTime = ApiParameters["LogStartDateTime"];
                }

                string EndTime = "";
                if (ApiParameters.ContainsKey("LogEndDateTime"))
                {
                    EndTime = ApiParameters["LogEndDateTime"];
                }

                dsDatas = devLog.Ado.UseStoredProcedure().GetDataSetAll(SQLConstants.SP_GET_FIELD_DATA_HISTORY, new
                {
                    EnumType = vEnumType,
                    FieldText = FieldText,
                    @OperationBy = 0,
                    @StartTime = StartTime,
                    @EndTime = EndTime
                });
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }

        public DataSet GetFieldOpsHistory()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                int vEnumType = -1;
                string EnumType = string.Empty;
                if (ApiParameters.ContainsKey("FieldCode") && !string.IsNullOrEmpty(ApiParameters["FieldCode"] + ""))
                {
                    int.TryParse((ApiParameters["FieldCode"] + ""), out vEnumType);
                }

                if (vEnumType == -1)
                {
                    throw new Exception("请选择字段类型！");
                }

                switch (vEnumType)
                {
                    case 0:
                        EnumType = "SymptomlDistinction";
                        break;
                    case 1:
                        EnumType = "MAINTENANCE";
                        break;
                    case 2:
                        EnumType = "WHEREFAILUREOCCURRED";
                        break;
                    case 3:
                        EnumType = "ACCESSORIES";
                        break;
                    default:
                        break;
                }

                string FieldText = "";
                if (ApiParameters.ContainsKey("FieldText"))
                {
                    FieldText = ApiParameters["FieldText"];
                }

                string StartTime = "";
                if (ApiParameters.ContainsKey("LogStartDateTime"))
                {
                    StartTime = ApiParameters["LogStartDateTime"];
                }

                string EndTime = "";
                if (ApiParameters.ContainsKey("LogEndDateTime"))
                {
                    EndTime = ApiParameters["LogEndDateTime"];
                }

                var exp = Expressionable.Create<v_FieldOpsHistory>()
                      .AndIF(!string.IsNullOrEmpty(FieldText), x => x.FieldText == FieldText)
                      .AndIF(!string.IsNullOrEmpty(EnumType), x => x.FieldCode == EnumType)
                      .AndIF(!string.IsNullOrEmpty(StartTime), x => x.OperationDate >= SqlFunc.ToDate(StartTime))
                      .AndIF(!string.IsNullOrEmpty(EndTime), x => x.OperationDate <= SqlFunc.ToDate(EndTime))
                      .ToExpression();

                var table = devLog.Queryable<v_FieldOpsHistory>().Where(exp).ToDataTable();
                table.DataSet?.Tables?.Remove(table);
                dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }
        #endregion

        #region 设备信息
        public DataSet GetDevOpsHistory()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                string EquipmentID = string.Empty;
                if (ApiParameters.ContainsKey("EquipmentID"))
                {
                    EquipmentID = ApiParameters["EquipmentID"];
                }

                string EquipmentName = string.Empty;
                if (ApiParameters.ContainsKey("EquipmentName"))
                {
                    EquipmentName = ApiParameters["EquipmentName"];
                }

                string StartTime = "";
                if (ApiParameters.ContainsKey("LogStartDateTime"))
                {
                    StartTime = ApiParameters["LogStartDateTime"];
                }

                string EndTime = "";
                if (ApiParameters.ContainsKey("LogEndDateTime"))
                {
                    EndTime = ApiParameters["LogEndDateTime"];
                }

                var exp = Expressionable.Create<v_DeviceOpsHistory>()
                      .AndIF(!string.IsNullOrEmpty(EquipmentID), x => x.EquipmentID == EquipmentID)
                      .AndIF(!string.IsNullOrEmpty(EquipmentName), x => x.EquipmentName == EquipmentName)
                      .AndIF(!string.IsNullOrEmpty(StartTime), x => x.OperationDate >= SqlFunc.ToDate(StartTime))
                      .AndIF(!string.IsNullOrEmpty(EndTime), x => x.OperationDate <= SqlFunc.ToDate(EndTime))
                      .ToExpression();

                var table = devLog.Queryable<v_DeviceOpsHistory>().Where(exp).ToDataTable();
                table.DataSet?.Tables?.Remove(table);
                dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }

        public DataSet GetDevDataHistory()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                string EquipmentID = string.Empty;
                if (ApiParameters.ContainsKey("EquipmentID"))
                {
                    EquipmentID = ApiParameters["EquipmentID"];
                }

                string EquipmentName = string.Empty;
                if (ApiParameters.ContainsKey("EquipmentName"))
                {
                    EquipmentName = ApiParameters["EquipmentName"];
                }

                string StartTime = "";
                if (ApiParameters.ContainsKey("LogStartDateTime"))
                {
                    StartTime = ApiParameters["LogStartDateTime"];
                }

                string EndTime = "";
                if (ApiParameters.ContainsKey("LogEndDateTime"))
                {
                    EndTime = ApiParameters["LogEndDateTime"];
                }

                var exp = Expressionable.Create<v_DeviceDataHistory>()
                      .AndIF(!string.IsNullOrEmpty(EquipmentID), x => x.EquipmentID == EquipmentID)
                      .AndIF(!string.IsNullOrEmpty(EquipmentName), x => x.EquipmentName == EquipmentName)
                      .AndIF(!string.IsNullOrEmpty(StartTime), x => x.OperationDate >= SqlFunc.ToDate(StartTime))
                      .AndIF(!string.IsNullOrEmpty(EndTime), x => x.OperationDate <= SqlFunc.ToDate(EndTime))
                      .ToExpression();

                var table = devLog.Queryable<v_DeviceDataHistory>().Where(exp).ToDataTable();
                table.DataSet?.Tables?.Remove(table);
                dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }
        #endregion
    }
}
