using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using FireBirdUtil.SqlUtils;
|
|
using Utils.Security;
|
|
using WeighBusiness.BO;
|
|
using WeighBusiness.Utils;
|
|
using WeighBusiness.Utils.SqlUtils;
|
|
using System.Configuration;
|
|
|
|
namespace WeighBusiness.BL
|
|
{
|
|
public class UserBL//TODO:操作数据库出错时,要报错
|
|
{
|
|
#region BL操作
|
|
public static User Load(string userName)
|
|
{
|
|
return LocalQueryUtil.GetLocalUser(userName);
|
|
}
|
|
|
|
public static User Load(long userID)
|
|
{
|
|
return LocalQueryUtil.GetLocalUser(userID);
|
|
}
|
|
|
|
public static string EncodeUserPassword(string userPassword)
|
|
{
|
|
if (userPassword == null)
|
|
userPassword = string.Empty;
|
|
return userPassword.EncodeUnicodePwd().ToUnicodeString();
|
|
}
|
|
|
|
public static string EncodeSystemUserPassword(string systemUserPassword)
|
|
{
|
|
if (systemUserPassword == null)
|
|
systemUserPassword = string.Empty;
|
|
return SecurityUtil.Change(systemUserPassword, a);
|
|
}
|
|
|
|
public static bool Save(User user, bool 修改用户密码 = true)
|
|
{
|
|
if (user.ID > 0)
|
|
return Update(user, 修改用户密码);
|
|
return Insert(user);
|
|
}
|
|
|
|
public static bool Update(User user, bool 修改用户密码 = true, Func<SqlHelperEx, User, bool> OperateInTransaction = null)
|
|
{
|
|
var oldUser = Load(user.ID);
|
|
IList<string> needUpdateItems = GetNeedUpdateItems(user, oldUser, 修改用户密码);
|
|
|
|
if (needUpdateItems.Count == 0)
|
|
return true;
|
|
|
|
string updateSql = UpdateUtil.GetUpdateString(TableNames.用户表, "ID", user.ID.ToString(), needUpdateItems.ToArray());
|
|
|
|
bool success = true;
|
|
using (var she = new SqlHelperEx()) {
|
|
she.CreateTransaction();
|
|
she.ExecuteNonQuery(updateSql, out success);
|
|
if (!success) {
|
|
she.Rollback();
|
|
return false;
|
|
}
|
|
|
|
if (OperateInTransaction != null) {
|
|
success = OperateInTransaction(she, user);
|
|
}
|
|
if (success)
|
|
she.Commit();
|
|
else
|
|
she.Rollback();
|
|
}
|
|
return success;
|
|
}
|
|
|
|
private static bool Insert(User user)
|
|
{
|
|
//if (user.ID > 0)
|
|
// return Update(user);
|
|
string insertSql = InsertUtil.GetInsertSql(TableNames.用户表,
|
|
new string[] { "ERP_User_Name", "ERP_User_Password", "ERP_User_ID", "IsDomainManager", "UrlPath", "IsAdmin" },
|
|
new string[] { user.ERP_User_Name, user.ERP_User_Password, user.ERP_User_ID.ToString(), user.IsDomainManager.ToString(),user.UrlPath,user.IsAdmin.ToString() });
|
|
|
|
bool success;
|
|
using (var she = new SqlHelperEx()) {
|
|
she.CreateTransaction();
|
|
she.ExecuteNonQuery(insertSql, out success);
|
|
if (!success)
|
|
she.Rollback();
|
|
else
|
|
she.Commit();
|
|
}
|
|
return success;
|
|
}
|
|
|
|
private static IList<string> GetNeedUpdateItems(User user, User oldUser, bool 修改用户密码)
|
|
{
|
|
IList<string> updateItems = new List<string>();
|
|
if (user.ERP_User_Name != oldUser.ERP_User_Name) {
|
|
updateItems.Add("User_Name");
|
|
updateItems.Add(user.ERP_User_Name);
|
|
}
|
|
if (修改用户密码) {
|
|
updateItems.Add("User_Password");
|
|
updateItems.Add(user.ERP_User_Name);
|
|
}
|
|
if (user.UrlPath != oldUser.UrlPath) {
|
|
updateItems.Add("UrlPath");
|
|
updateItems.Add(user.UrlPath);
|
|
}
|
|
if (user.IsAdmin != oldUser.IsAdmin)
|
|
{
|
|
updateItems.Add("IsAdmin");
|
|
updateItems.Add(user.IsAdmin.ToString());
|
|
}
|
|
return updateItems;
|
|
}
|
|
|
|
public static void Delete(long userID)
|
|
{
|
|
var sql = SqlUtil.GetDeleteSql(TableNames.用户表, "where id=" + userID.ToString());
|
|
using (var she = new SqlHelperEx()) {
|
|
bool success;
|
|
she.CreateTransaction();
|
|
she.ExecuteNonQuery(sql, out success);
|
|
if (!success)
|
|
she.Rollback();
|
|
else
|
|
she.Commit();
|
|
}
|
|
}
|
|
|
|
public static bool Exist(string userName)
|
|
{
|
|
var sql = "select count(1) from {0} where ERP_User_Name='{1}'".FormatWith(TableNames.用户表, userName);
|
|
int count = 0;
|
|
using (var she = new SqlHelperEx()) {
|
|
count = she.Query<int>(sql, obj => (int)obj);
|
|
}
|
|
return count > 0;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 登录
|
|
public const string a = "≯╬⒅ξǒЮЯ癶";
|
|
|
|
private static User _CurrentUser;
|
|
public static User CurrentUser { get { return _CurrentUser; } set { _CurrentUser = value; } }
|
|
|
|
public static bool LoginTerminal(string userName, string userPassword)
|
|
{
|
|
if (userPassword == null)
|
|
userPassword = string.Empty;
|
|
var password = userPassword.EncodeUnicodePwd().ToUnicodeString();
|
|
var user = Load(userName);
|
|
if (user == null) {
|
|
throw new LoginError("用户“{0}”不存在".FormatWith(userName));
|
|
}
|
|
//var url = ConfigurationManager.AppSettings["UrlPath"];
|
|
//if (url != user.UrlPath) {
|
|
// throw new LoginError("“{0}”为无效的地址".FormatWith(url));
|
|
//}
|
|
var isCorrect = password == user.ERP_User_Password;
|
|
if (isCorrect)
|
|
{
|
|
user.OffLine = true;
|
|
_CurrentUser = user;
|
|
}
|
|
return isCorrect;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|