module.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package module
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-resource/conf"
  5. "go-common/app/interface/main/app-resource/model"
  6. "go-common/app/interface/main/app-resource/model/module"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _moduleDevSQL = `SELECT p.id,d.name FROM resource_department AS d,resource_pool AS p WHERE d.id=p.department_id AND d.is_deleted=0 AND p.action=1 AND p.deleted=0`
  12. _moduleAllSQL = `SELECT rp.id,r.id,r.version,rp.name,rp.compresstype,rf.type,rf.url,rf.md5,rf.size,rf.file_type,rf.from_ver FROM resource_pool AS rp,resource AS r,resource_file AS rf
  13. WHERE rp.id=r.pool_id AND r.id=rf.resource_id ORDER BY rp.id ASC,r.version DESC,rf.from_ver DESC`
  14. _resourceConfigSQL = `SELECT id,resource_id,stime,etime,valid,valid_test,default_package,is_wifi FROM resource_config WHERE is_deleted=0`
  15. _resourceLimitSQL = `SELECT l.config_id,l.column,l.condition,l.value FROM resource_limit AS l WHERE l.is_deleted=0`
  16. )
  17. // Dao is a module dao.
  18. type Dao struct {
  19. db *sql.DB
  20. }
  21. // New new a module dao.
  22. func New(c *conf.Config) (d *Dao) {
  23. d = &Dao{
  24. db: sql.NewMySQL(c.MySQL.Show),
  25. }
  26. return
  27. }
  28. // ModuleDev get modelu dev
  29. func (d *Dao) ModuleDev(ctx context.Context) (res map[string]*module.ResourcePool, err error) {
  30. rows, err := d.db.Query(ctx, _moduleDevSQL)
  31. if err != nil {
  32. log.Error("d.db.Query error(%v)", err)
  33. return
  34. }
  35. defer rows.Close()
  36. res = make(map[string]*module.ResourcePool)
  37. for rows.Next() {
  38. // NOTE: don't ask way the map key is "pink", stupid demand! fuck!!!
  39. // rp := &module.ResourcePool{ID: 1, Name: "pink"}
  40. r := &module.Resource{}
  41. if err = rows.Scan(&r.ID, &r.Name); err != nil {
  42. log.Error("rows.Scan error(%v)", err)
  43. return
  44. }
  45. var (
  46. resTmp *module.ResourcePool
  47. ok bool
  48. )
  49. if resTmp, ok = res[r.Name]; !ok {
  50. resTmp = &module.ResourcePool{ID: r.ID, Name: r.Name}
  51. res[r.Name] = resTmp
  52. }
  53. resTmp.Resources = append(resTmp.Resources, r)
  54. }
  55. return
  56. }
  57. // ModuleAll get module
  58. func (d *Dao) ModuleAll(ctx context.Context) (res map[int][]*module.Resource, err error) {
  59. rows, err := d.db.Query(ctx, _moduleAllSQL)
  60. if err != nil {
  61. log.Error("d.db.Query error(%v)", err)
  62. return
  63. }
  64. defer rows.Close()
  65. res = make(map[int][]*module.Resource)
  66. for rows.Next() {
  67. r := &module.Resource{}
  68. if err = rows.Scan(&r.ID, &r.ResID, &r.Version, &r.Name, &r.Compresstype, &r.Type, &r.URL, &r.MD5, &r.Size, &r.Increment, &r.FromVer); err != nil {
  69. log.Error("d.db.rows.Scan error(%v)", err)
  70. return
  71. }
  72. r.URL = model.URLHTTPS(r.URL)
  73. res[r.ID] = append(res[r.ID], r)
  74. }
  75. return
  76. }
  77. // ResourceCondition
  78. func (d *Dao) ResourceConfig(ctx context.Context) (conditions map[int]*module.Condition, err error) {
  79. rows, err := d.db.Query(ctx, _resourceConfigSQL)
  80. if err != nil {
  81. log.Error("d.db.Query error(%v)", err)
  82. return
  83. }
  84. defer rows.Close()
  85. conditions = make(map[int]*module.Condition)
  86. for rows.Next() {
  87. var (
  88. condition = &module.Condition{}
  89. )
  90. if err = rows.Scan(&condition.ID, &condition.ResID, &condition.STime, &condition.ETime, &condition.Valid, &condition.ValidTest, &condition.Default, &condition.IsWifi); err != nil {
  91. log.Error("rows.Scan error(%v)", err)
  92. return
  93. }
  94. conditions[condition.ResID] = condition
  95. }
  96. return
  97. }
  98. // ResourceLimit
  99. func (d *Dao) ResourceLimit(ctx context.Context) (columns map[int]map[string][]*module.Column, err error) {
  100. rows, err := d.db.Query(ctx, _resourceLimitSQL)
  101. if err != nil {
  102. log.Error("d.db.Query error(%v)", err)
  103. return
  104. }
  105. defer rows.Close()
  106. columns = make(map[int]map[string][]*module.Column)
  107. for rows.Next() {
  108. var (
  109. cid int
  110. column = &module.Column{}
  111. columnValue string
  112. cm map[string][]*module.Column
  113. ok bool
  114. )
  115. if err = rows.Scan(&cid, &columnValue, &column.Condition, &column.Value); err != nil {
  116. log.Error("rows.Scan error(%v)", err)
  117. return
  118. }
  119. if cm, ok = columns[cid]; !ok {
  120. cm = make(map[string][]*module.Column)
  121. columns[cid] = cm
  122. }
  123. cm[columnValue] = append(cm[columnValue], column)
  124. }
  125. return
  126. }