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

namespace MWS.Data.Manager
{
    public class ZigbeeManager : DapperHelper<Zigbee>, IZigbee
    {
        public int Delete(int Id)
        {
            string deleteSql = "Delete from zigbee where id=@Id";
            return Execute<Zigbee>(deleteSql, new { Id = Id });
        }

        public List<Zigbee> GetAll()
        {
            var sqlStr = @"SELECT	* FROM	zigbee Order by id	";
            return Query<Zigbee>(sqlStr);
        }

        public List<Zigbee> GetAll(int device)
        {
            var sqlStr = @"SELECT	* FROM	zigbee where 1=1 ";
            if (device>0)
            {
                sqlStr += "and device="+device;
            }
            sqlStr += " order by id ";
            return Query<Zigbee>(sqlStr);
        }

        public async Task<List<Zigbee>> 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<Zigbee>(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<Zigbee>(predGroup, sort);
            });
            return task.ToList();
        }

        public List<Zigbee> 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<Zigbee>(predGroup, sort, pageIndex, pageSize, out totalCount).ToList();
        }

        public int Update(Zigbee zigbee)
        {
            throw new NotImplementedException();
        }
    }
}
