using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Data.Entity.Infrastructure;
using System.Collections;
using System.Collections.Generic;
public partial class admin_activity_kind_reg : MyWeb.config
{
DataTable treeDt = new DataTable();
const int LevelMax = Model.activity.KindLevelMax; //分類層數
private Model.ezEntities _db = new Model.ezEntities();
public string _this_id = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
BuildTreeView();
if (!isStrNull(Request["num"]))
{
_this_id = ValString(Request["num"]);
int _num = Val(Request["num"]);
var prod = _db.activity_kind.AsEnumerable().Where(q => q.num == _num).OrderBy(q=>q.kind).FirstOrDefault();
if (prod != null)
{
title_msg.Text = "修改【" + prod.kind + "】的分類名稱";
item_name.Text = prod.kind;
demo.Text = prod.demo;
calendar.SelectedValue = prod.calendar;
month.Value = prod.month.HasValue? prod.month.Value.ToString():"";
day.Value = prod.day.HasValue ? prod.day.Value.ToString() : "";
HiddenField1.Value = prod.kind;
}
else
{
Response.Redirect(Request.Url.AbsolutePath );
}
edit.Visible = true;
add.Visible = false;
down_table.Visible = true;
start.Visible = false;
}
else
{
table.Visible = false;
down_table.Visible = false;
if (Convert.ToString(Request["msg"]) == "A") { start.Text = "【資料修改成功】"; }
else if (Convert.ToString(Request["msg"]) == "B") { start.Text = "【資料新增成功】"; }
else if (Convert.ToString(Request["msg"]) == "C") { start.Text = "【資料刪除成功】"; }
else { start.Text = "【請點選下方欲新增、修改或刪除的分類】"; }
}
}
}
#region 建立分類
protected void TreeTopology()
{
MyWeb.sql sql = new MyWeb.sql();
OleDbConnection sqlConn = sql.conn(db, p_name);
try
{
sqlConn.Open();
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
sqlCmd.CommandText = "SELECT num,kind,root FROM activity_kind ORDER BY kind,root, range";
treeDt = sql.dataTable(sqlCmd);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
sqlConn.Close();
sqlConn.Dispose();
}
}
public void BuildTreeView()
{
TreeView2.Nodes.Clear();
TreeTopology();
BuildChild(0, TreeView2.Nodes);
treeDt.Dispose();
}
public void BuildChild(int RootUid, TreeNodeCollection Nodes, int Level=1)
{
DataTable dt = treeDt;
foreach (DataRow row in dt.Rows)
{
if (row["root"].ToString() == RootUid.ToString())
{
TreeNode NewNode = new TreeNode();
if (Convert.ToString(Request["num"]) == row["num"].ToString() & HiddenField1.Value != "AddMainItem")
{
NewNode.Text = "" + row["kind"].ToString() + "";
if (Level +1 > LevelMax)
{
ImageButton5.Visible = false;
}
}
else
{
NewNode.Text = row["kind"].ToString();
}
NewNode.NavigateUrl = Request.Url.AbsolutePath + "?num=" + row["num"].ToString() ;
NewNode.Expand();
Nodes.Add(NewNode);
if (Level + 1 <= LevelMax)
{
BuildChild((int)row["num"], NewNode.ChildNodes, Level + 1);
}
}
}
}
#endregion
#region 新增分類按鈕事件
protected void ImageButton5_Click(object sender, EventArgs e)
{
title_msg.Text = "於【" + HiddenField1.Value + "】分類下,新增次分類";
item_name.Text = "";
add.Visible = true;
edit.Visible = false;
down_table.Visible = false;
start.Visible = false;
calendar.SelectedValue = "";
demo.Text = "";
month.Value = "";
day.Value = "";
_this_id = ValString(Request["num"]);
_this_id = "";
}
protected void ImageButton4_Click(object sender, EventArgs e)
{
title_msg.Text = "於根目錄下,新增主分類";
table.Visible = true;
item_name.Text = "";
calendar.SelectedValue = "";
demo.Text = "";
month.Value = "";
day.Value = "";
add.Visible = true;
edit.Visible = false;
down_table.Visible = false;
start.Visible = false;
HiddenField1.Value = "AddMainItem";
BuildTreeView();
_this_id = "";
}
#endregion
#region 資料修改
protected void edit_Click(object sender, EventArgs e)
{
L_msg.Text = "";
int _num = Val(Request["num"]);
try
{
Model.activity_kind activity_kind = _db.activity_kind.Where(q => q.num == _num).FirstOrDefault();//修改
if (activity_kind != null)
{
activity_kind.kind = item_name.Text;
activity_kind.demo = demo.Text;
if (!isStrNull(calendar.SelectedValue)) { activity_kind.calendar = calendar.SelectedValue; } else { activity_kind.calendar = null; };
if (!isStrNull(month.Value)) { activity_kind.month = Val(month.Value); } else { activity_kind.month = null; };
if (!isStrNull(day.Value)) { activity_kind.day = Val(day.Value); } else { activity_kind.day = null; };
_db.SaveChanges();
Model.admin_log admin_log = new Model.admin_log();
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Activity, (int)Model.admin_log.Status.Update, "詳細分類:" + item_name.Text);
L_msg.Type = alert_type.success;
L_msg.Text = "操作成功";
}
else
{
L_msg.Type = alert_type.danger;
L_msg.Text = "無此資料";
}
}
catch (DbUpdateConcurrencyException)
{
L_msg.Type = alert_type.danger;
L_msg.Text = "Error";
throw;
}
BuildTreeView();
}
#endregion
#region 資料新增
protected void add_Click(object sender, EventArgs e)
{
L_msg.Text = "";
int range = 1;
int root = 0;
if (HiddenField1.Value != "AddMainItem")
{
root = Convert.ToInt32(Request["num"]);
}
try
{
var prod = _db.activity_kind.AsEnumerable().Where(q => q.root == root).OrderByDescending(q => q.range).FirstOrDefault();
if (prod != null)
if (prod.range.HasValue)
range = prod.range.Value + 1;
Model.activity_kind activity_kind = new Model.activity_kind();//新增
activity_kind.kind = item_name.Text;
activity_kind.demo = demo.Text;
activity_kind.root = root;
activity_kind.range = range;
if (!isStrNull(calendar.SelectedValue)) { activity_kind.calendar = calendar.SelectedValue; } else { activity_kind.calendar = null; };
if (!isStrNull(month.Value)) { activity_kind.month = Val(month.Value); } else { activity_kind.month = null; };
if (!isStrNull(day.Value)) { activity_kind.day = Val(day.Value); } else { activity_kind.day = null; };
_db.activity_kind.Add(activity_kind);
_db.SaveChanges();
int _id = activity_kind.num;
if (_id > 0)
{
Model.admin_log admin_log = new Model.admin_log();
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Activity, (int)Model.admin_log.Status.Insert, "詳細分類:" + item_name.Text);
}
else
{
L_msg.Type = alert_type.danger;
L_msg.Text = "Error";
}
L_msg.Type = alert_type.success;
L_msg.Text = "操作成功";
}
catch (DbUpdateConcurrencyException)
{
throw;
}
BuildTreeView();
}
#endregion
#region 資料刪除
protected void ImageButton6_Click(object sender, EventArgs e)
{
int num = Val(Request["num"]);
del_product(num);
var prod = _db.activity_kind.AsEnumerable().Where(q => q.num == num).FirstOrDefault(); //刪除該筆資料
if (prod != null)
{
_db.activity_kind.Remove(prod);
_db.SaveChanges(); //執行
}
Del_Ohter_Items(Val(Request["num"]));
Model.admin_log admin_log = new Model.admin_log();
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Activity, (int)Model.admin_log.Status.Delete, "詳細分類:" + item_name.Text);
Response.Redirect(Request.Url.AbsolutePath + "?msg=C");
}
public void Del_Ohter_Items(int d_num)
{
var prod = _db.activity_kind.AsEnumerable().Where(q => q.root == d_num).ToList();
if (prod.Count > 0)
{
foreach (var row in prod)
{
del_product(row.num);
Del_Ohter_Items(row.num);
}
//查詢結果全部刪除
_db.activity_kind.RemoveRange(prod);
_db.SaveChanges();
}
}
public void del_product(int num)
{
var prod = _db.activities.AsEnumerable().Where(q => q.kind == num).ToList();
if (prod.Count > 0)
{
//清空分類
foreach (var item in prod)
item.kind = null;
_db.SaveChanges();
}
}
#endregion
protected void sortButton_Click(object sender, EventArgs e)
{
Response.Redirect("range.aspx");
}
}