sqlService.go 1.7 KB

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