yz_server/Server/Controllers/AccountManagement/StaffController.cs

260 lines
9.2 KiB
C#
Raw Permalink Normal View History

2022-04-16 07:48:12 +00:00
using Common.Models.UnqTables;
using Server.MyClass.Views;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Server.Controllers.AccountManagement
{
public class StaffController:DefaultController
{
/// <summary>
/// 查询子账号
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult GetStaffs()
{
var Keyword = GetString("Keyword");
var PageIndex = GetInt("PageIndex");
var PageSize = GetInt("PageSize");
if (PageSize > 100) PageSize = 100;
var TotalNumber = 0;
var exp = Expressionable.Create<Staff>();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(a => a.Username.Contains(Keyword) || a.Remark.Contains(Keyword));
}
var DataList = Db.Queryable<Staff>()
.LeftJoin<Role>((a,b)=>a.RoleId == b.Id)
.Where(exp.ToExpression())
.Select((a,b)=>new StaffShow() { Id = a.Id, CreateTime = a.CreateTime,IsCreator = a.IsCreator,Password = a.IsCreator?String.Empty:a.Password ,Remark = a.Remark ,RoleId = b.Id,RoleName = b.Name,Username = a.Username, IsEnable = a.IsEnable})
.ToPageList(PageIndex, PageSize, ref TotalNumber);
return PutData(new PageResult<StaffShow>(DataList, TotalNumber, PageSize, PageIndex));
}
/// <summary>
/// 新增子账号
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult AddStaff()
{
var Username = GetString("Username",true);
var Remark = GetString("Remark");
var Password = GetString("Password",true);
var RoleId = GetInt("RoleId",true);
var Role = Db.Queryable<Role>().Single(f => f.Id == RoleId);
if (Role == null) return PutData("对不起,该权限不存在!");
var Staff = Db.Queryable<Staff>().Where(f=>f.Username == Username).First();
if(Staff!=null) return PutData("对不起,该子账号名称已存在!");
var IsEnable = GetBoolean("IsEnable");
Staff = new Staff()
{
Username = Username,
CreateTime = DateTime.Now,
Password = Password,
Remark = Remark,
RoleId = RoleId,
IsEnable = IsEnable,
IsCreator = false
};
Staff.Id = (int)Db.Insertable<Staff>(Staff).ExecuteReturnBigIdentity();
return PutData(Staff);
}
/// <summary>
/// 启用或禁用员工
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult EnableStaff()
{
var StaffId = GetInt("StaffId",true);
//var Remark = GetString("Remark");
//var Password = GetString("Password");
//var RoleId = GetInt("RoleId");
var IsEnable = GetBoolean("IsEnable",true);
//var Role = Db.Queryable<Role>().Single(f => f.Id == RoleId);
//if (Role == null) return PutData("对不起,该权限不存在!");
var Staff = Db.Queryable<Staff>().Single(f => f.Id == StaffId);
if (Staff == null) return PutData("对不起,该子账号不存在!");
else if (Staff.IsCreator) return PutData("操作失败,管理员账号禁止操作");
//Staff.Remark = Remark;
//Staff.Password = Password;
//Staff.RoleId = RoleId;
Staff.IsEnable = IsEnable;
Db.Updateable(Staff).ExecuteCommand();
return PutSuccess;
}
/// <summary>
/// 编辑子账号
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult UpdStaff()
{
var StaffId = GetInt("StaffId",true);
var Remark = GetString("Remark");
var Password = GetString("Password");
var RoleId = GetInt("RoleId");
var IsEnable = GetBoolean("IsEnable");
var Role = Db.Queryable<Role>().Single(f => f.Id == RoleId);
if (Role == null) return PutData("对不起,该权限不存在!");
var Staff = Db.Queryable<Staff>().Single(f => f.Id == StaffId);
if (Staff == null) return PutData("对不起,该子账号不存在!");
else if (Staff.IsCreator) return PutData("操作失败,管理员账号禁止编辑");
Staff.Remark = Remark;
Staff.Password = String.IsNullOrEmpty(Password)?Staff.Password: Password;
Staff.RoleId = RoleId;
Staff.IsEnable = IsEnable;
Db.Updateable(Staff).ExecuteCommand();
return PutSuccess;
}
/// <summary>
/// 删除子账号
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult DelStaff()
{
var Id = GetInt("StaffId");
var Rst = Db.Deleteable<Staff>().Where(f => f.Id == Id && f.IsCreator == false).ExecuteCommand();
if (Rst > 0) return PutSuccess;
else return PutData("删除失败,未找到数据!");
}
private static List<string> AllRoles = new List<string> { "Anlyze", "Data", "Tools", "Artificial", "Qunfa", "Social", "Account", "Lianmeng", "Robot", "Staff", "Member", "Grouping", "WechatUser", "Fans", "Blacklist", "Reminder", "Func", "Pub", "Base", "Rebate", "Feed", "Keywords", "Order", "TbOrder", "JdOrder", "DyOrder", "MtOrder", "SnOrder", "WphOrder", "PddOrder", "Financial", "CashList", "PayRecord", "IntegralRecord", "Help", "RunLog", "QA", "Guide", "About" };
/// <summary>
/// 查询权限
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult GetRoles()
{
var List = Db.Queryable<Role>().OrderBy(f => f.Id, OrderByType.Desc).ToList();
var superRole = List.FirstOrDefault(f => f.Name == "超级管理员");
if (superRole != null)
{
superRole.ControllerNames = AllRoles;
}
return PutData(List);
}
/// <summary>
/// 根据角色ID获取角色权限列表
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult GetRole()
{
var RoleId = Session.RoleId;
var Role = Db.Queryable<Role>().Single(f => f.Id == RoleId);
if (Role == null) return PutData("对不起,该权限不存在!");
else if(Role.Name=="超级管理员") Role.ControllerNames = AllRoles;
return PutData(Role);
}
/// <summary>
/// 删除权限
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult DelRole()
{
var Id = GetInt("RoleId",true);
var Role = Db.Queryable<Role>().Single(f=>f.Id ==Id);
if (Role != null && Role.Name == "超级管理员") return PutData("对不起,超级管理员权限禁止删除");
var Rst = Db.Deleteable<Role>().RemoveDataCache().Where(f => f.Id == Id).ExecuteCommand();
if (Rst > 0)
{
return PutSuccess;
}
else return PutData("删除失败,未找到数据!");
}
/// <summary>
/// 编辑权限
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult UpdRole()
{
var RoleId = GetInt("RoleId",true);
var Name = GetString("Name",true);
var Roles = GetString("ControllerNames",true);
var Role = Db.Queryable<Role>().Single(f => f.Id == RoleId);
if (Role == null) return PutData("对不起,该权限不存在!");
else if (Role != null && Role.Name == "超级管理员") return PutData("对不起,超级管理员权限禁止修改");
Role.Name = Name;
Role.ControllerNames = Roles.Split(',').ToList();
Db.Updateable(Role).RemoveDataCache().ExecuteCommand();
return PutSuccess;
}
/// <summary>
/// 新增权限
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpPost, ErrorFilter]
public WebResult AddRole()
{
var Name = GetString("Name");
var Roles = GetString("ControllerNames");
var Role = Db.Queryable<Role>().Where(f => f.Name == Name).First();
if (Role != null) return PutData("对不起,该权限已存在!");
Role = new Role()
{
Name = Name,
CreateTime = DateTime.Now,
ControllerNames = Roles.Split(',').ToList()
};
Role = Db.Insertable(Role).RemoveDataCache().ExecuteReturnEntity();
return PutData(Role);
}
}
}