mysql.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/answer/model"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _questionByIDSQL = "SELECT mid,question,ans,status FROM answer_extra_question WHERE id=?"
  10. _insQsSQL = "INSERT INTO answer_extra_question(question,ans,av_id,status,source,state,origin_id) VALUES(?,?,?,?,?,?,?)"
  11. _updateQsSQL = "UPDATE answer_extra_question SET ans=?,status=?,isdel=? WHERE origin_id=?"
  12. _questionExtraTypeSQL = "SELECT origin_id,ans,question,av_id,status,source FROM answer_extra_question WHERE isdel=1 and state=0 LIMIT ?"
  13. _updateStateSQL = "UPDATE answer_extra_question SET state=? WHERE origin_id=?"
  14. )
  15. // ByID get question by id.
  16. func (d *Dao) ByID(c context.Context, id int64) (que *model.LabourQs, err error) {
  17. var row = d.db.QueryRow(c, _questionByIDSQL, id)
  18. que = new(model.LabourQs)
  19. if err = row.Scan(&que.Mid, &que.Question, &que.Ans, &que.Status); err != nil {
  20. if err == sql.ErrNoRows {
  21. que = nil
  22. err = nil
  23. return
  24. }
  25. log.Error("row.Scan() error(%v)", err)
  26. }
  27. return
  28. }
  29. // AddQs add labour question log.
  30. func (d *Dao) AddQs(c context.Context, qs *model.LabourQs) (err error) {
  31. if _, err = d.db.Exec(c, _insQsSQL, qs.Question, qs.Ans, qs.AvID, qs.Status, qs.Source, qs.State, qs.ID); err != nil {
  32. log.Error("AddQs: db.Exec(as:%v) error(%v)", qs, err)
  33. }
  34. return
  35. }
  36. // UpdateQs update question.
  37. func (d *Dao) UpdateQs(c context.Context, que *model.LabourQs) (err error) {
  38. if _, err = d.db.Exec(c, _updateQsSQL, que.Ans, que.Status, que.Isdel, que.ID); err != nil {
  39. log.Error("setQs: db.Exec(%v) error(%v)", que, err)
  40. }
  41. return
  42. }
  43. // QidsExtraByState get extra question ids by check
  44. func (d *Dao) QidsExtraByState(c context.Context, size int) (res []*model.LabourQs, err error) {
  45. var rows *sql.Rows
  46. if rows, err = d.db.Query(c, _questionExtraTypeSQL, size); err != nil {
  47. log.Error("d._questionExtraTypeSQL.Query error(%v)", err)
  48. return
  49. }
  50. defer rows.Close()
  51. for rows.Next() {
  52. r := new(model.LabourQs)
  53. if err = rows.Scan(&r.ID, &r.Ans, &r.Question, &r.AvID, &r.Status, &r.Source); err != nil {
  54. log.Error("row.Scan() error(%v)", err)
  55. res = nil
  56. return
  57. }
  58. res = append(res, r)
  59. }
  60. err = rows.Err()
  61. return
  62. }
  63. // UpdateState update state.
  64. func (d *Dao) UpdateState(c context.Context, que *model.LabourQs) (err error) {
  65. if _, err = d.db.Exec(c, _updateStateSQL, que.State, que.ID); err != nil {
  66. log.Error("UpdateState: db.Exec(%v) error(%v)", que, err)
  67. }
  68. return
  69. }