using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using System.Data;
using System.Windows.Forms;
namespace Mesnac.Action.Default.Purview
{
///
/// 把当前选中的权限保存为默认权限
///
public class SaveDefaultPurviewAction : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("把当前选中的权限保存为默认权限");
DbMCControl basRolePurviewControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRolePermission").FirstOrDefault();
if (basRolePurviewControl == null || !basRolePurviewControl.BaseControl.GetType().IsSubclassOf(typeof(TreeView)))
{
ICSharpCode.Core.LoggingService.Warn("保存角色权限 缺少权限树形控件...");
return;
}
TreeView tv = basRolePurviewControl.BaseControl as TreeView;
if (tv != null)
{
DbHelper dbhelper = NewDbHelper(basRolePurviewControl.DesignSource);
if (dbhelper == null)
{
return;
}
save(dbhelper, tv.Nodes);
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_Purview_SavePurview_msg1")); //信息保存完毕!
ShowMsg(msg1);
}
}
private void save(DbHelper dbhelper, TreeNodeCollection tnc)
{
foreach (TreeNode n in tnc)
{
if (n.Checked)
{
insert(dbhelper, n.Tag as APurview);
}
else
{
delete(dbhelper, n.Tag as APurview);
}
if (n.Nodes != null)
{
save(dbhelper, n.Nodes);
}
}
}
private void insert(DbHelper dbhelper, APurview aPurview)
{
if (aPurview == null)
return;
try
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("IF NOT EXISTS(SELECT * FROM BasDefaultPermission WHERE PermissionItemGUID=@PermissionItemGUID AND PermissionType=@PermissionType)");
sb.AppendLine("INSERT INTO BasDefaultPermission(GUID,PermissionItemGUID,PermissionType)");
sb.AppendLine("VALUES(NEWID(),@PermissionItemGUID,@PermissionType)");
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = sb.ToString();
dbhelper.AddParameter("@PermissionItemGUID", aPurview.GUID);
dbhelper.AddParameter("@PermissionType", aPurview.PurviewType);
dbhelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("保存默认权限失败-insert:" + ex.Message, ex);
}
}
private void delete(DbHelper dbhelper, APurview aPurview)
{
if (aPurview == null)
return;
try
{
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = "DELETE FROM BasDefaultPermission WHERE PermissionItemGUID=@PermissionItemGUID AND PermissionType=@PermissionType";
dbhelper.AddParameter("@PermissionItemGUID", aPurview.GUID);
dbhelper.AddParameter("@PermissionType", aPurview.PurviewType);
dbhelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("保存默认权限失败-delete:" + ex.Message, ex);
}
}
}
}