using System;
using System.Collections.Generic;
using System.Linq;
using Whir.Domain;
using Whir.ezEIP.Web;
using Whir.Framework;
using Whir.Service;
using Whir.Config;
using Whir.Language;
using System.Text;
using Whir.Security.Service;
using Whir.Security.Domain;


public partial class Whir_System_Handler_Security_Roles : SysHandlerPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var action = RequestUtil.Instance.GetFormString("_action");
        Exec(this, action);
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <returns></returns>
    public HandlerResult Save()
    {
        int roleId = RequestUtil.Instance.GetFormString("RoleId").ToInt(0);
        string roleName = RequestUtil.Instance.GetFormString("RoleName");
        string remarks = RequestUtil.Instance.GetFormString("Remarks");
        int parentId = RequestUtil.Instance.GetFormString("ParentId").ToInt(0);
        if (roleId == 1) //角色为root，parentId强制为0
            parentId = 0;
        if (roleId <= 0)
        {
            #region 添加
            try
            {
                if (Whir.Security.ServiceFactory.RolesService.IsRoleExists(roleName))
                {
                    return new HandlerResult { Status = false, Message = "该角色名称已存在".ToLang() };
                }
                else
                {
                    var model = ModelFactory<Roles>.Insten();
                    model.Remarks = remarks;
                    model.RoleName = roleName;
                    model.ParentId = parentId == 0 ? model.ParentId : parentId;//parentId为0，则不改变原来的parentId
                    if (model.ParentId > 0)
                    {
                        var parent = Whir.Security.ServiceFactory.RolesService.SingleOrDefault<Roles>(model.ParentId);
                        if (parent != null)
                        {
                            model.Depth = parent.Depth + 1;
                        }
                    }
                    else
                    {
                        model.Depth = 0;
                    }
                    Whir.Security.ServiceFactory.RolesService.Save(model);
                    Whir.Service.ServiceFactory.OperateLogService.Save(string.Format("添加角色{0}", roleName));
                    return new HandlerResult { Status = true, Message = "操作成功".ToLang() };
                }
            }
            catch (Exception ex)
            {
                return new HandlerResult { Status = false, Message = "添加失败".ToLang() };
            }
            #endregion
        }
        else
        {
            #region 修改
            try
            {
                if (Whir.Security.ServiceFactory.RolesService.IsRoleExists(roleName, roleId))
                {

                    return new HandlerResult { Status = false, Message = "该角色名称已存在".ToLang() };
                }
                else
                {
                    var model = Whir.Security.ServiceFactory.RolesService.SingleOrDefault<Roles>(roleId);
                    if (model != null)
                    {
                        model.Remarks = remarks;
                        model.RoleName = roleName;
                        model.ParentId = parentId == 0 ? model.ParentId : parentId; //parentId为0，则不改变原来的parentId
                        if (model.ParentId > 0)
                        {
                            var parent = Whir.Security.ServiceFactory.RolesService.SingleOrDefault<Roles>(model.ParentId);
                            if (parent != null)
                            {
                                model.Depth = parent.Depth + 1;
                            }
                        }
                        else
                        {
                            model.Depth = 0;
                        }

                        Whir.Security.ServiceFactory.RolesService.Update(model);
                        ServiceFactory.OperateLogService.Save(string.Format("修改角色名称{0}", roleName));
                        return new HandlerResult { Status = true, Message = "操作成功".ToLang() };

                    }
                    else
                    {
                        return new HandlerResult { Status = false, Message = "找不到记录".ToLang() };
                    }
                }
            }
            catch (Exception ex)
            {
                return new HandlerResult { Status = false, Message = "修改失败".ToLang() };

            }
            #endregion
        }
    }


    /// <summary>
    /// 删除
    /// </summary>
    /// <returns></returns>
    public HandlerResult Delete()
    {
        int roleId = RequestUtil.Instance.GetFormString("RoleId").ToInt(0);
        int result = Whir.Security.ServiceFactory.RolesService.Delete<Roles>(roleId);
        if (result == 1)
        {
            return new HandlerResult { Status = true, Message = "操作成功" };
        }
        else
        {
            return new HandlerResult { Status = false, Message = "删除失败".ToLang() };
        }
    }



}