operate.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type Operate struct {
  11. NewId string
  12. OperateType int64
  13. }
  14. func (a *Operate) InfoOperate() bool {
  15. //数据查询
  16. data := BiService.FindOne("customer_data_ttzl", map[string]interface{}{"id_new": a.NewId}, "", "")
  17. if data == nil || len(*data) == 0 {
  18. logx.Info("没有找到该数据", a.NewId)
  19. return false
  20. }
  21. sendParam := map[string]interface{}{
  22. "type": a.OperateType,
  23. }
  24. if a.OperateType == 0 {
  25. //修改
  26. _, ok := CreateEs(*data)
  27. if !ok {
  28. return false
  29. }
  30. sendParam["nid"] = a.NewId
  31. } else {
  32. //清除es数据 清除mysql数据
  33. if !DelById("ttbid", a.NewId) || !BiService.Delete("customer_data_ttzl", map[string]interface{}{"id_new": a.NewId}) {
  34. return false
  35. }
  36. projectId := (*data)["projectId"]
  37. if projectId == "" {
  38. return true
  39. }
  40. sendParam["nid"] = projectId
  41. }
  42. marshalData, _ := json.Marshal(sendParam)
  43. DoPost(UpdataProjectUrl, marshalData)
  44. return true
  45. }
  46. func DoPost(url string, body []byte) ([]byte, error) {
  47. req, err := http.NewRequest("POST", url, bytes.NewReader(body))
  48. if err != nil {
  49. return nil, err
  50. }
  51. req.Header.Add("Content-Type", "application/json;charset=utf-8")
  52. resp, err := http.DefaultClient.Do(req)
  53. if err != nil {
  54. return nil, err
  55. }
  56. bs, err := ioutil.ReadAll(resp.Body)
  57. if err != nil {
  58. return nil, err
  59. }
  60. defer func() {
  61. _ = resp.Body.Close()
  62. }()
  63. logx.Info(url, "调用结果 ", string(bs))
  64. return bs, nil
  65. }