HTML部分:

@{

ViewData["Title"] = "用户列表";

}

用户列表

MVC部分:

using Microsoft.AspNetCore.Mvc;

using VoltageInvoiceBLL;

using VoltageInvoiceModel;

using VoltageInvoiceUI.Models;

namespace VoltageInvoiceUI.Controllers

{

public class UserController : BaseController

{

#region 页面

public UserController()

{

if (string.IsNullOrWhiteSpace(LOGIN_USER_NAME) || string.IsNullOrWhiteSpace(LOGIN_USER_TYPE) || string.IsNullOrWhiteSpace(LOGIN_USER_ACCOUNT))

{

RedirectToAction("LogIn", "Home");//返回登录页面

}

}

//列表页面

public IActionResult Index()

{

return View();

}

#endregion

#region 用户管理

public IActionResult Edit()

{

return View();

}

///

/// 分页查询用户

///

public IActionResult GetUserPage(string name, string type, int page, int size)

{

PageResponse result = new PageResponse();

UserBLL bll = new UserBLL();

int total = 0;//总记录数

int allPage = 0;//总分页数

var list = bll.GetUserPage(name, type, page, size, ref total);

if (list != null && list.Count > 0)

{

allPage = GetAllPage(total, size);

result.total = total;

result.allPage = allPage;

result.pageIndex = page;

result.pageSize = size;

result.data = list;

}

return Json(result);

}

///

/// 添加用户

///

[HttpPost]

public IActionResult AddUser(string name, string type)

{

RestFulClass rfc = new RestFulClass();

Userinfo user = new Userinfo()

{

Account = Guid.NewGuid().ToString(),

Name = name,

PWD = "123456",

TYPE = type,

IsDeleted = false,

TAX = "",

CreateBy = LOGIN_USER_NAME,

CreateDate = DateTime.Now,

};

try

{

UserBLL bll = new UserBLL();

var b = bll.AddUser(user);

if (b)

rfc.success = true;

else

rfc.success = false;

}

catch (Exception ex)

{

rfc.success = false;

rfc.message = ex.Message;

}

return Json(rfc);

}

///

/// 编辑用户

///

[HttpPost]

public IActionResult EditUser(string account, string name, string type)

{

RestFulClass rfc = new RestFulClass();

Userinfo user = new Userinfo()

{

Account = account,

Name = name,

TYPE = type,

UpdateBy = LOGIN_USER_NAME,

UpdateDate = DateTime.Now,

};

try

{

UserBLL bll = new UserBLL();

var b = bll.EditUser(user);

if (b)

rfc.success = true;

else

rfc.success = false;

}

catch (Exception ex)

{

rfc.success = false;

rfc.message = ex.Message;

}

return Json(rfc);

}

///

/// 删除用户

///

[HttpPost]

public IActionResult DelUser(string account)

{

RestFulClass rfc = new RestFulClass();

try

{

if (!string.IsNullOrWhiteSpace(account))

{

UserBLL bll = new UserBLL();

var b = bll.DelUser(account);

if (b)

rfc.success = true;

else

rfc.success = false;

}

else

{

rfc.success = false;

rfc.message = "缺少需删除的用户id";

}

}

catch (Exception ex)

{

rfc.success = false;

rfc.message = ex.Message;

}

return Json(rfc);

}

#endregion

}

}

SqlSugar部分:

using SqlSugar;

using VoltageInvoiceDAL;

using VoltageInvoiceModel;

namespace VoltageInvoiceBLL

