| using Microsoft.AspNetCore.Authorization; | 
| using Microsoft.AspNetCore.Http; | 
| using Microsoft.AspNetCore.Mvc; | 
| using Microsoft.Extensions.Options; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Reflection; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEA_Core.Attributes; | 
| using WIDESEA_Core.BaseServices; | 
|   | 
| namespace WIDESEA_Core.BaseController | 
| { | 
|     [Authorize, ApiController] | 
|     public class ApiBaseController<IService, TEntity> : Controller | 
|     { | 
|         protected IService Service; | 
|   | 
|         public ApiBaseController(IService service) | 
|         { | 
|             Service = service; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 分页获取数据 | 
|         /// </summary> | 
|         /// <param name="options"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("GetPageData")] | 
|         public virtual ActionResult GetPageData([FromBody] PageDataOptions options) | 
|         { | 
|             return Json(InvokeService("GetPageData", new object[] { options })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 分页获取明细数据 | 
|         /// </summary> | 
|         /// <param name="pageData"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("GetDetailPage")] | 
|         public virtual ActionResult GetDetailPage([FromBody] PageDataOptions pageData) | 
|         { | 
|             return Json(InvokeService("GetDetailPage", new object[] { pageData })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 添加数据 | 
|         /// </summary> | 
|         /// <param name="options"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("AddData")] | 
|         public virtual ActionResult AddData([FromBody] TEntity options) | 
|         { | 
|             return Json(InvokeService("AddData", new object[] { options })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 添加数据 | 
|         /// </summary> | 
|         /// <param name="options"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("Add"), MethodParamsValidate] | 
|         public virtual ActionResult Add([FromBody] SaveModel options) | 
|         { | 
|             return Json(InvokeService("AddData", new object[] { options })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 修改数据 | 
|         /// </summary> | 
|         /// <param name="options"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("Update"), MethodParamsValidate] | 
|         public virtual ActionResult Update([FromBody] SaveModel options) | 
|         { | 
|             return Json(InvokeService("UpdateData", new object[] { options })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 修改数据 | 
|         /// </summary> | 
|         /// <param name="options"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("UpdateData")] | 
|         public virtual ActionResult UpdateData([FromBody] TEntity options) | 
|         { | 
|             return Json(InvokeService("UpdateData", new object[] { options })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 删除数据 | 
|         /// </summary> | 
|         /// <param name="key"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("Del")] | 
|         public virtual ActionResult Del([FromBody] object[] key) | 
|         { | 
|             return Json(InvokeService("DeleteData", new object[] { key })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 导出数据 | 
|         /// </summary> | 
|         /// <param name="loadData"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("Export")] | 
|         public virtual ActionResult Export([FromBody] PageDataOptions loadData) | 
|         { | 
|             WebResponseContent result = InvokeService("Export", new object[] { loadData }) as WebResponseContent; | 
|             if (result.Status) | 
|                 return File( | 
|                        System.IO.File.ReadAllBytes(result.Data.ToString()), | 
|                        System.Net.Mime.MediaTypeNames.Application.Octet, | 
|                        Path.GetFileName(result.Data.ToString()) | 
|                    ); | 
|             return Json(result); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 下载导入模板 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         [HttpPost, HttpGet, Route("DownLoadTemplate")] | 
|         public virtual ActionResult DownLoadTemplate() | 
|         { | 
|             WebResponseContent result = InvokeService("DownLoadTemplate", new object[] { }) as WebResponseContent; | 
|             if (result.Status) | 
|                 return File( | 
|                        System.IO.File.ReadAllBytes(result.Data.ToString()), | 
|                        System.Net.Mime.MediaTypeNames.Application.Octet, | 
|                        Path.GetFileName(result.Data.ToString()) | 
|                    ); | 
|             return Json(result); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 导入数据 | 
|         /// </summary> | 
|         /// <param name="fileInput"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("Import")] | 
|         public virtual ActionResult Import(List<IFormFile> fileInput) | 
|         { | 
|             return Json(InvokeService("Import", new object[] { fileInput })); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 导出种子数据 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         [HttpPost, Route("ExportSeedData"), AllowAnonymous] | 
|         public ActionResult ExportSeedData() | 
|         { | 
|             return Json(InvokeService("ExportSeedData", new object[] { })); | 
|         } | 
|   | 
|         private object InvokeService(string methodName, object[] parameters) | 
|         { | 
|             Type t = Service.GetType(); | 
|             List<Type> types = new List<Type>(); | 
|             foreach (var param in parameters) | 
|             { | 
|                 types.Add(param.GetType()); | 
|             } | 
|             MethodInfo method = t.GetMethod(methodName, types.ToArray()); | 
|             return method.Invoke(Service, parameters); | 
|         } | 
|     } | 
| } |