order_pay.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. "gd_management/impl/base_api"
  9. "fmt"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. "time"
  13. )
  14. func ManagementPayOrder(ctx context.Context, req *apis.ManagementPayOrderReq, reply *apis.ManagementPayOrderReply) (err error) {
  15. task := func(o orm.Ormer) error {
  16. var status int
  17. now := time.Now()
  18. nowString := now.Format("2006-01-02 15:04:05")
  19. nowSecond := now.Unix()
  20. err := o.Raw("select status from t_gd_data_api_order where id=?", req.OrderId).QueryRow(&status)
  21. if err != nil {
  22. return errors.DataBaseError
  23. }
  24. if status == 2 {
  25. return errors.OrderPayed
  26. }
  27. //获取套餐信息
  28. var merchantId, comboId int64
  29. err = o.Raw("select merchant_id, combo_id from t_gd_data_api_order where id=?", req.OrderId).QueryRow(&merchantId, &comboId)
  30. if err != nil {
  31. l.Error("mysql",
  32. zap.String("sql", "select merchant_id, combo_id from t_data_api_order where id=?"),
  33. zap.String("fields", fmt.Sprintf("id:%d", req.OrderId)),
  34. zap.String("error", err.Error()))
  35. if err == orm.ErrNoRows {
  36. return errors.DataBaseNoRecord
  37. }
  38. return errors.DataBaseError
  39. }
  40. comTab := apis.DataCombo{}
  41. err = o.QueryTable("t_gd_data_api_combo").Filter("id", comboId).One(&comTab)
  42. if err != nil {
  43. l.Error("mysql",
  44. zap.String("sql", "select from t_gd_data_api_combo"),
  45. zap.String("fields", fmt.Sprintf("id:%d", comboId)),
  46. zap.String("error", err.Error()))
  47. if err == orm.ErrNoRows {
  48. return errors.DataBaseNoRecord
  49. }
  50. return errors.DataBaseError
  51. }
  52. //插入商户数据api
  53. mdaTab := apis.TGdMerchantDataApi{}
  54. err = o.QueryTable("t_gd_merchant_data_api").Filter("merchant_id", merchantId).Filter("query_type_id", comTab.QueryTypeId).One(&mdaTab)
  55. if err != nil {
  56. if err != orm.ErrNoRows {
  57. return errors.DataBaseError
  58. }
  59. }
  60. if err == nil {
  61. if mdaTab.ComboType == 0 {
  62. if mdaTab.EndTime == 0 {
  63. mdaTab.ComboType = 1
  64. } else {
  65. mdaTab.ComboType = 2
  66. }
  67. }
  68. if mdaTab.ComboType != comTab.Type && mdaTab.State == 0 {
  69. return errors.CanNotAddOrder
  70. }
  71. //存在相同api 只更新count, day_per_count, start_time, end_time, combo type, state字段
  72. mdaTab.State = 0
  73. mdaTab.UpdateTime = nowString
  74. if comTab.Type == 1 {
  75. //套餐已过期 或 以前套餐为按次数,目标套餐为按次数,累加count即可
  76. mdaTab.Count += int(comTab.Count)
  77. mdaTab.StartTime = 0
  78. mdaTab.EndTime = 0
  79. mdaTab.CountPerDay = 0
  80. } else if mdaTab.ComboType == 1 {
  81. //套餐已过期且之前是按次数,目标套餐为按天,根据当前时间更新时间
  82. mdaTab.Count = 0
  83. mdaTab.CountPerDay = int(comTab.Count)
  84. mdaTab.StartTime = nowSecond
  85. mdaTab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
  86. } else {
  87. //套餐未过期且之前是按天,目标套餐为按天,但单天次数不一样,不允许添加
  88. if mdaTab.State == 0 && int(comTab.Count) != mdaTab.CountPerDay {
  89. return errors.DayComboCountMisMatch
  90. }
  91. mdaTab.Count = 0
  92. mdaTab.CountPerDay = int(comTab.Count)
  93. if mdaTab.State == 1 {
  94. //套餐已过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据当前时间更新时间
  95. mdaTab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
  96. } else {
  97. //套餐未过期且之前是按天,目标套餐为按天,且前后单天次数一样, 根据以前end时间更新时间
  98. mdaTab.EndTime += int64(comTab.TotalDay * (24 * 3600))
  99. }
  100. }
  101. mdaTab.ComboType = comTab.Type
  102. _, err := o.Update(&mdaTab)
  103. if err != nil {
  104. return errors.DataBaseError
  105. }
  106. _, err = o.Raw("update t_gd_data_api_order set status=? where id=?", 2, req.OrderId).Exec()
  107. if err != nil {
  108. return errors.DataBaseError
  109. }
  110. return nil
  111. }
  112. mdaTab = apis.TGdMerchantDataApi{}
  113. mdaTab.ComboType = comTab.Type
  114. mdaTab.MerchantId = merchantId
  115. mdaTab.QueryTypeId = comTab.QueryTypeId
  116. if comTab.Type == 1 {
  117. //按次数
  118. mdaTab.Count = int(comTab.Count)
  119. mdaTab.StartTime = 0
  120. mdaTab.EndTime = 0
  121. }
  122. if comTab.Type == 2 {
  123. mdaTab.CountPerDay = int(comTab.Count)
  124. mdaTab.StartTime = nowSecond
  125. mdaTab.EndTime = nowSecond + int64(comTab.TotalDay*(24*3600))
  126. }
  127. mdaTab.CreateTime = nowString
  128. mdaTab.UpdateTime = nowString
  129. sql := "select unit_price from t_gd_data_api_query_type where id=?"
  130. err = o.Raw(sql, comTab.QueryTypeId).QueryRow(&mdaTab.UnitPrice)
  131. if err != nil {
  132. l.Error("mysql",
  133. zap.String("sql", sql),
  134. zap.String("fields", fmt.Sprintf("id:%d", comTab.QueryTypeId)),
  135. zap.String("error", err.Error()))
  136. return errors.DataBaseError
  137. }
  138. mdaId, err := o.Insert(&mdaTab)
  139. if err != nil {
  140. l.Error("mysql",
  141. zap.String("sql", "insert t_gd_merchant_data_api"),
  142. zap.String("fields", utils.MarshalJsonString(mdaTab)),
  143. zap.String("error", err.Error()))
  144. return errors.DataBaseError
  145. }
  146. //插入商户基础api
  147. //mcdaTabs := []apis.TGdMerchantChildDataApi{}
  148. cdaTabs := []apis.ManagementDataApiBaseApi{}
  149. _, err = o.QueryTable("t_gd_child_data_api").Filter("query_type_id", comTab.QueryTypeId).All(&cdaTabs)
  150. if err != nil {
  151. l.Error("mysql",
  152. zap.String("sql", "select t_gd_child_data_api"),
  153. zap.String("fields", fmt.Sprintf("query_type_id:%d", comTab.QueryTypeId)),
  154. zap.String("error", err.Error()))
  155. return errors.DataBaseError
  156. }
  157. for _, v := range cdaTabs {
  158. mcdaTab := apis.TGdMerchantChildDataApi{}
  159. mcdaTab.MerchantDataApiId = int64(mdaId)
  160. mcdaTab.ApiId = v.ApiId
  161. mcdaTab.SaleName = v.SaleName
  162. mcdaTab.CountType = v.CountType
  163. mcdaTab.CountCode = v.CountCode
  164. mcdaTab.RequestParam = v.RequestParam
  165. mcdaTab.ResponseParam = v.ResponseParam
  166. //mcdaTab.ProviderApis = v.ProviderApis
  167. mcdaTab.ForceUpdate = v.ForceUpdate
  168. mcdaTab.IsCrypto = v.IsCrypto
  169. mcdaTab.CreateTime = nowString
  170. mcdaTab.UpdateTime = nowString
  171. mcdaTab.IsEnable = true //TODO 从内存中读取
  172. mcdaTab.ReuseTime = v.ReuseTime
  173. //mcdaTabs = append(mcdaTabs, mcdaTab)
  174. id, err := o.Insert(&mcdaTab)
  175. if err != nil {
  176. return errors.DataBaseError
  177. }
  178. mReq := apis.ManagementGetBaseApiProviderListReq{ApiId: v.ApiId}
  179. mReply := apis.ManagementGetBaseApiProviderListReply{}
  180. base_api.ManagementGetBaseApiProviderList(ctx, &mReq, &mReply)
  181. merchantProviderRelatons := []apis.MerchantApiProviderRelation{}
  182. for _, group := range mReply.ProviderGroupList {
  183. for _, providerApi := range group.ProviderApiList {
  184. merchantProviderRelaton := apis.MerchantApiProviderRelation{}
  185. merchantProviderRelaton.ProviderApiId = providerApi.ProviderApiId
  186. merchantProviderRelaton.MerchantChildApiId = id
  187. merchantProviderRelaton.Enable = true
  188. merchantProviderRelaton.GroupNo = group.GroupNo
  189. merchantProviderRelatons = append(merchantProviderRelatons, merchantProviderRelaton)
  190. }
  191. }
  192. if len(merchantProviderRelatons) > 0 {
  193. _, err = o.InsertMulti(len(merchantProviderRelatons), &merchantProviderRelatons)
  194. if err != nil {
  195. return errors.DataBaseError
  196. }
  197. }
  198. }
  199. /*_, err = o.InsertMulti(len(mcdaTabs), &mcdaTabs)
  200. if err != nil {
  201. l.Error("mysql",
  202. zap.String("sql", "insert t_gd_merchant_child_data_api"),
  203. zap.String("fields", utils.MarshalJsonString(mcdaTabs)),
  204. zap.String("error", err.Error()))
  205. return errors.DataBaseError
  206. }*/
  207. _, err = o.Raw("update t_gd_data_api_order set status=? where id=?", 2, req.OrderId).Exec()
  208. if err != nil {
  209. return errors.DataBaseError
  210. }
  211. return nil
  212. }
  213. tasks := []storage.DbaTasker{}
  214. tasks = append(tasks, storage.GenerateDbaTask(task))
  215. storage.ExecTrans(tasks...)
  216. l.Debug(utils.MarshalJsonString(req, reply))
  217. return nil
  218. }