skill.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package academy
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/creative/model/academy"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. // select
  12. _getOccSQL = "SELECT id, rank, name, `desc`, main_step, main_software, logo FROM academy_occupation ORDER BY rank ASC"
  13. _getSkillSQL = "SELECT id, oid, name, `desc` FROM academy_skill ORDER BY id ASC"
  14. _getSkillArcSQL = "SELECT id, aid, type, pid, skid, sid FROM academy_arc_skill WHERE state=0"
  15. _getSkillArcCntSQL = "SELECT count(*) FROM academy_arc_skill WHERE state=0"
  16. )
  17. //Occupations get all occupation.
  18. func (d *Dao) Occupations(c context.Context) (res []*academy.Occupation, err error) {
  19. rows, err := d.db.Query(c, _getOccSQL)
  20. if err != nil {
  21. log.Error("Occupations d.db.Query error(%v)", err)
  22. return
  23. }
  24. defer rows.Close()
  25. res = make([]*academy.Occupation, 0)
  26. for rows.Next() {
  27. o := &academy.Occupation{}
  28. if err = rows.Scan(&o.ID, &o.Rank, &o.Name, &o.Desc, &o.MainStep, &o.MainSoftWare, &o.Logo); err != nil {
  29. log.Error("Occupations rows.Scan error(%v)", err)
  30. return
  31. }
  32. res = append(res, o)
  33. }
  34. return
  35. }
  36. //Skills get all Skill.
  37. func (d *Dao) Skills(c context.Context) (res []*academy.Skill, err error) {
  38. rows, err := d.db.Query(c, _getSkillSQL)
  39. if err != nil {
  40. log.Error("Skills d.db.Query error(%v)", err)
  41. return
  42. }
  43. defer rows.Close()
  44. res = make([]*academy.Skill, 0)
  45. for rows.Next() {
  46. o := &academy.Skill{}
  47. if err = rows.Scan(&o.ID, &o.OID, &o.Name, &o.Desc); err != nil {
  48. log.Error("Skills rows.Scan error(%v)", err)
  49. return
  50. }
  51. res = append(res, o)
  52. }
  53. return
  54. }
  55. //SkillArcs get all SkillArc.
  56. func (d *Dao) SkillArcs(c context.Context, pids, skids, sids []int64, offset, limit int) (res []*academy.SkillArc, err error) {
  57. var (
  58. whereStr = _getSkillArcSQL
  59. limiStr = " ORDER BY id ASC LIMIT ?,?"
  60. rows *sql.Rows
  61. )
  62. if len(pids) > 0 {
  63. whereStr += fmt.Sprintf(" AND pid IN (%s)"+limiStr, xstr.JoinInts(pids))
  64. } else if len(skids) > 0 {
  65. whereStr += fmt.Sprintf(" AND skid IN (%s)"+limiStr, xstr.JoinInts(skids))
  66. } else if len(sids) > 0 {
  67. whereStr += fmt.Sprintf(" AND sid IN (%s)"+limiStr, xstr.JoinInts(sids))
  68. } else {
  69. whereStr += limiStr
  70. }
  71. rows, err = d.db.Query(c, whereStr, offset, limit)
  72. if err != nil {
  73. log.Error("SkillArcs d.db.Query error(%v)", err)
  74. return
  75. }
  76. defer rows.Close()
  77. res = make([]*academy.SkillArc, 0)
  78. for rows.Next() {
  79. o := &academy.SkillArc{}
  80. if err = rows.Scan(&o.ID, &o.AID, &o.Type, &o.PID, &o.SkID, &o.SID); err != nil {
  81. log.Error("SkillArcs rows.Scan error(%v)", err)
  82. return
  83. }
  84. res = append(res, o)
  85. }
  86. return
  87. }
  88. //SkillArcCount get all skill achive count.
  89. func (d *Dao) SkillArcCount(c context.Context, pids, skids, sids []int64) (count int, err error) {
  90. var (
  91. whereStr = _getSkillArcCntSQL
  92. )
  93. if len(pids) > 0 {
  94. whereStr += fmt.Sprintf(" AND pid IN (%s)", xstr.JoinInts(pids))
  95. } else if len(skids) > 0 {
  96. whereStr += fmt.Sprintf(" AND skid IN (%s)", xstr.JoinInts(skids))
  97. } else if len(sids) > 0 {
  98. whereStr += fmt.Sprintf(" AND sid IN (%s)", xstr.JoinInts(sids))
  99. }
  100. if err = d.db.QueryRow(c, whereStr).Scan(&count); err != nil {
  101. if err == sql.ErrNoRows {
  102. err = nil
  103. return
  104. }
  105. log.Error("d.db.QueryRow error(%v)", err)
  106. }
  107. return
  108. }
  109. // PlayAdd add play archive.
  110. func (d *Dao) PlayAdd(c context.Context, p *academy.Play) (id int64, err error) {
  111. _inPlaySQL := "INSERT INTO academy_playlist (mid, aid, business, watch, ctime, mtime) VALUES (?, ?, ?, ?, ?,?) ON DUPLICATE KEY UPDATE state=0,watch=?,business=?,mtime=?"
  112. res, err := d.db.Exec(c, _inPlaySQL, p.MID, p.AID, p.Business, p.Watch, p.CTime, p.MTime, p.Watch, p.Business, p.MTime)
  113. if err != nil {
  114. log.Error("PlayAdd d.db.Exec error(%v)", err)
  115. return
  116. }
  117. id, err = res.RowsAffected()
  118. return
  119. }
  120. // PlayDel add play archive.
  121. func (d *Dao) PlayDel(c context.Context, p *academy.Play) (id int64, err error) {
  122. _upPlaySQL := "UPDATE academy_playlist SET state=? WHERE mid=? AND aid=? AND business=?"
  123. res, err := d.db.Exec(c, _upPlaySQL, -1, p.MID, p.AID, p.Business) //-1 删除
  124. if err != nil {
  125. log.Error("PlayDel d.db.Exec error(%v)", err)
  126. return
  127. }
  128. id, err = res.RowsAffected()
  129. return
  130. }
  131. //Plays get all play by mid.
  132. func (d *Dao) Plays(c context.Context, mid int64, offset, limit int) (res []*academy.Play, err error) {
  133. _getPlaySSQL := "SELECT mid, aid, business, watch, ctime, mtime FROM academy_playlist WHERE state=0 AND mid=? ORDER BY mtime DESC LIMIT ?,?"
  134. rows, err := d.db.Query(c, _getPlaySSQL, mid, offset, limit)
  135. if err != nil {
  136. log.Error("Plays d.db.Query error(%v)", err)
  137. return
  138. }
  139. defer rows.Close()
  140. res = make([]*academy.Play, 0)
  141. for rows.Next() {
  142. o := &academy.Play{}
  143. if err = rows.Scan(&o.MID, &o.AID, &o.Business, &o.Watch, &o.CTime, &o.MTime); err != nil {
  144. log.Error("Plays rows.Scan error(%v)", err)
  145. return
  146. }
  147. res = append(res, o)
  148. }
  149. return
  150. }
  151. //PlayCount get all play achive count.
  152. func (d *Dao) PlayCount(c context.Context, mid int64) (count int, err error) {
  153. _getPlayCntSQL := "SELECT count(*) FROM academy_playlist WHERE state=0 AND mid=?"
  154. if err = d.db.QueryRow(c, _getPlayCntSQL, mid).Scan(&count); err != nil {
  155. if err == sql.ErrNoRows {
  156. err = nil
  157. return
  158. }
  159. log.Error("d.db.QueryRow error(%v)", err)
  160. }
  161. return
  162. }
  163. //Play get play achive info.
  164. func (d *Dao) Play(c context.Context, p *academy.Play) (res *academy.Play, err error) {
  165. _getPlaySQL := "SELECT mid, aid, business, watch, ctime, mtime FROM academy_playlist WHERE mid=? AND aid=? AND business=? "
  166. res = &academy.Play{}
  167. if err = d.db.QueryRow(c, _getPlaySQL, p.MID, p.AID, p.Business).Scan(&res.MID, &res.AID, &res.Business, &res.Watch, &res.CTime, &res.MTime); err != nil {
  168. if err == sql.ErrNoRows {
  169. err = nil
  170. return
  171. }
  172. log.Error("d.db.QueryRow error(%v)", err)
  173. }
  174. return
  175. }