qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using WIDESEA_Core.Configuration;
using WIDESEA_Core.Extensions;
using WIDESEA_Core.Services;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
 
namespace WIDESEA_Core.Controllers.Basic
{
    public class BaseController<IServiceBase> : Controller
    {
        protected IServiceBase Service;
        private WebResponseContent ResponseContent { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public BaseController()
        {
 
        }
        public BaseController(IServiceBase service)
        {
            Service = service;
        }
        public BaseController(string projectName, string folder, string tablename, IServiceBase service)
        {
            Service = service;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual ActionResult Manager()
        {
            return View();
            //if (System.IO.File.Exists(($"Views\\PageExtension\\{projectName }\\{TableName}Extension.cshtml").MapPath()))
            //{
            //    ViewBag.UrlExtension = $"~/Views/PageExtension/{projectName}/{TableName}Extension.cshtml";
            //}
            //  return View("~/Views/" + projectName + "/" + folder + "/" + TableName + ".cshtml");
        }
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<ActionResult> GetPageData(PageDataOptions loadData)
        {
            string pageData = await Task.FromResult(InvokeService("GetPageData", new object[] { loadData }).Serialize());
            return Content(pageData);
        }
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<ActionResult> GetDetailPage(PageDataOptions loadData)
        {
            string pageData = await Task.FromResult(InvokeService("GetDetailPage", new object[] { loadData }).Serialize());
            return Content(pageData);
        }
 
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileInput"></param>
        /// <returns></returns>
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<ActionResult> Upload(List<IFormFile> fileInput)
        {
            object result = await Task.FromResult(InvokeService("Upload", new object[] { fileInput }));
            return Json(result);
        }
 
 
        /// <summary>
        /// 导入表数据Excel
        /// </summary>
        /// <param name="fileInput"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual async Task<ActionResult> Import(List<IFormFile> fileInput)
        {
            object result = await Task.FromResult(InvokeService("Import", new object[] { fileInput }));
            return Json(result);
        }
 
 
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<ActionResult> Export(PageDataOptions loadData)
        {
            return Json(await Task.FromResult(InvokeService("Export", new object[] { loadData })));
        }
 
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual ActionResult DownLoadFile(string path)
        {
            if (string.IsNullOrEmpty(path)) return Content("未找到文件");
            try
            {
                if (path.IndexOf("/") == -1 && path.IndexOf("\\") == -1)
                {
                    path = path.DecryptDES(AppSetting.Secret.ExportFile);
                }
                else
                {
                    path = path.MapPath();
                }
                string fileName = Path.GetFileName(path);
                return File(System.IO.File.ReadAllBytes(path), System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
            }
            catch (Exception ex)
            {
                Logger.Error($"文件下载出错:{path}{ex.Message}");
            }
            return Content("");
        }
 
        /// <summary>
        /// 下载Excel导入的模板
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = true)]
        public async virtual Task<ActionResult> DownLoadTemplate()
        {
            ResponseContent = await Task.FromResult(InvokeService("DownLoadTemplate", new object[] { })) as WebResponseContent;
            if (!ResponseContent.Status)
            {
                return Json(ResponseContent);
            }
            byte[] fileBytes = System.IO.File.ReadAllBytes(ResponseContent.Data.ToString());
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(ResponseContent.Data.ToString()));
        }
 
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<ActionResult> Del(object[] keys)
        {
            ResponseContent = await Task.FromResult(InvokeService("Del", new object[] { keys, true })) as WebResponseContent;
            Logger.Info(Enums.LoggerType.Del, keys.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message);
            return Json(ResponseContent);
        }
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<ActionResult> Audit(object[] id, int? auditStatus, string auditReason)
        {
            ResponseContent = await Task.FromResult(InvokeService("Audit", new object[] { id, auditStatus, auditReason })) as WebResponseContent;
            Logger.Info(Enums.LoggerType.Del, id?.Serialize() + "," + (auditStatus ?? -1) + "," + auditReason, ResponseContent.Status ? "Ok" : ResponseContent.Message);
            return Json(ResponseContent);
        }
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<WebResponseContent> Add(SaveModel saveModel)
        {
            ResponseContent = await Task.FromResult(InvokeService("Add", new Type[] { typeof(SaveModel) }, new object[] { saveModel })) as WebResponseContent;
            Logger.Info(Enums.LoggerType.Add, saveModel.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message);
            return ResponseContent;
        }
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<WebResponseContent> Update(SaveModel saveModel)
        {
            ResponseContent = await Task.FromResult(InvokeService("Update", new object[] { saveModel })) as WebResponseContent;
            Logger.Info(Enums.LoggerType.Edit, saveModel.Serialize(), ResponseContent.Status ? "Ok" : ResponseContent.Message);
            return ResponseContent;
        }
 
        /// <summary>
        /// 反射调用service方法
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private object InvokeService(string methodName, object[] parameters)
        {
            return Service.GetType().GetMethod(methodName).Invoke(Service, parameters);
        }
        /// <summary>
        /// 反射调用service方法
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="types">为要调用重载的方法参数类型:new Type[] { typeof(SaveDataModel)</param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private object InvokeService(string methodName, Type[] types, object[] parameters)
        {
            return Service.GetType().GetMethod(methodName, types).Invoke(Service, parameters);
        }
    }
}