using AutoMapper;
using Microsoft.IdentityModel.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.WebSockets;
using System.Net;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;
using Txgy.RBS.DbModel.Models;
using Txgy.RBS.DTO;
using Txgy.RBS.Framework.Api;
using Txgy.RBS.IServices;
using static System.Net.Mime.MediaTypeNames;
using Txgy.RBS.Framework;

namespace Txgy.RBS.Services
{
    public class GlobalConfigService : BaseService, IGlobalConfigService
    {
        private readonly IMapper _iMapper;

        public GlobalConfigService(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            this._iMapper = iMapper;
        }

        public ApiResult AddGlobalConfig(GlobalConfigDTO  globalConfigDTO)
        {
            int res = 0;
            global_config global_Config = _iMapper.Map<global_config>(globalConfigDTO);

            res = _Client.Insertable(global_Config).ExecuteCommand();
            if (res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "添加失败" };
        }

        public List<GlobalConfigDTO> GetAllGlobalConfig()
        {
            var global_Config = _Client.Queryable<global_config>().ToList();
            return _iMapper.Map<List<GlobalConfigDTO>>(global_Config);
        }


        public GlobalConfigDTO GetGlobalConfig()
        {
            var global_Config = _Client.Queryable<global_config>().First();
            return _iMapper.Map<GlobalConfigDTO>(global_Config);
        }

        public ApiResult UpdateGlobalConfig(GlobalConfigDTO GlobalConfigDTO)
        {
            global_config global_Config = _iMapper.Map<global_config> (GlobalConfigDTO);
            int res=  _Client.Updateable<global_config>(global_Config).ExecuteCommand();
            if(res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "更新失败" };
        }
    }
}
