using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Data.GetDataUser("Kriangkrai.R");
Console.WriteLine($"Manager = {Data.getNameManager()}");
Console.WriteLine($"Director = {Data.getNameDirector()}");
Console.WriteLine($"Administrator = {Data.getNameAdmin()}");
Console.ReadKey();
}
}
public class Data
{
public string Name;
public string Location;
public string Department;
public string Permission;
public string Groups;
public static string Manager;
public static string Director;
public static string Admin;
public Data(string Name,string Location, string Department, string Permission,string Groups)
{
this.Name = Name;
this.Location = Location;
this.Department = Department;
this.Permission = Permission;
this.Groups = Groups;
}
public static void GetDataUser(string Name)
{
List<Data> listData = new List<Data>();
SqlConnection con = new SqlConnection("Data Source=DESKTOP-BMFLGER\\SA;Initial Catalog=DataUser;User ID = sa; Password = 123456;");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [User]", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Data d = new Data(dr["Name"].ToString(), dr["LOcation"].ToString(), dr["Department"].ToString(), dr["Permission"].ToString(), dr["Groups"].ToString());
listData.Add(d);
}
dr.Close();
}
con.Close();
string department = "";
string Location = "";
foreach (var v in listData)
{
if(Name == v.Name)
{
department = v.Department;
Location = v.Location;
break;
}
}
//Get Name Manager
var NameManager = listData.Where(x => x.Department == department && x.Permission == "M").Select(x => x.Name).ToList();
foreach(var v in NameManager)
{
Manager += v.ToString() + ",";
}
Manager = Manager.Remove(Manager.Length - 1);
//Get Name Director
if (Location == "Bangkok" || Location == "Konkaen")
{
var NameDirector = listData.Where(x => x.Permission == "D" && (x.Location == "Bangkok")).Select(x => new DataDirector { Name = x.Name, Location = x.Location }).ToList();
foreach (var v in NameDirector)
{
Director = v.Name + ",";
}
Director = Director.Remove(Director.Length - 1);
}
else if(Location == "Rayong")
{
var NameDirector = listData.Where(x => x.Permission == "D" && (x.Location == "Rayong")).Select(x => new DataDirector { Name = x.Name, Location = x.Location }).ToList();
foreach (var v in NameDirector)
{
Director = v.Name + ",";
}
Director = Director.Remove(Director.Length - 1);
}
//Get Name Admin
var NameAdmin = listData.Where(x => x.Permission == "Admin").Select(x => x.Name).ToList();
foreach(var v in NameAdmin)
{
Admin += v.ToString() + ",";
}
Admin = Admin.Remove(Admin.Length - 1);
}
public static string getNameManager()
{
return Manager;
}
public static string getNameDirector()
{
return Director;
}
public static string getNameAdmin()
{
return Admin;
}
}
class DataDirector
{
public string Name { get; set; }
public string Location { get; set; }
}
}