GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.3 KiB

  1. package models
  2. import (
  3. "LAPP_GAAS_GFrame_BACKEND/conf"
  4. "LAPP_GAAS_GFrame_BACKEND/db"
  5. "LAPP_GAAS_GFrame_BACKEND/utils"
  6. "fmt"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "log"
  11. "context"
  12. )
  13. //数据库
  14. type Database struct {
  15. ID int `bson:"id" json:"database-id"` //ID:uuid唯一字符串
  16. DbName string `bson:"dbname" json:"database-dbname"` //数据库名称
  17. DriverName string `bson:"drivername" json:"database-drivername"` //引擎类型
  18. Host string `bson:"host" json:"database-host"` //链接地址
  19. Port int `bson:"port" json:"database-port"` //端口号
  20. User string `bson:"user" json:"database-user"` //用户名
  21. Pwd string `bson:"pwd" json:"database-pwd"` //密码
  22. Sourcetype string `bson:"sourcetype" json:"database-sourcetype"` //主库和从库
  23. }
  24. func (t *Database) TableName() string {
  25. return "database"
  26. }
  27. func (t *Database) InsertRecord() (insertID primitive.ObjectID) {
  28. //1.初始化链接
  29. client := db.MgoDb()
  30. //2.选择数据库,数据表
  31. collect := client.Database(conf.DbConfig.Mongdbname).Collection("database")
  32. insertRest, err := collect.InsertOne(context.TODO(), t)
  33. if err != nil {
  34. fmt.Println(err)
  35. return
  36. }
  37. insertID = insertRest.InsertedID.(primitive.ObjectID)
  38. return insertID
  39. }
  40. func (t *Database) UpdateData() error {
  41. //1.初始化链接
  42. client := db.MgoDb()
  43. //2.选择数据库,数据表
  44. collect := client.Database(conf.DbConfig.Mongdbname).Collection("database")
  45. // 修改一条数据,如果不存在则插入
  46. new := &Database{
  47. ID: t.ID,
  48. DbName: t.DbName,
  49. DriverName: t.DriverName,
  50. Host: t.Host,
  51. Port: t.Port,
  52. User: t.User,
  53. Pwd: t.Pwd,
  54. Sourcetype: t.Sourcetype,
  55. }
  56. update := bson.M{"$set": new}
  57. _, err := collect.UpdateOne(context.Background(), bson.M{"dbname": new.DbName,"drivername":new.DriverName}, update)
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (t *Database) FindList(logred SerchData, skip int64, limit int64) ([]*Database, int64) {
  64. res := bson.M{}
  65. // 创建需要过滤的条件
  66. if logred.Key == "dbname" {
  67. res = bson.M{"dbname": primitive.Regex{Pattern: logred.Val}}
  68. }
  69. if logred.Key == "drivername" {
  70. res = bson.M{"drivername": primitive.Regex{Pattern: logred.Val}}
  71. }
  72. if logred.Key == "host" {
  73. res = bson.M{"host": primitive.Regex{Pattern: logred.Val}}
  74. }
  75. res = bson.M{"sourcetype": primitive.Regex{Pattern: "Slave"}}
  76. //1.初始化链接
  77. client := db.MgoDb()
  78. //2.选择数据库,数据表
  79. collect := client.Database(conf.DbConfig.Mongdbname).Collection("database")
  80. //var skip int64 = 0//从那个开始
  81. //var limit int64 = 2//炼制几个输出字段
  82. cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
  83. Skip: &skip,
  84. Limit: &limit,
  85. Sort: bson.D{{"id", -1}},
  86. })
  87. if err != nil {
  88. fmt.Println(err)
  89. return nil, 0
  90. }
  91. defer cursor.Close(context.TODO())
  92. //创建需要反序列化成什么样子的结构体对象
  93. records := make([]*Database, 0)
  94. for cursor.Next(context.TODO()) {
  95. record := &Database{}
  96. //反序列化
  97. err = cursor.Decode(record)
  98. if err != nil {
  99. fmt.Println(err)
  100. return nil, 0
  101. }
  102. //追加
  103. records = append(records, record)
  104. }
  105. // 获取数据总数
  106. count, err := collect.CountDocuments(context.Background(), bson.D{})
  107. if err != nil {
  108. log.Fatal(count)
  109. }
  110. return records, count
  111. }
  112. func (t *Database) FindData() []*Database {
  113. res := bson.M{}
  114. // 创建需要过滤的条件
  115. if !utils.ValueIsEmpty(t.Sourcetype) {
  116. res = bson.M{"sourcetype": primitive.Regex{Pattern: t.Sourcetype}}
  117. }
  118. limit := int64(500)
  119. skip := int64(0)
  120. //1.初始化链接
  121. client := db.MgoDb()
  122. //2.选择数据库,数据表
  123. collect := client.Database(conf.DbConfig.Mongdbname).Collection("database")
  124. //var skip int64 = 0//从那个开始
  125. //var limit int64 = 2//炼制几个输出字段
  126. cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
  127. Skip: &skip,
  128. Limit: &limit,
  129. Sort: bson.D{{"id", -1}},
  130. })
  131. fmt.Println(cursor)
  132. if err != nil {
  133. return nil
  134. }
  135. defer cursor.Close(context.TODO())
  136. //创建需要反序列化成什么样子的结构体对象
  137. records := make([]*Database, 0)
  138. for cursor.Next(context.TODO()) {
  139. record := &Database{}
  140. //反序列化
  141. err = cursor.Decode(record)
  142. if err != nil {
  143. fmt.Println(err)
  144. return nil
  145. }
  146. //追加
  147. records = append(records, record)
  148. }
  149. return records
  150. }
  151. func (t *Database) FindOne() (Database,error) {
  152. //1.初始化链接
  153. client := db.MgoDb()
  154. //2.选择数据库,数据表
  155. collect := client.Database(conf.DbConfig.Mongdbname).Collection("database")
  156. record := Database{}
  157. err := collect.FindOne(context.Background(), bson.M{"dbname": t.DbName,"drivername":t.DriverName}).Decode(&record)
  158. if err != nil {
  159. return record,err
  160. }
  161. return record,nil
  162. }
  163. func (t *Database) DelData() error {
  164. //1.初始化链接
  165. client := db.MgoDb()
  166. //2.选择数据库,数据表
  167. collect := client.Database(conf.DbConfig.Mongdbname).Collection("database")
  168. // 修改一条数据,如果不存在则插入
  169. new := &Database{
  170. DbName: t.DbName,
  171. DriverName: t.DriverName,
  172. }
  173. // 删除一条数据
  174. _, err := collect.DeleteOne(context.Background(), bson.M{"dbname": new.DbName,"drivername":new.DriverName})
  175. if err != nil {
  176. return err
  177. }
  178. return nil
  179. }