version.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package version
  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/version"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. // version
  12. _allSQL = "SELECT id,plat,description,version,build,ptime FROM version WHERE state=0 ORDER BY ptime DESC"
  13. // version_update AND version
  14. _allUpsSQL = `SELECT up.id,up.channel,up.url,up.size,up.md5,v.build,v.version,v.description,up.sdkint_list,up.model,up.policy,up.is_force,
  15. up.is_push,up.policy_name,v.state,up.mtime,v.plat,up.policy_url,up.buvid_start,up.buvid_end
  16. FROM version v RIGHT JOIN version_update up ON v.id=up.vid AND v.plat in(0,7,8,9,11,12) WHERE up.state=1 AND v.state!=1 AND up.url!='' ORDER BY v.build DESC`
  17. _allUpsLimitSQL = `SELECT up_id,value,condi FROM version_update_limit ORDER BY value ASC`
  18. _allSosSQL = `SELECT id,package,name,description,clear,ver_code,ver_name,url,size,enable_state,force_state,md5,min_build,coverage,sdkint,model
  19. FROM version_so WHERE enable_state!=2 ORDER BY id DESC`
  20. _allIncreSQL = `SELECT i.id,i.target_version,i.target_build,i.target_id,i.source_version,i.source_build,i.source_id,i.task_id,i.file_path,i.url,i.md5,i.size,v.build,v.plat
  21. FROM version v RIGHT JOIN version_incremental i ON v.id=i.vid AND v.plat in(0,7,8,9,11,12) WHERE i.state=1 AND v.state!=1 AND i.url!='' ORDER BY v.build DESC`
  22. _allRnSQL = `SELECT id,deployment_key,bundle_id,url,md5,size,version FROM version_rn WHERE state=1 ORDER BY ctime DESC`
  23. )
  24. // Dao is a version dao
  25. type Dao struct {
  26. db *sql.DB
  27. get *sql.Stmt
  28. update *sql.Stmt
  29. so *sql.Stmt
  30. incre *sql.Stmt
  31. rn *sql.Stmt
  32. limit *sql.Stmt
  33. }
  34. // New new a version dao and return.
  35. func New(c *conf.Config) (d *Dao) {
  36. d = &Dao{
  37. db: sql.NewMySQL(c.MySQL.Show),
  38. }
  39. // prepare
  40. d.get = d.db.Prepared(_allSQL)
  41. d.update = d.db.Prepared(_allUpsSQL)
  42. d.so = d.db.Prepared(_allSosSQL)
  43. d.incre = d.db.Prepared(_allIncreSQL)
  44. d.rn = d.db.Prepared(_allRnSQL)
  45. d.limit = d.db.Prepared(_allUpsLimitSQL)
  46. return
  47. }
  48. // GetVersion get version.
  49. func (d *Dao) All(ctx context.Context) (ver map[int8][]*version.Version, err error) {
  50. rows, err := d.get.Query(ctx)
  51. if err != nil {
  52. log.Error("d.get error(%v)", err)
  53. return
  54. }
  55. defer rows.Close()
  56. ver = map[int8][]*version.Version{}
  57. for rows.Next() {
  58. a := &version.Version{}
  59. if err = rows.Scan(&a.Id, &a.Plat, &a.Desc, &a.Version, &a.Build, &a.PTime); err != nil {
  60. log.Error("row.Scan error(%v)", err)
  61. return
  62. }
  63. ver[a.Plat] = append(ver[a.Plat], a)
  64. }
  65. return
  66. }
  67. // GetUpdate get active version AND version_update
  68. func (d *Dao) Updates(ctx context.Context) (ver map[int8]map[string][]*version.VersionUpdate, err error) {
  69. var (
  70. masterChannel = "master"
  71. defaultChannel = "bili"
  72. )
  73. rows, err := d.update.Query(ctx)
  74. if err != nil {
  75. log.Error("d.update error(%v)", err)
  76. return
  77. }
  78. defer rows.Close()
  79. ver = map[int8]map[string][]*version.VersionUpdate{}
  80. for rows.Next() {
  81. a := &version.VersionUpdate{}
  82. if err = rows.Scan(&a.Id, &a.Channel, &a.Url, &a.Size, &a.MD5, &a.Build, &a.Version, &a.Desc, &a.SdkInts, &a.Model, &a.Policy, &a.IsForce,
  83. &a.IsPush, &a.PolicyName, &a.State, &a.Mtime, &a.Plat, &a.PolicyURL, &a.BuvidStart, &a.BuvidEnd); err != nil {
  84. log.Error("row.Scan error(%v)", err)
  85. return
  86. }
  87. a.VersionUpdateChange()
  88. if a.State == 2 {
  89. a.IsGray = 1
  90. } else {
  91. a.IsGray = 0
  92. }
  93. if a.Channel == masterChannel && a.Plat != model.PlatAndroidTVYST {
  94. a.Channel = defaultChannel
  95. }
  96. if v, ok := ver[a.Plat]; !ok {
  97. ver[a.Plat] = map[string][]*version.VersionUpdate{
  98. a.Channel: []*version.VersionUpdate{a},
  99. }
  100. } else {
  101. v[a.Channel] = append(v[a.Channel], a)
  102. }
  103. }
  104. return
  105. }
  106. // Limits version update limits
  107. func (d *Dao) Limits(ctx context.Context) (res map[int][]*version.UpdateLimit, err error) {
  108. res = map[int][]*version.UpdateLimit{}
  109. rows, err := d.limit.Query(ctx)
  110. if err != nil {
  111. log.Error("d.update error(%v)", err)
  112. return
  113. }
  114. defer rows.Close()
  115. for rows.Next() {
  116. l := &version.UpdateLimit{}
  117. if err = rows.Scan(&l.ID, &l.BuildLimit, &l.Conditions); err != nil {
  118. log.Error("version update limit sql error(%v)", err)
  119. return
  120. }
  121. res[l.ID] = append(res[l.ID], l)
  122. }
  123. return
  124. }
  125. // GetSos get version_so
  126. func (d *Dao) Sos(ctx context.Context) (ver map[string][]*version.VersionSo, err error) {
  127. rows, err := d.so.Query(ctx)
  128. if err != nil {
  129. log.Error("d.so error(%v)", err)
  130. return
  131. }
  132. defer rows.Close()
  133. ver = map[string][]*version.VersionSo{}
  134. for rows.Next() {
  135. s := &version.VersionSo{}
  136. if err = rows.Scan(&s.Id, &s.Package, &s.Name, &s.Description, &s.Clear, &s.Ver_code, &s.Ver_name, &s.Url, &s.Size, &s.Enable_state,
  137. &s.Force_state, &s.Md5, &s.Min_build, &s.Coverage, &s.Sdkint, &s.Model); err != nil {
  138. log.Error("row.Scan error(%v)", err)
  139. return
  140. }
  141. ver[s.Name] = append(ver[s.Name], s)
  142. }
  143. return
  144. }
  145. // Incrementals version incrementals
  146. func (d *Dao) Incrementals(ctx context.Context) (ver map[int8]map[string][]*version.Incremental, err error) {
  147. rows, err := d.incre.Query(ctx)
  148. if err != nil {
  149. log.Error("d.incre error(%v)", err)
  150. return
  151. }
  152. defer rows.Close()
  153. ver = map[int8]map[string][]*version.Incremental{}
  154. for rows.Next() {
  155. v := &version.Incremental{}
  156. if err = rows.Scan(&v.ID, &v.TargetVersion, &v.TargetBuild, &v.TargetID, &v.SourceVersion, &v.SourceBuild, &v.SourceID, &v.TaskID, &v.FilePath, &v.URL, &v.Md5,
  157. &v.Size, &v.Build, &v.Plat); err != nil {
  158. log.Error("row.Scan error(%v)", err)
  159. return
  160. }
  161. if vi, ok := ver[v.Plat]; !ok {
  162. ver[v.Plat] = map[string][]*version.Incremental{
  163. v.SourceID: []*version.Incremental{v},
  164. }
  165. } else {
  166. vi[v.SourceID] = append(vi[v.SourceID], v)
  167. }
  168. }
  169. return
  170. }
  171. // Rn version rn
  172. func (d *Dao) Rn(ctx context.Context) (ver map[string]map[string]*version.Rn, err error) {
  173. rows, err := d.rn.Query(ctx)
  174. if err != nil {
  175. log.Error("d.rn error(%v)", err)
  176. return
  177. }
  178. defer rows.Close()
  179. ver = map[string]map[string]*version.Rn{}
  180. for rows.Next() {
  181. v := &version.Rn{}
  182. if err = rows.Scan(&v.ID, &v.DeploymentKey, &v.BundleID, &v.URL, &v.Md5, &v.Size, &v.Version); err != nil {
  183. log.Error("row.Scan error(%v)", err)
  184. return
  185. }
  186. if vi, ok := ver[v.DeploymentKey]; !ok {
  187. ver[v.DeploymentKey] = map[string]*version.Rn{
  188. v.Version: v,
  189. }
  190. } else if _, ok := vi[v.Version]; !ok {
  191. vi[v.Version] = v
  192. }
  193. }
  194. return
  195. }
  196. // Close close db resource.
  197. func (dao *Dao) Close() {
  198. if dao.db != nil {
  199. dao.db.Close()
  200. }
  201. }