Blame view

web/WebMvc/Areas/Planned/Controllers/GeneratorController.cs 7.12 KB
1
2
using Hh.Mes.Common.config;
using Hh.Mes.Common.Reflect;
赖素文 authored
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.POJO.WebEntity.tool;
using Hh.Mes.Service.SystemAuth;
using Hh.Mes.Service.WebService.Tool;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace WebMvc.Areas.Planned.Controllers
{
    /// <summary>
    /// 代码生成器
    /// </summary>
    [Area("Planned")]
    public class GeneratorController : BaseController
    {
        private readonly CodeGeneratorService _generatorService;
27
        private readonly IWebHostEnvironment env;
赖素文 authored
28
29
        public GeneratorController(IAuth authUtil, CodeGeneratorService generatorService, IWebHostEnvironment environment) : base(authUtil)
赖素文 authored
30
31
32
33
34
35
36
        {
            _generatorService = generatorService;
            env = environment;
        }

        public IActionResult Index()
        {
唐召明 authored
37
            if (_loginInfo == null) return Redirect("/Login/Index");
38
39
40
41
            if (ConfigRead.GetInstance.GetAppsetConnection().IsDevelopment)
            {
                return Redirect("/htmlTemp/403.html");
            }
赖素文 authored
42
43
44
45
46
47
48
49
50
51
52
            return View();
        }

        /// <summary>
        /// 代码预览
        /// </summary>
        /// <param name="areaName"></param>
        /// <param name="entityName"></param>
        /// <returns></returns>
        public IActionResult CodePreview(string entityName, string areaName = "")
        {
53
            if (string.IsNullOrWhiteSpace(areaName))
赖素文 authored
54
            {
55
56
                areaName = string.Empty;
                ModelState.AddModelError(string.Empty, "请填写区域名称");
赖素文 authored
57
            }
58
59
            var code = _generatorService.GetCode(entityName, areaName, GetAreaSelectItems());
            return View(code);
赖素文 authored
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
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult CodePreview(CodePreviewVM codePreview)
        {
            string areaName = codePreview.AreaName;
            string entityName = codePreview.EntityName;
            if (string.IsNullOrWhiteSpace(areaName))
            {
                areaName = string.Empty;
                ModelState.AddModelError(string.Empty, "区域名称不能为空!");
            }
            var result = _generatorService.GetCode(entityName, areaName, GetAreaSelectItems());
            return View(result);
        }

        /// <summary>
        /// 文件保存路径预览
        /// </summary>
        /// <param name="entityName"></param>
        /// <param name="areaName"></param>
        /// <returns></returns>
        public IActionResult SaveCodeFile(string entityName, string areaName)
        {
            var fileName = _generatorService.ConvertToControllerName(entityName);
            var path = new CodeSavePath
            {
                JavaScriptPath = Path.GetFullPath($"./wwwroot/productjs/{areaName}/{fileName}.js"),
                CshtmlPath = Path.GetFullPath($"./Areas/{areaName}/Views/{fileName}/Index.cshtml"),
                ControllerPath = Path.GetFullPath($"./Areas/{areaName}/Controllers/{fileName}Controller.cs"),
                ModelPath = Path.GetFullPath($"../../sys/Hh.Mes.POJO/WebEntity/{areaName}/{entityName}.cs"),
                ServicePath = Path.GetFullPath($"../../sys/Hh.Mes.Service/WebService/{areaName}/{fileName}Service.cs"),
                EntityName = entityName,
                AreaName = areaName,
            };
            return View(path);
        }

        /// <summary>
        /// 代码文件保存
        /// </summary>
        /// <param name="path">文件保存路径</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult SaveCodeFile(CodeSavePath path)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var code = _generatorService.GetCode(path.EntityName, path.AreaName, GetAreaSelectItems());
                    var result = new Dictionary<string, Response>
                    {
                        { path.JavaScriptPath, _generatorService.SaveFile(path.JavaScriptPath, code.JavaScriptTemp) },
                        { path.ModelPath, _generatorService.SaveFile(path.ModelPath, code.ModelTemp) },
                        { path.ControllerPath, _generatorService.SaveFile(path.ControllerPath, code.ControllerTemp) },
                        { path.CshtmlPath, _generatorService.SaveFile(path.CshtmlPath, code.CshtmlTemp) },
                        { path.ServicePath, _generatorService.SaveFile(path.ServicePath, code.ServiceTemp) },
                    };
                    if (result.All(x => x.Value.Status))
                    {
                        var tableInfo = _generatorService.GetTableDescription(path.EntityName);
                        var module = new SysModule()
                        {
                            Code = path.EntityName,
                            Name = string.IsNullOrWhiteSpace(tableInfo.TableName) ? tableInfo.TableCode : tableInfo.TableName,
唐召明 authored
128
                            Url = $"/{path.AreaName}/{_generatorService.ConvertToControllerName(path.EntityName)}/Index",
赖素文 authored
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
                            IconName = "&#xe663;",
                            SortNo = 10,
                            IsShow = 1,
                        };
                        var addBtnsResult = _generatorService.InsertModelAndButton(module, path.AreaName);
                        result.Add("MenusAndBtns", addBtnsResult);
                    }
                    var json = JsonConvert.SerializeObject(result);
                    return RedirectToAction(nameof(ResultView), new { json });
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }
            return View();
        }

        /// <summary>
        /// 结果预览
        /// </summary>
        /// <returns></returns>
        public IActionResult ResultView(string json)
        {
            var result = new Dictionary<string, Response>();
            ViewBag.ErrorMessage = "";
            try
            {
                result = JsonConvert.DeserializeObject<Dictionary<string, Response>>(json);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
            }
            return View(result);
        }

        /// <summary>
        /// 加载及分页查询
        /// </summary>
        /// <param name="pageRequest">表单请求信息</param>
        /// <param name="entity">实体信息</param>
        /// <returns></returns>
        [HttpPost]
        public string Load(PageReq pageRequest, TableInfo entity)
        {
            return Serialize(_generatorService.Load(pageRequest, entity));
        }

        /// <summary>
        /// 获取所有Area的集合
        /// </summary>
        /// <returns></returns>
        private IEnumerable<string> GetAreaSelectItems()
        {
            return ReflectHelper.GetControlAreas<Controller>().Select(x => x.Key);
        }
    }
}