using DeviceRepair.Models;
using SqlSugar;
using System;
using System.Collections.Generic;

namespace DeviceRepair.DataAccess
{
    public class SystemUtil : DbContext<SysConfigInfo>
    {
        private static SystemUtil manager;
        public static SystemUtil Instance
        {
            get
            {
                if (manager == null)
                    manager = new SystemUtil();
                return manager;
            }
        }

        /// <summary>
        /// 获取附件信息
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="PrimaryValue"></param>
        /// <returns></returns>
        public APIResponseData GetAttachment(string TableName, string PrimaryKey, string PrimaryValue)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                if (string.IsNullOrWhiteSpace(TableName))
                    throw new Exception("参数TableName的值不能为空！");
                if (string.IsNullOrWhiteSpace(PrimaryKey))
                    throw new Exception("参数PrimaryKey的值不能为空！");
                if (string.IsNullOrWhiteSpace(PrimaryValue))
                    throw new Exception("参数PrimaryValue的值不能为空！");


                base.db.ChangeDatabase("main");
                List<AttachmentInfo> lst = db.Queryable<AttachmentInfo>().Where(x =>
                x.TableName == TableName && x.PrimaryKey == PrimaryKey && x.PrimaryValue == PrimaryValue && x.Status)?.ToList();
                apiResponseData.Code = 1;
                apiResponseData.Data = lst;
                apiResponseData.Message = "";
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="AutoId"></param>
        /// <returns></returns>
        public APIResponseData GetAttachmentImg(int AutoId)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                base.db.ChangeDatabase("main");
                AttachmentInfo Data = db.Queryable<AttachmentInfo>().Single(x => x.AutoID == AutoId && x.Status);
                apiResponseData.Code = 1;
                apiResponseData.Data = Data;
                apiResponseData.Message = "";
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAll()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                base.db.ChangeDatabase("main");
                List<SysConfigInfo> lst = db.Queryable<SysConfigInfo>()?.ToList();
                apiResponseData.Code = 1;
                apiResponseData.Data = lst;
                apiResponseData.Message = "";
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 批量编辑配置
        /// </summary>
        /// <param name="Datas"></param>
        /// <returns></returns>
        public APIResponseData EditConfigs(List<SysConfigInfo> Datas)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                if ((Datas?.Count ?? 0) == 0)
                {
                    throw new Exception($"未能获取到待修改的配置信息！");
                }

                base.db.ChangeDatabase("main");

                if (Datas.Count != db.Updateable(Datas).UpdateColumns(x => new { x.Value }).ExecuteCommand())
                {
                    throw new Exception("修改的配置信息出错！");
                }

                apiResponseData.Code = 1;
                apiResponseData.Message = "";
            }
            catch (SqlSugarException ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}

