version_update.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package http
  2. import (
  3. "fmt"
  4. "net/url"
  5. "go-common/app/admin/main/tv/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. _isVerDeleted = 2
  12. )
  13. func verUpdateList(c *bm.Context) {
  14. var (
  15. req = c.Request.Form
  16. err error
  17. items []*model.VersionUpdate
  18. limit []*model.VersionUpdateLimit
  19. rets []*model.VersionUpdateDetail
  20. count int64
  21. vid = atoi(req.Get("vid"))
  22. page = atoi(req.Get("page"))
  23. size = 20
  24. )
  25. if page == 0 {
  26. page = 1
  27. }
  28. db := tvSrv.DBShow.Where("vid=?", vid).Where("state!=?", _isVerDeleted)
  29. db.Model(&model.VersionUpdate{}).Count(&count)
  30. if err = db.Model(&model.VersionUpdate{}).Offset((page - 1) * size).Limit(size).Find(&items).Error; err != nil {
  31. log.Error("%v\n", err)
  32. c.JSON(nil, err)
  33. return
  34. }
  35. for _, val := range items {
  36. if err = tvSrv.DBShow.Model(&model.VersionUpdateLimit{}).Where("up_id=?", val.ID).Find(&limit).Error; err != nil {
  37. log.Error("%v\n", err)
  38. c.JSON(nil, err)
  39. return
  40. }
  41. rets = append(rets, &model.VersionUpdateDetail{VersionUpdate: val, VerLimit: limit})
  42. }
  43. version := model.Version{}
  44. if err = tvSrv.DBShow.Where("id=?", vid).Where("state!=?", _isDeleted).First(&version).Error; err != nil {
  45. log.Error("version info not exists-(%v)\n", err)
  46. c.JSON(nil, err)
  47. return
  48. }
  49. lists := map[string]interface{}{
  50. "verUpdate": rets,
  51. "version": version,
  52. }
  53. pager := &model.VersionUpdatePager{
  54. TotalCount: count,
  55. Pn: page,
  56. Ps: size,
  57. Items: lists,
  58. }
  59. c.JSON(pager, nil)
  60. }
  61. func saveVerUpdate(c *bm.Context) {
  62. var (
  63. req = c.Request.PostForm
  64. id = parseInt(req.Get("id"))
  65. err error
  66. )
  67. exist := model.VersionUpdate{}
  68. if err = tvSrv.DBShow.Where("id=?", id).Where("state!=?", _isVerDeleted).First(&exist).Error; err != nil {
  69. c.JSON(nil, err)
  70. return
  71. }
  72. alert, simple := validateVerUpdatePostData(c)
  73. if alert != "" {
  74. renderErrMsg(c, ecode.RequestErr.Code(), alert)
  75. return
  76. }
  77. if err = tvSrv.DBShow.Model(&model.VersionUpdate{}).Where("id = ?", id).Update(simple).Error; err != nil {
  78. log.Error("tvSrv.saveVerUpdate error(%v)", err)
  79. c.JSON(nil, err)
  80. return
  81. }
  82. if err = tvSrv.DBShow.Model(&model.VersionUpdate{}).Where("id = ?", id).Update(map[string]int8{"is_push": simple.IsPush, "is_force": simple.IsForce}).Error; err != nil {
  83. log.Error("tvSrv.saveVerUpdate error(%v)", err)
  84. c.JSON(nil, err)
  85. return
  86. }
  87. if err = tvSrv.DBShow.Model(&model.VersionUpdateLimit{}).Where("up_id=?", id).Delete(&model.VersionUpdateLimit{}).Error; err != nil {
  88. log.Error("tvSrv.DeleteVerUpdateLimit error(%v)\n", err)
  89. return
  90. }
  91. log.Info("saveVerUpdate exist.ID = %d", exist.ID)
  92. if exist.ID > 0 {
  93. addVerUpdateLimit(req, exist.ID)
  94. }
  95. c.JSON(nil, nil)
  96. }
  97. func addVerUpdate(c *bm.Context) {
  98. var (
  99. req = c.Request.PostForm
  100. err error
  101. )
  102. alert, simple := validateVerUpdatePostData(c)
  103. if alert != "" {
  104. renderErrMsg(c, ecode.RequestErr.Code(), alert)
  105. return
  106. }
  107. db := tvSrv.DBShow.Create(simple)
  108. if err = db.Error; err != nil {
  109. log.Error("tvSrv.addVerUpdate error(%v)", err)
  110. c.JSON(nil, err)
  111. return
  112. }
  113. insertID := (db.Value.(*model.VersionUpdate)).ID
  114. if insertID > 0 {
  115. addVerUpdateLimit(req, insertID)
  116. }
  117. c.JSON(nil, nil)
  118. }
  119. func addVerUpdateLimit(req url.Values, upid int64) (err error) {
  120. var (
  121. condi = req["condi[]"]
  122. value = req["value[]"]
  123. )
  124. if len(condi) > 0 {
  125. for key, val := range condi {
  126. li := &model.VersionUpdateLimit{UPID: int32(upid)}
  127. if key < len(condi) {
  128. li.Condi = val
  129. }
  130. if key < len(value) {
  131. li.Value = atoi(value[key])
  132. }
  133. if err = tvSrv.DBShow.Create(li).Error; err != nil {
  134. log.Error("tvSrv.addVerUpdateLimit error(%v)", err)
  135. break
  136. }
  137. }
  138. }
  139. return
  140. }
  141. func verUpdateEnable(c *bm.Context) {
  142. var (
  143. req = c.Request.PostForm
  144. id = parseInt(req.Get("id"))
  145. state = atoi(req.Get("state"))
  146. err error
  147. )
  148. exist := model.VersionUpdate{}
  149. if err = tvSrv.DBShow.Where("id=?", id).Where("state!=?", _isVerDeleted).First(&exist).Error; err != nil {
  150. c.JSON(nil, err)
  151. return
  152. }
  153. if err := tvSrv.DBShow.Model(&model.VersionUpdate{}).Where("id = ?", id).Update(map[string]int{"state": state}).Error; err != nil {
  154. log.Error("tvSrv.verUpdateEnable error(%v)", err)
  155. c.JSON(nil, err)
  156. return
  157. }
  158. c.JSON(nil, nil)
  159. }
  160. func fullPackageImport(c *bm.Context) {
  161. var (
  162. req = c.Request.Form
  163. vid = atoi(req.Get("vid"))
  164. build = atoi(req.Get("build"))
  165. err error
  166. )
  167. result, err := tvSrv.FullImport(c, build)
  168. if err != nil {
  169. log.Error("%v\n", err)
  170. c.JSON(nil, err)
  171. return
  172. }
  173. for _, val := range result {
  174. if !createApk2Version(val, vid) {
  175. renderErrMsg(c, ecode.RequestErr.Code(), fmt.Sprintf("fullPackageImport fail(%v)", val))
  176. return
  177. }
  178. }
  179. c.JSON(nil, nil)
  180. }
  181. func createApk2Version(val *model.APKInfo, vid int) (b bool) {
  182. var (
  183. err error
  184. simple *model.VersionUpdate
  185. limit *model.VersionUpdateLimit
  186. )
  187. b = false
  188. simple = new(model.VersionUpdate)
  189. simple.VID = vid
  190. simple.PolicyName = "指定版本导入更新"
  191. simple.IsForce = 0
  192. simple.IsPush = 0
  193. simple.Channel = "bili"
  194. simple.URL = val.CDNAddr
  195. simple.Size = val.Size
  196. simple.Md5 = val.SignMd5
  197. simple.Sdkint = 0
  198. simple.Model = ""
  199. simple.Policy = 0
  200. simple.Coverage = 100
  201. db := tvSrv.DBShow.Create(simple)
  202. if err = db.Error; err != nil {
  203. log.Error("tvSrv.createApk2Version error(%v)", err)
  204. return
  205. }
  206. insertID := (db.Value.(*model.VersionUpdate)).ID
  207. limit = &model.VersionUpdateLimit{UPID: int32(insertID), Condi: "", Value: 0}
  208. if err = tvSrv.DBShow.Create(limit).Error; err != nil {
  209. log.Error("tvSrv.createAPK2Version error(%v)", err)
  210. return
  211. }
  212. return true
  213. }
  214. func validateVerUpdatePostData(c *bm.Context) (alert string, simple *model.VersionUpdate) {
  215. var (
  216. req = c.Request.PostForm
  217. vid = atoi(req.Get("vid"))
  218. isForce = atoi(req.Get("is_force"))
  219. isPush = atoi(req.Get("is_push"))
  220. channel = req.Get("channel")
  221. url = req.Get("url")
  222. size = atoi(req.Get("size"))
  223. md5 = req.Get("md5")
  224. sdkint = atoi(req.Get("sdkint"))
  225. mod = req.Get("model")
  226. policy = atoi(req.Get("policy"))
  227. coverage = atoi(req.Get("coverage"))
  228. policyName = req.Get("policy_name")
  229. )
  230. alert = string("")
  231. simple = new(model.VersionUpdate)
  232. simple.VID = vid
  233. simple.PolicyName = policyName
  234. simple.IsForce = int8(isForce)
  235. simple.IsPush = int8(isPush)
  236. simple.Channel = channel
  237. simple.URL = url
  238. simple.Size = size
  239. simple.Md5 = md5
  240. simple.Sdkint = sdkint
  241. simple.Model = mod
  242. simple.Policy = int8(policy)
  243. simple.Coverage = int32(coverage)
  244. if simple.Channel == "" {
  245. alert = "渠道不能为空"
  246. return
  247. }
  248. if simple.URL == "" {
  249. alert = "安装包地址不能为空"
  250. return
  251. }
  252. if simple.Size == 0 {
  253. alert = "文件大小不能为0"
  254. return
  255. }
  256. if simple.Md5 == "" {
  257. alert = "文件hash值不能为空"
  258. return
  259. }
  260. return
  261. }