using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using System.Text;
using Gs.Entity.BaseInfo;
using Gs.Entity.Sys;
using Gs.Sys.Models;
using Gs.Toolbox;
using Gs.Toolbox.ApiCore.Abstract.Mvc;
using Gs.Toolbox.ApiCore.Common.Mvc;
using Gs.Toolbox.ApiCore.Group;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SqlSugar;
using static Gs.Toolbox.UtilityHelper;
namespace Gs.Sys.Services;
///
///
[ApiGroup(ApiGroupNames.Sys)]
public class UserController : Repository, IRomteService
{
private readonly IHttpContextAccessor _http;
private readonly string _userCode,_userGuid,_orgFids;
public UserController(IHttpContextAccessor httpContextAccessor)
{
_http = httpContextAccessor;
(_userCode, _userGuid, _orgFids) =
GetUserGuidAndOrgGuid(_http);
}
///
/// 用户登录
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto UserLogin([FromBody] UserLogin model)
{
var accountNo = model.accountNo;
var accountPwd = model.accountPwd;
string accountLog = model.accountLog;
var _strMsg = "";
var dset = new DataSet();
dynamic m = new ExpandoObject();
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
using (var cmd = new SqlCommand("[prc_sys_user_login]", conn))
{
try
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters =
{
new("@outMsg", SqlDbType.NVarChar, 300),
new("@accountNo", accountNo),
new("@accountPwd", MD5Encrypt32(accountPwd)),
new("@accountLog",accountLog),
};
parameters[0].Direction = ParameterDirection.Output;
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
using (var dt = new System.Data.SqlClient.SqlDataAdapter(cmd))
{
dt.Fill(dset, "0");
}
_strMsg = parameters[0].Value.ToString();
if (dset != null && dset.Tables.Count > 0 &&
dset.Tables[0].Rows.Count > 0)
{
var row = dset.Tables[0].Rows[0];
m.loginGuid = Guid.Parse(row["loginGuid"].ToString());
return ReturnDto.QuickReturn(m,
ReturnCode.Success, _strMsg);
}
}
catch (Exception ex)
{
_strMsg = ex.Message;
LogHelper.Debug(ToString(),
"UserLogin error:" + ex.Message);
}
finally
{
conn.Close();
}
}
}
return ReturnDto.QuickReturn(m, ReturnCode.Exception, _strMsg);
}
///
/// 读取用户登录信息
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto GetUserLoginInfo([FromBody] UserLogin model)
{
var userGuid = model.userGuid;
var dset = new DataSet();
dynamic m = new ExpandoObject();
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
using (var cmd = new SqlCommand("[prc_sys_user_info]", conn))
{
try
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters =
{
new("@userGuid", userGuid),
};
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
using (var dt = new System.Data.SqlClient.SqlDataAdapter(cmd))
{
dt.Fill(dset, "0");
}
if (dset != null && dset.Tables.Count > 0 &&
dset.Tables[0].Rows.Count > 0)
{
var row = dset.Tables[0].Rows[0];
m.loginGuid = Guid.Parse(row["loginGuid"].ToString());
m.loginMsg = row["msg"].ToString();
m.list = new List();
if (dset.Tables.Count > 1 &&
dset.Tables[1].Rows.Count > 1)
{
var _dy = dset.Tables[1].TableToDynamicList();
m.list = _dy;
return ReturnDto.QuickReturn(m,
ReturnCode.Success, "读取成功!");
}
}
}
catch (Exception ex)
{
LogHelper.Debug(ToString(),
"GetUserInfo error:" + ex.Message);
}
finally
{
conn.Close();
}
}
}
return ReturnDto.QuickReturn(m, ReturnCode.Exception, "读取成功!");
}
///
/// 修改用户密码
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto SetUserPass([FromBody] dynamic mode)
{
string userGuid = mode.userGuid;
string strPass = mode.newPass;
strPass = MD5Encrypt32(strPass);
dynamic m = new ExpandoObject();
m.outGuid = "";
m.outMsg = "";
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
using (var cmd = new SqlCommand("[prc_sys_user_edit_pass]", conn))
{
try
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters =
{
new("@outGuid", SqlDbType.NVarChar, 100),
new("@outMsg", SqlDbType.NVarChar, 300),
new("@edtUserGuid", _userGuid),
new("@userGuid", userGuid),
new("@newPass", strPass)
};
parameters[0].Direction = ParameterDirection.Output;
parameters[1].Direction = ParameterDirection.Output;
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
m.outOrderGuid = parameters[0].Value.ToString();
m.outMsg = parameters[1].Value.ToString();
}
catch (Exception ex)
{
m.outOrderGuid = "";
m.outMsg = ex.Message;
LogHelper.Debug(ToString(), "SetPass error:" + ex.Message);
return ReturnDto.QuickReturn(m, ReturnCode.Default,
"操作失败!");
}
finally
{
conn.Close();
}
}
}
return ReturnDto.QuickReturn(m, ReturnCode.Success, "操作成功!");
}
///
/// 用户退出
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto UserOut([FromBody] dynamic mode)
{
string userGuid = mode.userGuid;
dynamic m = new ExpandoObject();
m.outGuid = "";
m.outMsg = "";
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
using (var cmd = new SqlCommand("[prc_sys_user_out]", conn))
{
try
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters =
{
new("@outGuid", SqlDbType.NVarChar, 100),
new("@outMsg", SqlDbType.NVarChar, 300),
new("@userGuid", userGuid)
};
parameters[0].Direction = ParameterDirection.Output;
parameters[1].Direction = ParameterDirection.Output;
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
m.outOrderGuid = parameters[0].Value.ToString();
m.outMsg = parameters[1].Value.ToString();
}
catch (Exception ex)
{
m.outOrderGuid = "";
m.outMsg = ex.Message;
LogHelper.Debug(ToString(), "UserOut error:" + ex.Message);
return ReturnDto.QuickReturn(m, ReturnCode.Default,
"操作失败!");
}
finally
{
conn.Close();
}
}
}
return ReturnDto.QuickReturn(m, ReturnCode.Success, "操作成功!");
}
///
/// 修改用户组织
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto SetUserOrg([FromBody] dynamic mode)
{
string edtUserGuid = mode.edtUserGuid;
string userGuid = mode.userGuid;
string userOrgFids = mode.userOrgFids;
dynamic m = new ExpandoObject();
m.outGuid = "";
m.outMsg = "";
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
using (var cmd = new SqlCommand("[prc_sys_user_edit_org]", conn))
{
try
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters =
{
new("@outGuid", SqlDbType.NVarChar, 100),
new("@outMsg", SqlDbType.NVarChar, 300),
new("@edtUserGuid", edtUserGuid),
new("@userGuid", userGuid),
new("@userOrgFids", userOrgFids)
};
parameters[0].Direction = ParameterDirection.Output;
parameters[1].Direction = ParameterDirection.Output;
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
m.outOrderGuid = parameters[0].Value.ToString();
m.outMsg = parameters[1].Value.ToString();
}
catch (Exception ex)
{
m.outOrderGuid = "";
m.outMsg = ex.Message;
LogHelper.Debug(ToString(), "SetUserOrg error:" + ex.Message);
return ReturnDto.QuickReturn(m, ReturnCode.Default,
"操作失败!");
}
finally
{
conn.Close();
}
}
}
return ReturnDto.QuickReturn(m, ReturnCode.Success, "操作成功!");
}
#region
///
/// 查询列表,支持分页
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto> GetListPage(PageQuery query)
{
var pageList = new PageList();
try
{
var _sbWhere = new StringBuilder(" 1=1" + query.keyWhere);
var _sbBy =
new StringBuilder(query.sortName + " " + query.sortOrder);
var totalCount = 0;
var itemsList = Db.Queryable((a,b, org) =>
new object[]
{
JoinType.Left, a.StaffId == b.Id.ToString(),
JoinType.Left,b.FSubsidiary == org.Fid,
}).Select((a, b, org) => new SysUser
{
StaffId =("(" + org.FNumber + ")" + org.Name)+" / "+b.StaffName,
IsStatus = (a.IsStatus == true ? true : false),
FlagOnline = (a.FlagOnline == true ? true : false),
}, true)
.Where(_sbWhere.ToString())
.OrderBy(_sbBy.ToString())
.ToPageList(query.currentPage, query.everyPageSize,
ref totalCount);
pageList = new PageList(itemsList, totalCount,
query.everyPageSize);
return ReturnDto>.QuickReturn(pageList,
ReturnCode.Success, "读取成功");
}
catch (Exception ex)
{
return ReturnDto>.QuickReturn(pageList,
ReturnCode.Default, ex.Message);
}
}
///
/// 删除
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto DeleteModel([FromBody] JArray guidList)
{
var intArray = guidList.ToObject();
var guid = intArray[0];
int? rtnInt = (int)ReturnCode.Default;
var cont = 0;
var it1 = 0;
var it2 = 0;
try
{
cont = Db.Queryable()
.Where(c => c.Guid == Guid.Parse(guid) && c.IsSys == 1).Count();
if (cont > 0)
return ReturnDto.QuickReturn(default(int?),
ReturnCode.Exception, "删除失败,该条目为系统内置,不可删除!");
try
{
Db.Ado.BeginTran();
it1 = Db.Deleteable().In(intArray).ExecuteCommand();
it2 = Db.Deleteable()
.Where(it => it.UserGuid == Guid.Parse(guid))
.ExecuteCommand();
Db.Ado.CommitTran();
}
catch (Exception ex)
{
LogHelper.Debug(ToString(), "DeleteModel error:" + ex.Message);
Db.Ado.RollbackTran();
return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception,
"删除失败,请重试!");
}
}
catch (Exception ex)
{
LogHelper.Debug(ToString(), "DeleteModel error:" + ex.Message);
rtnInt = (int)ReturnCode.Exception;
}
rtnInt = it2 + it1;
return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success,
"操作成功,共删除" + rtnInt + "条数据!");
}
///
/// 增加
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto EditModel([FromBody] SysUser model)
{
var cont = 0;
if (Gs.Toolbox.UtilityHelper.CheckGuid(model.Guid) == false)
{
cont = base.GetList(it => it.Account == model.Account).Count;
if (cont > 0)
return ReturnDto.QuickReturn(default(string?),
ReturnCode.Exception, "增加失败,该账号已存在!");
}
if (Gs.Toolbox.UtilityHelper.CheckGuid(model.Guid) == true)
{
cont = base.GetList(it => it.Guid == model.Guid && it.IsSys == 1)
.Count;
if (cont > 0)
return ReturnDto.QuickReturn(default(string?),
ReturnCode.Exception, "操作失败,内置账号不能被修改!");
}
var _bl = false;
try
{
if (Gs.Toolbox.UtilityHelper.CheckGuid(model.Guid) == false)
{
var _password = model.Password;
var strPass = MD5Encrypt32(_password);
model.Password = strPass;
model.Guid = Guid.NewGuid();
model.CreateBy = _userCode;
model.CreateTime = DateTime.Now;
model.LastupdateBy = _userCode;
model.LastupdateTime = DateTime.Now;
model.FlagOnline = false;
_bl = base.Insert(model);
}
else
{
model.LastupdateBy = _userCode;
model.LastupdateTime = DateTime.Now;
//_bl = base.Update(model);
_bl = Db.Updateable(model).IgnoreColumns(true)
.ExecuteCommand() > 0
? true
: false;
}
}
catch (Exception ex)
{
LogHelper.Debug(ToString(), "EditModel error:" + ex.Message);
return ReturnDto.QuickReturn("", ReturnCode.Exception, ex.Message);
}
if (_bl)
return ReturnDto.QuickReturn(model.Guid.ToString(), ReturnCode.Success, "操作成功!");
return ReturnDto.QuickReturn("", ReturnCode.Exception, "增加失败,请重试!");
}
///
/// 读取
///
///
///
[RequestMethod(RequestMethods.POST)]
[AllowAnonymous]
public ReturnDto GetModel([FromBody] SysUser model)
{
var m = base.GetById(model.Guid);
if (m != null)
return ReturnDto.QuickReturn(m, ReturnCode.Success,
"读取成功!");
return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!");
}
///
/// 读取 ,有绑定
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto GetModel2([FromBody] dynamic model)
{
string guid = model.guid.ToString();
dynamic m = new ExpandoObject();
m.list = new List();
m.list2 = new List();
var dset = new DataSet();
System.Text.StringBuilder sbSql = new StringBuilder();
sbSql.Append(" select * from [dbo].[SYS_USER] where guid='" + guid + "'");
sbSql.Append(" select bd.userGuid, bd.guid,pg.PAGE_VIEW as gnName, bd.fType,bd.createBy,bd.createDate from [MES_SYS_PAGEVIEW] pg right join [dbo].[SYS_USER_BIND] bd on(bd.aboutGuid= pg.guid and bd.fType= 'PDA页面') where bd.fType= 'PDA页面' and bd.userGuid='" + guid + "' ");
sbSql.Append(" select bd.userGuid, bd.guid, pg.depot_name + '(' + pg.depot_code + ')' as gnName, bd.fType,bd.createBy,bd.createDate from [dbo].[MES_DEPOTS] pg right join[dbo].[SYS_USER_BIND] bd on bd.aboutGuid = pg.guid and bd.fType = '仓库' where bd.fType = '仓库' and bd.userGuid='" + guid + "'");
sbSql.Append(" select bd.userGuid, bd.guid,pg.ip as gnName, bd.fType,bd.createBy,bd.createDate from [dbo].[print_info] pg right join [dbo].[SYS_USER_BIND] bd on bd.aboutGuid = pg.guid and bd.fType = '打印机' where bd.fType = '打印机' and bd.userGuid='" + guid + "'");
try
{
dset = DbHelperSQL.Query(sbSql.ToString());
if (dset != null && dset.Tables.Count > 0 &&
dset.Tables[0].Rows.Count > 0)
{
var dr = dset.Tables[0].Rows[0];
m = dr.RowToDynamic();
var _tb = dset.Tables[1].TableToDynamicList();
m.list = _tb;
var _tb2 = dset.Tables[2].TableToDynamicList();
m.list2 = _tb2;
var _tb3 = dset.Tables[3].TableToDynamicList();
m.list3 = _tb3;
}
}
catch (Exception ex)
{
LogHelper.Debug(ToString(), ex.Message);
}
if (m != null)
return ReturnDto.QuickReturn(m, ReturnCode.Success,
"读取成功!");
return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!");
}
///
/// 查询列表,支持分页,用于各种绑定
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto> GetListPage2(PageQuery model)
{
var currentPage = model.currentPage;
var everyPageSize = model.everyPageSize;
var sortName = string.IsNullOrEmpty(model.sortName) ? "a.USER_NAME" : model.sortName;
var keyWhere = model.keyWhere;
string keyType = model.keyType;
System.Text.StringBuilder sbJoin = new StringBuilder();
sbJoin.Append(" FROM SYS_USER [a] ");
sbJoin.Append(" left join MES_STAFF b on a.STAFF_ID=b.id");
sbJoin.Append(" Left JOIN [SYS_ORGANIZATION] [Org] ON ( [b].[FSubsidiary] = [Org].[FID] )");
sbJoin.Append(keyWhere);
var sbSql = new StringBuilder();
sbSql.Append(" SELECT * FROM ");
sbSql.Append(" (SELECT N'(' +[Org].[FNumber] + N')' +[Org].[NAME] AS [FSubsidiary2]");
sbSql.Append(", [a].*,ROW_NUMBER() OVER(ORDER BY a.USER_NAME asc) AS RowIndex ");
//如果无关键字,无需找查绑定
if (string.IsNullOrEmpty(keyType))
{
sbSql.Append(",cast(0 as bit) as chkInt");
}
else
sbSql.Append(",cast( (select count(1) from SYS_USER_BIND b where b.userGuid='" + keyType + "' and b.aboutGuid=a.guid) as bit) as chkInt ");
sbSql.Append(sbJoin);
sbSql.Append(") T");
sbSql.Append(" where T.rowindex>(" + currentPage + "-1)*" + everyPageSize + " and T.rowindex<=" + currentPage + "*" + everyPageSize);
sbSql.Append(" select count(1) as intTotal ");
sbSql.Append(sbJoin);
var dset = new DataSet();
try
{
dset = DbHelperSQL.Query(sbSql.ToString());
}
catch (Exception ex)
{
LogHelper.Debug(ToString(), "GetListPage error:" + ex.Message);
return ReturnDto>.QuickReturn(default(PageList), ReturnCode.Exception, "读取失败");
}
var _pglist = new PageList
{
total = 0,
everyPageSize = 0,
pages = 0,
list = new List()
};
if (dset != null && dset.Tables.Count > 0 &&
dset.Tables[0].Rows.Count > 0) //有数据
{
var intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString());
var pages = intTotal % everyPageSize != 0
? intTotal / everyPageSize + 1
: intTotal / everyPageSize;
_pglist.total = intTotal;
_pglist.everyPageSize = everyPageSize;
_pglist.pages = pages;
var _dy = dset.Tables[0].TableToDynamicList();
_pglist.list = _dy;
}
return ReturnDto>.QuickReturn(_pglist,
ReturnCode.Success, "读取成功");
}
#endregion
}