sqlService.go 1.8 KB

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