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 StationsService : BaseService, IStationsService
    {
        private readonly IMapper _iMapper;

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

        public ApiResult AddStations(StationDTO  stationsDTO)
        {
            int res = 0;
            station station = _iMapper.Map<station>(stationsDTO);

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

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

        public List<StationDTO> GetAllStations()
        {
            var projects = _Client.Queryable<station>().ToList();
            return _iMapper.Map<List<StationDTO>>(projects);
        }

        public List<StationNumVpnDTO> GetStationNumVpn()
        {
            var station = _Client.Queryable<station_num_vpn>().ToList();
            var stationsDTO = _iMapper.Map<List<StationNumVpnDTO>>(station);
            return stationsDTO;
        }

        public StationDTO GetStations(int id)
        {
            station station = _Client.Queryable<station>().First(c => c.id == id);
            StationDTO stationsDTO = _iMapper.Map<StationDTO>(station);
            return stationsDTO;
        }

        public ApiResult UpdateStations(StationDTO stationsDTO)
        {
            station station = _iMapper.Map<station> (stationsDTO);
            int res=  _Client.Updateable<station>(station).ExecuteCommand();
            if(res > 0)
            {
                return new ApiResult();
            }
            return new ApiResult() { Message = "更新失败" };
        }
    }
}
