using Gs.Toolbox;
|
using Gs.User.Modes;
|
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Mvc;
|
using System;
|
using System.Collections.Generic;
|
using System.Data.SqlClient;
|
using System.Data;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Gs.User.Service
|
{
|
[ApiGroup(ApiGroupNames.Auth)]
|
public class OrganizationController : IRomteService
|
{
|
/// <summary>
|
/// 读取机构列表,支持分页
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public ReturnDto<PageList<dynamic>> GetListPage([FromBody] PageQuery model)
|
{
|
int currentPage = model.currentPage;
|
int everyPageSize = model.everyPageSize;
|
string sortName = string.IsNullOrEmpty(model.sortName) ? "a.name" : model.sortName;
|
System.Text.StringBuilder sbSql = new StringBuilder();
|
sbSql.Append("select * from ");
|
sbSql.Append("( ");
|
sbSql.Append("select top 100000 ROW_NUMBER() over(order by " + sortName + " " + model.sortOrder + ") as rowIndex,* from sys_Organization a where 1=1" + model.keyWhere);
|
sbSql.Append(") as T ");
|
sbSql.Append(" where T.rowindex>(" + currentPage + "-1)*" + everyPageSize + " and T.rowindex<=" + currentPage + "*" + everyPageSize + "");
|
sbSql.Append(" select count(1) as intTotal from sys_Organization a where 1=1 " + model.keyWhere).ToString();
|
DataSet dset = new DataSet();
|
try
|
{
|
dset = Gs.Toolbox.DbHelperSQL.Query(sbSql.ToString());
|
}
|
catch (Exception ex)
|
{
|
Gs.Toolbox.LogHelper.Debug(this.ToString(), "GetListPage error:" + ex.Message);
|
return ReturnDto<PageList<dynamic>>.QuickReturn(default(PageList<dynamic>), ReturnCode.Exception, "读取失败");
|
}
|
PageList<dynamic> _pglist = new PageList<dynamic>
|
{
|
total = 0,
|
everyPageSize = 0,
|
pages = 0,
|
list = new List<dynamic>()
|
};
|
if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)//有数据
|
{
|
int intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString());
|
int pages = (intTotal % everyPageSize != 0) ? (intTotal / everyPageSize + 1) : (intTotal / everyPageSize);
|
_pglist.total = intTotal;
|
_pglist.everyPageSize = everyPageSize;
|
_pglist.pages = pages;
|
foreach (DataRow dr in dset.Tables[0].Rows)
|
{
|
_pglist.list.Add(
|
new
|
{
|
guid = Guid.Parse(dr["guid"].ToString()),
|
upGuid = dr["upGuid"].ToString(),
|
name = dr["name"].ToString(),
|
conPeople = dr["conPeople"].ToString(),
|
conTel = dr["conTel"].ToString(),
|
status = int.Parse(dr["status"].ToString()),
|
}
|
);
|
}
|
}
|
return ReturnDto<PageList<dynamic>>.QuickReturn(_pglist, ReturnCode.Success, "读取成功");
|
}
|
|
|
/// <summary>
|
/// 删除机构
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public ReturnDto<int?> DeleteModel([FromBody] dynamic model)
|
{
|
int rtnInt = (int)ReturnCode.Default;
|
int cont = 0;
|
try
|
{
|
//是否内置
|
cont = int.Parse(DbHelperSQL.GetSingle("select count(1) from sys_Organization where guid='" + model.guid.ToString() + "' and [isSys]=1").ToString());
|
if (cont > 0)
|
{
|
return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,该条目为系统内置,不可删除!");
|
}
|
rtnInt = DbHelperSQL.ExecuteSql("delete from sys_Organization where guid='" + model.guid.ToString() + "'");
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug(this.ToString(), "DeleteModel error:" + ex.Message);
|
rtnInt = (int)ReturnCode.Exception;
|
}
|
if (rtnInt > 0)
|
return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Success, "操作成功,共删除" + rtnInt.ToString() + "条数据!");
|
else
|
return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,请重试!");
|
}
|
|
/// <summary>
|
/// 增加机构
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public ReturnDto<int?> EditModel([FromBody] dynamic model)
|
{
|
Guid? guid = UtilityHelper.GetGuid(model.guid.ToString());
|
Guid? upGuid = UtilityHelper.GetGuid(model.upGuid.ToString());
|
string name = model.name;
|
string conPeople = model.conPeople;
|
string conTel = model.conTel;
|
int status = model.status;
|
int? rtnInt = (int)ReturnCode.Default;
|
if (upGuid == null)//只能有一个根组织
|
{
|
int cont = 0;
|
cont = int.Parse(DbHelperSQL.GetSingle("select count(1) from sys_Organization where upguid is null").ToString());
|
if (cont > 0)
|
{
|
return ReturnDto<int>.QuickReturn(default(int?), ReturnCode.Exception, "增加失败,请选择上级组织!");
|
}
|
}
|
StringBuilder strSql = new StringBuilder();
|
if (guid != null)
|
{
|
strSql.Append(" update sys_Organization");
|
strSql.Append(" set upGuid=@upGuid,name=@name,conPeople=@conPeople,status=@status,conTel=@conTel");
|
strSql.Append(" where guid='" + guid + "'");
|
}
|
else
|
{
|
guid = Guid.NewGuid();
|
strSql.Append("insert into sys_Organization(");
|
strSql.Append(" guid,upGuid,name,conPeople,status,conTel)");
|
strSql.Append(" values (");
|
strSql.Append("'" + guid + "',@upGuid,@name,@conPeople,@status,@conTel)");
|
}
|
SqlParameter[] parameters = {
|
new SqlParameter("@upGuid", upGuid),
|
new SqlParameter("@name", name),
|
new SqlParameter("@conPeople",conPeople),
|
new SqlParameter("@status",status),
|
new SqlParameter("@conTel",conTel),
|
};
|
try
|
{
|
rtnInt = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug(this.ToString(), "EditModel error:" + ex.Message);
|
rtnInt = (int)ReturnCode.Exception;
|
}
|
if (rtnInt > 0)
|
return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success, "操作成功!");
|
else
|
return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, "增加失败,请重试!");
|
}
|
|
/// <summary>
|
/// 读取机构
|
/// </summary>
|
/// <param name="guid"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
[AllowAnonymous]
|
public ReturnDto<MenuAction> GetModel([FromBody] dynamic model)
|
{
|
dynamic m = new System.Dynamic.ExpandoObject();
|
System.Text.StringBuilder sbSql = new StringBuilder();
|
sbSql.Append("select top 1 * from sys_Organization where 1=1 and guid='" + model.guid.ToString() + "' ");
|
try
|
{
|
DataSet dset = new DataSet();
|
dset = DbHelperSQL.Query(sbSql.ToString());
|
if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)
|
{
|
System.Data.DataRow dr = dset.Tables[0].Rows[0];
|
m.guid = Guid.Parse(dr["guid"].ToString());
|
m.upGuid = dr["upGuid"].ToString();
|
m.name = dr["name"].ToString();
|
m.conPeople = dr["conPeople"].ToString();
|
m.status = int.Parse(dr["status"].ToString());
|
m.conTel = dr["conTel"].ToString();
|
return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "读取成功!");
|
}
|
else
|
return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Default, "读取失败!");
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug(this.ToString(), "GetModel error:" + ex.Message);
|
return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Default, "读取失败!");
|
}
|
}
|
}
|
}
|