BaseController.cs
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using PT.BLL; using PT.Common; using PT.Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; namespace PT.Web.Mvc.Controllers { public class BaseController<T, V> : Controller where T : class, new() where V : class, new() { public BaseBll<T, V> baseBll = new BaseBll<T, V>(); SystemLogBll systemLogBll = new SystemLogBll(); private SessionHelper sessionHelper; private readonly static IConfiguration Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true) .Build(); private static string conn = Configuration.GetConnectionString("BaseDb"); public virtual IActionResult QueryableConfiguration() { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { ////读取配置文件测试 //string a = Configuration["TestObj"]; //IPList b = new IPList(); //Configuration.Bind("TestJson", b); //List<string> c = new List<string>(); //Configuration.Bind("TestArr", c); //List<IPList> d = new List<IPList>(); //Configuration.Bind("TestList", d); result.data = conn.IndexOf("xx.xx.xx.xx") < 0 ? "此为测试环境" : ""; } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } #region 查询 /// <summary> /// 查询表所有数据 /// </summary> /// <returns></returns> public virtual IActionResult Queryable() { LayuiDataResult<T> dataResult = new LayuiDataResult<T>() { code = 0, msg = "成功", count = 0, data = new List<T>() }; try { List<T> list = baseBll.Queryable(); dataResult.count = list.Count; dataResult.data = list; } catch (Exception ex) { dataResult.code = -1; dataResult.msg = ex.Message; } var res = new JsonResult(dataResult); return res; } /// <summary> /// 查询表未删除数据 /// </summary> /// <returns></returns> public virtual IActionResult QueryableNotDeleted() { LayuiDataResult<T> dataResult = new LayuiDataResult<T>() { code = 0, msg = "成功", count = 0, data = new List<T>() }; try { List<T> list = baseBll.QueryableNotDeleted(); dataResult.count = list.Count; dataResult.data = list; } catch (Exception ex) { dataResult.code = -1; dataResult.msg = ex.Message; } var res = new JsonResult(dataResult); return res; } /// <summary> /// 查询表未删除数据 按条件查询 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult QueryableNotDeletedWhere(T t) { LayuiDataResult<T> dataResult = new LayuiDataResult<T>() { code = 0, msg = "成功", count = 0, data = new List<T>() }; try { List<T> list = baseBll.QueryableNotDeleted(t); dataResult.count = list.Count; dataResult.data = list; } catch (Exception ex) { dataResult.code = -1; dataResult.msg = ex.Message; } var res = new JsonResult(dataResult); return res; } /// <summary> /// 单主键查询 /// </summary> /// <param name="Id"></param> /// <returns></returns> public virtual IActionResult QueryableInSingle(string Id) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { T t = baseBll.QueryableInSingle(Id); result.data = t; } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 查询单条 没有返回Null,如果结果大于1条会抛出错误 (根据业务重写) /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult QueryableSingle(UserList t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { UserList data = baseBll.QueryableSingle(t); result.data = data; } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 是否存在记录 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult IsExist(T t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { bool b = baseBll.IsExist(t); result.data = b; } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } #endregion #region 插入 /// <summary> /// 插入 忽略null列 /// </summary> /// <param name="t"></param> /// <returns>返回自增id</returns> public virtual IActionResult InsertableIgnoreColumns(T t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.InsertableIgnoreColumns(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } #endregion #region 更新 /// <summary> /// 更新 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult Updateable(T t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.Updateable(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 更新 批量 /// </summary> /// <param name="ts"></param> /// <returns></returns> [HttpPost] public virtual IActionResult UpdateableBatch(List<T> ts) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.Updateable(ts); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 更新 忽略null列 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult UpdateableIgnoreColumns(T t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.UpdateableIgnoreColumns(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 更新 忽略null列 批量 /// </summary> /// <param name="ts"></param> /// <returns></returns> [HttpPost] public virtual IActionResult UpdateableIgnoreColumnsBatch(List<T> ts) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.UpdateableIgnoreColumns(ts); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 更新 根据自定义条件 忽略null列 (根据业务重写) /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult UpdateableIgnoreColumnsWhere(T t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.UpdateableIgnoreColumnsWhere(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } #endregion #region 删除 /// <summary> /// 删除 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult Deleteable(T t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.Deleteable(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } /// <summary> /// 逻辑删除 /// </summary> /// <param name="Ids"></param> /// <returns></returns> [HttpPost] public virtual IActionResult DeleteableLogic(List<int> Ids) { string ModifyBy = ""; sessionHelper = new SessionHelper(HttpContext.Session); UserList user = sessionHelper.GetUserInfoString(); ModifyBy = user.Id + "|" + user.UserID + "|" + user.UserName; ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { result.data = baseBll.DeleteableLogic(ModifyBy, Ids); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } #endregion #region 业务查询,根据业务需要重写方法 /// <summary> /// 视图 多表联查 查询表未删除数据 (根据业务重写) /// </summary> /// <returns></returns> public virtual IActionResult QueryableNotDeletedV() { LayuiDataResult<UserListV> dataResult = new LayuiDataResult<UserListV>() { code = 0, msg = "成功", count = 0, data = new List<UserListV>() }; try { List<UserListV> list = baseBll.QueryableNotDeletedV(); dataResult.count = list.Count; dataResult.data = list; } catch (Exception ex) { dataResult.code = -1; dataResult.msg = ex.Message; } var res = new JsonResult(dataResult); return res; } /// <summary> /// 视图 多表联查 查询表未删除数据 按条件查询 (根据业务重写) /// </summary> /// <returns></returns> public virtual IActionResult QueryableNotDeletedVWhere2(IPListV v) { LayuiDataResult<IPListV> dataResult = new LayuiDataResult<IPListV>() { code = 0, msg = "成功", count = 0, data = new List<IPListV>() }; try { List<IPListV> list = baseBll.QueryableNotDeletedV(v); dataResult.count = list.Count; dataResult.data = list; } catch (Exception ex) { dataResult.code = -1; dataResult.msg = ex.Message; } var res = new JsonResult(dataResult); return res; } /// <summary> /// 视图 多表联查 分页查询 查询表未删除数据 按条件查询 (根据业务重写) /// </summary> /// <param name="where"></param> /// <param name="page"></param> /// <param name="limit"></param> /// <returns></returns> public virtual IActionResult QueryablePageList2(IPListV where, int page, int limit) { LayuiDataResult<IPListV> dataResult = new LayuiDataResult<IPListV>() { code = 0, msg = "成功", count = 0, data = new List<IPListV>() }; try { int totalCount = 0; List<IPListV> list = baseBll.QueryablePageList(where, page, limit, out totalCount); dataResult.count = totalCount; dataResult.data = list; } catch (Exception ex) { dataResult.code = -1; dataResult.msg = ex.Message; } var res = new JsonResult(dataResult); return res; } #endregion #region 日志 /// <summary> /// 记录系统操作日志 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual IActionResult AddSystemLog(SystemLog t) { ResponseResult result = new ResponseResult() { code = 0, msg = "成功", data = 0 }; try { sessionHelper = new SessionHelper(HttpContext.Session); UserList userList = sessionHelper.GetUserInfoString(); //获取本机域账号的几种方式 System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent(); string strUserName = currentUser.Name.ToString(); string a = HttpContext.GetServerVariable("LOGON_USER"); string b = HttpContext.GetServerVariable("Remote_Host"); string c = User.Identity.Name; if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = a; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = b; } if (string.IsNullOrWhiteSpace(strUserName) || strUserName.IndexOf("APO") == -1) { strUserName = c; } //获取IP //string localIP = string.Empty; //using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) //{ // socket.Connect("8.8.8.8", 65530); // IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; // localIP = endPoint.Address.ToString(); //} t.OperatorIP = GetClientIPAddr(); t.APO = strUserName.Substring(strUserName.IndexOf('\\') + 1); t.Operator = userList.Id + "|" + userList.UserID + "|" + userList.UserName; t.OperateDate = DateTime.Now; result.data = systemLogBll.InsertableIgnoreColumns(t); } catch (Exception ex) { result.code = -1; result.msg = ex.Message; } var res = new JsonResult(result); return res; } public string GetClientIPAddr() { var remote = this.HttpContext.Connection.RemoteIpAddress; var local = this.HttpContext.Connection.LocalIpAddress; string ipAddr = HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR"); if (string.IsNullOrEmpty(ipAddr)) ipAddr = HttpContext.GetServerVariable("REMOTE_ADDR"); if (string.IsNullOrEmpty(ipAddr)) ipAddr = local.ToString(); return ipAddr; } #endregion } }