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.

229 lines
6.5 KiB

3 years ago
  1. package db
  2. import (
  3. "context"
  4. "go.mongodb.org/mongo-driver/bson"
  5. "go.mongodb.org/mongo-driver/bson/primitive"
  6. "go.mongodb.org/mongo-driver/mongo/options"
  7. "leit.com/leit_seat_aps/common"
  8. "leit.com/leit_seat_aps/glog"
  9. "strings"
  10. "time"
  11. )
  12. type Buffer struct {
  13. Orderid string `bson:"orderid" json:"buffer-orderid"` //流水号id
  14. Finr int `bson:"finr" json:"buffer-finr"` //映射关系
  15. Eid int `bson:"eid" json:"buffer-eid"` //映射关系
  16. Data string `bson:"data" json:"buffer-data"` //json串
  17. TimeStamp string `bson:"timestamp" json:"buffer-timestamp"` //时间戳
  18. Todb string `bson:"todb" json:"buffer-todb"` //导入数据库
  19. Todrivername string `bson:"todrivername" json:"buffer-todrivername"` //导入数据库引擎
  20. Totable string `bson:"totable" json:"buffer-totable"` //导入数据表
  21. Flag int `bson:"flag" json:"buffer-flag"` //是否导入状态
  22. Times int `bson:"times" json:"buffer-times"` //执行次数
  23. Status string `bson:"status" json:"buffer-status"` //ok or error
  24. Message string `bson:"message" json:"buffer-message"` //错误信息
  25. Funcspec string `bson:"funcspec" json:"buffer-funcspec"` //导入执行方法
  26. Dbtype string `bson:"dbtype" json:"buffer-dbtype"` //导入数据库类型
  27. }
  28. func (t *Buffer) TableName() string {
  29. return "buffer"
  30. }
  31. func (t *Buffer) InsertRecord() (insertID primitive.ObjectID) {
  32. //1.初始化链接
  33. client := MgoDb()
  34. //2.选择数据库,数据表
  35. collect := client.Database("logDb").Collection("buffer")
  36. insertRest, err := collect.InsertOne(context.TODO(), t)
  37. if err != nil {
  38. glog.InfoExtln("buffer 数据导入", "err", err)
  39. return
  40. }
  41. insertID = insertRest.InsertedID.(primitive.ObjectID)
  42. return insertID
  43. }
  44. func (t *Buffer) FindList(logred SerchData, skip int64, limit int64) ([]*Buffer, int64) {
  45. res := bson.M{}
  46. // 创建需要过滤的条件
  47. if logred.Key == "message" {
  48. res = bson.M{"message": primitive.Regex{Pattern: logred.Val}}
  49. }
  50. if logred.Key == "totable" {
  51. res = bson.M{"totable": primitive.Regex{Pattern: logred.Val}}
  52. }
  53. if logred.Key == "status" {
  54. res = bson.M{"status": primitive.Regex{Pattern: logred.Val}}
  55. }
  56. if logred.Key == "timestamp" {
  57. times := strings.Split(logred.Val, "-")
  58. stime := time.Unix(int64(common.ValueToInt(times[0], 0)), 0)
  59. etime := time.Unix(int64(common.ValueToInt(times[1], 0)), 0)
  60. stimestr := common.TimeFormat(stime, "yyyyMMddHHmmss")
  61. etimestr := common.TimeFormat(etime, "yyyyMMddHHmmss")
  62. res = bson.M{"timestamp": bson.M{"$lte": etimestr, "$gte": stimestr}}
  63. }
  64. //1.初始化链接
  65. client := MgoDb()
  66. //2.选择数据库,数据表
  67. collect := client.Database("logDb").Collection("buffer")
  68. //var skip int64 = 0//从那个开始
  69. //var limit int64 = 2//炼制几个输出字段
  70. cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
  71. Skip: &skip,
  72. Limit: &limit,
  73. Sort: bson.D{{"timestamp", -1}},
  74. })
  75. if err != nil {
  76. return nil, 0
  77. }
  78. defer cursor.Close(context.TODO())
  79. //创建需要反序列化成什么样子的结构体对象
  80. records := make([]*Buffer, 0)
  81. for cursor.Next(context.TODO()) {
  82. record := &Buffer{}
  83. //反序列化
  84. err = cursor.Decode(record)
  85. if err != nil {
  86. return nil, 0
  87. }
  88. //追加
  89. records = append(records, record)
  90. }
  91. // 获取数据总数
  92. count, err := collect.CountDocuments(context.Background(), bson.D{})
  93. if err != nil {
  94. return nil, 0
  95. }
  96. return records, count
  97. }
  98. func (t *Buffer) FindData() []*Buffer {
  99. limit := int64(500)
  100. skip := int64(0)
  101. //1.初始化链接
  102. client := MgoDb()
  103. //2.选择数据库,数据表
  104. collect := client.Database("logDb").Collection("buffer")
  105. //var skip int64 = 0//从那个开始
  106. //var limit int64 = 2//炼制几个输出字段
  107. cursor, err := collect.Find(context.TODO(), bson.D{
  108. {"flag", 0},
  109. {"status", "ok"},
  110. }, &options.FindOptions{
  111. Skip: &skip,
  112. Limit: &limit,
  113. Sort: bson.D{{"timestamp", -1}},
  114. })
  115. if err != nil {
  116. return nil
  117. }
  118. defer cursor.Close(context.TODO())
  119. //创建需要反序列化成什么样子的结构体对象
  120. records := make([]*Buffer, 0)
  121. for cursor.Next(context.TODO()) {
  122. record := &Buffer{}
  123. //反序列化
  124. err = cursor.Decode(record)
  125. if err != nil {
  126. return nil
  127. }
  128. //追加
  129. records = append(records, record)
  130. }
  131. return records
  132. }
  133. func (t *Buffer) UpdateData() error {
  134. //1.初始化链接
  135. client := MgoDb()
  136. //2.选择数据库,数据表
  137. collect := client.Database("logDb").Collection("buffer")
  138. new := &Buffer{
  139. Orderid: t.Orderid,
  140. Eid: t.Eid,
  141. Finr: t.Finr,
  142. Data: t.Data,
  143. TimeStamp: t.TimeStamp,
  144. Todrivername: t.Todrivername,
  145. Totable: t.Totable,
  146. Todb: t.Todb,
  147. Status: t.Status,
  148. Message: t.Message,
  149. Funcspec: t.Funcspec,
  150. Dbtype: t.Dbtype,
  151. Times: t.Times + 1,
  152. Flag: 1,
  153. }
  154. update := bson.M{"$set": new}
  155. _, err := collect.UpdateOne(context.Background(), bson.M{"orderid": new.Orderid}, update)
  156. if err != nil {
  157. return err
  158. }
  159. return nil
  160. }
  161. //累计更新次数,超过5次的直接关闭订单
  162. func (t *Buffer) UpdateDataTimes() error {
  163. //1.初始化链接
  164. client := MgoDb()
  165. //2.选择数据库,数据表
  166. collect := client.Database("logDb").Collection("buffer")
  167. if t.Times > 5 {
  168. // 修改一条数据,如果不存在则插入
  169. new := &Buffer{
  170. Orderid: t.Orderid,
  171. Eid: t.Eid,
  172. Finr: t.Finr,
  173. Data: t.Data,
  174. TimeStamp: t.TimeStamp,
  175. Todrivername: t.Todrivername,
  176. Totable: t.Totable,
  177. Todb: t.Todb,
  178. Status: t.Status,
  179. Message: t.Message,
  180. Funcspec: t.Funcspec,
  181. Dbtype: t.Dbtype,
  182. Times: t.Times + 1,
  183. Flag: 1,
  184. }
  185. update := bson.M{"$set": new}
  186. _, err := collect.UpdateOne(context.Background(), bson.M{"orderid": new.Orderid}, update)
  187. if err != nil {
  188. return err
  189. }
  190. return nil
  191. } else {
  192. new := &Buffer{
  193. Orderid: t.Orderid,
  194. Eid: t.Eid,
  195. Finr: t.Finr,
  196. Data: t.Data,
  197. TimeStamp: t.TimeStamp,
  198. Todrivername: t.Todrivername,
  199. Totable: t.Totable,
  200. Todb: t.Todb,
  201. Status: t.Status,
  202. Message: t.Message,
  203. Funcspec: t.Funcspec,
  204. Dbtype: t.Dbtype,
  205. Times: t.Times + 1,
  206. Flag: 0,
  207. }
  208. update := bson.M{"$set": new}
  209. _, err := collect.UpdateOne(context.Background(), bson.M{"orderid": new.Orderid}, update)
  210. if err != nil {
  211. return err
  212. }
  213. return nil
  214. }
  215. }