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
{
///
/// 读取机构列表,支持分页
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto> 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>.QuickReturn(default(PageList), ReturnCode.Exception, "读取失败");
}
PageList _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)//有数据
{
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>.QuickReturn(_pglist, ReturnCode.Success, "读取成功");
}
///
/// 删除机构
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto 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.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.QuickReturn(default(int?), ReturnCode.Success, "操作成功,共删除" + rtnInt.ToString() + "条数据!");
else
return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,请重试!");
}
///
/// 增加机构
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto 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.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.QuickReturn(rtnInt, ReturnCode.Success, "操作成功!");
else
return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "增加失败,请重试!");
}
///
/// 读取机构
///
///
///
[RequestMethod(RequestMethods.POST)]
[AllowAnonymous]
public ReturnDto 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.QuickReturn(m, ReturnCode.Success, "读取成功!");
}
else
return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!");
}
catch (Exception ex)
{
LogHelper.Debug(this.ToString(), "GetModel error:" + ex.Message);
return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!");
}
}
}
}