using DeviceRepair.DataAccess.Data;
using DeviceRepair.Models;
using DeviceRepair.Models.Plan;
using DeviceRepair.Utils;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;


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

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

        }

        /// <summary>
        /// 获取全部配置信息
        /// </summary>
        /// <returns></returns>
        public DataSet GetDatas()
        {
            var dsDatas = new DataSet("Datas");
            try
            {
                var EquipmentID = string.Empty;
                DateTime? CheckDate = null;

                if (ApiParameters.ContainsKey("EquipmentID"))
                {
                    EquipmentID = base.GetParamString("EquipmentID", "设备编号");
                }

                if (ApiParameters.ContainsKey("CheckDate"))
                {
                    CheckDate = base.GetParamDateTime("CheckDate", "校验日期");
                }

                var exp = Expressionable.Create<EquipmentJumpPlanCheckInfo, DeviceInformationInfo>()
                    .And((x, dev) => x.Status == "A")
                    .AndIF(!EquipmentID.IsNull(), (x, dev) => x.EquipmentID == EquipmentID)
                    .AndIF(CheckDate.HasValue, (x, dev) => x.CheckDate == CheckDate.Value)
                    .ToExpression();//拼接表达式

                var Datas = devMain.Queryable<EquipmentJumpPlanCheckInfo, DeviceInformationInfo>((x, dev) => new object[] {
                    JoinType.Inner , x.EquipmentAutoID == dev.AutoID
                }).Where(exp)?.Select((x, dev) => new
                {
                    AutoID = x.AutoID,
                    EquipmentAutoID = x.EquipmentAutoID,
                    CheckDate = x.CheckDate,
                    CreateBy = x.CreateBy,
                    CreateClient = x.CreateClient,
                    CreateOn = x.CreateOn,
                    EquipmentID = x.EquipmentID,
                    EquipmentName = dev.EquipmentName,
                    Status = x.Status
                }).ToList();
                var table = Datas.ToDataTable();
                dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }

        /// <summary>
        /// 数据新增
        /// </summary>
        /// <returns></returns>
        public APIResponseData DataNews()
        {
            try
            {
                var EquipmentID = base.GetParamString("EquipmentID", "设备编号");
                var CheckDate = base.GetParamDateTime("CheckDate", "校验日期");
                int OperationID = base.GetParamInt("OPERATORAUTOID", "操作员");

                var dev = devMain.Queryable<DeviceInformationInfo>().First(x => x.EquipmentID.Equals(EquipmentID, StringComparison.CurrentCultureIgnoreCase));
                if (dev == null)
                    throw new ArgumentException($"没有找到设备编号为【{EquipmentID}】的设备！");

                var Item = new EquipmentJumpPlanCheckInfo
                {
                    CheckDate = CheckDate.Date,
                    EquipmentAutoID = dev.AutoID,
                    EquipmentID = dev.EquipmentID,
                    Status = "A",
                    CreateBy = OperationID,
                    CreateClient = ApiParameters["CLIENTNAME"],
                    CreateOn = DateTime.Now
                };

                devMain.BeginTran();
                if (devMain.Insertable(Item).ExecuteCommand() > 0)
                {
                    devMain.CommitTran();
                    return new APIResponseData { Code = 1 };
                }
                else
                {
                    devMain.RollbackTran();
                    return new APIResponseData { Code = -1, Message = "操作失败！" };
                }
            }
            catch (SqlException sqlEx)
            {
                devMain.RollbackTran();
                throw sqlEx;
            }
            catch (Exception ex)
            {
                devMain.RollbackTran();
                log.Error(ex);
                throw ex;
            }
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <returns></returns>
        public APIResponseData DataDel()
        {
            try
            {
                var AutoID = base.GetParamInt("AutoID", "主键编号");
                var Item = devMain.Queryable<EquipmentJumpPlanCheckInfo>().First(x => x.AutoID == AutoID);
                if (Item == null)
                    throw new ArgumentNullException("当前数据不存在或已被其他人处理！");

                int OperationID = base.GetParamInt("OPERATORAUTOID", "操作员");

                Item.Status = "C";
                Item.ModifyBy = OperationID;
                Item.ModifyClient = ApiParameters["CLIENTNAME"];
                Item.ModifyOn = DateTime.Now;

                devMain.BeginTran();
                if (devMain.Updateable(Item).ExecuteCommand() > 0)
                {
                    devMain.CommitTran();
                    return new APIResponseData { Code = 1 };
                }
                else
                {
                    devMain.RollbackTran();
                    return new APIResponseData { Code = -1, Message = "操作失败！" };
                }
            }
            catch (SqlException sqlEx)
            {
                devMain.RollbackTran();
                throw sqlEx;
            }
            catch (Exception ex)
            {
                devMain.RollbackTran();
                log.Error(ex);
                throw ex;
            }
        }
    }
}
