using DeviceRepair.DataAccess.Data;
using DeviceRepair.Models;
using DeviceRepair.Models.History;
using DeviceRepair.Models.Preserve;
using DeviceRepair.Utils;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;

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

        public CommonDa() : base()
        {

        }

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

        }

        /// <summary>
        /// 获取附件数据
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="PrimaryValue"></param>
        /// <returns></returns>
        public DataSet GetAttachment()
        {
            DataSet dsDatas = new DataSet("Datas");
            try
            {
                string TableName = "";
                string PrimaryKey = "";
                string PrimaryValue = "";
                if (!ApiParameters.ContainsKey("TableName")
                    || !ApiParameters.ContainsKey("PrimaryKey")
                    || !ApiParameters.ContainsKey("PrimaryValue")
                    )
                {
                    throw new ArgumentException($"传入的附件查询参数不正确！");
                }
                TableName = ApiParameters["TableName"];
                PrimaryKey = ApiParameters["PrimaryKey"];
                PrimaryValue = ApiParameters["PrimaryValue"];

                List<AttachmentInfo> Datas = devMain.Queryable<AttachmentInfo>().Where(x =>
              x.TableName == TableName && x.PrimaryKey == PrimaryKey && x.PrimaryValue == PrimaryValue && x.Status)?.ToList();

                DataTable table = Datas.ToDataTable();
                dsDatas.Tables.Add(table);
                return dsDatas;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public AttachmentInfo Get_Attach_Single(int AutoID)
        {
            try
            {
                AttachmentInfo Data = devMain.Queryable<AttachmentInfo>().First(x => x.AutoID == AutoID);
                return Data;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 附件下载
        /// </summary>
        /// <param name="AttaID"></param>
        /// <param name="Datas"></param>
        /// <returns></returns>
        public APIResponseData GetAttachment(int AttaID, out byte[] Datas)
        {
            Datas = null;
            try
            {
                AttachmentInfo Data = devMain.Queryable<AttachmentInfo>().First(x => x.AutoID == AttaID && x.Status);
                string filePath = Data.FilePath;

                byte[] buffur = null;
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    Datas = new byte[fs.Length];
                    fs.Read(buffur, 0, (int)fs.Length);
                }
                return new APIResponseData { Code = 1, Data = $"{Data.FileName}-{Guid.NewGuid()}.{Data.Extension}" };
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
