蛋蛋星球RabbitMq消费项目
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

255 rader
8.2 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. utils2 "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/consume/md"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  12. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  14. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  15. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg"
  16. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  17. "context"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "github.com/go-pay/gopay"
  22. "github.com/go-pay/gopay/alipay"
  23. "github.com/go-pay/xlog"
  24. "github.com/streadway/amqp"
  25. "time"
  26. )
  27. func EggFinWithdrawApplyDataConsume(queue md.MqQueue) {
  28. fmt.Println(">>>>>>>>>>>>EggFinWithdrawApplyDataConsume>>>>>>>>>>>>")
  29. ch, err := rabbit.Cfg.Pool.GetChannel()
  30. if err != nil {
  31. logx.Error(err)
  32. return
  33. }
  34. defer ch.Release()
  35. //1、将自己绑定到交换机上
  36. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  37. //2、取出数据进行消费
  38. ch.Qos(1)
  39. delivery := ch.Consume(queue.Name, false)
  40. egg_system_rules.Init(cfg.RedisAddr)
  41. var alipayStruct *InitAlipayStruct
  42. client, err := InitAlipay(alipayStruct)
  43. if err != nil {
  44. fmt.Println("EggFinWithdrawApplyDataConsume:::::", err.Error())
  45. utils2.FilePutContents("EggFinWithdrawApplyDataConsume", utils2.SerializeStr(map[string]interface{}{
  46. "err": err.Error(),
  47. }))
  48. return
  49. }
  50. var res amqp.Delivery
  51. var ok bool
  52. for {
  53. res, ok = <-delivery
  54. if ok == true {
  55. err = handleEggFinWithdrawApplyDataConsume(res.Body, client, ch)
  56. if err != nil {
  57. fmt.Println("EggFinWithdrawApplyDataConsume:::::", err.Error())
  58. utils2.FilePutContents("EggFinWithdrawApplyDataConsume", utils2.SerializeStr(map[string]interface{}{
  59. "body": res.Body,
  60. "err": err.Error(),
  61. }))
  62. //// 如果需要重试 推回队尾
  63. //if err.Error() == "Lock wait timeout exceeded; try restarting transaction" {
  64. // var msg *md.CommUserId
  65. // json.Unmarshal(res.Body, &msg)
  66. // ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  67. //}
  68. var data md2.EggFinWithdrawApplyErrorData
  69. data.ErrorInfo = err.Error()
  70. // 尝试解析数据结构体
  71. var msg *md2.EggFinWithdrawApplyData
  72. err1 := json.Unmarshal(res.Body, &msg)
  73. if err1 != nil {
  74. // 无法解析 直接将[]byte推进队列
  75. data.Ext = res.Body
  76. } else {
  77. // 解析成功
  78. data.EggFinWithdrawApplyData = *msg
  79. }
  80. ch.Publish(md2.EggAppExchange, data, md2.EggFinWithdrawApplyError)
  81. }
  82. //_ = res.Reject(false)
  83. err = res.Ack(true)
  84. fmt.Println("err ::: ", err)
  85. } else {
  86. panic(errors.New("error getting message"))
  87. }
  88. }
  89. }
  90. func handleEggFinWithdrawApplyDataConsume(msgData []byte, client *alipay.Client, ch *rabbit.Channel) error {
  91. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  92. // 1.解析mq中queue的数据结构体
  93. var msg *md2.EggFinWithdrawApplyData
  94. err := json.Unmarshal(msgData, &msg)
  95. if err != nil {
  96. return err
  97. }
  98. applyDb := implement.NewFinWithdrawApplyDb(db.Db)
  99. apply, err := applyDb.FinWithdrawApplyGet(msg.Id)
  100. if err != nil {
  101. return err
  102. }
  103. if apply == nil {
  104. return errors.New("提现记录不存在~")
  105. }
  106. // 如果不在队列处理阶段或不是自动处理订单,直接返回
  107. if apply.State != 4 && apply.Type != 2 {
  108. return errors.New("提现记录无需处理~")
  109. }
  110. // 2.3 更新 apply 信息
  111. apply.State = 2
  112. affected, err2 := applyDb.UpdateFinWithdrawApply(apply, "state")
  113. if err2 != nil {
  114. return err2
  115. }
  116. if affected == 0 {
  117. return errors.New("更新 apply 状态失败")
  118. }
  119. if msg.WithdrawKind == 1 {
  120. // 3.支付宝提现
  121. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  122. userInfo, err1 := alipayUserInfoDb.GetAlipayUserInfo(msg.Uid)
  123. if err != nil {
  124. return err1
  125. }
  126. bm := make(gopay.BodyMap)
  127. bm.Set("out_biz_no", msg.Id).
  128. Set("trans_amount", msg.Amount).
  129. Set("biz_scene", "DIRECT_TRANSFER").
  130. Set("product_code", "TRANS_ACCOUNT_NO_PWD").
  131. SetBodyMap("payee_info", func(bm gopay.BodyMap) {
  132. bm.Set("identity", userInfo.OpenId)
  133. bm.Set("identity_type", "ALIPAY_OPEN_ID")
  134. })
  135. _, err = client.FundTransUniTransfer(context.Background(), bm)
  136. if err != nil {
  137. //TODO::处理提现失败
  138. err11 := dealFinWithdrawApplyStateForBad(apply)
  139. if err11 != nil {
  140. return errors.New("处理提现失败:" + err11.Error())
  141. }
  142. return err
  143. }
  144. } else if msg.WithdrawKind == 2 {
  145. }
  146. return nil
  147. }
  148. func dealFinWithdrawApplyStateForBad(finWithdrawApply *model.FinWithdrawApply) error {
  149. session := db.Db.NewSession()
  150. defer session.Close()
  151. session.Begin()
  152. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  153. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForBad)
  154. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApplyBySession(session, finWithdrawApply, "state")
  155. if err1 != nil {
  156. return err1
  157. }
  158. if updateAffected <= 0 {
  159. return errors.New("更新提现单状态失败")
  160. }
  161. dealUserWalletReq := md3.DealUserWalletReq{
  162. Direction: "add",
  163. Kind: int(enum.UserWithdrawBad),
  164. Title: enum.UserWithdrawBad.String(),
  165. Uid: finWithdrawApply.Uid,
  166. Amount: utils2.StrToFloat64(finWithdrawApply.Amount),
  167. }
  168. err := rule.DealUserWallet(session, dealUserWalletReq)
  169. if err != nil {
  170. session.Rollback()
  171. return err
  172. }
  173. err = session.Commit()
  174. if err != nil {
  175. _ = session.Rollback()
  176. }
  177. return err
  178. }
  179. type InitAlipayStruct struct {
  180. IsProd bool `json:"is_prod" label:"是否生产环境"`
  181. AlipayAppId string `json:"alipay_app_id" label:"支付宝商家应用appid"`
  182. AlipayPrivateKey string `json:"alipay_private_key" label:"支付宝商家应用私钥"`
  183. AlipayPublicKey string `json:"alipay_public_key" label:"支付宝商家应用公钥"`
  184. AlipayPublicContentRSA2 []byte `json:"alipay_public_content_rsa_2" label:"支付宝公钥证书"`
  185. AlipayRootContent []byte `json:"alipay_root_content" label:"支付宝根证书"`
  186. AppPublicContent []byte `json:"app_public_content" label:"应用公钥证书"`
  187. }
  188. // InitAlipay 初始化支付宝客户端
  189. // appid:应用ID
  190. // privateKey:应用私钥,支持PKCS1和PKCS8
  191. // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。
  192. func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) {
  193. if initData == nil {
  194. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  195. sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent)
  196. initData = &InitAlipayStruct{
  197. IsProd: true,
  198. AlipayAppId: sysCfgMap[enum.AlipayAppId],
  199. AlipayPrivateKey: sysCfgMap[enum.AlipayPrivateKey],
  200. AlipayPublicKey: sysCfgMap[enum.AlipayPublicKey],
  201. AlipayPublicContentRSA2: []byte(sysCfgMap[enum.AlipayPublicContentRSA2]),
  202. AlipayRootContent: []byte(sysCfgMap[enum.AlipayRootContent]),
  203. AppPublicContent: []byte(sysCfgMap[enum.AppPublicContent]),
  204. }
  205. }
  206. client, err = alipay.NewClient(initData.AlipayAppId, initData.AlipayPrivateKey, initData.IsProd)
  207. if err != nil {
  208. xlog.Error(err)
  209. return
  210. }
  211. // 自定义配置http请求接收返回结果body大小,默认 10MB
  212. client.SetBodySize(10) // 没有特殊需求,可忽略此配置
  213. // 打开Debug开关,输出日志,默认关闭
  214. client.DebugSwitch = gopay.DebugOn
  215. client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间
  216. SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
  217. SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2
  218. // SetAppAuthToken("") //授权token
  219. //SetReturnUrl("https://www.fmm.ink"). // 设置返回URL
  220. // SetNotifyUrl("https://www.fmm.ink"). // 设置异步通知URL
  221. // 传入 支付宝公钥证书 alipayPublicCert.crt 内容
  222. client.AutoVerifySign(initData.AlipayPublicContentRSA2)
  223. // 传入证书内容
  224. err = client.SetCertSnByContent(initData.AppPublicContent, initData.AlipayRootContent, initData.AlipayPublicContentRSA2)
  225. if err != nil {
  226. xlog.Debug("SetCertSn:", err)
  227. return
  228. }
  229. return
  230. }