SJA APS后端代码
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.

188 lines
5.1 KiB

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