session.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/feedback/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. "strings"
  11. )
  12. const (
  13. _selSsn = "SELECT id,mid,content,img_url,log_url,state,ctime FROM session WHERE buvid=? AND system=? AND version=? AND mid=?"
  14. _selSsnByMid = "SELECT id,mid,content,img_url,log_url,state,ctime FROM session WHERE mid=? AND platform IN (%s)"
  15. _selSSnCntByMid = `SELECT COUNT(id) AS count FROM session WHERE mid=? AND state IN (0,1,2) AND platform IN ("ugc","article")`
  16. _inSsn = "INSERT INTO session (buvid,system,version,mid,aid,content,img_url,log_url,device,channel,ip,net_state,net_operator,agency_area,platform,browser,qq,email,state,laster_time,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  17. _upSsn = "UPDATE session SET device=?,channel=?,ip=?,net_state=?,net_operator=?,agency_area=?,platform=?,browser=?,qq=?,email=?,state=?,laster_time=?,mtime=? WHERE id=?"
  18. _upSsnMtime = "UPDATE session SET mtime=? WHERE id=?"
  19. _upSsnState = "UPDATE session SET state=? where id=?"
  20. _selTagByPlat = "SELECT id,name,platform,type FROM tag where type=? AND platform IN (%s)"
  21. _selSsnIDbByTagID = "SELECT session_id FROM session_tag WHERE tag_id IN (%s)"
  22. _selSSnBySsnID = "SELECT id,content,ctime,state FROM session WHERE id IN (%s) AND state IN (%s) AND session.ctime>? AND session.ctime<? ORDER BY id DESC"
  23. _selSSnBySsnIDAllSate = "SELECT id,content,ctime,state FROM session WHERE id IN (%s) AND session.ctime>? AND session.ctime<? ORDER BY id DESC"
  24. _selSSnID = "select id from session where id=? limit 1"
  25. )
  26. // JudgeSsnRecord judge session is exist or not .
  27. func (d *Dao) JudgeSsnRecord(c context.Context, sid int64) (cnt int, err error) {
  28. res, err := d.selSSnID.Exec(c, sid)
  29. if err != nil {
  30. log.Error("d.upSsnSta.Exec error(%v)", err)
  31. return
  32. }
  33. res.RowsAffected()
  34. return
  35. }
  36. // Session select feedback session
  37. func (d *Dao) Session(c context.Context, buvid, system, version string, mid int64) (ssn *model.Session, err error) {
  38. row := d.selSsn.QueryRow(c, buvid, system, version, mid)
  39. ssn = &model.Session{}
  40. if err = row.Scan(&ssn.ID, &ssn.Mid, &ssn.Content, &ssn.ImgURL, &ssn.LogURL, &ssn.State, &ssn.CTime); err != nil {
  41. if err == sql.ErrNoRows {
  42. err, ssn = nil, nil
  43. } else {
  44. log.Error("row.Scan() error(%v)", err)
  45. }
  46. }
  47. return
  48. }
  49. //SessionCount session count.
  50. func (d *Dao) SessionCount(c context.Context, mid int64) (cnt int, err error) {
  51. row := d.selSSnCntByMid.QueryRow(c, mid)
  52. if err = row.Scan(&cnt); err != nil {
  53. log.Error("row.Scan error(%v)", err)
  54. }
  55. return
  56. }
  57. // UpSsnMtime up ssn mtime.
  58. func (d *Dao) UpSsnMtime(c context.Context, now time.Time, id int64) (err error) {
  59. _, err = d.upSsnMtime.Exec(c, now, id)
  60. if err != nil {
  61. log.Error("d.upSsnMtime error(%v)", err)
  62. }
  63. return
  64. }
  65. // TxUpSsnMtime up ssn mtime.
  66. func (d *Dao) TxUpSsnMtime(tx *sql.Tx, now time.Time, id int64) (err error) {
  67. _, err = tx.Exec(_upSsnMtime, now, id)
  68. if err != nil {
  69. log.Error("d.upSsnMtime error(%v)", err)
  70. }
  71. return
  72. }
  73. // SessionIDByTagID session find by time state and mid .
  74. func (d *Dao) SessionIDByTagID(c context.Context, tagID []int64) (sid []int64, err error) {
  75. rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSsnIDbByTagID, xstr.JoinInts(tagID)))
  76. if err != nil {
  77. log.Error("d.dbMs.Query error(%v)", err)
  78. return
  79. }
  80. defer rows.Close()
  81. for rows.Next() {
  82. var id int64
  83. if err = rows.Scan(&id); err != nil {
  84. log.Error("row.Scan() error(%v)", err)
  85. return
  86. }
  87. sid = append(sid, id)
  88. }
  89. return
  90. }
  91. // SessionBySsnID get session by ssnID
  92. func (d *Dao) SessionBySsnID(c context.Context, sid []int64, state string, start, end time.Time) (ssns []*model.Session, err error) {
  93. rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSSnBySsnID, xstr.JoinInts(sid), state), start, end)
  94. if err != nil {
  95. log.Error("d.selSSnBySsnID.Query error(%v)", err)
  96. return
  97. }
  98. defer rows.Close()
  99. for rows.Next() {
  100. ssn := &model.Session{}
  101. if err = rows.Scan(&ssn.ID, &ssn.Content, &ssn.CTime, &ssn.State); err != nil {
  102. log.Error("rows.Scan error(%v)", err)
  103. return
  104. }
  105. ssns = append(ssns, ssn)
  106. }
  107. return
  108. }
  109. // SSnBySsnIDAllSate get all SSn by ssnID all sate.
  110. func (d *Dao) SSnBySsnIDAllSate(c context.Context, sid []int64, start, end time.Time) (ssns []*model.Session, err error) {
  111. rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSSnBySsnIDAllSate, xstr.JoinInts(sid)), start, end)
  112. if err != nil {
  113. log.Error("d.selSSnBySsnID.Query error(%v)", err)
  114. return
  115. }
  116. defer rows.Close()
  117. for rows.Next() {
  118. ssn := &model.Session{}
  119. if err = rows.Scan(&ssn.ID, &ssn.Content, &ssn.CTime, &ssn.State); err != nil {
  120. log.Error("rows.Scan error(%v)", err)
  121. return
  122. }
  123. ssns = append(ssns, ssn)
  124. }
  125. return
  126. }
  127. // SessionByMid select feedback session by mid
  128. func (d *Dao) SessionByMid(c context.Context, mid int64, platform string) (ssns []*model.Session, err error) {
  129. rows, err := d.dbMs.Query(c, fmt.Sprintf(_selSsnByMid, platConvert(platform)), mid)
  130. if err != nil {
  131. log.Error("d.dbMs.Query error(%v)", err)
  132. return
  133. }
  134. defer rows.Close()
  135. ssns = []*model.Session{}
  136. for rows.Next() {
  137. ssn := &model.Session{}
  138. if err = rows.Scan(&ssn.ID, &ssn.Mid, &ssn.Content, &ssn.ImgURL, &ssn.LogURL, &ssn.State, &ssn.CTime); err != nil {
  139. log.Error("row.Scan() error(%v)", err)
  140. return
  141. }
  142. ssns = append(ssns, ssn)
  143. }
  144. return
  145. }
  146. // TxUpdateSessionState up date session state.
  147. func (d *Dao) TxUpdateSessionState(tx *sql.Tx, state int, sid int64) (err error) {
  148. res, err := tx.Exec(_upSsnState, state, sid)
  149. if err != nil {
  150. log.Error("d.upSsnSta.Exec error(%v)", err)
  151. return
  152. }
  153. _, err = res.RowsAffected()
  154. return
  155. }
  156. // UpdateSessionState update session state.
  157. func (d *Dao) UpdateSessionState(c context.Context, state int, sid int64) (err error) {
  158. res, err := d.upSsnSta.Exec(c, state, sid)
  159. if err != nil {
  160. log.Error("d.upSsnSta.Exec error(%v)", err)
  161. return
  162. }
  163. _, err = res.RowsAffected()
  164. return
  165. }
  166. // Tags get tags.
  167. func (d *Dao) Tags(c context.Context, mold int, platform string) (tMap map[string][]*model.Tag, err error) {
  168. rows, err := d.dbMs.Query(c, fmt.Sprintf(_selTagByPlat, platConvert(platform)), mold)
  169. if err != nil {
  170. log.Error("d.selTagByPlat.Query error(%v)", err)
  171. return
  172. }
  173. defer rows.Close()
  174. tMap = make(map[string][]*model.Tag)
  175. for rows.Next() {
  176. tag := &model.Tag{}
  177. if err = rows.Scan(&tag.ID, &tag.Name, &tag.Platform, &tag.Type); err != nil {
  178. log.Error("row.Scan() error(%v)", err)
  179. return
  180. }
  181. if tag.Type == 0 {
  182. tMap[tag.Platform] = append(tMap[tag.Platform], tag)
  183. }
  184. }
  185. return
  186. }
  187. // AddSession add feedback session
  188. func (d *Dao) AddSession(c context.Context, s *model.Session) (id int64, err error) {
  189. res, err := d.inSsn.Exec(c, s.Buvid, s.System, s.Version, s.Mid, s.Aid, s.Content, s.ImgURL, s.LogURL, s.Device, s.Channel, s.IP, s.NetState, s.NetOperator, s.AgencyArea, s.Platform, s.Browser, s.QQ, s.Email, s.State, s.LasterTime, s.CTime, s.MTime)
  190. if err != nil {
  191. log.Error("AddSession tx.Exec() error(%v)", err)
  192. return
  193. }
  194. return res.LastInsertId()
  195. }
  196. // TxAddSession add session
  197. func (d *Dao) TxAddSession(tx *sql.Tx, s *model.Session) (id int64, err error) {
  198. res, err := tx.Exec(_inSsn, s.Buvid, s.System, s.Version, s.Mid, s.Aid, s.Content, s.ImgURL, s.LogURL, s.Device, s.Channel, s.IP, s.NetState, s.NetOperator, s.AgencyArea, s.Platform, s.Browser, s.QQ, s.Email, s.State, s.LasterTime, s.CTime, s.MTime)
  199. if err != nil {
  200. log.Error("AddSession tx.Exec() error(%v)", err)
  201. return
  202. }
  203. return res.LastInsertId()
  204. }
  205. // AddSessionTag add feedback session and tag.
  206. func (d *Dao) AddSessionTag(c context.Context, sessionID, tagID int64, now time.Time) (id int64, err error) {
  207. res, err := d.inSsnTag.Exec(c, sessionID, tagID, now)
  208. if err != nil {
  209. log.Error("AddSession tx.Exec() error(%v)", err)
  210. return
  211. }
  212. return res.LastInsertId()
  213. }
  214. // TxAddSessionTag add session tag.
  215. func (d *Dao) TxAddSessionTag(tx *sql.Tx, sessionID, tagID int64, now time.Time) (id int64, err error) {
  216. res, err := tx.Exec(_inSsnTag, sessionID, tagID, now)
  217. if err != nil {
  218. log.Error("TxAddSessionTag tx.Exec() error(%v)", err)
  219. return
  220. }
  221. return res.LastInsertId()
  222. }
  223. // UpdateSession update feedback session
  224. func (d *Dao) UpdateSession(c context.Context, s *model.Session) (affected int64, err error) {
  225. res, err := d.upSsn.Exec(c, s.Device, s.Channel, s.IP, s.NetState, s.NetOperator, s.AgencyArea, s.Platform, s.Browser, s.QQ, s.Email, s.State, s.LasterTime, s.MTime, s.ID)
  226. if err != nil {
  227. log.Error("UpdateSession tx.Exec() error(%v)", err)
  228. return
  229. }
  230. return res.RowsAffected()
  231. }
  232. // TagIDBySid get tagid by sid.
  233. func (d *Dao) TagIDBySid(c context.Context, sids []int64) (tsMap map[int64]int64, err error) {
  234. rows, err := d.dbMs.Query(c, fmt.Sprintf(_selTagID, xstr.JoinInts(sids)))
  235. if err != nil {
  236. log.Error("d.dbMs.Query() error(%v)", err)
  237. return
  238. }
  239. defer rows.Close()
  240. tsMap = make(map[int64]int64)
  241. for rows.Next() {
  242. var (
  243. tid int64
  244. sid int64
  245. )
  246. if err = rows.Scan(&tid, &sid); err != nil {
  247. log.Error("row.Scan() error(%v)", err)
  248. return
  249. }
  250. tsMap[tid] = sid
  251. }
  252. return
  253. }
  254. // platConvert plat covert.
  255. func platConvert(platform string) (s string) {
  256. s = `"` + strings.Replace(platform, ",", `","`, -1) + `"`
  257. return
  258. }