res.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package resource
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/web-show/model/resource"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _selAllResSQL = `SELECT id,platform,name,parent,counter,position FROM resource WHERE state=0 ORDER BY counter desc`
  11. _selAllAssignSQL = `SELECT id,name,contract_id,resource_id,pic,litpic,url,atype,weight,rule,agency FROM resource_assignment WHERE stime<? AND etime>? AND state=0 ORDER BY weight,stime desc`
  12. _selDefBannerSQL = `SELECT id,name,contract_id,resource_id,pic,litpic,url,atype,weight,rule FROM default_one WHERE state=0`
  13. )
  14. func (dao *Dao) initRes() {
  15. dao.selAllResStmt = dao.db.Prepared(_selAllResSQL)
  16. dao.selAllAssignStmt = dao.db.Prepared(_selAllAssignSQL)
  17. dao.selDefBannerStmt = dao.db.Prepared(_selDefBannerSQL)
  18. }
  19. // Resources get resource infos from db
  20. func (dao *Dao) Resources(c context.Context) (rscs []*resource.Res, err error) {
  21. rows, err := dao.selAllResStmt.Query(c)
  22. if err != nil {
  23. log.Error("dao.selAllResStmt query error (%v)", err)
  24. return
  25. }
  26. defer rows.Close()
  27. rscs = make([]*resource.Res, 0)
  28. for rows.Next() {
  29. rsc := &resource.Res{}
  30. if err = rows.Scan(&rsc.ID, &rsc.Platform, &rsc.Name, &rsc.Parent, &rsc.Counter, &rsc.Position); err != nil {
  31. PromError("Resources", "rows.scan err(%v)", err)
  32. return
  33. }
  34. rscs = append(rscs, rsc)
  35. }
  36. return
  37. }
  38. // Assignment get assigment from db
  39. func (dao *Dao) Assignment(c context.Context) (asgs []*resource.Assignment, err error) {
  40. rows, err := dao.selAllAssignStmt.Query(c, time.Now(), time.Now())
  41. if err != nil {
  42. log.Error("dao.selAllAssignmentStmt query error (%v)", err)
  43. return
  44. }
  45. defer rows.Close()
  46. asgs = make([]*resource.Assignment, 0)
  47. for rows.Next() {
  48. asg := &resource.Assignment{}
  49. if err = rows.Scan(&asg.ID, &asg.Name, &asg.ContractID, &asg.ResID, &asg.Pic, &asg.LitPic, &asg.URL, &asg.Atype, &asg.Weight, &asg.Rule, &asg.Agency); err != nil {
  50. PromError("Assignment", "rows.scan err(%v)", err)
  51. return
  52. }
  53. asgs = append(asgs, asg)
  54. }
  55. return
  56. }
  57. // DefaultBanner set
  58. func (dao *Dao) DefaultBanner(c context.Context) (asg *resource.Assignment, err error) {
  59. row := dao.selDefBannerStmt.QueryRow(c)
  60. asg = &resource.Assignment{}
  61. if err = row.Scan(&asg.ID, &asg.Name, &asg.ContractID, &asg.ResID, &asg.Pic, &asg.LitPic, &asg.URL, &asg.Atype, &asg.Weight, &asg.Rule); err != nil {
  62. if err == sql.ErrNoRows {
  63. err = nil
  64. } else {
  65. PromError("DefaultBanner", "dao.DefaultBanner.QueryRow error(%v)", err)
  66. }
  67. }
  68. return
  69. }