mgo_base.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package models
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_chart_lib/utils"
  7. "eta/eta_chart_lib/utils/mgodb"
  8. "fmt"
  9. )
  10. func init() {
  11. if utils.MgoUrlData != `` {
  12. var mgoConfig mgodb.MgoConfig
  13. if e := json.Unmarshal([]byte(utils.MgoUrlData), &mgoConfig); e != nil {
  14. panic("mongodb链接失败,Err:" + e.Error())
  15. return
  16. }
  17. mgoCli := mgodb.MgoNewClient(mgoConfig)
  18. utils.MgoDataCli = mgoCli
  19. utils.MgoDataDbName = mgoConfig.Database
  20. //result, err := TestMgoFindOne("data_ths", bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"})
  21. //fmt.Println(time.Now())
  22. //result, err := TestMgoFindOne("data_wind", bson.M{"edbcode": "s0033227", "datatime": "2007-12-29"})
  23. //fmt.Println(time.Now())
  24. //
  25. //fmt.Println(err)
  26. //fmt.Println(result)
  27. }
  28. }
  29. // TestMgoFindOne
  30. // @Description: 获取单条数据
  31. // @author: Roc
  32. // @datetime 2024-04-25 15:44:07
  33. // @param colName string 集合名词:`data_ths`
  34. // @param whereParams interface{} bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  35. // @return result interface{}
  36. // @return err error
  37. func TestMgoFindOne(colName string, whereParams interface{}) (result interface{}, err error) {
  38. if colName == "" {
  39. err = errors.New("集合名称不可为空")
  40. return
  41. }
  42. if utils.MgoDataCli == nil {
  43. err = errors.New("mongodb连接失败")
  44. return
  45. }
  46. db := utils.MgoDataCli.Database("hz_data")
  47. coll := db.Collection("data_ths")
  48. ctx := context.TODO()
  49. if err != nil {
  50. fmt.Println("MgoGetColl Err:", err.Error())
  51. return
  52. }
  53. //err = coll.Find(ctx, whereParams).One(&result)
  54. err = coll.Find(ctx, whereParams).All(&result)
  55. return
  56. }
  57. // 创建集合
  58. //func MgoCreateCollection(collectionName string) (err error) {
  59. // ctx := context.TODO()
  60. //
  61. // mgoClient := mgodb.MgoNewClient()
  62. //
  63. // defer func() {
  64. // mgoClient.Close(ctx)
  65. // }()
  66. //
  67. // colName := collectionName
  68. // err = mgoClient.Database("hz_data").CreateCollection(ctx, colName)
  69. // return
  70. //}
  71. // 批量新增多条数据
  72. func MgoBatchInsertData(colName string, dataList interface{}) (err error) {
  73. if colName == "" {
  74. err = errors.New("集合名称不可为空")
  75. return
  76. }
  77. ctx := context.TODO()
  78. coll, err := mgodb.MgoGetColl(colName)
  79. if err != nil {
  80. fmt.Println("MgoGetColl Err:", err.Error())
  81. return
  82. }
  83. defer func() {
  84. coll.Close(ctx)
  85. }()
  86. _, err = coll.Collection.InsertMany(ctx, dataList)
  87. if err != nil {
  88. fmt.Println("InsertMany:Err:" + err.Error())
  89. return
  90. }
  91. return
  92. }
  93. // 插入单条数据
  94. func MgoInsertData(colName string, dataItem interface{}) (insertedID interface{}, err error) {
  95. if colName == "" {
  96. err = errors.New("集合名称不可为空")
  97. return
  98. }
  99. ctx := context.TODO()
  100. coll, err := mgodb.MgoGetColl(colName)
  101. if err != nil {
  102. fmt.Println("MgoGetColl Err:", err.Error())
  103. return
  104. }
  105. defer func() {
  106. coll.Close(ctx)
  107. }()
  108. result, err := coll.Collection.InsertOne(ctx, dataItem)
  109. if err != nil {
  110. return
  111. }
  112. insertedID = result.InsertedID
  113. return
  114. }
  115. /*
  116. 删除数据
  117. colName := `data_gl`
  118. whereParams := bson.M{"edbcode": "s0033227"}
  119. err := MgoRemove(colName, whereParams)
  120. */
  121. func MgoRemove(colName string, whereParams interface{}) (err error) {
  122. if colName == "" {
  123. err = errors.New("集合名称不可为空")
  124. return
  125. }
  126. ctx := context.TODO()
  127. coll, err := mgodb.MgoGetColl(colName)
  128. if err != nil {
  129. fmt.Println("MgoGetColl Err:", err.Error())
  130. return
  131. }
  132. defer func() {
  133. coll.Close(ctx)
  134. }()
  135. err = coll.Remove(ctx, whereParams)
  136. return
  137. }
  138. /*
  139. 删除多条数据
  140. colName := `data_gl`
  141. whereParams := bson.M{"edbcode": "s0033227"}
  142. err := MgoRemoveAll(colName, whereParams)
  143. */
  144. func MgoRemoveAll(colName string, whereParams interface{}) (count int64, err error) {
  145. if colName == "" {
  146. err = errors.New("集合名称不可为空")
  147. return
  148. }
  149. ctx := context.TODO()
  150. coll, err := mgodb.MgoGetColl(colName)
  151. if err != nil {
  152. fmt.Println("MgoGetColl Err:", err.Error())
  153. return
  154. }
  155. defer func() {
  156. coll.Close(ctx)
  157. }()
  158. res, err := coll.RemoveAll(ctx, whereParams)
  159. if err != nil {
  160. fmt.Println("RemoveAll Err:", err.Error())
  161. return
  162. }
  163. count = res.DeletedCount
  164. return
  165. }
  166. /*
  167. 修改单条数据
  168. colName := `data_ths`
  169. whereParams := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  170. params := bson.M{"$set": bson.M{"value": "1000"}}
  171. */
  172. func MgoUpdateData(colName string, params, whereParams interface{}) (err error) {
  173. if colName == "" {
  174. err = errors.New("集合名称不可为空")
  175. return
  176. }
  177. ctx := context.TODO()
  178. coll, err := mgodb.MgoGetColl(colName)
  179. if err != nil {
  180. fmt.Println("MgoGetColl Err:", err.Error())
  181. return
  182. }
  183. defer func() {
  184. coll.Close(ctx)
  185. }()
  186. err = coll.UpdateOne(ctx, whereParams, params)
  187. return
  188. }
  189. /*
  190. 获取单条数据
  191. colName := `data_ths`
  192. whereParams := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  193. */
  194. func MgoFindOne(colName string, whereParams interface{}) (result interface{}, err error) {
  195. if colName == "" {
  196. err = errors.New("集合名称不可为空")
  197. return
  198. }
  199. ctx := context.TODO()
  200. coll, err := mgodb.MgoGetColl(colName)
  201. if err != nil {
  202. fmt.Println("MgoGetColl Err:", err.Error())
  203. return
  204. }
  205. defer func() {
  206. coll.Close(ctx)
  207. }()
  208. err = coll.Find(ctx, whereParams).One(&result)
  209. return
  210. }
  211. /*
  212. 分页获取数据
  213. colName := `data_ths`
  214. whereParams := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  215. sort:="-weight"
  216. skip:0
  217. limit:10
  218. */
  219. func MgoBatchFind(colName, sort string, whereParams interface{}, skip, limit int64) (list []interface{}, err error) {
  220. if colName == "" {
  221. err = errors.New("集合名称不可为空")
  222. return
  223. }
  224. ctx := context.TODO()
  225. coll, err := mgodb.MgoGetColl(colName)
  226. if err != nil {
  227. fmt.Println("MgoGetColl Err:", err.Error())
  228. return
  229. }
  230. defer func() {
  231. coll.Close(ctx)
  232. }()
  233. err = coll.Find(ctx, whereParams).Sort(sort).Skip(skip).Limit(limit).All(&list)
  234. return
  235. }
  236. /*
  237. MgoUpsertData
  238. 更新插入数据-数据存在即更新, 否则新增
  239. colName := `data_ths`
  240. filter := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  241. replacement := bson.M{"edb_info_id":0,"edb_code":m.EdbCode,"data_time":d.DataTime,"value":f,"create_time":now,"modify_time":now,"data_timestamp": stamp}
  242. */
  243. func MgoUpsertData(colName string, filter, replacement interface{}) (err error) {
  244. if colName == "" {
  245. err = errors.New("集合名称不可为空")
  246. return
  247. }
  248. ctx := context.TODO()
  249. coll, err := mgodb.MgoGetColl(colName)
  250. if err != nil {
  251. fmt.Println("MgoGetColl Err:", err.Error())
  252. return
  253. }
  254. defer func() {
  255. _ = coll.Close(ctx)
  256. }()
  257. _, err = coll.Upsert(ctx, filter, replacement)
  258. return
  259. }