demo20230512/Service/UserService.cs

202 lines
8.2 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using CScommon;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Threading;
using Weee.DAL;
using Weee.Models;
namespace Weee.Service
{
public class UserService
{
private readonly WeeeDataContext db;
private readonly UserManager<User> manager;
public UserService(WeeeDataContext db, UserManager<User> m)
{
this.db = db;
manager = m;
}
public int getCompanyId(string userId)
{
var qry = db.Users.Where(x => x.Id == userId).FirstOrDefault();
if (qry != null && qry.CompanyID!=null)
return (int)qry.CompanyID;
return 0;
}
public bool isCompanyAdmin(string UserId)
{
var qry = db.Users.Where(x => x.Id == UserId && x.IsCompanyAdmin == true);
bool ret = qry.Any();
return ret;
}
public string GetNameByUserName(string username)
{
string ret = "";
var qry = (from a in db.Users
where a.UserName== username
select a.Name).FirstOrDefault();
if (qry != null)
ret = qry;
return ret;
}
public IEnumerable<User> GetByMyCompany(string UserId)
{
var currentId = UserId;// User.Identity.GetUserId();
var qry = db.Users.Where(x => x.Id == currentId && x.IsCompanyAdmin==true);
if (!qry.Any())
return new List<User>();
var currentCompanyId = qry.Select(x => x.CompanyID).Single();
return db.Users
.Where(x => !x.IsSystemAdmin && !x.IsCompanyAdmin && x.CompanyID == currentCompanyId)
.ToList();
}
public object GetByUid(string uid, string UserId)
{
var currentId = UserId;// User.Identity.GetUserId();
var currentCompanyId = db.Users.Where(x => x.Id == currentId).Select(x => x.CompanyID).Single();
return db.Users
.Where(x => !x.IsSystemAdmin && !x.IsCompanyAdmin && x.CompanyID == currentCompanyId)
.Where(x => x.Id == uid)
.FirstOrDefault();
}
public int Delete(User toBeSave, string UserId)
{
var trans = db.Database.BeginTransaction();
try
{
var qry = (from a in db.Users
where a.Id == toBeSave.Id
select a).FirstOrDefault();
if (qry != null)
{
db.Users.Remove(qry);
var qry2 = (from b in db.SynergerUser
where b.userId == toBeSave.Id
select b).FirstOrDefault();
if (qry2 != null)
db.SynergerUser.Remove(qry2);
db.SaveChanges();
trans.Commit();
return 0;
}
}
catch(Exception ex)
{
trans.Rollback();
}
return -1;
//db.Users.Remove(toBeSave).Where(x.id == toBeSave.Id);
//// 研究使用者如何建立
//var currentId = UserId;// User.Identity.GetUserId();
//var currentCompanyId = db.Users.Where(x => x.Id == currentId).Select(x => x.CompanyID).Single();
//toBeSave.CompanyID = currentCompanyId;
//toBeSave.IsCompanyAdmin = false;
//toBeSave.IsSystemAdmin = false;
//var user = manager.FindById(toBeSave.Id);
//if (user == null)
//{
// var result = manager.Create(toBeSave, toBeSave.DefaultPassword);
// if (!result.Succeeded)
// return -1;
// manager.AddToRole(toBeSave.Id, ProgramConstants.normalcompany);
//}
//else
//{
// user.Name = toBeSave.Name;
// user.Job = toBeSave.Job;
// user.Email = toBeSave.Email;
// user.PhoneNumber = toBeSave.PhoneNumber;
// user.MobileNumber = toBeSave.MobileNumber;
// db.SaveChanges();
//}
//return 0;
}
public int Save(User toBeSave, string UserId)
{
// 研究使用者如何建立
var currentId = UserId;// User.Identity.GetUserId();
var currentCompanyId = db.Users.Where(x => x.Id == currentId).Select(x => x.CompanyID).Single();
toBeSave.CompanyID = currentCompanyId;
toBeSave.IsCompanyAdmin = false;
toBeSave.IsSystemAdmin = false;
var user = manager.FindById(toBeSave.Id);
if (user == null)
{
using (var trans = db.Database.BeginTransaction())
{
try
{
string err = CScommon.FieldCheck.loginID(toBeSave.UserName);
if (err != "")
throw new Exception(err);
var result = manager.Create(toBeSave, toBeSave.DefaultPassword);
var var2 = result.Errors.ToList();
var var3 = var2.Count();
if (result.Errors.Count() > 0)
throw new Exception(var2[0]);
if (!result.Succeeded)
return -1;
var qry = db.Users.Where(x => x.UserName == toBeSave.UserName).FirstOrDefault();
if (qry == null)
throw new Exception($"使用者 {toBeSave.UserName} 建立失敗!");
var qry2 = db.UserAccountType.Where(x => x.UserId == UserId).FirstOrDefault();
if (qry2 == null)
throw new Exception($"目前使用者帳戶型態資料錯誤,請洽管理者!");
var UserAccountType = new UserAccountType();
UserAccountType.AccountType = qry2.AccountType;//.UserAccountType;
UserAccountType.UserId = qry.Id;
UserAccountType.StartTime = qry2.StartTime;
UserAccountType.EndTime = qry2.EndTime;
UserAccountType.Enabled = true;
db.UserAccountType.Add(UserAccountType);
db.SaveChanges();
trans.Commit();
}
catch (DbEntityValidationException dbe)
{
trans.Rollback();
var var1 = dbe.EntityValidationErrors.ToList();
var var2 = var1[0].ValidationErrors.ToList();
var var3 = var2[0].ErrorMessage;
string err = var3;// var1[0].ValidationErrors[0]..ToString();
throw new Exception(err);
}
catch (Exception ex)
{
trans.Rollback();
throw;
}
}
manager.AddToRole(toBeSave.Id, ProgramConstants.normalcompany);
}
else
{
manager.RemovePassword(toBeSave.Id);
var result = manager.AddPassword(toBeSave.Id, toBeSave.DefaultPassword);
//var result = manager.ChangePassword(toBeSave.Id, user.DefaultPassword, toBeSave.DefaultPassword);
if (!result.Succeeded)
{
manager.AddPassword(toBeSave.Id, user.DefaultPassword);
throw new Exception(result.Errors.ToList()[0]);
}
user.Name = toBeSave.Name;
user.Job = toBeSave.Job;
user.Email = toBeSave.Email;
user.PhoneNumber = toBeSave.PhoneNumber;
user.MobileNumber = toBeSave.MobileNumber;
user.DefaultPassword = toBeSave.DefaultPassword;
db.SaveChanges();
}
return 0;
}
}
}