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

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

        public ApiResult AddTimeTab(TimeTabDTO project)
        {
            int res = 0;
            time_tab station = _iMapper.Map<time_tab>(project);

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

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

        public List<TimeTabDTO> GetAllTimeTab()
        {
            var projects = _Client.Queryable<time_tab>().ToList();
            return _iMapper.Map<List<TimeTabDTO>>(projects);
        }


        public TimeTabDTO GetTimeTab(int id)
        {
            time_tab time_Tab   = _Client.Queryable<time_tab>().First(c => c.id == id);
            TimeTabDTO TimeTabDTO = _iMapper.Map<TimeTabDTO>(time_Tab);
            return TimeTabDTO;
        }

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