sys_tables.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package tools
  2. import (
  3. common "go-admin/common/models"
  4. "strings"
  5. "gorm.io/gorm"
  6. "go-admin/app/admin/models"
  7. )
  8. type SysTables struct {
  9. TableId int `gorm:"primaryKey;autoIncrement" json:"tableId"` //表编码
  10. TBName string `gorm:"column:table_name;size:255;" json:"tableName"` //表名称
  11. MLTBName string `gorm:"-" json:"-"` //表名称
  12. TableComment string `gorm:"size:255;" json:"tableComment"` //表备注
  13. ClassName string `gorm:"size:255;" json:"className"` //类名
  14. TplCategory string `gorm:"size:255;" json:"tplCategory"` //
  15. PackageName string `gorm:"size:255;" json:"packageName"` //包名
  16. ModuleName string `gorm:"size:255;" json:"moduleName"` //go文件名
  17. ModuleFrontName string `gorm:"size:255;comment:前端文件名;" json:"moduleFrontName"` //前端文件名
  18. BusinessName string `gorm:"size:255;" json:"businessName"` //
  19. FunctionName string `gorm:"size:255;" json:"functionName"` //功能名称
  20. FunctionAuthor string `gorm:"size:255;" json:"functionAuthor"` //功能作者
  21. PkColumn string `gorm:"size:255;" json:"pkColumn"`
  22. PkGoField string `gorm:"size:255;" json:"pkGoField"`
  23. PkJsonField string `gorm:"size:255;" json:"pkJsonField"`
  24. Options string `gorm:"size:255;" json:"options"`
  25. TreeCode string `gorm:"size:255;" json:"treeCode"`
  26. TreeParentCode string `gorm:"size:255;" json:"treeParentCode"`
  27. TreeName string `gorm:"size:255;" json:"treeName"`
  28. Tree bool `gorm:"size:1;default:0;" json:"tree"`
  29. Crud bool `gorm:"size:1;default:1;" json:"crud"`
  30. Remark string `gorm:"size:255;" json:"remark"`
  31. IsDataScope int `gorm:"size:1;" json:"isDataScope"`
  32. IsActions int `gorm:"size:1;" json:"isActions"`
  33. IsAuth int `gorm:"size:1;" json:"isAuth"`
  34. IsLogicalDelete string `gorm:"size:1;" json:"isLogicalDelete"`
  35. LogicalDelete bool `gorm:"size:1;" json:"logicalDelete"`
  36. LogicalDeleteColumn string `gorm:"size:128;" json:"logicalDeleteColumn"`
  37. common.ModelTime
  38. common.ControlBy
  39. DataScope string `gorm:"-" json:"dataScope"`
  40. Params Params `gorm:"-" json:"params"`
  41. Columns []SysColumns `gorm:"-" json:"columns"`
  42. models.BaseModel
  43. }
  44. func (SysTables) TableName() string {
  45. return "sys_tables"
  46. }
  47. type Params struct {
  48. TreeCode string `gorm:"-" json:"treeCode"`
  49. TreeParentCode string `gorm:"-" json:"treeParentCode"`
  50. TreeName string `gorm:"-" json:"treeName"`
  51. }
  52. func (e *SysTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]SysTables, int, error) {
  53. var doc []SysTables
  54. table := tx.Table("sys_tables")
  55. if e.TBName != "" {
  56. table = table.Where("table_name = ?", e.TBName)
  57. }
  58. if e.TableComment != "" {
  59. table = table.Where("table_comment = ?", e.TableComment)
  60. }
  61. var count int64
  62. if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  63. return nil, 0, err
  64. }
  65. //table.Where("`deleted_at` IS NULL").Count(&count)
  66. return doc, int(count), nil
  67. }
  68. func (e *SysTables) Get(tx *gorm.DB, exclude bool) (SysTables, error) {
  69. var doc SysTables
  70. var err error
  71. table := tx.Table("sys_tables")
  72. if e.TBName != "" {
  73. table = table.Where("table_name = ?", e.TBName)
  74. }
  75. if e.TableId != 0 {
  76. table = table.Where("table_id = ?", e.TableId)
  77. }
  78. if e.TableComment != "" {
  79. table = table.Where("table_comment = ?", e.TableComment)
  80. }
  81. if err := table.First(&doc).Error; err != nil {
  82. return doc, err
  83. }
  84. var col SysColumns
  85. col.TableId = doc.TableId
  86. if doc.Columns, err = col.GetList(tx, exclude); err != nil {
  87. return doc, err
  88. }
  89. return doc, nil
  90. }
  91. func (e *SysTables) GetTree(tx *gorm.DB) ([]SysTables, error) {
  92. var doc []SysTables
  93. var err error
  94. table := tx.Table("sys_tables")
  95. if e.TBName != "" {
  96. table = table.Where("table_name = ?", e.TBName)
  97. }
  98. if e.TableId != 0 {
  99. table = table.Where("table_id = ?", e.TableId)
  100. }
  101. if e.TableComment != "" {
  102. table = table.Where("table_comment = ?", e.TableComment)
  103. }
  104. if err := table.Find(&doc).Error; err != nil {
  105. return doc, err
  106. }
  107. for i := 0; i < len(doc); i++ {
  108. var col SysColumns
  109. //col.FkCol = append(col.FkCol, SysColumns{ColumnId: 0, ColumnName: "请选择"})
  110. col.TableId = doc[i].TableId
  111. if doc[i].Columns, err = col.GetList(tx, false); err != nil {
  112. return doc, err
  113. }
  114. }
  115. return doc, nil
  116. }
  117. func (e *SysTables) Create(tx *gorm.DB) (SysTables, error) {
  118. var doc SysTables
  119. e.CreateBy = 0
  120. result := tx.Table("sys_tables").Create(&e)
  121. if result.Error != nil {
  122. err := result.Error
  123. return doc, err
  124. }
  125. doc = *e
  126. for i := 0; i < len(e.Columns); i++ {
  127. e.Columns[i].TableId = doc.TableId
  128. _, _ = e.Columns[i].Create(tx)
  129. }
  130. return doc, nil
  131. }
  132. func (e *SysTables) Update(tx *gorm.DB) (update SysTables, err error) {
  133. //if err = orm.Eloquent.Table("sys_tables").First(&update, e.TableId).Error; err != nil {
  134. // return
  135. //}
  136. //参数1:是要修改的数据
  137. //参数2:是修改的数据
  138. e.UpdateBy = 0
  139. if err = tx.Table("sys_tables").Where("table_id = ?", e.TableId).Updates(&e).Error; err != nil {
  140. return
  141. }
  142. tableNames := make([]string, 0)
  143. for i := range e.Columns {
  144. if e.Columns[i].FkTableName != "" {
  145. tableNames = append(tableNames, e.Columns[i].FkTableName)
  146. }
  147. }
  148. tables := make([]SysTables, 0)
  149. tableMap := make(map[string]*SysTables)
  150. if len(tableNames) > 0 {
  151. if err = tx.Table("sys_tables").Where("table_name in (?)", tableNames).Find(&tables).Error; err != nil {
  152. return
  153. }
  154. for i := range tables {
  155. tableMap[tables[i].TBName] = &tables[i]
  156. }
  157. }
  158. for i := 0; i < len(e.Columns); i++ {
  159. if e.Columns[i].FkTableName != "" {
  160. t, ok := tableMap[e.Columns[i].FkTableName]
  161. if ok {
  162. e.Columns[i].FkTableNameClass = t.ClassName
  163. t.MLTBName = strings.Replace(t.TBName, "_", "-", -1)
  164. e.Columns[i].FkTableNamePackage = t.MLTBName
  165. } else {
  166. tableNameList := strings.Split(e.Columns[i].FkTableName, "_")
  167. e.Columns[i].FkTableNameClass = ""
  168. //e.Columns[i].FkTableNamePackage = ""
  169. for a := 0; a < len(tableNameList); a++ {
  170. strStart := string([]byte(tableNameList[a])[:1])
  171. strEnd := string([]byte(tableNameList[a])[1:])
  172. e.Columns[i].FkTableNameClass += strings.ToUpper(strStart) + strEnd
  173. //e.Columns[i].FkTableNamePackage += strings.ToLower(strStart) + strings.ToLower(strEnd)
  174. }
  175. }
  176. }
  177. _, _ = e.Columns[i].Update(tx)
  178. }
  179. return
  180. }
  181. func (e *SysTables) Delete(db *gorm.DB) (success bool, err error) {
  182. tx := db.Begin()
  183. defer func() {
  184. if err != nil {
  185. tx.Rollback()
  186. } else {
  187. tx.Commit()
  188. }
  189. }()
  190. if err = tx.Table("sys_tables").Delete(SysTables{}, "table_id = ?", e.TableId).Error; err != nil {
  191. success = false
  192. return
  193. }
  194. if err = tx.Table("sys_columns").Delete(SysColumns{}, "table_id = ?", e.TableId).Error; err != nil {
  195. success = false
  196. return
  197. }
  198. success = true
  199. return
  200. }
  201. func (e *SysTables) BatchDelete(tx *gorm.DB, id []int) (Result bool, err error) {
  202. if err = tx.Unscoped().Table(e.TableName()).Where(" table_id in (?)", id).Delete(&SysColumns{}).Error; err != nil {
  203. return
  204. }
  205. Result = true
  206. return
  207. }