///////////////////////////////////////////////////////////////////////////////// // 宁波拓通e智造平台 ToTong Next Builder // // https://git.tuotong-tech.com/tnb/tnb.server // ///////////////////////////////////////////////////////////////////////////////// using JNPF.Common.Extension; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using Yitter.IdGenerator; namespace Tnb.Vengine.Domain; /// /// 字段属性 /// public class VmDbProp : VmBaseProp { #region Properties /// /// 字段名称 /// public string field { get; set; } = string.Empty; /// /// 数据类型 /// [JsonIgnore] public string dataType { get; set; } = "varchar"; /// /// 数据类型 /// public string csType { get; set; } = "string"; /// /// 长度 /// public int length { get; set; } /// /// 精度 /// public int digit { get; set; } /// /// 排序 /// public int ordinal { get; set; } /// /// 非空 /// public bool required { get; set; } /// /// 是否主键 /// public bool pkey { get; set; } /// /// 是否模糊搜索 /// public bool fuzzy { get; set; } /// /// 默认值 /// public string? defValue { get; set; } /// /// 描述 /// public string? descrip { get; set; } #endregion Properties /// /// 获取可空类型的字符串名称 /// /// public string GetCsType() { return required ? csType : csType + "?"; } /// /// 获取默认值 /// /// public object? GetDefaultValue() { object? val = null; if (!required) { return val; } if (string.IsNullOrEmpty(defValue)) { val = csType switch { "string" => string.Empty, "short" or "int" or "long" => 0, "float" or "double" or "decimal" => 0f, "DateTime" => DateTime.Now, _ => null }; } else { val = defValue switch { "@@snowid" => YitIdHelper.NextId().ToString(), "@@now" => DateTime.Now, "@@userid" => YitIdHelper.NextId().ToString(), "@@orgid" => YitIdHelper.NextId().ToString(), _ => null }; } return val; } /// /// 获取默认值文本 /// /// public string? GetDefaultValueString() { string val = ""; if (!required) return val; if (!string.IsNullOrWhiteSpace(defValue)) { val = defValue switch { "@@snowid" => "YitIdHelper.NextId().ToString()", "@@now" => "DateTime.Now", _ => defValue }; } if (string.IsNullOrEmpty(val)) { val = csType switch { "string" => "string.Empty;", "DateTime" => "DateTime.Now;", _ => "" }; } return string.IsNullOrEmpty(val) ? "" : " = " + val; } /// /// 获取默认宽度 /// /// public string GetDefaultWidth() { return csType switch { "string" => "\"width\": \"auto\"", "int" or "short" or "long" => "\"width\": 80", "DateTime" => "\"width\": 150", _ => "" }; } /// /// 获取默认组件 /// /// public CompOption GetDefaultComp() { CompOption comp = new CompOption(); if (pkey) { comp.attr.Add("disabled", true); return comp; } switch (csType) { case "string": comp.attr.Add("clearable", true); comp.attr.Add("maxlength", length); comp.attr.Add("showWordLimit", true); break; case "int": case "short": case "long": comp.type = "el-input-number"; break; case "DateTime": comp.type = "el-date-picker"; break; }; return comp; } /// /// 获取SqlSugar特性文本 /// /// public string GetAttributeString() { var attr = ""; var sb = new List(); if (!string.IsNullOrEmpty(field) && code != field) sb.Add($"ColumnName = \"{field}\""); if (pkey) sb.Add("IsPrimaryKey = true"); //sb.Add("IsIdentity = true"); if (required && !pkey) sb.Add("IsNullable = false"); if (csType == "string" && length > 0) { sb.Add($"Length = {CodeHelper.LengthToString(length)}"); } if (csType == "decimal") { if (length > 0) sb.Add($"Length = {length}"); if (digit > 0) sb.Add($"DecimalDigits = {digit}"); } //if(isJson) sb.Add("IsJson = true") if (sb.Any()) attr += $"\t[SugarColumn({sb.JoinAsString(", ")})]{Environment.NewLine}"; return attr; } }