data_api_set_data_combo_free.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package data_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/storage"
  6. "gd_management/errors"
  7. "github.com/astaxie/beego/orm"
  8. "time"
  9. )
  10. func DataApiSetFreeDataCombo(ctx context.Context, req *apis.ManagementDataApiSetFreeDataComboReq, reply *apis.ManagementDataApiSetFreeDataComboReply) error {
  11. if req.QueryTypeId == 0 {
  12. return errors.ArgsError
  13. }
  14. task := func(o orm.Ormer) error {
  15. _, err := o.Raw("update t_gd_data_api_query_type set is_free=?,unit_price=? where id=?", req.DataApiQueryTypeIsFree, req.UnitPrice, req.QueryTypeId).Exec()
  16. if err != nil {
  17. return errors.DataBaseError
  18. }
  19. // TODO 同步商户单价数据(购买后同步)
  20. // update t_gd_merchant_data_api set unit_price=1 where unit_price=0
  21. if req.DataApiQueryTypeIsFree == false {
  22. var dataCombo apis.DataCombo
  23. err := o.QueryTable("t_gd_data_api_combo").Filter("query_type_id", req.QueryTypeId).Filter("type", 0).One(&dataCombo)
  24. if err != nil && err != orm.ErrNoRows {
  25. return errors.DataBaseError
  26. }
  27. var timeLayout = "2006-01-02 15:04:05"
  28. timeNow := time.Now().Format(timeLayout)
  29. dataCombo.QueryTypeId = req.QueryTypeId
  30. dataCombo.Count = req.Count
  31. dataCombo.UpdateTime = timeNow
  32. //dataCombo.IsFree = true
  33. dataCombo.Type = 0 //免费套餐
  34. if err == nil {
  35. _, err := o.Update(&dataCombo)
  36. if err != nil {
  37. return errors.DataBaseError
  38. }
  39. } else if err != nil && err == orm.ErrNoRows {
  40. dataCombo.CreateTime = timeNow
  41. _, err := o.Insert(&dataCombo)
  42. if err != nil {
  43. return errors.DataBaseError
  44. }
  45. }
  46. }
  47. return nil
  48. }
  49. tasks := []storage.DbaTasker{}
  50. tasks = append(tasks, storage.GenerateDbaTask(task))
  51. storage.ExecTrans(tasks...)
  52. return nil
  53. }