{

public class UserBLL

{

SqlSugarHelper sugar = new SqlSugarHelper();

#region 登录验证

///

/// 登录验证

///

public Userinfo CheckLogin(string name, string pwd)

{

var db = sugar.SqlClient();//数据库连接对象

var user = db.Queryable()

.Where(a => a.Name.Equals(name) && a.PWD.Equals(pwd) && a.IsDeleted == false)

.ToList().FirstOrDefault();//查询

return user;//返回登录成功的用户对象

}

public Userinfo CheckUser(string account, string pwdOld)

{

var db = sugar.SqlClient();//数据库连接对象

var user = db.Queryable()

.Where(a => a.Account.Equals(account) && a.PWD.Equals(pwdOld) && a.IsDeleted == false)

.ToList().FirstOrDefault();//查询

return user;//返回登录成功的用户对象

}

///

/// 修改密码

///

public bool ChangePwd(string account, string pwdNew)

{

var db = sugar.SqlClient();//数据库连接对象

string sql = $"update Userinfo set PWD='{pwdNew}' where Account='{account}'";

int i = db.Ado.ExecuteCommand(sql);

return i > 0;

}

#endregion

#region 编辑用户

///

/// 分页查询用户数据

///

public List GetUserPage(string name, string type, int pageNumber, int pageSize, ref int totalNumber)

{

var db = sugar.SqlClient();//数据库连接对象

var list = db.Queryable()

.Where(a => a.IsDeleted == false)

.WhereIF(!string.IsNullOrWhiteSpace(name), (a => a.Name.Contains(name)))

.WhereIF(!string.IsNullOrWhiteSpace(type), (a => a.TYPE.Equals(type)))

.OrderBy(a => a.CreateDate, OrderByType.Desc)

.ToPageList(pageNumber, pageSize, ref totalNumber);

int total = totalNumber;

return list;

}

///

/// 新增用户

///

public bool AddUser(Userinfo userinfo)

{

var db = sugar.SqlClient();//数据库连接对象

var i = db.Insertable(userinfo).ExecuteCommand();

return i > 0;

}

///

/// 编辑用户

///

public bool EditUser(Userinfo userinfo)

{

var db = sugar.SqlClient();//数据库连接对象

var i = db.Updateable(userinfo)

.IgnoreColumns(a => a.Account)

.IgnoreColumns(a => a.PWD)

.IgnoreColumns(a => a.IsDeleted)

.IgnoreColumns(a => a.TAX)

.IgnoreColumns(a => a.CreateBy)

.IgnoreColumns(a => a.CreateDate)

.Where(a => a.Account == userinfo.Account)

.ExecuteCommand();

return i > 0;

}

///

/// 逻辑删除用户

///

public bool DelUser(string account)

{

var db = sugar.SqlClient();//数据库连接对象

string sql = $"update Userinfo set IsDeleted=1 where Account='{account}'";// IsDeleted=1 逻辑删除

int i = db.Ado.ExecuteCommand(sql);

return i > 0;

}

#endregion

}

}

using SqlSugar;

using VoltageInvoiceTools;

namespace VoltageInvoiceDAL

{

public class SqlSugarHelper

{

public string _connectionString = CustomConfigManager.GetConfig("ConnectionString:DB");//获取SQL连接字符串

public SqlSugarClient _db = null;

///

/// 构造函数(初始化)

/// 调用方法:

/// SqlSugarHelper sugar = new SqlSugarHelper();

/// var db = sugar.SqlClient();

/// var user = db.Queryable().Where(a => a.Name.Equals(uid) && a.PWD.Equals(pwd)).ToList().FirstOrDefault();

///

public SqlSugarClient SqlClient()

{

if (string.IsNullOrEmpty(_connectionString))

throw new ArgumentNullException("数据库连接字符串为空");

_db = new SqlSugarClient(new ConnectionConfig()

{

ConnectionString = _connectionString,//数据库连接字符串,见UI的appsettings.json内配置

DbType = DbType.SqlServer,//数据库类型

IsAutoCloseConnection = true,//自动断开连接

MoreSettings = new ConnMoreSettings()

{

IsWithNoLockQuery = true,//为true表式查询的时候默认会加上.With(SqlWith.NoLock),

IsAutoRemoveDataCache = true//为true自动清除缓存

}

});

//输入最终SQL语句...

_db.Aop.OnLogExecuting = (sql, pars) =>

{

var s1 = sql;//断点打在这里看内部生成的sql语句...

};

return _db;

}

}

}

{

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft.AspNetCore": "Warning"

}

},

"AllowedHosts": "*",

//数据库连接字符串:

"ConnectionString": {

"DB": "Server=.;Database=VoltageInvoiceDB;User ID=sa;Password=123456;"

}

}

界面预览:

本例源码下载: 

http://download.csdn.net/download/djk8888/88595401

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: