using System;
using System.Collections.Generic;
using System.Text;
using DllUfpEntity;
using DllUfpDal;
using System.Collections;
using Cksoft.Data;
using Cksoft.Data.Repository;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;
using DllUfpEntity.Dto;
using Cksoft.Unity.Log4NetConfig;
using Cksoft.Unity;
using System.Threading.Tasks;
using DllUfpUtil;
namespace DllUfpBll
{
[Route("ufp/api/[controller]/[action]")]
//[Authorize]
[ApiController]
public class FunctionController : ControllerBase
{
///
/// 获取根菜单
///
///
public UfpResponse GetFunctionRoots(int sysId)
{
//int sysId = Convert.ToInt32(Request.Headers["sysid"]);
IDatabase db = null;
try
{
db = DbFactory.Base("ufp");
var dal = new FunctionDal(db);
string errorinfo = string.Empty;
var roots = dal.GetRoots(sysId, errorinfo);
var response = new UfpResponse
{
Code = 1,
data = roots,
Msg = errorinfo
};
if (roots == null || roots.Count() <= 0)
{
response.Code = -1;
}
// db.Commit();
return response;
}
catch (Exception e)
{
// db.Rollback();
return new UfpResponse()
{
Code = -1,
Msg = e.Message
};
}
finally
{
if (db != null)
db.Close();
}
}
///
/// 获取子菜单
///
///
///
///
///
public UfpResponse GetSubFunctions(int parentId)
{
int sysId = Convert.ToInt32(Request.Headers["sysid"]);
IEnumerable subFunctions = null;
using (IDatabase db = DbFactory.Base("ufp"))
{
var dal = new FunctionDal(db);
string errorinfo = string.Empty;
subFunctions = dal.GetSubFunctions(sysId, parentId, errorinfo);
var response = new UfpResponse
{
Code = 1,
data = subFunctions,
Msg = errorinfo
};
if (subFunctions == null || subFunctions.Count() <= 0)
{
response.Code = -1;
}
return response;
}
}
///
/// 根据角色Id获取树菜单
///
///
///
[HttpGet]
public UfpResponse GetFuncTree(int roleId)
{
// int sysId = Convert.ToInt32(Request.Headers["sysid"]);
using (IDatabase db = DbFactory.Base("ufp"))
{
var funcDal = new FunctionDal(db);
int sysId = funcDal.GetSysIdByRoleId(roleId);
return new UfpResponse()
{
Code = 1,
data = funcDal.GetFuncTreesAll(sysId),
Msg = ""
};
}
}
public EapFunction GetFunction(int id)
{
using (IDatabase db = DbFactory.Base("ufp"))
{
var dal = new FunctionDal(db);
return dal.GetFunction(id);
}
}
[HttpPost]
public string Add([FromBody] EapFunction function)
{
var usercode = HttpContext.Request.Headers["userCode"];
int res = 0;
IDatabase db = null;
try
{
db = DbFactory.Base("ufp");
db.BeginTrans();
var dal = new FunctionDal(db);
function.ModCode = usercode;
function.ModTime = DateTime.Now;
if (function.ID == 0)
{
function.RecCode = usercode;
function.RecTime = function.ModTime;
res = dal.Insert(function, usercode);
if (res < 0)
{
db.Rollback();
return "-1";
}
}
else
{
res = dal.Update(function, usercode);
if (res < 0)
{
db.Rollback();
return "-1";
}
}
db.Commit();
if (function.ID == 0)
{
LogHelper.LogFatal("新增Function-->" + Json.ToJson(function), "用户操作", usercode);
}
else
{
LogHelper.LogFatal("修改Function-->" + Json.ToJson(function), "用户操作", usercode);
}
return res.ToString();
}
catch (Exception e)
{
db.Rollback();
return "-1";
}
finally
{
if (db != null)
db.Close();
}
}
[HttpPost]
public string Delete([FromBody] int id)
{
using (IDatabase db = DbFactory.Base("ufp"))
{
var dal = new FunctionDal(db);
var model = dal.GetFunction(id);
if (dal.Delete(id) > 0)
{
LogHelper.LogFatal("删除Function-->:" + Json.ToJson(model), "用户操作", Request.Headers["usercode"]);
return "1";
}
else
{
return "-1";
}
}
}
[HttpPost]
public string SetFunction([FromBody] AuthorizeModel model)
{
string usercode = Request.Headers["usercode"];
IDatabase db = null;
try
{
db = DbFactory.Base("ufp");
db.BeginTrans();
var dal = new FunctionDal(db);
if (dal.DeleteRoleFuncs(model.Multity.Removes, model.Single) < 0)
{
db.Rollback();
return JsonConvert.SerializeObject(new
{
code = -1,
msg = "删除未授权菜单时出错"
});
}
if (dal.SetFunction(model.Multity.Adds, model.Single, usercode) < 0)
{
db.Rollback();
return JsonConvert.SerializeObject(new
{
code = -1,
msg = "新增菜单授权时出错"
});
}
db.Commit();
return JsonConvert.SerializeObject(new
{
code = 1,
msg = ""
});
}
catch (Exception e)
{
db.Rollback();
return JsonConvert.SerializeObject(new
{
code = -1,
msg = e.Message
});
}
finally
{
db.Close();
}
}
[HttpGet]
public IEnumerable GetAlreadyAuthFuncIds(int roleId, int type)
{
using (IDatabase db = DbFactory.Base("ufp"))
{
var dal = new FunctionDal(db);
return dal.GetAuthedFunIds(roleId, type);
}
}
[HttpGet]
public IEnumerable GetFuncIds(string filter)
{
using (IDatabase db = DbFactory.Base("ufp"))
{
var dal = new FunctionDal(db);
return dal.GetFuncIds(filter);
}
}
///
/// 获取首页菜单
///
///
[HttpGet]
public UfpResponse