upper_staff.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/database/sql"
  6. "go-common/library/xstr"
  7. )
  8. const (
  9. _staffSQL = "SELECT aid FROM archive_staff WHERE staff_mid = ? AND state = 1"
  10. _staffsSQL = "SELECT aid, staff_mid FROM archive_staff WHERE staff_mid IN (%s) AND state = 1"
  11. _staffAidSQL = "SELECT staff_mid FROM archive_staff WHERE aid = ? AND state = 1"
  12. )
  13. // Staff get upper staff aids by mid.
  14. func (d *Dao) Staff(c context.Context, mid int64) (aids []int64, err error) {
  15. d.infoProm.Incr("Staff")
  16. rows, err := d.archiveDB.Query(c, _staffSQL, mid)
  17. if err != nil {
  18. return
  19. }
  20. defer rows.Close()
  21. for rows.Next() {
  22. var aid int64
  23. if err = rows.Scan(&aid); err != nil {
  24. if err == sql.ErrNoRows {
  25. err = nil
  26. return
  27. }
  28. return
  29. }
  30. aids = append(aids, aid)
  31. }
  32. return
  33. }
  34. // Staffs get uppers staff aids by mids.
  35. func (d *Dao) Staffs(c context.Context, mids []int64) (aidm map[int64][]int64, err error) {
  36. d.infoProm.Incr("Staffs")
  37. rows, err := d.archiveDB.Query(c, fmt.Sprintf(_staffsSQL, xstr.JoinInts(mids)))
  38. if err != nil {
  39. return
  40. }
  41. defer rows.Close()
  42. aidm = make(map[int64][]int64, len(mids))
  43. for rows.Next() {
  44. var (
  45. aid int64
  46. staffMid int64
  47. )
  48. if err = rows.Scan(&aid, &staffMid); err != nil {
  49. if err == sql.ErrNoRows {
  50. err = nil
  51. return
  52. }
  53. return
  54. }
  55. aidm[staffMid] = append(aidm[staffMid], aid)
  56. }
  57. return
  58. }
  59. // StaffAid get uppers staff mid-list by aid.
  60. func (d *Dao) StaffAid(c context.Context, aid int64) (mids []int64, err error) {
  61. d.infoProm.Incr("StaffAid")
  62. rows, err := d.archiveDB.Query(c, _staffAidSQL, aid)
  63. if err != nil {
  64. return
  65. }
  66. defer rows.Close()
  67. for rows.Next() {
  68. var mid int64
  69. if err = rows.Scan(&mid); err != nil {
  70. if err == sql.ErrNoRows {
  71. err = nil
  72. return
  73. }
  74. return
  75. }
  76. mids = append(mids, mid)
  77. }
  78. return
  79. }