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 : AbstractApiController where TEntity : EntityBase, new() { protected ILogicBase _baseLogic; [HttpGet] public virtual TableModel Table(int pageIndex = 1, int pageSize = 20) { var model = new TableModel(); 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 GetList(int pageIndex = 1, int pageSize = 20) { var data = _baseLogic.GetEntities(pageSize, pageIndex, out int rowsCount); return data; } /// /// 编辑获取查看试图 /// /// /// [HttpGet] public virtual FormModel View(int id) { var entity = _baseLogic.GetEntity(id); var properties = PropertyHelper.Get(typeof(TEntity)); return new FormModel() { Model = entity, Properties = properties }; } /// /// 添加获取视图 /// /// [HttpGet] public virtual object Add() { var properties = PropertyHelper.Get(typeof(TEntity)); return properties; } /// /// 编辑保存 /// /// /// [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; } /// /// 添加保存 /// /// /// [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 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; } } }