merchant_charge.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "gd_management/apis"
  7. "gd_management/common.in/storage"
  8. "gd_management/common.in/utils"
  9. "gd_management/errors"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/astaxie/beego/orm"
  13. "go.uber.org/zap"
  14. "time"
  15. )
  16. func MerchantCharge(ctx context.Context, req *apis.MerchantChargeReq, reply *apis.MerchantChargeReply) error {
  17. // update t_gd_merchants set balance=balance+1000,bill_balance=bill_balance+1000 where id=9
  18. // insert into 插入账单
  19. if req.MerchantId == 0 {
  20. return errors.ArgsError
  21. }
  22. task := func(o orm.Ormer) error {
  23. sql := fmt.Sprintf("update t_gd_merchants set balance=balance+%f,bill_balance=bill_balance+%f where id=%d", req.Amount, req.Amount, req.MerchantId)
  24. _, err := o.Raw(sql).Exec()
  25. if err != nil {
  26. l.Error("func",
  27. zap.String("call", "MerchantCharge"),
  28. zap.String("args", utils.MarshalJsonString(req)),
  29. zap.String("error", err.Error()))
  30. return errors.DataBaseError
  31. }
  32. balance := float64(0)
  33. err = o.Raw("select balance from t_gd_merchants where id=?", req.MerchantId).QueryRow(&balance)
  34. if err != nil {
  35. l.Error("func",
  36. zap.String("call", "MerchantCharge"),
  37. zap.String("args", utils.MarshalJsonString(req)),
  38. zap.String("error", err.Error()))
  39. return errors.DataBaseError
  40. }
  41. bill := &apis.GdBill{}
  42. bill.MerchantId = req.MerchantId
  43. bill.BillType = 1
  44. bill.UserName = req.UserName
  45. bill.BillTime = time.Now().Format("2006-01")
  46. bill.BillCreateTime = time.Now().Format("2006-01-02")
  47. bill.Amount = req.Amount
  48. tmp := []string{}
  49. tmp = append(tmp, req.Remark)
  50. remarkByte, _ := json.Marshal(tmp)
  51. bill.Remark = string(remarkByte)
  52. bill.Balance = balance
  53. bill.CreatedAt = time.Now().Unix()
  54. _, err = o.Insert(bill)
  55. if err != nil {
  56. l.Error("func",
  57. zap.String("call", "MerchantCharge"),
  58. zap.String("args", utils.MarshalJsonString(req)),
  59. zap.String("error", err.Error()))
  60. return errors.DataBaseError
  61. }
  62. return nil
  63. }
  64. tasks := []storage.DbaTasker{}
  65. tasks = append(tasks, storage.GenerateDbaTask(task))
  66. storage.ExecTrans(tasks...)
  67. l.Debug(utils.MarshalJsonString(req, reply))
  68. return nil
  69. }