using Masuit.Tools.Models;
using PVDEMCS.Common.DI;
using PVDEMCS.Common;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories.Entities;
using PVDEMCS.Services.Repositories;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PVDEMCS.Common.Tools;
using PVDEMCS.Services.Repositories.Impl;

namespace PVDEMCS.Services.Impl
{
    /// <summary>
    /// 系统参数设置 服务层 实现
    /// </summary>
    internal class SysConfigService : ISysConfigService
    {
        private readonly ISysConfigRepository _sysConfigRepository;

        public SysConfigService(ISysConfigRepository sysConfigRepository)
        {
            this._sysConfigRepository = sysConfigRepository;
        }
        /// <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)
        {
            var result = _sysConfigRepository.GetSysConfigPageList(configName, configKey, page, size);
            return result;
        }

        /// <summary>
        /// 获取系统参数设置列表
        /// </summary>
        /// <param name="configName">参数名称</param>
        /// <param name="configKey">参数键值</param>
        /// <returns></returns>
        public Result<List<SysConfig>> GetSysConfigList(string configName, string configKey)
        {
            var result = _sysConfigRepository.GetSysConfigList(configName, configKey);
            return result;
        }

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

            var value = _sysConfigRepository.GetSysConfigDetailById(id);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<SysConfigEntity, SysConfig>.Trans(value.Content);

            return result;
        }

        /// <summary>
        /// 获取获取系统参数设置明细
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public Result<SysConfig> GetSysConfigDetailNameOrKey(string name, string key = "")
        {
            var result = new Result<SysConfig>();

            var value = _sysConfigRepository.GetSysConfigDetailNameOrKey(name, key);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<SysConfigEntity, SysConfig>.Trans(value.Content);

            return result;
        }

        /// <summary>
        /// 添加系统参数设置
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result AddSysConfig(SysConfig info)
        {
            var entity = ModelTools.PubClone<SysConfig, SysConfigEntity>.Trans(info);
            var result = _sysConfigRepository.AddSysConfig(entity);
            return result;
        }

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

        public Result UpdateSysConfig(SysConfig info)
        {
            var entity = ModelTools.PubClone<SysConfig, SysConfigEntity>.Trans(info);
            var result = _sysConfigRepository.UpdateSysConfig(entity);
            return result;
        }

        /// <summary>
        /// 删除系统参数设置
        /// </summary>
        /// <param name="id"></param>
        public Result DeleteSysConfig(string id)
        {
            var result = _sysConfigRepository.DeleteSysConfig(id);
            return result;
        }
    }
}
