dao.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package tab
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-card/model/card/operate"
  5. "go-common/app/interface/main/app-feed/conf"
  6. "go-common/library/database/sql"
  7. )
  8. const (
  9. _getAllMenuSQL = "SELECT id,plat,name,ctype,cvalue,plat_ver,stime,etime,status,color,badge FROM app_menus ORDER BY `order`"
  10. _getAllActiveSQL = "SELECT id,parent_id,name,background,type,content FROM app_active"
  11. )
  12. type Dao struct {
  13. db *sql.DB
  14. menuGet *sql.Stmt
  15. activeGet *sql.Stmt
  16. }
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. db: sql.NewMySQL(c.MySQL.Show),
  20. }
  21. // prepare
  22. d.menuGet = d.db.Prepared(_getAllMenuSQL)
  23. d.activeGet = d.db.Prepared(_getAllActiveSQL)
  24. return
  25. }
  26. func (d *Dao) Menus(c context.Context) (menus []*operate.Menu, err error) {
  27. var rows *sql.Rows
  28. if rows, err = d.menuGet.Query(c); err != nil {
  29. return
  30. }
  31. defer rows.Close()
  32. for rows.Next() {
  33. m := &operate.Menu{}
  34. if err = rows.Scan(&m.TabID, &m.Plat, &m.Name, &m.CType, &m.CValue, &m.PlatVersion, &m.STime, &m.ETime, &m.Status, &m.Color, &m.Badge); err != nil {
  35. return
  36. }
  37. if m.CValue != "" {
  38. m.Change()
  39. menus = append(menus, m)
  40. }
  41. }
  42. return
  43. }
  44. func (d *Dao) Actives(c context.Context) (acs []*operate.Active, err error) {
  45. var rows *sql.Rows
  46. if rows, err = d.activeGet.Query(c); err != nil {
  47. return
  48. }
  49. defer rows.Close()
  50. for rows.Next() {
  51. ac := &operate.Active{}
  52. if err = rows.Scan(&ac.ID, &ac.ParentID, &ac.Name, &ac.Background, &ac.Type, &ac.Content); err != nil {
  53. return
  54. }
  55. ac.Change()
  56. acs = append(acs, ac)
  57. }
  58. return
  59. }