天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
15 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using QMFrameWork.Data;
using QMFrameWork.Common.Serialization;
using QMFrameWork.WebUI.Attribute;
using QMAPP.Web.Models.Sys;
using QMAPP.Entity.Sys;
using QMAPP.Common.Web.Util;
using QMAPP.Common.Web.Controllers;
using QMFrameWork.WebUI.DataSource;
using System.Data;
namespace QMAPP.Web.Controllers
{
/// <summary>
/// 角色管理控制器
/// </summary>
[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class RoleController : QController
{
#region 列表载入
/// <summary>
/// 加载角色列表
/// </summary>
/// <returns>结果</returns>
[HandleException]
public ActionResult RoleList()
{
RoleModel seachModel = new RoleModel();
TryGetSelectBuffer<RoleModel>(out seachModel);
seachModel.rownumbers = false;
seachModel.url = "/Role/GetList";
return View("RoleList", seachModel);
}
/// <summary>
/// 获取角色信息列表
/// </summary>
/// <param name="callBack">是否回调</param>
/// <returns>角色信息列表</returns>
[HandleException]
public ActionResult GetList(bool? callBack)
{
List<RoleModel> Roles = new List<RoleModel>();
RoleModel seachModel = null;
DataPage page = new DataPage();
Role condition = null;
try
{
//获取查询对象
seachModel = GetModel<RoleModel>();
#region 获取缓存值
if (callBack != null)
{
TryGetSelectBuffer<RoleModel>(out seachModel);
}
else
{
//保存搜索条件
SetSelectBuffer<RoleModel>(seachModel);
}
#endregion
//获取前台分页控件的分页大小
page = this.GetDataPage(seachModel);
condition = CopyToModel<Role, RoleModel>(seachModel);
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
page = agent.InvokeServiceFunction<DataPage>("RoleManageBLL_GetList", condition, page);
DateGridResult<Role> result = new DateGridResult<Role>();
result.Total = page.RecordCount;
result.Rows = JsonConvertHelper.GetDeserialize<List<Role>>(page.Result.ToString());
return Content(result.GetJsonSource());
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 编辑
#region 编辑载入
/// <summary>
/// 编辑载入
/// </summary>
/// <returns>处理结果</returns>
[HandleException]
public ActionResult RoleEdit()
{
RoleModel model = new RoleModel();
string RoleID = Request.Params["RoleID"];
Role role = new Role();
try
{
if (string.IsNullOrEmpty(RoleID) == false)
{
//获取原数据
role.RoleID = RoleID;
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
role = agent.InvokeServiceFunction<Role>("RoleManageBLL_Get", role);
model = CopyToModel<RoleModel, Role>(role);
model.isEdit = "true";
}
return View(model);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 权限信息树
/// <summary>
/// 权限信息树
/// </summary>
/// <returns>处理结果</returns>
public ActionResult GetTreePowers()
{
List<TreeNodeResult> list = new List<TreeNodeResult>();
List<PowerInfo> powers = null;
PowerUtil pu = new PowerUtil();
pu.OwnPowers = new List<PowerInfo>();
string roleID = Request.Params["roleID"];
string powerFile = null;
string systemID = Request.QueryString["SystemID"];
try
{
//获取系统权限信息
if (systemID == null)
systemID = "Main";
powerFile = MvcApplication.PhysicsRootPath + @"App_Data\" + systemID + "Power.xml";
powers = pu.GetAllPowerInfos(powerFile);
if (string.IsNullOrEmpty(roleID) == false)
{
//获取角色信息
Role role = new Role();
role.RoleID = roleID;
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
role = agent.InvokeServiceFunction<Role>("RoleManageBLL_Get", role);
//role = roleBll.Get(role);
foreach (RoleAuthority power in role.Powers)
{
pu.OwnPowers.Add(new PowerInfo { PowerID = power.AuthorityID });
}
}
foreach (PowerInfo info in powers)
{
TreeNodeResult node = new TreeNodeResult();
node.Tid = info.PowerID;
node.Ttext = info.PowerDes;
//循环已经存在的权限,
//foreach (PowerInfo selected in pu.OwnPowers)
//{
// //如果当前权限在选中范围内,结束循环,否者继续
// if (selected.PowerID == info.PowerID)
// {
// info.Selected = true;
// break;
// }
// else
// {
// info.Selected = false;
// }
//}
info.Selected = pu.OwnPowers.Exists(p => p.PowerID == info.PowerID);
node.TChecked = info.Selected;
//添加子权限
this.BuildChildItems(node, info.ChildPowers, pu, true);
list.Add(node);
}
return Content(TreeNodeResult.GetResultJosnS(list.ToArray()));
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建子权限
/// </summary>
/// <param name="parentNode">父节点</param>
/// <param name="childPowers">子权限</param>
/// <param name="pu">已经选中的权限列表</param>
private void BuildChildItems(TreeNodeResult parentNode, List<PowerInfo> childPowers,PowerUtil pu ,bool option)
{
foreach (PowerInfo info in childPowers)
{
TreeNodeResult node = new TreeNodeResult();
node.Tid = info.PowerID;
node.Ttext = info.PowerDes;
//循环已经存在的权限,
//如果是选择编辑的,那么判断是否选中了。
if (option)
{
//foreach (PowerInfo selected in pu.OwnPowers)
//{
// //如果当前权限在选中范围内,结束循环,否者继续
// if (selected.PowerID == info.PowerID)
// {
// info.Selected = true;
// break;
// }
// else
// {
// info.Selected = false;
// }
//}
info.Selected = pu.OwnPowers.Exists(p => p.PowerID == info.PowerID);
node.TChecked = info.Selected;
}
//如果为空,那么结束循环
if (info.ChildPowers != null)
{
//添加子权限
this.BuildChildItems(node, info.ChildPowers, pu, option);
}
parentNode.AddchildNode(node);
}
}
#endregion
#region 保存
/// <summary>
/// 保存
/// </summary>
/// <param name="model"></param>
/// <returns>处理结果</returns>
[HttpPost]
[HandleException]
public ActionResult RoleSave(RoleModel saveModel)
{
Role role = null;
string powerList = "";
try
{
role = CopyToModel<Role, RoleModel>(saveModel);
//处理所选权限
string[] powers = null;
powerList += string.IsNullOrEmpty(saveModel.SelectedPowers1) == false ? saveModel.SelectedPowers1 : "";
powerList += string.IsNullOrEmpty(saveModel.SelectedPowers2) == false ? ":"+saveModel.SelectedPowers2 : "";
powerList += string.IsNullOrEmpty(saveModel.SelectedPowers3) == false ? ":" + saveModel.SelectedPowers3 : "";
powerList += string.IsNullOrEmpty(saveModel.SelectedPowers4) == false ? ":" + saveModel.SelectedPowers4 : "";
powerList += string.IsNullOrEmpty(saveModel.SelectedPowers5) == false ? ":" + saveModel.SelectedPowers5 : "";
if (string.IsNullOrEmpty(powerList) == false)
{
powers = powerList.Split(":".ToArray());
role.Powers = new List<RoleAuthority>();
foreach (string pw in powers)
{
if(string.IsNullOrEmpty(pw)==true)
continue;
bool isExists=role.Powers.Exists(p=>p.AuthorityID==pw);
if (isExists == true)
continue;
role.Powers.Add(new RoleAuthority {AuthorityID=pw });
}
}
else
{
role.Powers = new List<RoleAuthority>();
}
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
if (string.IsNullOrEmpty(saveModel.isEdit) == true)
{
//新增
role.IsUpdate = false;
}
else
{
//修改
role.IsUpdate = true;
}
bool isExist = agent.InvokeServiceFunction<bool>("RoleManageBLL_ExistsRole", role);
if (isExist)
{
SetMessage("当前保存的角色名称已经存在,请修改!");
return View("RoleEdit", saveModel);
}
if (string.IsNullOrEmpty(saveModel.isEdit) == true)
{
//新增
agent.InvokeServiceFunction<bool>("RoleManageBLL_Insert", role);
}
else
{
//修改
agent.InvokeServiceFunction<bool>("RoleManageBLL_Update", role);
}
return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();"
, AppResource.SaveMessge));
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#endregion
#region 删除
/// <summary>
/// 删除角色信息
/// </summary>
/// <returns>结果</returns>
[HttpPost]
[HandleException]
public ActionResult RoleDelete()
{
string selectKey = Request.Form["selectKey"];
string[] arrayData = selectKey.Split(":".ToCharArray());
ArrayList list = new ArrayList();
try
{
for (int i = 0; i < arrayData.Length; i++)
{
list.Add(arrayData[i]);
}
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
agent.InvokeServiceFunction<int>("RoleManageBLL_BatchDelete", list);
SetMessage(AppResource.DeleteMessage);
return RoleList();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 导出excel
/// <summary>
/// 导出excel
/// </summary>
/// <returns>结果</returns>
[HttpPost]
[HandleException]
public ActionResult ExportExcel()
{
RoleModel seachModel = null;
Role condition = null;
DataTable exportDt = new DataTable();
QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
PowerUtil pu = new PowerUtil();
string selectKey = Request.Form["selectKey"];
try
{
//获取查询对象
seachModel = GetModel<RoleModel>();
condition = CopyToModel<Role, RoleModel>(seachModel);
//获取数据
exportDt = agent.InvokeServiceFunction<DataTable>("RoleManageBLL_GetExportData", condition);
if (selectKey != "")
{
DataView dv = new DataView(exportDt);
string strWhere = "";
string[] list = selectKey.Split(":".ToCharArray());
foreach (string RoleID in list)
{
strWhere += " RoleID='" + RoleID + "' or";
}
if (strWhere != "")
{
strWhere = strWhere.Remove((strWhere.Length - 2), 2);
}
dv.RowFilter = strWhere;
exportDt = dv.ToTable();
}
exportDt.Columns.Add("CREATEDATESHOW");
exportDt.Columns.Add("UPDATEDATESHOW");
foreach (DataRow dr in exportDt.Rows)
{
if (dr["CREATEDATE"] != null)
{
dr["CREATEDATESHOW"] = Convert.ToDateTime(dr["CREATEDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
}
if (dr["UPDATEDATE"] != null)
{
dr["UPDATEDATESHOW"] = Convert.ToDateTime(dr["UPDATEDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
}
}
//导出
QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
return efTool.GetExcelFileResult("RoleList", "角色信息.xls", exportDt);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}