using CZFW.Core; using CZFW.Framework.Interface; using CZFW.Framework.Model.Entity; using CZFW.Framework.Model.ViewModel; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; namespace CZFW.Framework.WebApi { public class ApiControllerBase<TEntity> : AbstractApiController where TEntity : EntityBase, new() { protected ILogicBase<TEntity> _baseLogic; [HttpGet] public virtual TableModel<TEntity> Table(int pageIndex = 1, int pageSize = 20) { var model = new TableModel<TEntity>(); int rowsCount = 0; var data = _baseLogic.GetEntities(pageSize, pageIndex, out rowsCount); model.SetData(data); model.Pager = new PagerModel(rowsCount, pageIndex, pageSize); model.TableHeads = PropertyHelper.Get(typeof(TEntity)); return model; } [HttpGet] public virtual IList<TEntity> GetList(int pageIndex = 1, int pageSize = 20) { var data = _baseLogic.GetEntities(pageSize, pageIndex, out int rowsCount); return data; } /// <summary> /// 编辑获取查看试图 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet] public virtual FormModel<TEntity> View(int id) { var entity = _baseLogic.GetEntity(id); var properties = PropertyHelper.Get(typeof(TEntity)); return new FormModel<TEntity>() { Model = entity, Properties = properties }; } /// <summary> /// 添加获取视图 /// </summary> /// <returns></returns> [HttpGet] public virtual object Add() { var properties = PropertyHelper.Get(typeof(TEntity)); return properties; } /// <summary> /// 编辑保存 /// </summary> /// <param name="form"></param> /// <returns></returns> [HttpPost] public virtual ResultModel Edit([FromBody] TEntity form) { var res = _baseLogic.EditEntity(form); _baseLogic.ExcuteSql($"INSERT INTO operationlog(`Time`,`Type`,`UserId`,`UserName`)VALUES('{Utility.GetNowString()}','修改',{CurrentUser.UserId},'{CurrentUser.UserName}')"); return res; } /// <summary> /// 添加保存 /// </summary> /// <param name="form"></param> /// <returns></returns> [HttpPost] public virtual ResultModel Add([FromBody]TEntity form) { _baseLogic.ExcuteSql($"INSERT INTO operationlog(`Time`,`Type`,`UserId`,`UserName`)VALUES('{Utility.GetNowString()}','新增',{CurrentUser.UserId},'{CurrentUser.UserName}')"); var res = _baseLogic.AddEntity(form); return res; } [HttpPut] public virtual ResultModel AddList([FromBody] IList<TEntity> from) { var res = _baseLogic.AddEntities(from); return res; } [HttpPost] public virtual ResultModel Remove(int id) { _baseLogic.ExcuteSql($"INSERT INTO operationlog(`Time`,`Type`,`UserId`,`UserName`)VALUES('{Utility.GetNowString()}','删除',{CurrentUser.UserId},'{CurrentUser.UserName}')"); var res = _baseLogic.RemoveEntity(id); return res; } } }