using Masuit.Tools;
using Masuit.Tools.Models;
using PVDEMCS.Common;
using PVDEMCS.Common.DI;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories.Entities;

namespace PVDEMCS.Services.Repositories.Impl
{
    /// <summary>
    /// 系统参数设置 数据层 实现
    /// </summary>
    internal class SysConfigRepository : ISysConfigRepository
    {
        /// <summary>
        /// 获取系统参数设置分页列表
        /// </summary>
        /// <param name="configName">参数名称</param>
        /// <param name="configKey">参数键值</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<SysConfig>> GetSysConfigPageList(string configName, string configKey, int page, int size)
        {
            using (var context = new EFContext())
            {
                var query = QuerySysConfig(configName, configKey, context);

                var result = query.OrderBy(f => f.ConfigName).ToPagedList(page, size);

                return new Result<PagedList<SysConfig>>(result);
            }
        }

        /// <summary>
        /// 获取系统参数设置列表
        /// </summary>
        /// <param name="configName">参数名称</param>
        /// <param name="configKey">参数键值</param>
        /// <returns></returns>
        public Result<List<SysConfig>> GetSysConfigList(string configName, string configKey)
        {
            using (var context = new EFContext())
            {
                var query = QuerySysConfig(configName, configKey, context);

                var result = query.OrderBy(f => f.ConfigName).ToList();

                return new Result<List<SysConfig>>(result);
            }
        }

        private IQueryable<SysConfig> QuerySysConfig(string configName, string configKey, EFContext context)
        {
            var query = context.SysConfigs.Where(f => !f.IsDelete).Select(f =>
            new SysConfig
            {
                Id = f.Id,
                ConfigKey = f.ConfigKey,
                ConfigName = f.ConfigName,
                ConfigValue = f.ConfigValue,
                ConfigType = f.ConfigType,
                Remark = f.Remark,
            });

            if (!configName.IsNullOrEmpty())
            {
                query = query.Where(f => f.ConfigName.StartsWith(configName));
            }
            if (!configName.IsNullOrEmpty())
            {
                query = query.Where(f => f.ConfigKey.StartsWith(configKey));
            }

            return query;
        }

        /// <summary>
        /// 获取获取系统参数设置明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result<SysConfigEntity> GetSysConfigDetailById(string id)
        {
            using (var context = new EFContext())
            {
                var result = new Result<SysConfigEntity>();

                var detail = context.SysConfigs.Where(f => !f.IsDelete && f.Id == id).FirstOrDefault();
                if (detail == null)
                {
                    result.Message = $"没有找到Id:【{id}】的数据";
                    return result;
                }
                result.Content = detail;

                return result;
            }
        }

        /// <summary>
        /// 获取获取系统参数设置明细
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public Result<SysConfigEntity> GetSysConfigDetailNameOrKey(string name, string key)
        {
            using (var context = new EFContext())
            {
                var result = new Result<SysConfigEntity>();
                var detail = default(SysConfigEntity);

                var query = context.SysConfigs.Where(f => !f.IsDelete);
                if (!name.IsNullOrEmpty()) 
                {
                    detail = query.Where(f => f.ConfigName == name).FirstOrDefault();
                    if (detail == null)
                    {
                        result.Message = $"没有找到ConfigName:【{name}】的数据";
                        return result;
                    }
                }
                if (!key.IsNullOrEmpty())
                {
                    detail = query.Where(f => f.ConfigKey == key).FirstOrDefault();
                    if (detail == null)
                    {
                        result.Message = $"没有找到ConfigKey:【{key}】的数据";
                        return result;
                    }
                }
                result.Content = detail;

                return result;
            }
        }

        /// <summary>
        /// 添加系统参数设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Result AddSysConfig(SysConfigEntity entity)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var query = context.SysConfigs.Where(f => !f.IsDelete);
                if (query.Where(f => f.ConfigName == entity.ConfigName).FirstOrDefault() != null)
                {
                    result.Message = $"已存在名称为:【{entity.ConfigName}】的参数名称";
                    return result;
                }
                if (query.Where(f => f.ConfigKey == entity.ConfigKey).FirstOrDefault() != null)
                {
                    result.Message = $"已存在编号为:【{entity.ConfigKey}】的参数键值";
                    return result;
                }

                entity.Create();
                context.SysConfigs.Add(entity);
                context.SaveChanges();

                return result;
            }
        }

        /// <summary>
        /// 更新系统参数设置
        /// </summary>
        /// <param name="entity"></param>

        public Result UpdateSysConfig(SysConfigEntity entity)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var query = context.SysConfigs.Where(f => !f.IsDelete);
                var update = query.Where(f => f.Id == entity.Id).FirstOrDefault();
                if (update == null)
                {
                    result.Message = $"没有找到Id:【{entity.Id}】的数据";
                    return result;
                }
                if (query.Where(f => f.ConfigName == entity.ConfigName && f.Id != entity.Id).FirstOrDefault() != null)
                {
                    result.Message = $"已存在名称为:【{entity.ConfigName}】的参数名称";
                    return result;
                }
                if (query.Where(f => f.ConfigKey == entity.ConfigKey && f.Id != entity.Id).FirstOrDefault() != null)
                {
                    result.Message = $"已存在编号为:【{entity.ConfigKey}】的参数键值";
                    return result;
                }

                update.ConfigName = entity.ConfigName;
                update.ConfigKey = entity.ConfigKey;
                update.ConfigValue = entity.ConfigValue;
                update.ConfigType = entity.ConfigType;
                update.Remark = entity.Remark;
                entity.Update();
                context.SaveChanges();

                return result;
            }
        }

        /// <summary>
        /// 删除系统参数设置
        /// </summary>
        /// <param name="id"></param>
        public Result DeleteSysConfig(string id)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var entity = context.SysConfigs.Where(f => f.Id == id).FirstOrDefault();
                if (entity == null)
                {
                    result.Message = $"没有找到Id:【{id}】的数据";
                    return result;
                }

                entity.Delete();
                context.SaveChanges();

                return result;
            }
        }
    }
}
