2025-04-11 09:50:48 +08:00
|
|
|
using MySqlConnector;
|
|
|
|
using DBObj;
|
|
|
|
using System.IO;
|
2025-04-15 10:59:03 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
2025-04-11 09:50:48 +08:00
|
|
|
namespace DualScreenDemo{
|
|
|
|
public partial class PrimaryForm
|
|
|
|
{
|
2025-07-03 18:15:21 +08:00
|
|
|
|
2025-06-03 16:52:44 +08:00
|
|
|
private static string GetConnectionString()
|
|
|
|
{
|
2025-07-04 14:45:39 +08:00
|
|
|
return Utils.Env.GetDBConnection();
|
2025-06-03 16:52:44 +08:00
|
|
|
}
|
|
|
|
|
2025-04-16 11:20:08 +08:00
|
|
|
public bool isLoggedIn = false;
|
|
|
|
public string userPhone = string.Empty;
|
2025-04-22 15:25:58 +08:00
|
|
|
public List<SongData> SearchSongs_Mysql(string query)
|
2025-04-11 09:50:48 +08:00
|
|
|
{
|
|
|
|
List<SongData> searchResults = new List<SongData>();
|
|
|
|
Console.WriteLine(query);
|
2025-06-03 16:52:44 +08:00
|
|
|
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
|
|
|
|
string connectionString = GetConnectionString();
|
2025-04-11 09:50:48 +08:00
|
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
|
|
{
|
2025-04-15 10:59:03 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
2025-04-11 09:50:48 +08:00
|
|
|
connection.Open();
|
2025-04-15 10:59:03 +08:00
|
|
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
2025-04-11 09:50:48 +08:00
|
|
|
Console.WriteLine("MyDB 連線成功!");
|
|
|
|
|
|
|
|
using (var command = new MySqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
|
|
|
|
using (var reader = command.ExecuteReader())
|
|
|
|
{
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
|
|
|
searchResults.Add(new SongData(
|
2025-07-04 18:19:41 +08:00
|
|
|
reader["song_id"].ToString(),
|
|
|
|
reader["song_name"].ToString(),
|
|
|
|
reader["artistA"].ToString(),
|
|
|
|
reader["artistB"].ToString(),
|
|
|
|
reader["song_filename"].ToString(),
|
|
|
|
reader["artistA_simplified"].ToString(),
|
|
|
|
reader["artistB_simplified"].ToString(),
|
|
|
|
reader["song_simplified"].ToString(),
|
|
|
|
Convert.ToInt32(reader["vocal"])
|
2025-04-11 09:50:48 +08:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.Close();
|
2025-04-15 10:59:03 +08:00
|
|
|
stopwatch.Stop();
|
|
|
|
Console.WriteLine($"MyDB 連線已關閉!執行時間: {stopwatch.ElapsedMilliseconds} 毫秒");
|
2025-04-15 11:31:02 +08:00
|
|
|
writeLogforSearchTime(stopwatch.ElapsedMilliseconds);
|
2025-04-15 10:59:03 +08:00
|
|
|
Console.WriteLine($"查詢到 {searchResults.Count} 筆資料。");
|
2025-04-11 09:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return searchResults;
|
|
|
|
}
|
2025-04-18 15:22:47 +08:00
|
|
|
public static List<Artist> SearchSingers_Mysql(string query){
|
2025-04-11 15:42:57 +08:00
|
|
|
List<Artist> searchResults = new List<Artist>();
|
|
|
|
Console.WriteLine(query);
|
2025-06-03 16:52:44 +08:00
|
|
|
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
|
|
|
|
string connectionString = GetConnectionString();
|
2025-04-11 15:42:57 +08:00
|
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
|
|
{
|
2025-04-15 11:31:02 +08:00
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
2025-04-11 15:42:57 +08:00
|
|
|
connection.Open();
|
2025-04-15 11:31:02 +08:00
|
|
|
stopwatch.Start();
|
2025-04-11 15:42:57 +08:00
|
|
|
Console.WriteLine("MyDB 連線成功!");
|
|
|
|
|
|
|
|
using (var command = new MySqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
|
|
|
|
using (var reader = command.ExecuteReader())
|
|
|
|
{
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
2025-04-18 15:22:47 +08:00
|
|
|
string artist = reader["name"].ToString();
|
2025-04-23 13:55:11 +08:00
|
|
|
string artistSimplified = reader ["simplified"].ToString();
|
|
|
|
searchResults.Add(new Artist(artist,artistSimplified));
|
2025-04-11 15:42:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connection.Close();
|
2025-04-15 11:31:02 +08:00
|
|
|
stopwatch.Stop();
|
|
|
|
Console.WriteLine($"MyDB 連線已關閉!執行時間: {stopwatch.ElapsedMilliseconds} 毫秒");
|
|
|
|
writeLogforSearchTime(stopwatch.ElapsedMilliseconds);
|
|
|
|
Console.WriteLine($"查詢到 {searchResults.Count} 筆資料。");
|
2025-04-11 15:42:57 +08:00
|
|
|
}
|
|
|
|
return searchResults;
|
|
|
|
|
|
|
|
}
|
2025-04-16 11:20:08 +08:00
|
|
|
public void InsertNewFavoriteSong(string songNumber){
|
|
|
|
|
2025-05-22 09:45:23 +08:00
|
|
|
string query = $"INSERT INTO FavoriteSongs (userPhone,songNumber) VALUES ('{userPhone}','{songNumber}');";
|
2025-04-16 11:20:08 +08:00
|
|
|
Console.WriteLine(query);
|
2025-06-03 16:52:44 +08:00
|
|
|
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
|
|
|
|
string connectionString = GetConnectionString();
|
2025-04-16 11:20:08 +08:00
|
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
|
|
{
|
|
|
|
|
|
|
|
connection.Open();
|
|
|
|
Console.WriteLine("MyDB 連線成功!");
|
|
|
|
|
|
|
|
using (var command = new MySqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
int rowsAffected = command.ExecuteNonQuery();
|
|
|
|
if (rowsAffected > 0)
|
|
|
|
{
|
|
|
|
Console.WriteLine("插入成功!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine("插入失敗(沒有資料被新增)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connection.Close();
|
|
|
|
Console.WriteLine("MyDB 連線已關閉!");
|
|
|
|
}
|
|
|
|
}
|
2025-05-22 13:33:48 +08:00
|
|
|
public void InsertNewFavoriteUser(string phonenumber){
|
|
|
|
string songlist = phonenumber + "的歌單";
|
|
|
|
string query = $"INSERT INTO FavoriteSongs (userPhone,songNumber) VALUES ('{phonenumber}','{songlist}');";
|
|
|
|
Console.WriteLine(query);
|
2025-06-03 16:52:44 +08:00
|
|
|
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
|
|
|
|
string connectionString = GetConnectionString();
|
2025-05-22 13:33:48 +08:00
|
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
|
|
{
|
|
|
|
|
|
|
|
connection.Open();
|
|
|
|
Console.WriteLine("MyDB 連線成功!");
|
|
|
|
|
|
|
|
using (var command = new MySqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
int rowsAffected = command.ExecuteNonQuery();
|
|
|
|
if (rowsAffected > 0)
|
|
|
|
{
|
|
|
|
Console.WriteLine("插入成功!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine("插入失敗(沒有資料被新增)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connection.Close();
|
|
|
|
Console.WriteLine("MyDB 連線已關閉!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public string SearchFavoriteSongs_Mysql()
|
|
|
|
{
|
2025-04-16 11:20:08 +08:00
|
|
|
string query = $"SELECT " +
|
2025-05-22 13:33:48 +08:00
|
|
|
"sl.*" +
|
2025-04-16 11:20:08 +08:00
|
|
|
"FROM FavoriteSongs fs " +
|
2025-05-22 09:45:23 +08:00
|
|
|
"JOIN song_library_cache sl ON fs.songNumber = sl.song_id " +
|
|
|
|
$"WHERE fs.userPhone = '{userPhone}';";
|
2025-04-16 11:20:08 +08:00
|
|
|
return query;
|
|
|
|
|
|
|
|
}
|
|
|
|
public bool checkPhoneNumberExist(string phonenumber){
|
2025-06-03 16:52:44 +08:00
|
|
|
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
|
|
|
|
string connectionString = GetConnectionString();
|
2025-04-16 11:20:08 +08:00
|
|
|
bool exists = false;
|
|
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
|
|
{
|
|
|
|
connection.Open();
|
|
|
|
Console.WriteLine("MyDB 連線成功!");
|
2025-05-28 11:04:03 +08:00
|
|
|
string query = $"SELECT COUNT(*) FROM FavoriteSongs WHERE userPhone = '{phonenumber}';";
|
2025-05-22 13:33:48 +08:00
|
|
|
using (var command = new MySqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
int count = Convert.ToInt32(command.ExecuteScalar());
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
exists = true; // 存在
|
|
|
|
userPhone = phonenumber;
|
|
|
|
isLoggedIn = true;
|
|
|
|
//點歌我的最愛按鈕,調整按鈕狀態
|
|
|
|
favoriteButton.Enabled = true;
|
|
|
|
favoriteButton.BackColor = Color.Transparent;
|
|
|
|
disabledPanel.Visible = false;
|
|
|
|
this.Invalidate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exists = false; // 不存在
|
|
|
|
}
|
2025-04-16 11:20:08 +08:00
|
|
|
}
|
|
|
|
connection.Close();
|
|
|
|
Console.WriteLine("MyDB 連線關閉!");
|
2025-05-22 13:33:48 +08:00
|
|
|
|
2025-04-16 11:20:08 +08:00
|
|
|
}
|
|
|
|
return exists;
|
|
|
|
}
|
2025-05-08 10:09:14 +08:00
|
|
|
public void logout(){
|
|
|
|
isLoggedIn=false;
|
|
|
|
userPhone=string.Empty;
|
|
|
|
}
|
2025-06-17 09:28:28 +08:00
|
|
|
//private static int countforSearch = 0;
|
2025-04-18 15:22:47 +08:00
|
|
|
private static void writeLogforSearchTime(long elapsedMs){
|
2025-06-17 09:28:28 +08:00
|
|
|
/*
|
2025-04-15 11:31:02 +08:00
|
|
|
countforSearch++;
|
|
|
|
|
|
|
|
string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "txt", "searchTimeLog.txt");
|
|
|
|
string data = $"{countforSearch}, {elapsedMs} " + Environment.NewLine;
|
|
|
|
File.AppendAllText(logFilePath, data);
|
2025-06-17 09:28:28 +08:00
|
|
|
*/
|
2025-04-15 11:31:02 +08:00
|
|
|
}
|
2025-04-18 15:22:47 +08:00
|
|
|
|
2025-05-14 11:18:01 +08:00
|
|
|
public void AddSongCount(string id){
|
2025-06-04 11:09:00 +08:00
|
|
|
string query = $"UPDATE song_library_cache SET song_counts = song_counts+1 WHERE song_id = '{id}';";
|
2025-05-14 11:18:01 +08:00
|
|
|
Console.WriteLine(query);
|
2025-06-03 16:52:44 +08:00
|
|
|
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
|
|
|
|
string connectionString = GetConnectionString();
|
2025-05-14 11:18:01 +08:00
|
|
|
using (var connection = new MySqlConnection(connectionString))
|
|
|
|
{
|
|
|
|
|
|
|
|
connection.Open();
|
|
|
|
Console.WriteLine("MyDB 連線成功!");
|
|
|
|
|
|
|
|
using (var command = new MySqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
int rowsAffected = command.ExecuteNonQuery();
|
|
|
|
if (rowsAffected > 0)
|
|
|
|
{
|
|
|
|
Console.WriteLine("新增成功!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine("新增失敗(點播次數沒有新增)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connection.Close();
|
|
|
|
Console.WriteLine("MyDB 連線已關閉!");
|
|
|
|
}
|
|
|
|
}
|
2025-04-18 15:22:47 +08:00
|
|
|
|
2025-04-11 09:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|