using MWS.Data.InterFace;
using MWS.Library.Common.Module;
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 UserLogManager : DapperHelper<UserLog>, IUserLog
    {
        public PageData<UserLog> GetAll(string kewWord, DateTime? beginTime, DateTime? endTime, int pageIndex, int pageSize)
        {
            PageData<UserLog> pd = new PageData<UserLog>()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            try
            {
                string sql = "Select * From userlog WHERE 1 = 1 ";
                string countsql = "Select count(1) From userlog WHERE 1 = 1 ";
                string sqlWhe = "";
                if (!string.IsNullOrEmpty(kewWord))
                {
                    sqlWhe += " and username like '%" + kewWord + "%' ";
                }
                if (beginTime != null && endTime != null)
                {
                    sqlWhe += "and createtime >= '" + beginTime.ToString() + "' and createtime <= '" + endTime.ToString() + "'";
                }
                sql += sqlWhe;
                // 查询条件
                sql += " order by Id desc";
                countsql += sqlWhe;
                pd.TotalCount = Query<int>(countsql).SingleOrDefault();
                string pageSql = sql + " limit " + (pageIndex - 1) * pageSize + "," + pageSize;
                pd.Data = Query<UserLog>(pageSql);
                return pd;
            }
            catch (Exception ex)
            {
                pd.Data = null;
                return pd;
            }
        }

        public int GetAllRecordsCount(string kewWord, DateTime? beginTime, DateTime? endTime)
        {
            string countsql = "Select count(1) From user WHERE 1 = 1 ";
            string sqlWhe = "";
            if (!string.IsNullOrEmpty(kewWord))
            {
                sqlWhe += " and username like '%" + kewWord + "%' ";
            }
            if (beginTime != null && endTime != null)
            {
                sqlWhe += " and createtime >= '" + beginTime.ToString() + "' and createtime <= '" + endTime.ToString() + "' ";
            }

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

        public int GetId()
        {
            string sql = "select max(id) as id from userlog";
            return Query<int>(sql).SingleOrDefault();
        }

        public int UpdateOutTime(int id)
        {
            string sql = " update userlog set logouttime = '"+DateTime.Now+"'  where id = "+id+" ";
            return Execute<int>(sql);
        }
       
    }
}
 