user_merchant_data_api_add.go 7.9 KB

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