sys_columns.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package tools
  2. import (
  3. "IotAdmin/common/models"
  4. "IotAdmin/core/tools/utils"
  5. "gorm.io/gorm"
  6. )
  7. type SysColumns struct {
  8. ColumnId int `gorm:"primaryKey;autoIncrement" json:"columnId"`
  9. TableId int `gorm:"" json:"tableId"`
  10. ColumnName string `gorm:"size:128;" json:"columnName"`
  11. ColumnComment string `gorm:"column:column_comment;size:128;" json:"columnComment"`
  12. ColumnType string `gorm:"column:column_type;size:128;" json:"columnType"`
  13. GoType string `gorm:"column:go_type;size:128;" json:"goType"`
  14. GoField string `gorm:"column:go_field;size:128;" json:"goField"`
  15. JsonField string `gorm:"column:json_field;size:128;" json:"jsonField"`
  16. BJsonField string `gorm:"-" json:"-"`
  17. IsPk string `gorm:"column:is_pk;size:4;" json:"isPk"`
  18. IsIncrement string `gorm:"column:is_increment;size:4;" json:"isIncrement"`
  19. IsRequired string `gorm:"column:is_required;size:4;" json:"isRequired"`
  20. IsInsert string `gorm:"column:is_insert;size:4;" json:"isInsert"`
  21. IsSort string `gorm:"column:is_sort;size:4;" json:"isSort"`
  22. IsList string `gorm:"column:is_list;size:4;" json:"isList"`
  23. IsQuery string `gorm:"column:is_query;size:4;" json:"isQuery"`
  24. Required bool `gorm:"column:required;size:1;" json:"required"`
  25. QueryType string `gorm:"column:query_type;size:128;" json:"queryType"`
  26. HtmlType string `gorm:"column:html_type;size:128;" json:"htmlType"`
  27. DictType string `gorm:"column:dict_type;size:128;" json:"dictType"`
  28. Sort int `gorm:"column:sort;" json:"sort"`
  29. Pk bool `gorm:"column:pk;size:1;" json:"pk"`
  30. Remark string `gorm:"column:remark;size:255;" json:"remark"`
  31. FkTableName string `gorm:"" json:"fkTableName"`
  32. BFkTableName string `gorm:"-" json:"-"`
  33. FkTableNameClass string `gorm:"" json:"fkTableNameClass"`
  34. BFkTableNameClass string `gorm:"-" json:"-"`
  35. FkTableNamePackage string `gorm:"" json:"fkTableNamePackage"`
  36. BFkTableNamePackage string `gorm:"-" json:"-"`
  37. FkTableNameBusiness string `gorm:"" json:"fkTableNameBusiness"`
  38. BFkTableNameBusiness string `gorm:"-" json:"-"`
  39. FkCol []SysColumns `gorm:"-" json:"fkCol"`
  40. FkLabelId string `gorm:"" json:"fkLabelId"`
  41. FkLabelName string `gorm:"size:255;" json:"fkLabelName"`
  42. CreateBy int `gorm:"column:create_by;size:20;" json:"createBy"`
  43. UpdateBy int `gorm:"column:update_By;size:20;" json:"updateBy"`
  44. models.ModelTime
  45. }
  46. func (*SysColumns) TableName() string {
  47. return "__gen_columns"
  48. }
  49. func (e *SysColumns) GetList(tx *gorm.DB, exclude bool) ([]SysColumns, error) {
  50. var doc []SysColumns
  51. table := tx.Table(e.TableName())
  52. table = table.Where("table_id = ? ", e.TableId)
  53. if exclude {
  54. notIn := make([]string, 0, 6)
  55. notIn = append(notIn, "id")
  56. notIn = append(notIn, "create_by")
  57. notIn = append(notIn, "update_by")
  58. notIn = append(notIn, "created_at")
  59. notIn = append(notIn, "updated_at")
  60. notIn = append(notIn, "deleted_at")
  61. table = table.Where(" column_name not in(?)", notIn)
  62. }
  63. if err := table.Find(&doc).Error; err != nil {
  64. return nil, err
  65. }
  66. for i := range doc {
  67. doc[i].BJsonField = utils.FirstUpper(doc[i].JsonField)
  68. doc[i].BFkTableNamePackage = utils.FirstUpper(doc[i].FkTableNamePackage)
  69. doc[i].BFkTableName = utils.FirstUpper(doc[i].FkTableName)
  70. doc[i].BFkTableNameClass = utils.FirstUpper(doc[i].FkTableNameClass)
  71. doc[i].BFkTableNameBusiness = utils.FirstUpper(doc[i].FkTableNameBusiness)
  72. }
  73. return doc, nil
  74. }
  75. func (e *SysColumns) Create(tx *gorm.DB) (SysColumns, error) {
  76. var doc SysColumns
  77. e.CreateBy = 0
  78. result := tx.Table(e.TableName()).Create(&e)
  79. if result.Error != nil {
  80. err := result.Error
  81. return doc, err
  82. }
  83. doc = *e
  84. return doc, nil
  85. }
  86. func (e *SysColumns) Update(tx *gorm.DB) (update SysColumns, err error) {
  87. if err = tx.Table(e.TableName()).First(&update, e.ColumnId).Error; err != nil {
  88. return
  89. }
  90. //参数1:是要修改的数据
  91. //参数2:是修改的数据
  92. e.UpdateBy = 0
  93. if err = tx.Table(e.TableName()).Model(&update).Save(&e).Error; err != nil {
  94. return
  95. }
  96. return
  97. }