using MWS.Data.InterFace;
using MWS.Library.Common.Module;
using MWS.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MWS.Data.Manager
{
    public class TeamManager : DapperHelper<Team>, ITeam
    {
        public List<Team> GetAll(string kewWord)
        {
            List<Team> teams;
            try
            {
                string sql = "Select * From team WHERE 1 = 1 ";
                string sqlWhe = "";
                if (!string.IsNullOrEmpty(kewWord))
                {
                    sqlWhe += " and Name like '%" + kewWord + "%' ";
                }
               
                sql += sqlWhe;
                // 查询条件
                sql += " order by Id ";
                //pd.TotalCount = Query<int>(countsql).SingleOrDefault();
                //string pageSql = sql + " Offset " + (pageIndex - 1) * pageSize + " row FETCH NEXT " + pageSize + " ROWS ONLY ";
                teams = Query<Team>(sql).ToList();
                return teams;
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }

        public PageData<Team> GetAll(string kewWord, int pageIndex, int pageSize)
        {
            PageData<Team> pd = new PageData<Team>()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            try
            {
                string sql = "Select * From team WHERE 1 = 1 ";
                string countsql = "Select count(1) From  team WHERE 1 = 1 ";
                string sqlWhe = "";
                if (!string.IsNullOrEmpty(kewWord))
                {
                    sqlWhe += " and Name like '%" + kewWord + "%' ";
                }

                sql += sqlWhe;
                // 查询条件
                sql += " order by Id ";
                countsql += sqlWhe;
                pd.TotalCount = Query<int>(countsql).SingleOrDefault();
                string pageSql = sql + " limit " + (pageIndex - 1) * pageSize + "," + pageSize;
                pd.Data = Query<Team>(pageSql);
                return pd;
            }
            catch (Exception ex)
            {
                pd.Data = null;
                return pd;
            }
        }

        public int GetAllRecordsCount(string kewWord)
        {
            string countsql = "Select count(1) From team WHERE 1 = 1 ";
            string sqlWhe = "";
            if (string.IsNullOrEmpty(kewWord))
            {
                sqlWhe += " and Name like '%" + kewWord + "%' ";
            }

            countsql += sqlWhe;
            return Query<int>(countsql).SingleOrDefault();
        }
    }
}
