恒石成品库WCS
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.

346 lines
13 KiB

using System;
using System.Net;
using System.Net.Http;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using WcfControlMonitorLib;
using System.Data;
using System.Collections.Generic;
using System.Linq;
namespace WcfControlMonitorWebLib.Controllers
{
/// <summary>
///
/// </summary>
[ApiController, Route("api/roles")]
public class RolesController : ControllerBase
{
DataView dw;
/// <summary>
/// 获取角色列表
/// <param name="name"></param>
/// <param name="account"></param>
/// <param name="sort"></param>
/// <param name="offset"></param>
/// <param name="limit"></param>
/// <response code="200">成功</response>
[HttpGet]
public IActionResult GetList(
[FromQuery] string name,
[FromQuery] string account,
[FromQuery] string sort,
[FromQuery] int? offset,
[FromQuery] int? limit)
{
// 根据条件从数据库中查询,VO 如下:
if (offset == null)
{
offset = 0;
}
if (limit == null)
{
limit = 30;
}
// 排序信息需要转一下
if (sort?.Length > 1)
{
var format = sort?.StartsWith("~") == true ? "{0} DESC" : "{0}";
if ((bool)sort?.StartsWith("~"))
{
switch (sort.Substring(2))
{
case "d":
sort = string.Format(format, "ROLE_ID");
break;
case "ame":
sort = string.Format(format, "ROLE_NAME");
break;
case "emark":
sort = string.Format(format, "ROLE_REMARK");
break;
default:
sort = "ROLE_ID";
break;
}
}
else
{
switch (sort.Substring(1))
{
case "d":
sort = string.Format(format, "ROLE_ID");
break;
case "ame":
sort = string.Format(format, "ROLE_NAME");
break;
case "emark":
sort = string.Format(format, "ROLE_REMARK");
break;
default:
sort = "ROLE_ID";
break;
}
}
}
else
{
sort = "ROLE_ID";
}
// 拼接 where 条件
var where = "1=1";
if (!string.IsNullOrEmpty(name))
{
where = $"{where} AND ROLE_NAME LIKE '%{name}%'";
}
// 拼接 sql 语句
var sql = $"SELECT TOP {limit} id,name,remark FROM (SELECT ROLE_ID AS id,ROLE_NAME AS name,ROLE_REMARK AS remark,ROW_NUMBER() OVER (ORDER BY {sort}) AS ROW_NUM FROM T_SYS_ROLE WHERE {where}) AS T WHERE ROW_NUM > {offset}";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
var sqlAll = $"SELECT id,name,remark FROM (SELECT ROLE_ID AS id,ROLE_NAME AS name,ROLE_REMARK AS remark,ROW_NUMBER() OVER (ORDER BY {sort}) AS ROW_NUM FROM T_SYS_ROLE WHERE {where}) AS T WHERE ROW_NUM > 0";
DataView dwAll = CStaticClass.dbo.ExceSQL(sqlAll.ToString()).Tables[0].DefaultView;
var items = new dynamic[dw.Count];
for (int j = 0; j < dw.Count; j++)
{
items[j] = new { id = Convert.ToInt32(dw[j]["id"]), name = Convert.ToString(dw[j]["name"]), remark = Convert.ToString(dw[j]["remark"]) };
}
return Ok(new { total = dwAll.Count, items });
}
/// <summary>
/// 获取当前用户菜单
/// </summary>
[HttpGet("{id}/menu")]
public IActionResult GetMenu(
string session,
[FromRoute] int id) // 这个是用户id
{
// 可以结合用户权限,从数据库中取,格式如下:
//var list = new dynamic[]
//{
// new { id = 1, parent = 0, header = "系统管理", url = "", icon = "settings" },
// new { id = 11, parent = 1, header = "用户管理", url = "/basic/user", icon = "" },
// new { id = 12, parent = 1, header = "角色管理", url = "/basic/role", icon = "" }
//};
//return Ok(new
//{
// menu = new Tree<dynamic>(list, item => item.id, item => item.parent), // 从列表建立一个树结构
// permission = new int[] { 1, 11, 12 } // 拥有的权限,对应 list 的 id
//});
//当前角色拥有的权限
List<int> permissionList = new List<int>();
var sql = $"select distinct t.MENU_ID from (select T_SYS_MENU.MENU_ID as MENU_ID from T_SYS_ROLE inner join T_SYS_ROLE_MENU on T_SYS_ROLE.ROLE_ID=T_SYS_ROLE_MENU.ROLE_ID inner join T_SYS_MENU on T_SYS_ROLE_MENU.MENU_ID=T_SYS_MENU.MENU_ID where T_SYS_MENU.MENU_FLAG='true' and T_SYS_ROLE.ROLE_ID={id}) T";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dw.Count > 0)
{
for (int i = 0; i < dw.Count; i++)
{
permissionList.Add(Convert.ToInt32(dw[i]["MENU_ID"]));
}
}
//菜单列表
sql = $"select MENU_ID,MENU_URL,MENU_PARENT_ID,MENU_ICON,MENU_HEADER,MENU_ORDER from T_SYS_MENU where MENU_FLAG='true'";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
var list = new dynamic[dw.Count];
for (int i = 0; i < dw.Count; i++)
{
list[i] = new { id = Convert.ToInt32(dw[i]["MENU_ID"]), parent = Convert.ToInt32(dw[i]["MENU_PARENT_ID"]), header = Convert.ToString(dw[i]["MENU_HEADER"]), url = Convert.ToString(dw[i]["MENU_URL"]), icon = Convert.ToString(dw[i]["MENU_ICON"]) };
}
return Ok(new
{
menu = new Tree<dynamic>(list, item => item.id, item => item.parent), // 从列表建立一个树结构
permission = permissionList // 拥有的权限,对应 list 的 id
});
}
/// <summary>
/// 设置角色菜单
/// </summary>
[HttpPost]
[Route("{id}/menu")]
public IActionResult SetMenuPermission(
[FromQuery] string session,
[FromRoute] int id,//角色ID
[FromBody] int[] data
)
{
// 写入数据库
int[] MenuId = data;//Menu_id
List<int> isHavePermission = new List<int>();
List<int> newPermissoin = new List<int>();
var sql = $"SELECT MENU_ID from T_SYS_ROLE_MENU where ROLE_ID={id}";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dw.Count < 0)
{
int count = 0;
//没有分配过权限
for (int i = 0; i < MenuId.Length; i++)
{
sql = $"insert into T_SYS_ROLE_MENU (ROLE_ID,MENU_ID) values ('{id}','{MenuId[i]}') ";
if (CStaticClass.dbo.ExecuteSql(sql.ToString())>0)
{
count++;
}
}
if (count > 0)
{
return Ok();
}
else
{
return StatusCode(422);
}
}
else
{
int count = 0;
for (int i = 0; i < dw.Count; i++)
{
isHavePermission.Add(Convert.ToInt32(dw[i]["MENU_ID"]));
}
for (int j = 0; j < data.Length; j++)
{
newPermissoin.Add(data[j]);
}
IEnumerable<int> exceptLsDEL = isHavePermission.Except(newPermissoin);
foreach (int item in exceptLsDEL)
{
//del
sql = $"delete from T_SYS_ROLE_MENU where ROLE_ID={id} and MENU_ID={item}";
if (CStaticClass.dbo.ExecuteSql(sql.ToString()) > 0)
{
count++;
}
}
IEnumerable<int> exceptLsADD = newPermissoin.Except(isHavePermission);
foreach (int item in exceptLsADD)
{
//add
sql = $"insert into T_SYS_ROLE_MENU (ROLE_ID,MENU_ID) values ('{id}','{item}') ";
if (CStaticClass.dbo.ExecuteSql(sql.ToString()) > 0)
{
count++;
}
}
if (count > 0)
{
return Ok();
}
else
{
return StatusCode(422);
}
}
}
/// <summary>
/// 批量处理角色
/// </summary>
[HttpPost]
[Route("batch")]
public IActionResult Batch(
[FromMember] string[] data)//
{
// 写入数据库
int count = 0;
foreach (var item in data)
{
//查一下这个角色是否已分配了用户
var sql = $"select USER_ID from T_SYS_USER_ROLE where ROLE_ID={item}";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dw.Count > 0)
{
return StatusCode(409, "Fail");
}
else
{
sql = $"delete from T_SYS_ROLE where ROLE_ID={item}";
if (CStaticClass.dbo.ExecuteSql(sql.ToString()) > 0)
{
sql = $"delete from T_SYS_ROLE_MENU where ROLE_ID={item}";
if (CStaticClass.dbo.ExecuteSql(sql.ToString()) > 0)
{
count++;
}
count++;
}
}
}
if (count > 0)
{
return Ok();
}
else
{
return StatusCode(422);
}
}
/// <summary>
/// 创建角色
/// </summary>
[HttpPost]
public IActionResult Create(
[FromMember] string name, // 角色名
[FromMember] string remark)//备注
{
// 写入数据库
var sql = $"select ROLE_ID from T_SYS_ROLE where ROLE_NAME='{name}'";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dw.Count > 0)
{
return StatusCode(409,"name");//传出去的statuscode=409 content=name 这个name 在前端拼接成取出实际含义的地址串 然后通过翻译方法 找到对应的汉字解释
}
else
{
sql = $"insert into T_SYS_ROLE (ROLE_NAME,ROLE_REMARK) values ('{name}','{remark}')";
int count=CStaticClass.dbo.ExecuteSql(sql.ToString());
if (count > 0)
{
return Ok();
}
else
{
return StatusCode(422);
}
}
}
/// <summary>
/// 修改角色
/// </summary>
[HttpPut]
[Route("{id}")]
public IActionResult Modify(
[FromMember] string name, // 角色名
[FromMember] string remark,
[FromRoute] int id)
{
// 写入数据库
var sql = $"select ROLE_ID from T_SYS_ROLE where ROLE_ID='{id}'";
dw = CStaticClass.dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dw.Count <= 0)
{
return StatusCode(409, "No");
}
else
{
sql = $"update T_SYS_ROLE set ROLE_NAME='{name}',ROLE_REMARK='{remark}' where ROLE_ID='{id}'";
if (CStaticClass.dbo.ExecuteSql(sql.ToString()) > 0)
{
return Ok();
}
else
{
return StatusCode(422);
}
}
}
}
}