staff.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package archive
  2. import (
  3. "context"
  4. "go-common/app/service/main/videoup/model/archive"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _upStateStaffSQL = "UPDATE archive_staff SET state =? where id=?"
  10. _inStaffSQL = "INSERT into archive_staff(aid,mid,staff_mid,staff_title,staff_title_id,state) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE staff_title=?,staff_title_id=?,state=?"
  11. _staffsSQL = "SELECT id,aid,mid,staff_mid,staff_title,staff_title_id,state FROM archive_staff WHERE aid=? AND state=?"
  12. _staffByIDSQL = "SELECT id,aid,mid,staff_mid,staff_title,staff_title_id,state FROM archive_staff WHERE id=?"
  13. _staffByAIdMIDSQL = "SELECT id,aid,mid,staff_mid,staff_title,staff_title_id,state FROM archive_staff WHERE aid=? AND staff_mid=? limit 1"
  14. )
  15. // TxAddStaff tx.
  16. func (d *Dao) TxAddStaff(tx *sql.Tx, param *archive.Staff) (id int64, err error) {
  17. res, err := tx.Exec(_inStaffSQL, param.AID, param.MID, param.StaffMID, param.StaffTitle, param.StaffTitleID, param.State, param.StaffTitle, param.StaffTitleID, param.State)
  18. if err != nil {
  19. log.Error("d.TxAddStaff.Exec() error(%v)", err)
  20. return
  21. }
  22. id, err = res.LastInsertId()
  23. return
  24. }
  25. // TxUpStaffState tx .
  26. func (d *Dao) TxUpStaffState(tx *sql.Tx, state int8, id int64) (rows int64, err error) {
  27. res, err := tx.Exec(_upStateStaffSQL, state, id)
  28. if err != nil {
  29. log.Error("d.TxUpStaffState.Exec() error(%v)", err)
  30. return
  31. }
  32. rows, err = res.RowsAffected()
  33. return
  34. }
  35. // Staffs get .
  36. func (d *Dao) Staffs(c context.Context, AID int64) (fs []*archive.Staff, err error) {
  37. rows, err := d.db.Query(c, _staffsSQL, AID, archive.STATEON)
  38. if err != nil {
  39. log.Error("d.db.Staffs aid(%d) error(%v)", AID, err)
  40. return
  41. }
  42. defer rows.Close()
  43. for rows.Next() {
  44. f := &archive.Staff{}
  45. if err = rows.Scan(&f.ID, &f.AID, &f.MID, &f.StaffMID, &f.StaffTitle, &f.StaffTitleID, &f.State); err != nil {
  46. log.Error("rows.Scan error(%v)", err)
  47. return
  48. }
  49. fs = append(fs, f)
  50. }
  51. return
  52. }
  53. // Staff get .
  54. func (d *Dao) Staff(c context.Context, ID int64) (s *archive.Staff, err error) {
  55. row := d.db.QueryRow(c, _staffByIDSQL, ID)
  56. s = &archive.Staff{}
  57. if err = row.Scan(&s.ID, &s.AID, &s.MID, &s.StaffMID, &s.StaffTitle, &s.StaffTitleID, &s.State); err != nil {
  58. if err == sql.ErrNoRows {
  59. s = nil
  60. err = nil
  61. } else {
  62. log.Error("row.Scan error(%v)", err)
  63. }
  64. return
  65. }
  66. return
  67. }
  68. // StaffByAidAndMid get .
  69. func (d *Dao) StaffByAidAndMid(c context.Context, AID, StaffMid int64) (s *archive.Staff, err error) {
  70. row := d.db.QueryRow(c, _staffByAIdMIDSQL, AID, StaffMid)
  71. s = &archive.Staff{}
  72. if err = row.Scan(&s.ID, &s.AID, &s.MID, &s.StaffMID, &s.StaffTitle, &s.StaffTitleID, &s.State); err != nil {
  73. if err == sql.ErrNoRows {
  74. s = nil
  75. err = nil
  76. } else {
  77. log.Error("row.Scan error(%v)", err)
  78. }
  79. return
  80. }
  81. return
  82. }