bill_confirm.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package accounting
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gd_management/apis"
  9. "gd_management/common.in/config"
  10. "gd_management/common.in/storage"
  11. "gd_management/common.in/utils"
  12. "gd_management/errors"
  13. "gd_management/thirdparty"
  14. "github.com/astaxie/beego/orm"
  15. "github.com/tidwall/gjson"
  16. "go.uber.org/zap"
  17. "time"
  18. )
  19. func RobotMsg(content string) (err error) {
  20. body := map[string]interface{}{
  21. "msgtype": "text",
  22. "text": map[string]interface{}{"content": content},
  23. }
  24. bytes, _ := json.Marshal(body)
  25. h := thirdparty.HttpRequestWithHeadCommon{
  26. Method: "POST",
  27. Url: config.Conf.ThirdPart.RobotUrl,
  28. Body: bytes,
  29. TimeOut: 10 * time.Second,
  30. }
  31. bytes, err = h.Request()
  32. if err != nil {
  33. l.Error("func",
  34. zap.String("call", "RobotMsg"),
  35. zap.String("params", content),
  36. zap.String("error", err.Error()))
  37. return errors.VendorError
  38. }
  39. errcode := gjson.GetBytes(bytes, "errcode").Int()
  40. errmsg := gjson.GetBytes(bytes, "errmsg").String()
  41. if errcode != 0 {
  42. l.Error("func",
  43. zap.String("call", "RobotMsg"),
  44. zap.String("params", content),
  45. zap.String("error", errmsg))
  46. return errors.VendorError
  47. }
  48. return nil
  49. }
  50. func BillConfirm(ctx context.Context, req *apis.BillConfirmReq, reply *apis.BillConfirmReply) error {
  51. // update t_gd_merchants set merchant_type=1 where id=8
  52. if req.Uid == 0 || req.BillId == 0 {
  53. return errors.ArgsError
  54. }
  55. task := func(o orm.Ormer) error {
  56. sql := fmt.Sprintf("update t_gd_bill set bill_status=1,uid=%d where id=%d", req.Uid, req.BillId)
  57. _, err := o.Raw(sql).Exec()
  58. if err != nil {
  59. l.Error("func",
  60. zap.String("call", "BillConfirm"),
  61. zap.String("args", utils.MarshalJsonString(req)),
  62. zap.String("error", err.Error()))
  63. return errors.DataBaseError
  64. }
  65. month := ""
  66. merchantId := int64(0)
  67. sql = "select bill_time,merchant_id from t_gd_bill where id=?"
  68. err = o.Raw(sql, req.BillId).QueryRow(&month, &merchantId)
  69. if err != nil {
  70. l.Error("func",
  71. zap.String("call", "BillConfirm"),
  72. zap.String("args", utils.MarshalJsonString(req)),
  73. zap.String("error", err.Error()))
  74. return errors.DataBaseError
  75. }
  76. merhcnatName := ""
  77. sql = "select merchant_name from t_gd_merchants where id=?"
  78. err = o.Raw(sql, merchantId).QueryRow(&merhcnatName)
  79. if err != nil {
  80. l.Error("func",
  81. zap.String("call", "BillConfirm"),
  82. zap.String("args", utils.MarshalJsonString(req)),
  83. zap.String("error", err.Error()))
  84. return errors.DataBaseError
  85. }
  86. // 发送企业微信消息(注册)
  87. //d := ding.Webhook{AccessToken: config.Conf.ThirdPart.DingDing.AccessToken, Secret: config.Conf.ThirdPart.DingDing.Secret}
  88. msg := fmt.Sprintf("用户(%s)已确认%s(月份)账单,请关注。", merhcnatName, month)
  89. _ = RobotMsg(msg)
  90. //_ = d.SendMessage(msg)
  91. return nil
  92. }
  93. tasks := []storage.DbaTasker{}
  94. tasks = append(tasks, storage.GenerateDbaTask(task))
  95. storage.ExecTrans(tasks...)
  96. //l.Debug(utils.MarshalJsonString(req, reply))
  97. return nil
  98. }