///////////////////////////////////////////////////////////////////////////////// // 宁波拓通e智造平台 ToTong Next Builder // // https://git.tuotong-tech.com/tnb/tnb-server // ///////////////////////////////////////////////////////////////////////////////// using System.Text; using JNPF.Common.Security; using Mapster; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using SqlSugar; using Tnb.Core; using Tnb.Vengine.DataAccess; using Tnb.Vengine.Domain; namespace Tnb.Vengine.AppService; /// /// 视图模型服务类 /// [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1104, KeepVerb = true)] public class VmodelPageAppService : VengineAppService, IVmodelPageAppService { /// /// 构造函数 /// public VmodelPageAppService(IDataAccess da) : base(da) { } /// /// 从数据表创建模型 /// public async Task CreateByVmodel(VmodelPageCreateFromVmodelInput input) { ThrowIf.IsNull(input.vmid); var vm = await _dataAccess.GetVmodelAsync(input.vmid); ThrowIf.IsNull(vm, $"找不到id={input.vmid}的模型数据"); var page = await _db.Queryable().FirstAsync(a => a.vmid == vm.id); if (page == null) { page = new VmodelPage { vmid = vm.id, code = vm.fullCode, name = vm.vmName }; page.pageSchema = CreatePageSchema(vm, page.id); await _db.Insertable(page).ExecuteCommandAsync(); } else { page.code = vm.fullCode; page.pageSchema = CreatePageSchema(vm, page.id); await _db.Updateable(page).ExecuteCommandAsync(); } return page; } private JObject CreatePageSchema(Vmodel vm, string pageid) { StringBuilder str = new StringBuilder(); str.AppendLine("{"); str.AppendLine($"\"page\": {{ \"loadList\": true, \"watchClient\": false }},"); str.AppendLine($"\"queryData\": {{ }},"); str.AppendLine($"\"queryForm\": {{"); str.AppendLine($"\"show\": false,"); str.AppendLine($"\"attr\": {{ \"labelWidth\": \"106px\", \"hasKeyword\":false }},"); str.AppendLine($"\"cols\": {{"); var pQuery = vm.dbProps.Skip(1).Take(1).FirstOrDefault(); if (pQuery != null) { str.AppendLine($"\"{pQuery.code}\": {{ \"label\": \"{pQuery.name}\", \"span\": 8, \"qtype\": 2, \"isQuick\": true, \"comp\": {{ \"type\": \"el-input\", \"attr\": {{ \"placeholder\": \"{pQuery.name}\", \"clearable\": true, \"maxlength\": 20 }} }} }}"); } str.AppendLine($"}}"); str.AppendLine($"}},"); str.AppendLine($"\"list\": {{"); str.AppendLine($"\"opt\": {{ \"isPage\": true, \"isCheck\": true, \"sortBy\": \"\", \"pkey\": \"{vm.GetPrimary().code}\" }},"); str.AppendLine($"\"attr\": {{ \"border\": false }},"); str.AppendLine($"\"cols\": {{"); foreach (var p in vm.dbProps) { str.AppendLine($"\"{p.code}\":{{ \"label\": \"{p.name}\", \"show\": true, \"attr\": {{ {p.GetDefaultWidth()} }}, \"comp\": {{}} }},"); } str.AppendLine($"}}"); str.AppendLine($"}},"); str.AppendLine($"\"editData\": {vm.GetDefaultDObject().ToJsonString()},"); str.AppendLine($"\"editDlg\": {{ \"isAdd\": true, \"tabHeight\": 300, \"name\": \"{vm.vmName}\" }},"); str.AppendLine($"\"editForm\": {{"); str.AppendLine($"\"attr\": {{ \"labelWidth\": \"106px\" }},"); str.AppendLine($"\"rules\": {{"); foreach (var p in vm.dbProps.Where(a => a.required && !a.pkey)) { str.AppendLine($"\"{p.code}\": [{{ \"required\": true, \"message\": \"必填项不能为空\", \"trigger\": \"blur\" }}],"); } str.AppendLine($"}},"); str.AppendLine($"\"cols\": {{"); foreach (var p in vm.dbProps) { str.AppendLine($"\"{p.code}\": {{ \"label\": \"{p.name}\", \"show\": true, \"comp\": {p.GetDefaultComp().ToJsonString()} }},"); } str.AppendLine($"}}"); str.AppendLine($"}},"); str.AppendLine($"\"tree\": {{ \"key\": \"id\", \"height\": 300, \"props\": {{ \"label\": \"enumName\" }}, \"data\": [] }}"); str.AppendLine($"}}"); var s = str.ToString(); Console.WriteLine(s); return JObject.Parse(s); } }