using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataLinkMesWork;
using System.Security.Cryptography;
//using SystemFramework;

namespace bizFacade
{
    public class UserLogin
    {
        /// <summary>
        /// 登陆者查询_Easyui
        /// </summary>
        /// <param name="opName">登陆名</param>
        /// <param name="message">密码</param>
        /// <param name="IsSuccess"></param>
        /// <returns></returns>
        public static bool Getlogin(string opName, string message, out int IsSuccess)
        {
            IsSuccess = 0;
            string produceName = "MES_用户管理_人员登录_扫码";
            SqlParameter[] thisParms = new SqlParameter[3];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@条码", message);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@是否符合", IsSuccess);
            thisParms[2].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(produceName, ref thisParms))
            {
                IsSuccess = Convert.ToInt32(thisParms[2].Value);
                return true;
            }
            return false;
        }


        public static void GetUrl(string opname, out DataTable tb)
        {
            string sql = "SELECT TOP 1 [WebHtml],[工位号],[工步属性],[服务器IP],[通讯端口] FROM [dbo].[MES_计划BOM_工位与名称_视图] WHERE [工位号]='" + opname + "'";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            tb = null;
            DataAccess.ExecuteDataTable(sql, out tb);
        }
        public static bool ConfirmClass(string classes, out string confirmclass)
        {
            string procedureName = "AMES_物料拉动_人员班组管理_班次信息_查询";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            confirmclass = "";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@班次", SqlDbType.NVarChar, 50);
            sqlParameters[0].Direction = ParameterDirection.Output;
            if (DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters))
            {
                confirmclass = sqlParameters[0].Value.ToString();
                return true;
            }
            return false;
        }
        public static void ClassName_Select(string ClassCode, out DataTable dt)
        {
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            string sql;
            sql = "SELECT top 1 班次描述 FROM [dbo].[AMES_物料拉动_人员班组管理_班次信息] WHERE [班次代码]='" + ClassCode + "'";
            dt = null;
            DataAccess.ExecuteDataTable(sql, out dt);
        }
        /// <summary>
        /// 修改新密码
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="tb"></param>
        public static void updatePassWord(string userName, string passWord, out DataTable tb)
        {
            string sql = "update [dbo].[MES_用户管理_人员] set 密码='" + passWord + "' WHERE [用户名]='" + userName + "'";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            tb = null;
            DataAccess.ExecuteDataTable(sql, out tb);
        }
        /// <summary>
        /// 登陆界面设置窗口提交
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="tb"></param>
        public static void InitLoginCombobox(string clientIp,out DataTable tb)
        {
            string sql = "SELECT [工位代码],[工位号] FROM [dbo].[MES_计划BOM_工位与名称_视图] WHERE [工控机IP]='" + clientIp + "'";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            tb = null;
            DataAccess.ExecuteDataTable(sql, out tb);
        }


        /// <summary>
        /// 登陆界面设置窗口提交
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="tb"></param>
        public static void submitSetOk(string opNum,string opName,string serverIp,string serverPort,string manchineIp, out DataTable tb)
        {
            string sql = "update [dbo].[MES_计划BOM_工位与名称] set 工位代码='" + opNum + "',工控机IP='" + manchineIp + "',工位号='" + opName + "',服务器IP='" + serverIp + "',通讯端口='" + serverPort + "'  WHERE [工位号]='" + opName + "'";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            tb = null;
            DataAccess.ExecuteDataTable(sql, out tb);
        }
        /// <summary>
        /// 初始化设置串口的工位号
        /// </summary>
        /// <param name="tb"></param>
        public static void InitSetCombobox(out DataTable tb)
        {
            string sql = "SELECT [工位代码],[工位号] FROM [dbo].[MES_计划BOM_工位与名称_视图]";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            tb = null;
            DataAccess.ExecuteDataTable(sql, out tb);
        }
        /// <summary>
        /// 登陆者查询_Easyui
        /// </summary>
        /// <param name="loginUser">登陆名</param>
        /// <param name="passWord">密码</param>
        /// <param name="tb"></param>
        /// <returns></returns>
        public static bool loginUserSelect(string loginUser, string passWord, out DataTable tb)
        {
            string procedureName = "登陆者查询_Easyui";
            //string connectionString = ApplicationConfig.ConnectionString_MES;
            DataSet ds;
            string errorMessage;
            bool isOK;
            SqlParameter[] sqlParameters = new SqlParameter[2];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@登陆名", loginUser);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@密码", passWord);
            isOK = DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out  ds);
            if (isOK)
            {
                tb = ds.Tables[0];
                return true;
            }
            else
            {
                tb = null;
                return false;
            }

        }




        public static void ClassCode_Select(string ClassName, out DataTable dt)
        {
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            string sql;
            sql = "SELECT top 1 班次代码 FROM AMES_物料拉动_人员班组管理_班次信息 WHERE [班次描述]='" + ClassName + "'";
            dt = null;
            DataAccess.ExecuteDataTable(sql, out dt);
        }


        /// <summary>
        /// 查询是否登录过
        /// </summary>
        /// <param name="opname"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static void SearchUrl(string opname, out DataTable dt)
        {
            string procedureName;
            procedureName = "测量数据工位_监控系统_操作者工号_查询";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@工位号", opname);
            DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters, out dt);
        }




        /// <summary>
        /// [测量数据工位_监控系统_员工在线信息表_员工下线]
        /// </summary>
        /// <param name="Opname"></param>
        /// <param name="OperatorName"></param>
        /// <param name="OperatorManger"></param>
        /// <param name="Password"></param>
        /// <param name="Type"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public static bool Getoff_WorkingHours(string Opname, string OperatorName, string OperatorManger, string Password, int Type, out string Msg)
        {
            string procedureName = "AMES_物料拉动_测量数据工位_监控系统_员工在线信息表_员工下线";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            Msg = "";
            SqlParameter[] sqlParameters = new SqlParameter[6];
            sqlParameters[0] = new System.Data.SqlClient.SqlParameter("@操作者工号", OperatorName);
            sqlParameters[1] = new System.Data.SqlClient.SqlParameter("@管理者工号", OperatorManger);
            sqlParameters[2] = new System.Data.SqlClient.SqlParameter("@工位号", Opname);
            sqlParameters[3] = new System.Data.SqlClient.SqlParameter("@密码", Password);
            sqlParameters[4] = new System.Data.SqlClient.SqlParameter("@下线类型", Type);
            sqlParameters[5] = new SqlParameter("@Result", SqlDbType.NVarChar, 50);
            sqlParameters[5].Direction = ParameterDirection.Output;

            if (DataAccess.ExecuteStoredProcedure(procedureName, ref sqlParameters))
            {
                Msg = sqlParameters[5].Value.ToString();
                return true;
            }

            return false;


        }
        /// <summary>
        /// 查询班次
        /// </summary>
        /// <param name="tb"></param>
        public static void Getclasses(out DataTable tb)
        {
            string sql = "SELECT [班次代码],[班次描述] FROM [dbo].[AMES_物料拉动_人员班组管理_班次信息] ORDER BY 班次代码 ";
            //string connectionString = ApplicationConfig.ConnectionString;
            string errorMessage;
            tb = null;
            DataAccess.ExecuteDataTable(sql, out tb);
        }

    }
}
