From e8d8b3dcceb7f7c2a651a48c7bf2f3bf5667e998 Mon Sep 17 00:00:00 2001 From: wenjy Date: Thu, 11 Apr 2024 16:00:53 +0800 Subject: [PATCH] =?UTF-8?q?add=20-=20=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Generate/GenerateCode.cs | 113 ++++++++++++ SlnMesnac.Generate/SlnMesnac.Generate.csproj | 16 ++ .../Templates/Service/IServiceCreate.cs | 78 +++++++++ .../Templates/Service/IServices.vm | 13 ++ .../Templates/Service/Impl/ServiceCreate.cs | 75 ++++++++ .../Templates/Service/Impl/Services.vm | 14 ++ SlnMesnac.Ioc/DependencyConfigurator.cs | 3 + SlnMesnac.Model/domain/EXECUTE_PLANINFO.cs | 146 ++++++++++++++++ .../service/IEXECUTE_PLANINFOServices.cs | 13 ++ .../service/Impl/EXECUTE_PLANINFOServices.cs | 14 ++ .../Converter/Generate/RowToIndexConverter.cs | 53 ++++++ SlnMesnac.WPF/MainWindow.xaml | 1 + .../Page/Generate/GenerateControl.xaml | 56 ++++++ .../Page/Generate/GenerateControl.xaml.cs | 30 ++++ SlnMesnac.WPF/SlnMesnac.WPF.csproj | 8 +- .../Generate/GenerateControlViewModel.cs | 163 ++++++++++++++++++ .../ViewModel/MainWindowViewModel.cs | 20 +-- SlnMesnac.WPF/appsettings.json | 4 +- SlnMesnac.sln | 8 +- 19 files changed, 808 insertions(+), 20 deletions(-) create mode 100644 SlnMesnac.Generate/GenerateCode.cs create mode 100644 SlnMesnac.Generate/SlnMesnac.Generate.csproj create mode 100644 SlnMesnac.Generate/Templates/Service/IServiceCreate.cs create mode 100644 SlnMesnac.Generate/Templates/Service/IServices.vm create mode 100644 SlnMesnac.Generate/Templates/Service/Impl/ServiceCreate.cs create mode 100644 SlnMesnac.Generate/Templates/Service/Impl/Services.vm create mode 100644 SlnMesnac.Model/domain/EXECUTE_PLANINFO.cs create mode 100644 SlnMesnac.Repository/service/IEXECUTE_PLANINFOServices.cs create mode 100644 SlnMesnac.Repository/service/Impl/EXECUTE_PLANINFOServices.cs create mode 100644 SlnMesnac.WPF/Converter/Generate/RowToIndexConverter.cs create mode 100644 SlnMesnac.WPF/Page/Generate/GenerateControl.xaml create mode 100644 SlnMesnac.WPF/Page/Generate/GenerateControl.xaml.cs create mode 100644 SlnMesnac.WPF/ViewModel/Generate/GenerateControlViewModel.cs diff --git a/SlnMesnac.Generate/GenerateCode.cs b/SlnMesnac.Generate/GenerateCode.cs new file mode 100644 index 0000000..812b0f9 --- /dev/null +++ b/SlnMesnac.Generate/GenerateCode.cs @@ -0,0 +1,113 @@ +using SlnMesnac.Generate.Templates.Service; +using SlnMesnac.Generate.Templates.Service.Impl; +using SqlSugar; +using System; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Generate +* 唯一标识:78595105-fab6-40f0-97b4-1272dc3e0e86 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-11 13:35:01 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Generate +{ + /// + /// 生成代码 + /// + public class GenerateCode + { + private readonly ISqlSugarClient _sqlSugarClient; + + public GenerateCode(ISqlSugarClient sqlSugarClient) + { + _sqlSugarClient = sqlSugarClient; + } + + public bool CreateCode(string configId,string tableName,string savePath,string nameSpace) + { + if (string.IsNullOrEmpty(configId)) + { + throw new ArgumentNullException($"代码生成异常:configId参数为空"); + } + + if (string.IsNullOrEmpty(tableName)) + { + throw new ArgumentNullException($"代码生成异常:表格名称参数为空"); + } + + if (string.IsNullOrEmpty(savePath)) + { + throw new ArgumentNullException($"代码生成异常:文件存储路径参数为空"); + } + + if (string.IsNullOrEmpty(nameSpace)) + { + throw new ArgumentNullException($"代码生成异常:命名空间参数为空"); + } + + try + { + savePath += $"\\{tableName}"; + + var scope = _sqlSugarClient.AsTenant().GetConnectionScope(configId); + + scope.DbFirst.IsCreateAttribute() + .FormatPropertyName(it=> ToCamelCase(it)).Where(p => p == tableName).CreateClassFile($"{savePath}\\Entity", nameSpace); + + var isc = new IServiceCreate(); + bool iscRes = isc.Create(tableName, nameSpace, savePath); + if (!iscRes) + { + throw new InvalidOperationException($"Service接口生成失败"); + } + + var sc = new ServiceCreate(); + var scRes = sc.Create(tableName, nameSpace, savePath); + + if (!scRes) + { + throw new InvalidOperationException($"Service实现类生成失败"); + } + + return true; + }catch (Exception ex) + { + throw new InvalidOperationException($"代码生成异常:{ex.Message}"); + } + } + + private static string ToCamelCase(string input) + { + // 将字符串转换为驼峰格式,但保持每个单词的首字母大写 + string[] words = input.Split('_'); + for (int i = 0; i < words.Length; i++) + { + if (i > 0) + { + words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1).ToLower(); + } + else + { + words[i] = words[i].ToLower(); + } + } + return string.Join("", words); + } + } +} diff --git a/SlnMesnac.Generate/SlnMesnac.Generate.csproj b/SlnMesnac.Generate/SlnMesnac.Generate.csproj new file mode 100644 index 0000000..7e094ef --- /dev/null +++ b/SlnMesnac.Generate/SlnMesnac.Generate.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.1 + enable + + + + + + + + + + + diff --git a/SlnMesnac.Generate/Templates/Service/IServiceCreate.cs b/SlnMesnac.Generate/Templates/Service/IServiceCreate.cs new file mode 100644 index 0000000..272c376 --- /dev/null +++ b/SlnMesnac.Generate/Templates/Service/IServiceCreate.cs @@ -0,0 +1,78 @@ +using Commons.Collections; +using NVelocity.App; +using NVelocity.Runtime; +using NVelocity; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Generate.Templates +* 唯一标识:4dbafd45-d689-4d1a-b54d-b936dae7d17c +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-11 13:28:04 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Generate.Templates.Service +{ + public class IServiceCreate + { + private static readonly string templateDir = @"E:\桌面\SlnMesnac\SlnMesnac.Generate\Templates\Service\"; + + public bool Create(string tableName, string NameSpace, string outdir) + { + + try + { + VelocityEngine velocityEngine = new VelocityEngine(); + ExtendedProperties props = new ExtendedProperties(); + props.AddProperty(RuntimeConstants.RESOURCE_LOADER, @"file"); + props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir); + props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); + props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); + //模板的缓存设置 + props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存 + props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒) + velocityEngine.Init(props); + //为模板变量赋值 + VelocityContext context = new VelocityContext(); + context.Put("tableName", tableName); + context.Put("NameSpace", NameSpace); + context.Put("outdir", outdir); + //从文件中读取模板 + Template template = velocityEngine.GetTemplate(@"\IServices.vm"); + if (!Directory.Exists(outdir + "\\IServices")) + { + Directory.CreateDirectory(outdir + "\\IServices"); + } + //合并模板 + using (StreamWriter writer = new StreamWriter(outdir + $"\\IServices\\I{tableName.Substring(0, 1).ToUpper()}{tableName.Substring(1)}Service.cs", false)) + { + template.Merge(context, writer); + } + return true; + }catch(Exception ex) + { + throw new InvalidOperationException($"Service接口模板创建异常:{ex.Message}"); + } + } + + + } +} diff --git a/SlnMesnac.Generate/Templates/Service/IServices.vm b/SlnMesnac.Generate/Templates/Service/IServices.vm new file mode 100644 index 0000000..a5b8992 --- /dev/null +++ b/SlnMesnac.Generate/Templates/Service/IServices.vm @@ -0,0 +1,13 @@ +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ${NameSpace}.service +{ + public interface I${tableName}Services: IBaseService<${tableName}> + { + + } +} \ No newline at end of file diff --git a/SlnMesnac.Generate/Templates/Service/Impl/ServiceCreate.cs b/SlnMesnac.Generate/Templates/Service/Impl/ServiceCreate.cs new file mode 100644 index 0000000..14830ea --- /dev/null +++ b/SlnMesnac.Generate/Templates/Service/Impl/ServiceCreate.cs @@ -0,0 +1,75 @@ +using Commons.Collections; +using NVelocity.App; +using NVelocity.Runtime; +using NVelocity; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Generate.Templates +* 唯一标识:4acc596a-6223-4156-b16c-952c225eff25 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-11 13:27:33 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Generate.Templates.Service.Impl +{ + public class ServiceCreate + { + private static readonly string templateDir = @"E:\桌面\SlnMesnac\SlnMesnac.Generate\Templates\Service\Impl\"; + + public bool Create(string tableName, string NameSpace, string outdir) + { + try + { + VelocityEngine velocityEngine = new VelocityEngine(); + ExtendedProperties props = new ExtendedProperties(); + props.AddProperty(RuntimeConstants.RESOURCE_LOADER, @"file"); + props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir); + props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); + props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); + //模板的缓存设置 + props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存 + props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒) + velocityEngine.Init(props); + //为模板变量赋值 + VelocityContext context = new VelocityContext(); + context.Put("tableName", tableName); + context.Put("NameSpace", NameSpace); + context.Put("outdir", outdir); + //从文件中读取模板 + Template template = velocityEngine.GetTemplate(@"\Services.vm"); + if (!Directory.Exists(outdir + "\\Services")) + { + Directory.CreateDirectory(outdir + "\\Services"); + } + //合并模板 + using (StreamWriter writer = new StreamWriter(outdir + $"\\Services\\{tableName.Substring(0, 1).ToUpper()}{tableName.Substring(1)}ServiceImpl.cs", false)) + { + template.Merge(context, writer); + } + return true; + }catch(Exception ex) + { + throw new InvalidOperationException($"Service实现类模板创建异常:{ex.Message}"); + } + } + } +} diff --git a/SlnMesnac.Generate/Templates/Service/Impl/Services.vm b/SlnMesnac.Generate/Templates/Service/Impl/Services.vm new file mode 100644 index 0000000..bcf9a49 --- /dev/null +++ b/SlnMesnac.Generate/Templates/Service/Impl/Services.vm @@ -0,0 +1,14 @@ +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; +using System; +using System.Collections.Generic; + +namespace ${NameSpace}.service.Impl +{ + public class ${tableName}ServiceImpl : BaseServiceImpl<${tableName}>, I${tableName}Service + { + public ${tableName}ServiceImpl(Repository<${tableName}> repository):base(repository) + { + } + } +} \ No newline at end of file diff --git a/SlnMesnac.Ioc/DependencyConfigurator.cs b/SlnMesnac.Ioc/DependencyConfigurator.cs index a95ae35..77f1be8 100644 --- a/SlnMesnac.Ioc/DependencyConfigurator.cs +++ b/SlnMesnac.Ioc/DependencyConfigurator.cs @@ -60,6 +60,9 @@ namespace SlnMesnac.Ioc //注入业务类 RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Business.dll")); + + //注入代码生成 + RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Generate.dll")); } diff --git a/SlnMesnac.Model/domain/EXECUTE_PLANINFO.cs b/SlnMesnac.Model/domain/EXECUTE_PLANINFO.cs new file mode 100644 index 0000000..3a6e1fa --- /dev/null +++ b/SlnMesnac.Model/domain/EXECUTE_PLANINFO.cs @@ -0,0 +1,146 @@ +using System; +using System.Linq; +using System.Text; +using SqlSugar; + +namespace SlnMesnac.Repository +{ + /// + ///执行计划 + /// + [SugarTable("EXECUTE_PLANINFO"), TenantAttribute("mcs")] + public partial class EXECUTE_PLANINFO + { + public EXECUTE_PLANINFO(){ + + + } + /// + /// Desc:主键标识 + /// Default: + /// Nullable:False + /// + [SugarColumn(IsPrimaryKey=true)] + public decimal OBJ_ID {get;set;} + + /// + /// Desc:执行计划编号 + /// Default: + /// Nullable:True + /// + public string EXECUTE_PLANCODE {get;set;} + + /// + /// Desc:生产计划编号 + /// Default: + /// Nullable:True + /// + public string PRODUCT_PLANCODE {get;set;} + + /// + /// Desc:工单编号 + /// Default: + /// Nullable:True + /// + public string ORDER_CODE {get;set;} + + /// + /// Desc:物料编号 + /// Default: + /// Nullable:True + /// + public string MATERIAL_CODE {get;set;} + + /// + /// Desc:物料名称 + /// Default: + /// Nullable:True + /// + public string MATERIAL_NAME {get;set;} + + /// + /// Desc:计划工位 + /// Default: + /// Nullable:True + /// + public string PRODUCTLINE_CODE {get;set;} + + /// + /// Desc:执行顺序 + /// Default: + /// Nullable:True + /// + public decimal? EXECUTE_ORDER {get;set;} + + /// + /// Desc:执行方式:1-手动;2-自动 + /// Default: + /// Nullable:True + /// + public decimal? EXECUTE_METHOD {get;set;} + + /// + /// Desc:执行状态:1-待执行;2-执行中;3-完成 + /// Default: + /// Nullable:True + /// + public decimal? EXECUTE_STATUS {get;set;} + + /// + /// Desc:计划数量 + /// Default: + /// Nullable:True + /// + public decimal? PLAN_AMOUNT {get;set;} + + /// + /// Desc:完成数量 + /// Default: + /// Nullable:True + /// + public decimal? COMPLETE_AMOUNT {get;set;} + + /// + /// Desc:开始时间 + /// Default: + /// Nullable:True + /// + public DateTime? BEGIN_TIME {get;set;} + + /// + /// Desc:完成时间 + /// Default: + /// Nullable:True + /// + public DateTime? END_TIME {get;set;} + + /// + /// Desc:是否标识 + /// Default: + /// Nullable:True + /// + public decimal? IS_FLAG {get;set;} + + /// + /// Desc:创建时间 + /// Default: + /// Nullable:True + /// + public DateTime? CREATED_TIME {get;set;} + + /// + /// Desc:计划类型(1=联合下发;2=前板下发;3=后板下发) + /// Default: + /// Nullable:True + /// + public decimal? PLAN_TYPE {get;set;} + + /// + /// Desc:计划任务执行编号 + /// Default: + /// Nullable:True + /// + public string TASK_CODE {get;set;} + + } +} diff --git a/SlnMesnac.Repository/service/IEXECUTE_PLANINFOServices.cs b/SlnMesnac.Repository/service/IEXECUTE_PLANINFOServices.cs new file mode 100644 index 0000000..2999788 --- /dev/null +++ b/SlnMesnac.Repository/service/IEXECUTE_PLANINFOServices.cs @@ -0,0 +1,13 @@ +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; +using System; +using System.Collections.Generic; +using System.Text; + +namespace SlnMesnac.Repository.service +{ + public interface IEXECUTE_PLANINFOServices: IBaseService + { + + } +} \ No newline at end of file diff --git a/SlnMesnac.Repository/service/Impl/EXECUTE_PLANINFOServices.cs b/SlnMesnac.Repository/service/Impl/EXECUTE_PLANINFOServices.cs new file mode 100644 index 0000000..2a94ad4 --- /dev/null +++ b/SlnMesnac.Repository/service/Impl/EXECUTE_PLANINFOServices.cs @@ -0,0 +1,14 @@ +using SlnMesnac.Model.domain; +using SlnMesnac.Repository.service.@base; +using System; +using System.Collections.Generic; + +namespace SlnMesnac.Repository.service.Impl +{ + public class EXECUTE_PLANINFOServiceImpl : BaseServiceImpl, IEXECUTE_PLANINFOServices + { + public EXECUTE_PLANINFOServiceImpl(Repository repository):base(repository) + { + } + } +} \ No newline at end of file diff --git a/SlnMesnac.WPF/Converter/Generate/RowToIndexConverter.cs b/SlnMesnac.WPF/Converter/Generate/RowToIndexConverter.cs new file mode 100644 index 0000000..bfed787 --- /dev/null +++ b/SlnMesnac.WPF/Converter/Generate/RowToIndexConverter.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; +using System.Windows.Data; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.WPF.Converter.Generate +* 唯一标识:38e34c93-1c10-4a1c-83b0-c545affdc224 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-11 10:27:10 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.WPF.Converter.Generate +{ + internal class RowToIndexConverter : IMultiValueConverter + { + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + { + var item = values[0]; + var dataGrid = values[1] as DataGrid; + + if (item == null || dataGrid == null) + return null; + + var index = dataGrid.Items.IndexOf(item) + 1; + return index; + } + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/SlnMesnac.WPF/MainWindow.xaml b/SlnMesnac.WPF/MainWindow.xaml index 6c6a0a4..150870a 100644 --- a/SlnMesnac.WPF/MainWindow.xaml +++ b/SlnMesnac.WPF/MainWindow.xaml @@ -62,6 +62,7 @@