user_merchant_h5_add.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package user_merchant
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/storage"
  6. "gd_management/common.in/utils"
  7. "gd_management/errors"
  8. "github.com/astaxie/beego/orm"
  9. "go.uber.org/zap"
  10. "time"
  11. )
  12. func ManagementAddMerchantH5Service(ctx context.Context, req *apis.ManagementAddMerchantH5ServiceReq, reply *apis.ManagementAddMerchantH5ServiceReply) (err error) {
  13. task := func(o orm.Ormer) error {
  14. now := time.Now()
  15. nowString := now.Format("2006-01-02 15:04:05")
  16. nowSecond := now.Unix()
  17. //1 获取套餐详情,判断是否是有效套餐
  18. comTab := apis.TGdH5ServiceCombo{}
  19. err := o.QueryTable("t_gd_h5_service_combo").Filter("id", req.ComboId).One(&comTab)
  20. if err != nil {
  21. if err == orm.ErrNoRows {
  22. return errors.DataComboNotExist
  23. }
  24. return errors.DataBaseError
  25. }
  26. if comTab.Type == 0 {
  27. exist := o.QueryTable("t_gd_h5_service_order").Filter("merchant_id", req.MerchantId).Filter("h5_service_combo_id", req.ComboId).Exist()
  28. if exist {
  29. return errors.OrderFreeExist
  30. }
  31. }
  32. orderTab := apis.TGdH5ServiceOrder{
  33. MerchantId: req.MerchantId,
  34. H5ServiceComboId: req.ComboId,
  35. ThemeColor: req.ThemeColor,
  36. Status: 2,
  37. CreateTime: nowString,
  38. }
  39. _, err = o.Insert(&orderTab)
  40. if err != nil {
  41. return errors.DataBaseError
  42. }
  43. mh5Tab := apis.TGdH5ServiceMerchant{}
  44. err = o.QueryTable("t_gd_h5_service_merchant").Filter("merchant_id", req.MerchantId).Filter("h5_service_id", comTab.H5ServiceId).One(&mh5Tab)
  45. if err != nil {
  46. if err != orm.ErrNoRows {
  47. return errors.DataBaseError
  48. }
  49. }
  50. comboType := 0
  51. if err == nil {
  52. if mh5Tab.EndTime == 0 {
  53. comboType = 1
  54. } else {
  55. comboType = 2
  56. }
  57. if comboType != comTab.Type && mh5Tab.State == 0 {
  58. return errors.CanNotAddOrder
  59. }
  60. //存在相同api 只更新count, day_per_count, start_time, end_time, combo type, state字段
  61. mh5Tab.State = 0
  62. if comTab.Type == 1 {
  63. //套餐已过期 或 以前套餐为按次数,目标套餐为按次数,累加count即可
  64. mh5Tab.Count += int(comTab.Count)
  65. mh5Tab.StartTime = 0
  66. mh5Tab.EndTime = 0
  67. } else if comboType == 1 {
  68. //套餐已过期且之前是按次数,目标套餐为按天,根据当前时间更新时间
  69. mh5Tab.Count = int(comTab.Count)
  70. mh5Tab.StartTime = nowSecond
  71. mh5Tab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
  72. } else {
  73. //套餐未过期且之前是按天,目标套餐为按天,但单天次数不一样,不允许添加
  74. if mh5Tab.State == 0 && int(comTab.Count) != mh5Tab.Count {
  75. return errors.DayComboCountMisMatch
  76. }
  77. mh5Tab.Count = int(comTab.Count)
  78. if mh5Tab.State == 1 {
  79. //套餐已过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据当前时间更新时间
  80. mh5Tab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
  81. } else {
  82. //套餐未过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据以前end时间更新时间
  83. mh5Tab.EndTime += int64(comTab.TotalDay * (24 * 3600))
  84. }
  85. }
  86. _, err := o.Update(&mh5Tab)
  87. if err != nil {
  88. return errors.DataBaseError
  89. }
  90. return nil
  91. }
  92. //插入商户h5 service
  93. mh5Tab = apis.TGdH5ServiceMerchant{}
  94. mh5Tab.MerchantId = req.MerchantId
  95. mh5Tab.H5ServiceId = comTab.H5ServiceId
  96. if comTab.Type == 1 || comTab.Type == 0 {
  97. //按次数
  98. mh5Tab.Count = int(comTab.Count)
  99. mh5Tab.StartTime = 0
  100. mh5Tab.EndTime = 0
  101. }
  102. if comTab.Type == 2 {
  103. mh5Tab.Count = int(comTab.Count)
  104. mh5Tab.StartTime = nowSecond
  105. mh5Tab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
  106. }
  107. mh5Tab.CreateTime = nowString
  108. mh5Id, err := o.Insert(&mh5Tab)
  109. if err != nil {
  110. l.Error("mysql",
  111. zap.String("sql", "insert t_gd_h5_service_merchant"),
  112. zap.String("fields", utils.MarshalJsonString(mh5Tab)),
  113. zap.String("error", err.Error()))
  114. return errors.DataBaseError
  115. }
  116. reply.MerchantH5ServiceId = mh5Id
  117. return nil
  118. }
  119. tasks := []storage.DbaTasker{}
  120. tasks = append(tasks, storage.GenerateDbaTask(task))
  121. storage.ExecTrans(tasks...)
  122. l.Debug(utils.MarshalJsonString(req, reply))
  123. return nil
  124. }