data_api_add_data_combo.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package data_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "time"
  7. "gd_management/common.in/storage"
  8. "github.com/astaxie/beego/orm"
  9. )
  10. func DataApiAddDataCombo(ctx context.Context, req *apis.ManagementDataApiAddDataComboReq, reply *apis.ManagementDataApiAddDataComboReply) error {
  11. if len(req.ManagementAddDataComboList) == 0 {
  12. return errors.ArgsError
  13. }
  14. task := func(o orm.Ormer) error {
  15. //deleted := false
  16. for index, _ := range req.ManagementAddDataComboList {
  17. if req.ManagementAddDataComboList[index].IsDelete {
  18. if req.ManagementAddDataComboList[index].ComboId > 0 {
  19. _, err := o.Delete(&apis.DataCombo{Id: req.ManagementAddDataComboList[index].ComboId})
  20. if err != nil {
  21. return errors.DataBaseError
  22. }
  23. }
  24. }
  25. if req.ManagementAddDataComboList[index].QueryTypeId == 0 {
  26. return errors.ArgsError
  27. }
  28. if req.ManagementAddDataComboList[index].Price == 0 {
  29. return errors.ComboCostIsZero
  30. }
  31. if req.ManagementAddDataComboList[index].Type != 1 && req.ManagementAddDataComboList[index].Type != 2 {
  32. return errors.ComboTypeNotSupport
  33. }
  34. /*if deleted == false {
  35. deleted = true
  36. _, err := o.Raw("delete from t_gd_data_api_combo where query_type_id=? and type=?", req.ManagementAddDataComboList[index].QueryTypeId, req.ManagementAddDataComboList[index].Type).Exec()
  37. if err != nil {
  38. return errors.DataBaseError
  39. }
  40. }*/
  41. var timeLayout = "2006-01-02 15:04:05"
  42. timeNow := time.Now().Format(timeLayout)
  43. var dataCombo apis.DataCombo
  44. dataCombo.QueryTypeId = req.ManagementAddDataComboList[index].QueryTypeId
  45. dataCombo.Type = req.ManagementAddDataComboList[index].Type
  46. dataCombo.Price = req.ManagementAddDataComboList[index].Price
  47. dataCombo.Count = req.ManagementAddDataComboList[index].Count
  48. if req.ManagementAddDataComboList[index].Type == 2 {
  49. dataCombo.TotalDay = req.ManagementAddDataComboList[index].TotalDay
  50. }
  51. dataCombo.Id = req.ManagementAddDataComboList[index].ComboId
  52. dataCombo.UpdateTime = timeNow
  53. dataCombo.CreateTime = timeNow
  54. //ataCombo.IsFree = false
  55. var err error
  56. if dataCombo.Id == 0 {
  57. _, err = o.Insert(&dataCombo)
  58. } else {
  59. _, err = o.Update(&dataCombo)
  60. }
  61. //_, err = o.Insert(&dataCombo)
  62. //_, err = o.Update(&dataCombo)
  63. if err != nil {
  64. return errors.DataBaseError
  65. }
  66. }
  67. return nil
  68. }
  69. tasks := []storage.DbaTasker{}
  70. tasks = append(tasks, storage.GenerateDbaTask(task))
  71. storage.ExecTrans(tasks...)
  72. //reply.ComboId = id
  73. return nil
  74. }