tools_gen_table_column.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * @desc:代码生成表格字段维护
  3. * @company:云南省奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2021/7/22 17:39
  6. */
  7. package service
  8. import (
  9. "gfast/app/system/dao"
  10. "gfast/app/system/model"
  11. "github.com/gogf/gf/database/gdb"
  12. "github.com/gogf/gf/errors/gerror"
  13. "github.com/gogf/gf/frame/g"
  14. "github.com/gogf/gf/text/gregex"
  15. "github.com/gogf/gf/text/gstr"
  16. "github.com/gogf/gf/util/gconv"
  17. "strings"
  18. )
  19. var (
  20. ToolsGenTableColumn = &toolsGenTableColumn{
  21. ColumnTypeStr: []string{"char", "varchar", "narchar", "varchar2", "tinytext", "text", "mediumtext", "longtext"},
  22. ColumnTypeTime: []string{"datetime", "time", "date", "timestamp"},
  23. ColumnTypeNumber: []string{"tinyint", "smallint", "mediumint", "int", "number", "integer", "bigint", "float", "float", "double", "decimal"},
  24. ColumnNameNotEdit: []string{"id", "created_by", "created_at", "updated_by", "updated_at", "deleted_at"},
  25. ColumnNameNotList: []string{"id", "created_by", "updated_by", "created_at", "updated_at", "deleted_at"},
  26. ColumnNameNotQuery: []string{"id", "created_by", "updated_by", "created_at", "updated_at", "deleted_at", "remark"},
  27. }
  28. )
  29. type toolsGenTableColumn struct {
  30. ColumnTypeStr []string //数据库字符串类型
  31. ColumnTypeTime []string //数据库时间类型
  32. ColumnTypeNumber []string //数据库数字类型
  33. ColumnNameNotEdit []string //页面不需要编辑字段
  34. ColumnNameNotList []string //页面不需要显示的列表字段
  35. ColumnNameNotQuery []string //页面不需要查询字段
  36. }
  37. // SelectDbTableColumnsByName 根据表名称查询列信息
  38. func (s *toolsGenTableColumn) SelectDbTableColumnsByName(tableName string) ([]*model.ToolsGenTableColumn, error) {
  39. db := g.DB()
  40. var res []*model.ToolsGenTableColumn
  41. sql := " select column_name, (case when (is_nullable = 'no' && column_key != 'PRI') then '1' else null end) as is_required, " +
  42. "(case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment," +
  43. " (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type from information_schema.columns" +
  44. " where table_schema = (select database()) "
  45. sql += " and " + gdb.FormatSqlWithArgs(" table_name=? ", []interface{}{tableName}) + " order by ordinal_position ASC "
  46. err := db.GetScan(&res, sql)
  47. if err != nil {
  48. g.Log().Error(err)
  49. return nil, gerror.New("查询列信息失败")
  50. }
  51. return res, nil
  52. }
  53. // InitColumnField 初始化列属性字段
  54. func (s *toolsGenTableColumn) InitColumnField(column *model.ToolsGenTableColumn, table *model.ToolsGenTable) {
  55. dataType := s.GetDbType(column.ColumnType)
  56. columnName := column.ColumnName
  57. column.TableId = table.TableId
  58. //设置字段名
  59. column.GoField = gstr.CaseCamel(columnName)
  60. column.HtmlField = gstr.CaseCamelLower(columnName)
  61. if s.IsStringObject(dataType) {
  62. //字段为字符串类型
  63. column.GoType = "string"
  64. columnLength := s.GetColumnLength(column.ColumnType)
  65. if columnLength >= 500 {
  66. column.HtmlType = "textarea"
  67. } else {
  68. column.HtmlType = "input"
  69. }
  70. } else if s.IsTimeObject(dataType) {
  71. //字段为时间类型
  72. column.GoType = "Time"
  73. column.HtmlType = "datetime"
  74. } else if s.IsNumberObject(dataType) {
  75. //字段为数字类型
  76. column.HtmlType = "input"
  77. t, _ := gregex.ReplaceString(`\(.+\)`, "", column.ColumnType)
  78. t = gstr.Split(gstr.Trim(t), " ")[0]
  79. t = gstr.ToLower(t)
  80. // 如果是浮点型
  81. switch t {
  82. case "float", "double", "decimal":
  83. column.GoType = "float64"
  84. case "bit", "int", "tinyint", "small_int", "smallint", "medium_int", "mediumint":
  85. if gstr.ContainsI(column.ColumnType, "unsigned") {
  86. column.GoType = "uint"
  87. } else {
  88. column.GoType = "int"
  89. }
  90. case "big_int", "bigint":
  91. if gstr.ContainsI(column.ColumnType, "unsigned") {
  92. column.GoType = "uint64"
  93. } else {
  94. column.GoType = "int64"
  95. }
  96. }
  97. }
  98. //新增字段
  99. if s.IsNotEdit(columnName) {
  100. column.IsRequired = "0"
  101. column.IsInsert = "0"
  102. } else {
  103. column.IsInsert = "1"
  104. if strings.Index(columnName, "name") >= 0 || strings.Index(columnName, "status") >= 0 {
  105. column.IsRequired = "1"
  106. }
  107. }
  108. // 编辑字段
  109. if s.IsNotEdit(columnName) {
  110. column.IsEdit = "0"
  111. } else {
  112. if column.IsPk == "1" {
  113. column.IsEdit = "0"
  114. } else {
  115. column.IsEdit = "1"
  116. }
  117. }
  118. // 列表字段
  119. if s.IsNotList(columnName) {
  120. column.IsList = "0"
  121. } else {
  122. column.IsList = "1"
  123. }
  124. // 查询字段
  125. if s.IsNotQuery(columnName) {
  126. column.IsQuery = "0"
  127. } else {
  128. column.IsQuery = "1"
  129. }
  130. // 查询字段类型
  131. if s.CheckNameColumn(columnName) {
  132. column.QueryType = "LIKE"
  133. } else {
  134. column.QueryType = "EQ"
  135. }
  136. // 状态字段设置单选框
  137. if s.CheckStatusColumn(columnName) {
  138. column.HtmlType = "radio"
  139. } else if s.CheckTypeColumn(columnName) || s.CheckSexColumn(columnName) {
  140. // 类型&性别字段设置下拉框
  141. column.HtmlType = "select"
  142. }
  143. }
  144. // GetDbType 获取数据库类型字段
  145. func (s *toolsGenTableColumn) GetDbType(columnType string) string {
  146. if strings.Index(columnType, "(") > 0 {
  147. return columnType[0:strings.Index(columnType, "(")]
  148. } else {
  149. return columnType
  150. }
  151. }
  152. // IsExistInArray 判断 value 是否存在在切片array中
  153. func (s *toolsGenTableColumn) IsExistInArray(value string, array []string) bool {
  154. for _, v := range array {
  155. if v == value {
  156. return true
  157. }
  158. }
  159. return false
  160. }
  161. // GetColumnLength 获取字段长度
  162. func (s *toolsGenTableColumn) GetColumnLength(columnType string) int {
  163. start := strings.Index(columnType, "(")
  164. end := strings.Index(columnType, ")")
  165. result := ""
  166. if start >= 0 && end >= 0 {
  167. result = columnType[start+1 : end-1]
  168. }
  169. return gconv.Int(result)
  170. }
  171. // IsStringObject 判断是否是数据库字符串类型
  172. func (s *toolsGenTableColumn) IsStringObject(dataType string) bool {
  173. return s.IsExistInArray(dataType, s.ColumnTypeStr)
  174. }
  175. // IsTimeObject 判断是否是数据库时间类型
  176. func (s *toolsGenTableColumn) IsTimeObject(dataType string) bool {
  177. return s.IsExistInArray(dataType, s.ColumnTypeTime)
  178. }
  179. // IsNumberObject 是否数字类型
  180. func (s *toolsGenTableColumn) IsNumberObject(dataType string) bool {
  181. return s.IsExistInArray(dataType, s.ColumnTypeNumber)
  182. }
  183. // IsNotEdit 是否不可编辑
  184. func (s *toolsGenTableColumn) IsNotEdit(name string) bool {
  185. return s.IsExistInArray(name, s.ColumnNameNotEdit)
  186. }
  187. // IsNotList 不在列表显示
  188. func (s *toolsGenTableColumn) IsNotList(name string) bool {
  189. return s.IsExistInArray(name, s.ColumnNameNotList)
  190. }
  191. // IsNotQuery 不可用于查询
  192. func (s *toolsGenTableColumn) IsNotQuery(name string) bool {
  193. return s.IsExistInArray(name, s.ColumnNameNotQuery)
  194. }
  195. // CheckNameColumn 检查字段名后4位是否是name
  196. func (s *toolsGenTableColumn) CheckNameColumn(columnName string) bool {
  197. if len(columnName) >= 4 {
  198. end := len(columnName)
  199. start := end - 4
  200. if start <= 0 {
  201. start = 0
  202. }
  203. tmp := columnName[start:end]
  204. if tmp == "name" {
  205. return true
  206. }
  207. }
  208. return false
  209. }
  210. // CheckStatusColumn 检查字段名后6位是否是status
  211. func (s *toolsGenTableColumn) CheckStatusColumn(columnName string) bool {
  212. if len(columnName) >= 6 {
  213. end := len(columnName)
  214. start := end - 6
  215. if start <= 0 {
  216. start = 0
  217. }
  218. tmp := columnName[start:end]
  219. if tmp == "status" {
  220. return true
  221. }
  222. }
  223. return false
  224. }
  225. // CheckTypeColumn 检查字段名后4位是否是type
  226. func (s *toolsGenTableColumn) CheckTypeColumn(columnName string) bool {
  227. if len(columnName) >= 4 {
  228. end := len(columnName)
  229. start := end - 4
  230. if start <= 0 {
  231. start = 0
  232. }
  233. if columnName[start:end] == "type" {
  234. return true
  235. }
  236. }
  237. return false
  238. }
  239. // CheckSexColumn 检查字段名后3位是否是sex
  240. func (s *toolsGenTableColumn) CheckSexColumn(columnName string) bool {
  241. if len(columnName) >= 3 {
  242. end := len(columnName)
  243. start := end - 3
  244. if start <= 0 {
  245. start = 0
  246. }
  247. if columnName[start:end] == "sex" {
  248. return true
  249. }
  250. }
  251. return false
  252. }
  253. // SelectGenTableColumnListByTableId 查询业务字段列表
  254. func (s *toolsGenTableColumn) SelectGenTableColumnListByTableId(tableId int64) (list []*model.ToolsGenTableColumn, err error) {
  255. err = dao.ToolsGenTableColumn.Where(dao.ToolsGenTableColumn.C.TableId, tableId).
  256. Order(dao.ToolsGenTableColumn.C.Sort + " asc, " + dao.ToolsGenTableColumn.C.ColumnId + " asc").Scan(&list)
  257. if err != nil {
  258. g.Log().Error(err)
  259. return nil, gerror.New("获取字段信息出错")
  260. }
  261. return list, nil
  262. }
  263. // GetAllTableColumns 获取所有字段信息
  264. func (s *toolsGenTableColumn) GetAllTableColumns() (list []*model.ToolsGenTableColumn, err error) {
  265. err = dao.ToolsGenTableColumn.Order(dao.ToolsGenTableColumn.C.Sort + " asc, " + dao.ToolsGenTableColumn.C.ColumnId + " asc").Scan(&list)
  266. if err != nil {
  267. g.Log().Error(err)
  268. err = gerror.New("获取字段信息出错")
  269. return
  270. }
  271. return
  272. }
  273. func (s *toolsGenTableColumn) SetPkColumn(table *dao.ToolsGenTableExtend, columns []*model.ToolsGenTableColumn) {
  274. for _, column := range columns {
  275. if column.IsPk == "1" {
  276. table.PkColumn = column
  277. break
  278. }
  279. }
  280. if table.PkColumn == nil {
  281. table.PkColumn = columns[0]
  282. }
  283. }