using DapperExtensions;
using MWS.Data.InterFace;
using MWS.Library.Common;
using MWS.Model;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MWS.Data.Manager
{
    public class DeviceManager : DapperHelper<Device>, IDevice
    {
        public int Delete(int Id)
        {
            string deleteSql = "Delete from device where id=@Id";
            return Execute<Device>(deleteSql, new { Id = Id });
            
        }

        public List<Device> GetAll()
        {
            var sqlStr = @"SELECT	* FROM	device Order by Id	";
            return Query<Device>(sqlStr);
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <param name="keyword">检索关键字</param>
        /// <param name="sortingProperty">排序字段 默认Id</param>
        /// <param name="ascending">是否升序 默认false</param>
        /// <returns></returns>
        public async Task<List<Device>> GetModels(string keyword, string sortingProperty = "Id", bool ascending = false)
        {
            //条件 Eq 等于 Gt 大于 Ge 大于或等于 Lt 小于 Le 小于或等于 Like 模糊
            IList<IPredicate> predList = new List<IPredicate>();
            if (!keyword.IsNullOrEmptyOrWhiteSpace())
            {
                predList.Add(Predicates.Field<Device>(p => p.Name, Operator.Like, keyword));
            }
            IPredicateGroup predGroup = Predicates.Group(GroupOperator.Or, predList.ToArray());
            //排序
            IList<ISort> sort = new List<ISort>
            {
                new Sort { PropertyName = sortingProperty, Ascending = ascending }
            };
            var task = await Task.Run(() =>
            {
                return GetAll<Device>(predGroup, sort);
            });
            return task.ToList();
        }

 
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="keyword">检索关键字</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页尺寸</param>
        /// <param name="sortingProperty">排序字段 默认Id</param>
        /// <param name="ascending">是否升序 默认false</param>
        /// <returns></returns>
        public List<Device> GetPagingModels(string keyword, out int totalCount, int pageIndex = 1, int pageSize = 20, string sortingProperty = "Id", bool ascending = false)
        {
            //条件 Eq 等于 Gt 大于 Ge 大于或等于 Lt 小于 Le 小于或等于 Like 模糊
            IList<IPredicate> predList = new List<IPredicate>();
            if (!keyword.IsNullOrEmptyOrWhiteSpace())
            {
                predList.Add(Predicates.Field<Device>(p => p.Name, Operator.Like, keyword));
            }
            IPredicateGroup predGroup = Predicates.Group(GroupOperator.Or, predList.ToArray());
            //排序
            IList<ISort> sort = new List<ISort>
            {
                new Sort { PropertyName = sortingProperty, Ascending = ascending }
            };
            return GetPage<Device>(predGroup, sort, pageIndex, pageSize, out totalCount).ToList();
        }

        public int Update(Device t_Device)
        {
            throw new System.NotImplementedException();
        }
    }
}