using System;
using System.Data;
using System.Data.SqlClient;
using Common;

namespace Business.System.Op.Vo
{
    public class LmtVo
    {
        /// <summary>
        /// 根据userid获取用户权限
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns>用户不可操作代码</returns>
        public string GetLmt(string userid)
        {
            string check = "select count(*) from SysLmt where userid=@userid ";
            string result = SqlHelper.ExecSqlSc(check, new SqlParameter("userid", userid));
            if (!SqlHelper.CheckSc(result))
            {
                return "**";
            }
            int count = Convert.ToInt32(result);
            if (count <= 0)
            {
                return "**";
            }

            string sql = "select ops from SysLmt where userid=@userid ";
            result=SqlHelper.ExecSqlSc(sql,new SqlParameter( "userid",userid));
            if (!SqlHelper.CheckSc(result))
            {
                result = "**";
            }
            return result;

        }

        /// <summary>
        /// 修改用户权限
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="ops">操作集合</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string UpdateLmt(string userid,string ops)
        {
            string sql = "update SysLmt set ops=@ops where userid=@userid ";
            return SqlHelper.ExecSql(sql, new SqlParameter("@userid", userid), new SqlParameter("@ops", ops));
        }


        /// <summary>
        /// 获取系统日志
        /// </summary>
        /// <param name="searchTime">搜索字段</param>
        ///  <param name="searchText">搜索字段</param>
        /// <returns>数据表</returns>
        public DataTable GetSysLog(string searchTime,string searchText)
        {
            var sql =
                "select top(500) l.time ,u.username,l.remark from SysLog l left join SysUser u on l.userid=u.userid ";
            if (!string.IsNullOrEmpty(searchTime))
            {
                //根据查询字段 进行查询
                sql +=
                    string.Format("where (l.time between '{0}' and '{0} 23:59:59') ", searchTime);
                if (!string.IsNullOrEmpty(searchText))
                {
                    //根据查询字段 进行查询
                    sql +=
                        string.Format("and  (u.username like '%{0}%' or l.remark like '%{0}%')", searchText);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(searchText))
                {
                    //根据查询字段 进行查询
                    sql +=
                        string.Format("where ( u.username like '%{0}%' or l.remark like '%{0}%')", searchText);
                }
            }
          
            sql += " order by l.time DESC";

            return SqlHelper.ExecSqlDateTable(sql);
        }


      
    }
}
