using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Txgy.RBS.DbModel.Models;
using Txgy.RBS.DTO;
using Txgy.RBS.Framework.Api;
using Txgy.RBS.IServices;

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

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

        public ApiResult AddRedisInfo(RedisInfoDTO redisServerDTO)
        {
            int res = 0;
            var redisServer = _iMapper.Map<redis_server>(redisServerDTO);

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

        public ApiResult AddResultInfo(ResultDTO  resultJsonDTO)
        {
            int res = 0;
            result result_Json = _iMapper.Map<result>(resultJsonDTO);

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

        public ApiResult DeleteRedisInfo(int id)
        {
            int res = _Client.Deleteable<redis_server>(p => p.id == id).ExecuteCommand();
            if (res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "删除失败" };
        }

        public ApiResult DeleteResultInfo(int id)
        {
           int res= _Client.Deleteable<result>(p=>p.id==id).ExecuteCommand();
            if (res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "删除失败" };
        }

        public List<ResultDTO> GetAllResultInfo()
        {
            var projects = _Client.Queryable<result>().ToList();
            return _iMapper.Map<List<ResultDTO>>(projects);
        }
        public List<RedisInfoDTO> GetAllRidesInfo()
        {
            var projects = _Client.Queryable<redis_server>().ToList();
            return _iMapper.Map<List<RedisInfoDTO>>(projects);
        }

        public RedisInfoDTO GetRedisInfo(int id)
        {
            var RedisServer = _Client.Queryable<redis_server>().First(c => c.id == id);
            var redisServerDTO = _iMapper.Map<RedisInfoDTO>(RedisServer);
            return redisServerDTO;
        }

        public ResultDTO GetResultInfo(int id)
        {
            result result_Json = _Client.Queryable<result>().First(c => c.id == id);
            ResultDTO  resultJsonDTO = _iMapper.Map<ResultDTO>(result_Json);
            return resultJsonDTO;
        }

        public List<ResultDTO> GetResultInfo(string projectName, DateTime startTime, DateTime endTime)
        {
          var pro=  _Client.Queryable<project_info>().Where(p => p.project_name == projectName).First();
            if (pro == null)
            {
                return new List<ResultDTO>();
            }
             var results=  _Client.Queryable<result>().Where(r => r.project_id == pro.project_id 
                                       && (startTime < Convert.ToDateTime(r.otime) && Convert.ToDateTime(r.otime) < endTime)).ToList();
            return _iMapper.Map<List<ResultDTO>>(results);
        }

        public ApiResult UpdateRedisInfo(RedisInfoDTO redisServerDTO)
        {
            var redisServer = _iMapper.Map<redis_server>(redisServerDTO);
            int res = _Client.Updateable(redisServer).ExecuteCommand();
            if (res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "更新失败" };
        }

        public ApiResult UpdateResultInfo(ResultDTO resultJsonDTO)
        {
            result result_Json = _iMapper.Map<result> (resultJsonDTO);
            int res=  _Client.Updateable<result>(result_Json).ExecuteCommand();
            if(res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "更新失败" };
        }
    }
}
