using LYFZ.BLL;
using LYFZ.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace lyfzAttendance.Controller
{
public static class ClientProcess
{
public static bool ConnectStatus { get; set; } = false;
///
/// 客户端配置信息;
///
public static Model_Config ClientConfig { get; set; } = null;
private static BLL_Config BllConfig { get; set; } = new BLL_Config();
public static lyfzAttendanceLib.MachineConfig DeviceConfig { get; set; } = new lyfzAttendanceLib.MachineConfig();
public static lyfzAttendanceLib.IAttendance Attendance { get; set; } = null;
///
/// 云客户端模式下的tcp客户端连接对象;
///
private static readonly LYFZ.Network.CloudServer.CloudServerClient cloudClient = new LYFZ.Network.CloudServer.CloudServerClient();
///
/// 本地客户端模式下载的tcp客户端连接对象;
///
private static readonly LYFZ.Network.TCPNetworkClient.TCP_NetworkClient localClient = new LYFZ.Network.TCPNetworkClient.TCP_NetworkClient();
public static bool Connect(out string result)
{
result = "连接服务器成功";
// 获取配置文件信息;
ClientConfig = BllConfig.GetModelSysConfig();
// 是否是云客户端;
if ( ClientConfig.IsRemoteCloudClient )
{
cloudClient.Initialization(ClientConfig.EnterpriseID, "", ClientConfig.DongleDomain, ClientConfig.CloudPort);
if ( cloudClient.ConnectServer() )
{
HPSocketCS.Extended.ReturnData returnData = cloudClient.GetCloudServerDbConnInfo();
if ( returnData.ReturnStatus )
{
string dbConnectString = returnData.DataByteToModelObject();
dynamic dbConn = Newtonsoft.Json.JsonConvert.DeserializeObject(dbConnectString);
LYFZ.Helper.GetConnectionString connModel = new LYFZ.Helper.GetConnectionString();
connModel.DataBaseServer = dbConn.DataBaseServer;
connModel.DataBaseName = dbConn.DataBaseName;
connModel.DataBaseUser = dbConn.DataBaseUser;
connModel.DataBasePass = dbConn.DataBasePass;
connModel.DataBasePort = dbConn.DataBasePort;
connModel.UpdateConnInfo();
HPSocketCS.Extended.HP_SQLHelper.HpClient = cloudClient.TcpClient.Client;
LYFZ.Network.TCPNetworkClient.TCP_NetworkClient.IsRemoteCloudClient = true;
}
else
{
result = "获取“CloudServerConn”失败:" + returnData.ReturnMessage;
return false;
}
}
else
{
result = "连接服务器失败," + cloudClient.ReturnMessage;
return false;
}
}
else
{// 本地客户端;
if ( !localClient.ConnectionServer())
{
result = "连接服务器失败,请检查网络配置";
return false;
}
else
{
HPSocketCS.Extended.HP_SQLHelper.HpClient = localClient.Client;
LYFZ.Network.TCPNetworkClient.TCP_NetworkClient.IsRemoteCloudClient = false;
try
{
string dataConneStr = localClient.SendCommandToServer(HPSocketCS.Extended.TransferRequestCommand.GetDataConne, millisecondsTimeout: 5000);
if (dataConneStr.Trim().Length <= 0)
{
result = "向服务器发出请求失败,请检查服务器地址是否正确或服务端安装是否正确。";
return false;
}
if (!dataConneStr.Contains("错误:"))
{
LYFZ.Helper.GetConnectionString connModel = GetConnectionStringObj(dataConneStr, ClientConfig.ServerHostOrIP);
connModel.UpdateConnInfo();
}
}
catch (Exception ex)
{
result = "向服务器发出请求失败:" + ex.Message;
return false;
}
}
}
return ConnectStatus = true;
}
public static void DisConnect()
{
if (ClientConfig.IsRemoteCloudClient)
cloudClient.CloseConnect();
else
localClient.CloseConnected();
}
///
/// 获取数据库连接
///
/// 传入从服务器端获取到的数据库连接字符串
/// 服务器的主机名或IP地址
///
public static LYFZ.Helper.GetConnectionString GetConnectionStringObj(string _connectionString, string _ServerHostOrIP)
{
string[] connstrings = _connectionString.Split('|');
if (!LYFZ.WinAPI.CustomPublicMethod.CheckInternaNetworkAddress(connstrings[4]))
{
_ServerHostOrIP = connstrings[4];
}
LYFZ.Helper.GetConnectionString connModel = new LYFZ.Helper.GetConnectionString();
connModel.DataBaseServer = _ServerHostOrIP;
connModel.DataBaseName = connstrings[0];
connModel.DataBasePort = Convert.ToInt32(connstrings[1]);
connModel.DataBaseUser = connstrings[2];
connModel.DataBasePass = connstrings[3];
try
{
if (connstrings.Length >= 7)
{
connModel.YS_EnableMapped = Convert.ToBoolean(connstrings[5]);
connModel.YS_DatabasePort = Convert.ToInt32(connstrings[6]);
}
}
catch { }
try
{
bool isBranchStore = false;//是否为分店
if (connstrings.Length >= 8)
{
try
{
isBranchStore = Convert.ToBoolean(connstrings[7]);
}
catch { }
}
string DataBaseServer = connstrings[4];
if (isBranchStore)
{
connModel.DataBaseServer = DataBaseServer;
}
else
{
//WIN-92443G56CU2\SQLEXPRESS
if (DataBaseServer.Contains("\\"))
{
connModel.DataBaseServer = _ServerHostOrIP + "\\" + DataBaseServer.Split('\\')[1];
}
}
}
catch { }
return connModel;
}
public static void LoadMachineConfig()
{
if (File.Exists("machineconfig.json"))
{
try
{
string strJson = File.ReadAllText("machineconfig.json");
ClientProcess.DeviceConfig = JsonConvert.DeserializeObject(strJson);
}
catch (Exception ex)
{
}
}
}
public static void SaveMachineConfig()
{
if (File.Exists("machineconfig.json"))
{
File.Delete("machineconfig.json");
}
string strJson = JsonConvert.SerializeObject(DeviceConfig);
File.WriteAllText("machineconfig.json", strJson);
}
}
}