username@email.com
2024-10-29 18a8fab394f764e5b30c48c8e0d6887ef7d44cbf
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Chuanyin.Attribute;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities.Distribution;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Repository.UnitOfWork;
using CoreCms.Net.Utility.Helper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
 
namespace CoreCms.Net.Web.Admin.Controllers
{
    /// <summary>
    /// 代码生成器
    /// </summary>
    [Description("代码生成器")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class CodeGeneratorController : ControllerBase
    {
        private readonly ICodeGeneratorServices _codeGeneratorServices;
        private readonly IWebHostEnvironment _webHostEnvironment;
 
        private readonly IMapper _mapper;
        
    
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public CodeGeneratorController(ICodeGeneratorServices codeGeneratorServices, IMapper mapper, IWebHostEnvironment webHostEnvironment)
        {
            _codeGeneratorServices = codeGeneratorServices;
            _mapper = mapper;
            _webHostEnvironment = webHostEnvironment;
         
        }
 
 
        /// <summary>
        /// 获取数据表格列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取数据表格列表")]
        public AdminUiCallBack GetTables()
        {
            var tables = _codeGeneratorServices.GetDbTables();
 
            var fristDb = new SqlSugar.DbTableInfo { Name = "AllTable", Description = "所有数据表含视图" };
 
            tables.Insert(0, fristDb);
 
            var jm = new AdminUiCallBack
            {
                code = 0,
                data = _mapper
                    .Map<List<SqlSugar.DbTableInfo>, List<DbTableInfoTree>>(tables)
            };
            return jm;
        }
 
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fileType"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("生成代码")]
        public IActionResult CodeGenDown([FromQuery] string tableName, [FromQuery] string fileType)
        {
            var jm = new AdminUiCallBack();
            if (string.IsNullOrEmpty(tableName))
            {
                jm.msg = "请选择数据库";
                return new JsonResult(jm);
            }
            if (string.IsNullOrEmpty(fileType))
            {
                jm.msg = "请选择要导出的文件类型";
                return new JsonResult(jm);
            }
 
            if (tableName == "AllTable")
            {
                var data = _codeGeneratorServices.CodeGenByAll(fileType);
                if (data != null)
                {
                    return File(data, System.Net.Mime.MediaTypeNames.Application.Zip, tableName + "-" + fileType + ".zip");
                }
                else
                {
                    jm.msg = tableName + "获取数据库字段失败";
                    return new JsonResult(jm);
                }
            }
            else
            {
                var data = _codeGeneratorServices.CodeGen(tableName, fileType);
                if (data != null)
                {
                    return File(data, System.Net.Mime.MediaTypeNames.Application.Zip, tableName + "-" + fileType + ".zip");
                }
                else
                {
                    jm.msg = tableName + "获取数据库字段失败";
                    return new JsonResult(jm);
                }
            }
 
        }
 
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fileType"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("生成代码Vue")]
        public IActionResult CodeGenDownVue([FromQuery] string tableName, [FromQuery] string fileType)
        {
            var jm = new AdminUiCallBack();
            if (string.IsNullOrEmpty(tableName))
            {
                jm.msg = "请选择数据库";
                return new JsonResult(jm);
            }
            if (string.IsNullOrEmpty(fileType))
            {
                jm.msg = "请选择要导出的文件类型";
                return new JsonResult(jm);
            }
 
            if (tableName == "AllTable")
            {
                var data = _codeGeneratorServices.CodeGenByAll(fileType, true);
                if (data != null)
                {
                    return File(data, System.Net.Mime.MediaTypeNames.Application.Zip, tableName + "-" + fileType + ".zip");
                }
                else
                {
                    jm.msg = tableName + "获取数据库字段失败";
                    return new JsonResult(jm);
                }
            }
            else
            {
                var data = _codeGeneratorServices.CodeGen(tableName, fileType, true);
                if (data != null)
                {
                    return File(data, System.Net.Mime.MediaTypeNames.Application.Zip, tableName + "-" + fileType + ".zip");
                }
                else
                {
                    jm.msg = tableName + "获取数据库字段失败";
                    return new JsonResult(jm);
                }
            }
 
        }
 
 
        #region 获取所有表和表名并生成excel
 
        /// <summary>
        /// 获取所有表和表名并生成excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取所有表和表名并生成excel")]
        public AdminUiCallBack GetDataBaseTablesToExcel()
        {
            var jm = new AdminUiCallBack();
 
                //创建Excel文件的对象
                var book = new HSSFWorkbook();
                //添加一个sheet
                var mySheet = book.CreateSheet("Sheet1");
                //获取list数据
                var listmodel = _codeGeneratorServices.GetDbTables();
 
                var headerStyle = ExcelHelper.GetHeaderStyle(book);
                var commonCellStyle = ExcelHelper.GetCommonStyle(book);
 
 
                //给sheet1添加第一行的头部标题
                var headerRow = mySheet.CreateRow(0);
 
                var cell0 = headerRow.CreateCell(0); cell0.SetCellValue("序列"); cell0.CellStyle = headerStyle;
                mySheet.SetColumnWidth(0, 10 * 256);
 
                var cell1 = headerRow.CreateCell(1); cell1.SetCellValue("表名"); cell1.CellStyle = headerStyle;
                mySheet.SetColumnWidth(1, 50 * 256);
 
                var cell2 = headerRow.CreateCell(2); cell2.SetCellValue("描述"); cell2.CellStyle = headerStyle;
                mySheet.SetColumnWidth(2, 50 * 256);
 
                //将数据逐步写入sheet1各个行
                for (var i = 0; i < listmodel.Count; i++)
                {
                    var rowtemp = mySheet.CreateRow(i + 1);
 
                    var rowtemp01 = rowtemp.CreateCell(0);
                    rowtemp01.SetCellValue(i);
                    rowtemp01.CellStyle = commonCellStyle;
 
                    var rowtemp02 = rowtemp.CreateCell(1);
                    rowtemp02.SetCellValue(listmodel[i].Name);
                    rowtemp02.CellStyle = commonCellStyle;
 
                    var rowtemp03 = rowtemp.CreateCell(2);
                    rowtemp03.SetCellValue(listmodel[i].Description);
                    rowtemp03.CellStyle = commonCellStyle;
                }
                // 导出excel
                string webRootPath = _webHostEnvironment.WebRootPath;
                string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-数据库表导出.xls";
                string filePath = webRootPath + tpath;
                DirectoryInfo di = new DirectoryInfo(filePath);
                if (!di.Exists)
                {
                    di.Create();
                }
                FileStream fileHssf = new FileStream(filePath + fileName, FileMode.Create);
                book.Write(fileHssf);
                fileHssf.Close();
 
                jm.code = 0;
                jm.msg = GlobalConstVars.ExcelExportSuccess;
                jm.data = tpath + fileName;
 
            return jm;
        }
 
        #endregion
 
        #region 生成或者更新数据库
        /// <summary>
        ///  创建和更新数据库
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fileType"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("创建新表")]
        public IActionResult CreateAndUpdateDatabase([FromServices] IUnitOfWork work)
        {
            var jm = new AdminUiCallBack();
            try
            {
                Type[]? types = AssemblyHelper.DbCodeFirstModes("CoreCms.Net.Model",thisAttribute: typeof(SqlCodeFirstAttribute));
 
           
                work.GetDbClient().CodeFirst.InitTables(types??new Type[] { });
                jm.code = 0;
              
                jm.msg = "创建成功";
                return new JsonResult(jm);
            }
            catch (Exception e)
            {
                jm.msg = e.Message + ":-----" + e.StackTrace;
                return new JsonResult(jm);
 
            }
        
      
           
        }
        #endregion
    }
}