mgo_base.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package models
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta_gn/eta_chart_lib/utils"
  7. "eta_gn/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. }
  21. }
  22. func TestMgoFindOne(colName string, whereParams interface{}) (result interface{}, err error) {
  23. if colName == "" {
  24. err = errors.New("集合名称不可为空")
  25. return
  26. }
  27. if utils.MgoDataCli == nil {
  28. err = errors.New("mongodb连接失败")
  29. return
  30. }
  31. db := utils.MgoDataCli.Database("hz_data")
  32. coll := db.Collection("data_ths")
  33. ctx := context.TODO()
  34. if err != nil {
  35. fmt.Println("MgoGetColl Err:", err.Error())
  36. return
  37. }
  38. err = coll.Find(ctx, whereParams).All(&result)
  39. return
  40. }
  41. func MgoBatchInsertData(colName string, dataList interface{}) (err error) {
  42. if colName == "" {
  43. err = errors.New("集合名称不可为空")
  44. return
  45. }
  46. ctx := context.TODO()
  47. coll, err := mgodb.MgoGetColl(colName)
  48. if err != nil {
  49. fmt.Println("MgoGetColl Err:", err.Error())
  50. return
  51. }
  52. defer func() {
  53. coll.Close(ctx)
  54. }()
  55. _, err = coll.Collection.InsertMany(ctx, dataList)
  56. if err != nil {
  57. fmt.Println("InsertMany:Err:" + err.Error())
  58. return
  59. }
  60. return
  61. }
  62. func MgoInsertData(colName string, dataItem interface{}) (insertedID interface{}, err error) {
  63. if colName == "" {
  64. err = errors.New("集合名称不可为空")
  65. return
  66. }
  67. ctx := context.TODO()
  68. coll, err := mgodb.MgoGetColl(colName)
  69. if err != nil {
  70. fmt.Println("MgoGetColl Err:", err.Error())
  71. return
  72. }
  73. defer func() {
  74. coll.Close(ctx)
  75. }()
  76. result, err := coll.Collection.InsertOne(ctx, dataItem)
  77. if err != nil {
  78. return
  79. }
  80. insertedID = result.InsertedID
  81. return
  82. }
  83. /*
  84. 删除数据
  85. colName := `data_gl`
  86. whereParams := bson.M{"edbcode": "s0033227"}
  87. err := MgoRemove(colName, whereParams)
  88. */
  89. func MgoRemove(colName string, whereParams interface{}) (err error) {
  90. if colName == "" {
  91. err = errors.New("集合名称不可为空")
  92. return
  93. }
  94. ctx := context.TODO()
  95. coll, err := mgodb.MgoGetColl(colName)
  96. if err != nil {
  97. fmt.Println("MgoGetColl Err:", err.Error())
  98. return
  99. }
  100. defer func() {
  101. coll.Close(ctx)
  102. }()
  103. err = coll.Remove(ctx, whereParams)
  104. return
  105. }
  106. /*
  107. 删除多条数据
  108. colName := `data_gl`
  109. whereParams := bson.M{"edbcode": "s0033227"}
  110. err := MgoRemoveAll(colName, whereParams)
  111. */
  112. func MgoRemoveAll(colName string, whereParams interface{}) (count int64, err error) {
  113. if colName == "" {
  114. err = errors.New("集合名称不可为空")
  115. return
  116. }
  117. ctx := context.TODO()
  118. coll, err := mgodb.MgoGetColl(colName)
  119. if err != nil {
  120. fmt.Println("MgoGetColl Err:", err.Error())
  121. return
  122. }
  123. defer func() {
  124. coll.Close(ctx)
  125. }()
  126. res, err := coll.RemoveAll(ctx, whereParams)
  127. if err != nil {
  128. fmt.Println("RemoveAll Err:", err.Error())
  129. return
  130. }
  131. count = res.DeletedCount
  132. return
  133. }
  134. /*
  135. 修改单条数据
  136. colName := `data_ths`
  137. whereParams := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  138. params := bson.M{"$set": bson.M{"value": "1000"}}
  139. */
  140. func MgoUpdateData(colName string, params, whereParams interface{}) (err error) {
  141. if colName == "" {
  142. err = errors.New("集合名称不可为空")
  143. return
  144. }
  145. ctx := context.TODO()
  146. coll, err := mgodb.MgoGetColl(colName)
  147. if err != nil {
  148. fmt.Println("MgoGetColl Err:", err.Error())
  149. return
  150. }
  151. defer func() {
  152. coll.Close(ctx)
  153. }()
  154. err = coll.UpdateOne(ctx, whereParams, params)
  155. return
  156. }
  157. /*
  158. 获取单条数据
  159. colName := `data_ths`
  160. whereParams := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  161. */
  162. func MgoFindOne(colName string, whereParams interface{}) (result interface{}, err error) {
  163. if colName == "" {
  164. err = errors.New("集合名称不可为空")
  165. return
  166. }
  167. ctx := context.TODO()
  168. coll, err := mgodb.MgoGetColl(colName)
  169. if err != nil {
  170. fmt.Println("MgoGetColl Err:", err.Error())
  171. return
  172. }
  173. defer func() {
  174. coll.Close(ctx)
  175. }()
  176. err = coll.Find(ctx, whereParams).One(&result)
  177. return
  178. }
  179. /*
  180. 分页获取数据
  181. colName := `data_ths`
  182. whereParams := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  183. sort:="-weight"
  184. skip:0
  185. limit:10
  186. */
  187. func MgoBatchFind(colName, sort string, whereParams interface{}, skip, limit int64) (list []interface{}, err error) {
  188. if colName == "" {
  189. err = errors.New("集合名称不可为空")
  190. return
  191. }
  192. ctx := context.TODO()
  193. coll, err := mgodb.MgoGetColl(colName)
  194. if err != nil {
  195. fmt.Println("MgoGetColl Err:", err.Error())
  196. return
  197. }
  198. defer func() {
  199. coll.Close(ctx)
  200. }()
  201. err = coll.Find(ctx, whereParams).Sort(sort).Skip(skip).Limit(limit).All(&list)
  202. return
  203. }
  204. /*
  205. MgoUpsertData
  206. 更新插入数据-数据存在即更新, 否则新增
  207. colName := `data_ths`
  208. filter := bson.M{"edbcode": "s0033227", "datatime": "2015-12-04"}
  209. 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}
  210. */
  211. func MgoUpsertData(colName string, filter, replacement interface{}) (err error) {
  212. if colName == "" {
  213. err = errors.New("集合名称不可为空")
  214. return
  215. }
  216. ctx := context.TODO()
  217. coll, err := mgodb.MgoGetColl(colName)
  218. if err != nil {
  219. fmt.Println("MgoGetColl Err:", err.Error())
  220. return
  221. }
  222. defer func() {
  223. _ = coll.Close(ctx)
  224. }()
  225. _, err = coll.Upsert(ctx, filter, replacement)
  226. return
  227. }