sqlService.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/es"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. MC "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  8. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  9. "fmt"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. )
  12. type SqlService struct {
  13. }
  14. func (l SqlService) SqlManage(in *biservice.SqlManageReq) []byte {
  15. sqlData := MC.BiService.FindOne("sql_manage", map[string]interface{}{
  16. "id": common.Int64All(in.Id),
  17. }, "", "")
  18. if sqlData == nil || len(*sqlData) == 0 {
  19. return []byte{}
  20. }
  21. dbType := (*sqlData)["db_type"]
  22. cache_time := common.IntAll((*sqlData)["cache_time"])
  23. key := common.GetMd5String(gconv.String(in))
  24. if cache_time != 0 {
  25. dataStr := redis.Get("newother", key)
  26. if dataStr != nil {
  27. data := gconv.Bytes(dataStr)
  28. return data
  29. }
  30. }
  31. rs := &[]map[string]interface{}{}
  32. count := int64(0)
  33. switch dbType {
  34. case "es":
  35. count, rs = elastic.GetWithCount(common.InterfaceToStr((*sqlData)["db_name"]), common.InterfaceToStr((*sqlData)["db_name"]), "", Sqlformat(common.InterfaceToStr((*sqlData)["content"]), *in))
  36. }
  37. for k, v := range *rs {
  38. (*rs)[k]["id"] = encrypt.EncodeArticleId2ByCheck(common.InterfaceToStr(v["_id"]))
  39. delete((*rs)[k], "_id")
  40. }
  41. result := map[string]interface{}{
  42. "data": rs,
  43. "count": count,
  44. }
  45. resultByte := gconv.Bytes(result)
  46. if count > 0 && cache_time != 0 {
  47. redis.Put("newother", key, result, cache_time)
  48. }
  49. return resultByte
  50. }
  51. func Sqlformat(sqlStr string, sqlData biservice.SqlManageReq) string {
  52. data := []interface{}{}
  53. for _, v := range sqlData.Params {
  54. switch v.Type {
  55. case "string":
  56. data = append(data, v.Value)
  57. case "int":
  58. data = append(data, common.Int64All(v.Value))
  59. }
  60. }
  61. return fmt.Sprintf(sqlStr, data...)
  62. }