flow.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _upFlowSQL = "UPDATE flow_design SET group_id=?,uid=? WHERE id=?"
  12. _inFlowLogSQL = "INSERT into flow_design_log(pool,oid,group_id,uid,action,remark) VALUES (?,?,?,?,?,?)"
  13. _inFlowSQL = "INSERT into flow_design(pool,oid,group_id,uid,remark) VALUES (?,?,?,?,?)"
  14. _flowsSQL = "SELECT id,name FROM flow_group WHERE state=0"
  15. _flowPoolSQL = "SELECT id FROM flow_design WHERE pool=? AND oid=? AND state=0 order by id desc limit 1"
  16. _findGroupIDByScopeSQL = "SELECT group_id FROM flow_scope WHERE pool= ? AND industry_id=? AND brand_id=? AND official=? AND state=0 order by id desc limit 1;"
  17. _upFlowStateSQL = "UPDATE flow_design SET state=? WHERE id=?"
  18. _flowsByOIDSQL = "SELECT fd.id,fd.pool,fd.oid,fd.group_id,fd.parent,fd.state,fg.value FROM flow_design fd LEFT JOIN flow_group fg ON fd.group_id=fg.id WHERE fd.oid=? AND fd.state=0 AND fg.state=0"
  19. _flowUniqueSQL = "SELECT id,pool,oid,group_id,parent,state FROM flow_design WHERE oid=? AND pool=? AND group_id=? LIMIT 1"
  20. _flowGroupPool = "SELECT id, pool FROM flow_group WHERE id IN (%s)"
  21. )
  22. // TxUpFlow tx up flow_design.
  23. func (d *Dao) TxUpFlow(tx *sql.Tx, flowID, groupID, UID int64) (rows int64, err error) {
  24. res, err := tx.Exec(_upFlowSQL, groupID, UID, flowID)
  25. if err != nil {
  26. log.Error("d.TxUpFlow.Exec() error(%v)", err)
  27. return
  28. }
  29. rows, err = res.RowsAffected()
  30. return
  31. }
  32. // TxAddFlow tx add flow_design.
  33. func (d *Dao) TxAddFlow(tx *sql.Tx, pool int8, oid, uid, groupID int64, remark string) (id int64, err error) {
  34. res, err := tx.Exec(_inFlowSQL, pool, oid, groupID, uid, remark)
  35. if err != nil {
  36. log.Error("d.TxAddFlow.Exec() error(%v)", err)
  37. return
  38. }
  39. id, err = res.LastInsertId()
  40. return
  41. }
  42. //FindGroupIDByScope .
  43. func (d *Dao) FindGroupIDByScope(c context.Context, pool int8, IndustryID, brandID int64, official int8) (groupID int64, err error) {
  44. row := d.rddb.QueryRow(c, _findGroupIDByScopeSQL, pool, IndustryID, brandID, official)
  45. if err = row.Scan(&groupID); err != nil {
  46. if err == sql.ErrNoRows {
  47. err = nil
  48. } else {
  49. log.Error("row.Scan error(%v)", err)
  50. }
  51. groupID = 1
  52. log.Info("FindGroupIDByScope match no scope AND hit default scope (%v)", groupID)
  53. }
  54. return
  55. }
  56. // TxAddFlowLog tx add flow_design log.
  57. func (d *Dao) TxAddFlowLog(tx *sql.Tx, pool, action int8, oid, uid, groupID int64, remark string) (id int64, err error) {
  58. res, err := tx.Exec(_inFlowLogSQL, pool, oid, groupID, uid, action, remark)
  59. if err != nil {
  60. log.Error("d._inFlowLog.Exec() error(%v)", err)
  61. return
  62. }
  63. id, err = res.LastInsertId()
  64. return
  65. }
  66. // Flows get flow_control id and remark.
  67. func (d *Dao) Flows(c context.Context) (fs map[int64]string, err error) {
  68. rows, err := d.rddb.Query(c, _flowsSQL)
  69. if err != nil {
  70. log.Error("d.db.Query(%s) error(%v)", err)
  71. return
  72. }
  73. defer rows.Close()
  74. fs = make(map[int64]string)
  75. for rows.Next() {
  76. var (
  77. id int64
  78. name string
  79. )
  80. if err = rows.Scan(&id, &name); err != nil {
  81. log.Error("rows.Scan error(%v)", err)
  82. return
  83. }
  84. fs[id] = name
  85. }
  86. return
  87. }
  88. //FlowByPool .
  89. func (d *Dao) FlowByPool(pool int8, oid int64) (id int64, err error) {
  90. row := d.rddb.QueryRow(context.TODO(), _flowPoolSQL, pool, oid)
  91. if err = row.Scan(&id); err != nil {
  92. if err == sql.ErrNoRows {
  93. err = nil
  94. } else {
  95. log.Error("row.Scan error(%v)", err)
  96. }
  97. }
  98. return
  99. }
  100. //TxUpFlowState 更新pool!=1的流量套餐资源的状态
  101. func (d *Dao) TxUpFlowState(tx *sql.Tx, id int64, state int8) (rows int64, err error) {
  102. res, err := tx.Exec(_upFlowStateSQL, state, id)
  103. if err != nil {
  104. log.Error("TxUpFlowState.Exec() error(%v)", err)
  105. return
  106. }
  107. rows, err = res.RowsAffected()
  108. return
  109. }
  110. //FlowsByOID 获取所有命中的流量套餐记录
  111. func (d *Dao) FlowsByOID(c context.Context, oid int64) (res []*archive.FlowData, err error) {
  112. var (
  113. rows *sql.Rows
  114. )
  115. res = []*archive.FlowData{}
  116. if rows, err = d.rddb.Query(context.TODO(), _flowsByOIDSQL, oid); err != nil {
  117. log.Error("FlowsByOID d.rddb.Query error(%v) oid(%d)", err, oid)
  118. return
  119. }
  120. defer rows.Close()
  121. for rows.Next() {
  122. f := &archive.FlowData{}
  123. if err = rows.Scan(&f.ID, &f.Pool, &f.OID, &f.GroupID, &f.Parent, &f.State, &f.GroupValue); err != nil {
  124. log.Error("FlowsByOID rows.Scan error(%v) oid(%d)", err, oid)
  125. return
  126. }
  127. res = append(res, f)
  128. }
  129. return
  130. }
  131. //FlowUnique 获取命中 指定流量套餐的记录
  132. func (d *Dao) FlowUnique(c context.Context, oid, groupID int64, pool int8) (f *archive.FlowData, err error) {
  133. f = &archive.FlowData{}
  134. if err = d.rddb.QueryRow(context.TODO(), _flowUniqueSQL, oid, pool, groupID).Scan(&f.ID, &f.Pool, &f.OID, &f.GroupID, &f.Parent, &f.State); err != nil {
  135. if err == sql.ErrNoRows {
  136. err = nil
  137. f = nil
  138. } else {
  139. log.Error("row.Scan error(%v)", err)
  140. }
  141. }
  142. return
  143. }
  144. //FlowGroupPools 获取指定流量套餐的pool
  145. func (d *Dao) FlowGroupPools(c context.Context, ids []int64) (res map[int64]int8, err error) {
  146. var (
  147. rows *sql.Rows
  148. id int64
  149. pool int8
  150. )
  151. res = map[int64]int8{}
  152. idstr := xstr.JoinInts(ids)
  153. if rows, err = d.rddb.Query(context.TODO(), fmt.Sprintf(_flowGroupPool, idstr)); err != nil {
  154. log.Error("FlowGroupPools d.rddb.Query error(%v) ids(%s)", err, idstr)
  155. return
  156. }
  157. defer rows.Close()
  158. for rows.Next() {
  159. if err = rows.Scan(&id, &pool); err != nil {
  160. log.Error("FlowGroupPools rows.Scan error(%v) ids(%d)", err, idstr)
  161. return
  162. }
  163. res[id] = pool
  164. }
  165. return
  166. }