material.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package http
  2. import (
  3. "strconv"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/admin/main/creative/model/app"
  7. "go-common/app/admin/main/creative/model/logcli"
  8. Mamdl "go-common/app/admin/main/creative/model/material"
  9. "go-common/app/admin/main/creative/model/music"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/binding"
  14. "go-common/library/xstr"
  15. "strings"
  16. "github.com/jinzhu/gorm"
  17. "github.com/pkg/errors"
  18. )
  19. //素材库
  20. func searchMaterialDb(c *bm.Context) {
  21. var (
  22. req = c.Request.Form
  23. err error
  24. items, dItems []*Mamdl.Material
  25. count int64
  26. typeID = req.Get("type")
  27. Platform = atoi(req.Get("platform"))
  28. categoryID = req.Get("category_id")
  29. id = atoi(req.Get("id"))
  30. name = req.Get("name")
  31. page = atoi(req.Get("page"))
  32. size = 20
  33. )
  34. if page == 0 {
  35. page = 1
  36. }
  37. db := svc.DB.Model(&Mamdl.Material{}).Where("material.state!=?", Mamdl.StateDelete).
  38. Joins("left join material_with_category on material_with_category.material_id=material.id and material_with_category.state !=?", Mamdl.StateOff).
  39. Joins("left join material_category on material_with_category.category_id=material_category.id and material_category.state !=?", Mamdl.StateOff).
  40. Select("material.*,material_category.name as category_name,material_with_category.category_id,material_with_category.index as category_index")
  41. if id != 0 {
  42. //id查询时需要提供type参数否则可能导致搜索为空
  43. db = db.Where("material.id=?", id)
  44. }
  45. if Platform != -1 {
  46. db = db.Where("material.platform=?", Platform)
  47. }
  48. //类型filter
  49. if categoryID != "" {
  50. db = db.Where("material_with_category.category_id=?", atoi(categoryID))
  51. }
  52. if typeID != "" {
  53. db = db.Where("material.type=?", atoi(typeID))
  54. }
  55. if name != "" {
  56. db = db.Where("material.name=?", name)
  57. }
  58. db.Count(&count)
  59. if categoryID != "" {
  60. db = db.Order("material_with_category.index")
  61. } else {
  62. db = db.Order("material.rank")
  63. }
  64. if err = db.Offset((page - 1) * size).Limit(size).Find(&dItems).Error; err != nil {
  65. log.Error("%v\n", err)
  66. c.JSON(nil, err)
  67. return
  68. }
  69. if len(dItems) > 0 {
  70. items = make([]*Mamdl.Material, 0, len(dItems))
  71. for _, v := range dItems {
  72. i := &Mamdl.Material{}
  73. if v == nil {
  74. continue
  75. }
  76. i.ID = v.ID
  77. i.UID = v.UID
  78. i.Name = v.Name
  79. i.Extra = v.Extra
  80. i.Rank = v.Rank
  81. i.Type = v.Type
  82. i.Platform = v.Platform
  83. i.Build = v.Build
  84. i.State = v.State
  85. i.CategoryID = v.CategoryID
  86. i.CategoryIndex = v.CategoryIndex
  87. i.CategoryName = v.CategoryName
  88. if i.CategoryName == "" {
  89. i.CategoryID = 0
  90. i.CategoryIndex = 0
  91. }
  92. i.CTime = v.CTime
  93. i.MTime = v.MTime
  94. items = append(items, i)
  95. }
  96. } else {
  97. items = []*Mamdl.Material{}
  98. }
  99. pager := &Mamdl.Result{
  100. Items: items,
  101. Pager: &Mamdl.Pager{Num: page, Size: size, Total: count},
  102. }
  103. c.JSON(pager, nil)
  104. }
  105. func infoMaterial(c *bm.Context) {
  106. var (
  107. req = c.Request.Form
  108. id = parseInt(req.Get("id"))
  109. err error
  110. )
  111. m := &Mamdl.Material{}
  112. if err = svc.DB.Where("id=?", id).First(&m).Error; err != nil {
  113. c.JSON(nil, err)
  114. return
  115. }
  116. c.JSON(map[string]*Mamdl.Material{
  117. "data": m,
  118. }, nil)
  119. }
  120. func getUIDName(c *bm.Context) (uid int64, uname string) {
  121. unamei, ok := c.Get("username")
  122. if ok {
  123. uname = unamei.(string)
  124. }
  125. uidi, ok := c.Get("uid")
  126. if ok {
  127. uid = uidi.(int64)
  128. }
  129. return
  130. }
  131. func syncMaterial(c *bm.Context) {
  132. var (
  133. err error
  134. m *Mamdl.Material
  135. action string
  136. )
  137. mp := &Mamdl.Param{}
  138. if err = c.BindWith(mp, binding.Form); err != nil {
  139. log.Error("syncMaterial bind error trace(%+v)", errors.Wrap(err, "sync bind error"))
  140. httpCode(c, fmt.Sprintf("bind error(%v)", err), ecode.RequestErr)
  141. return
  142. }
  143. m, err = transferDB(c, mp)
  144. if err != nil {
  145. log.Error("transferDB find error(%+v)", err)
  146. httpCode(c, fmt.Sprintf("transferDB validate error(%v)", err), ecode.RequestErr)
  147. return
  148. }
  149. uid, uname := getUIDName(c)
  150. m.UID = uid
  151. if m.ID == 0 {
  152. action = "add"
  153. if err = svc.DB.Create(m).Error; err != nil {
  154. log.Error("syncMaterial Create error(%+v)", err)
  155. httpCode(c, fmt.Sprintf("syncMaterial Create error(%v)", err), ecode.RequestErr)
  156. return
  157. }
  158. } else {
  159. action = "edit"
  160. exist := &Mamdl.Material{}
  161. if err = svc.DB.Where("id=?", mp.ID).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  162. log.Error("syncMaterial find error(%+v)", err)
  163. httpCode(c, fmt.Sprintf("syncMaterial find error(%v)", err), ecode.RequestErr)
  164. return
  165. }
  166. if exist.ID > 0 {
  167. m.ID = exist.ID
  168. if err = svc.DB.Model(&Mamdl.Material{}).Where("id=?", mp.ID).Update(m).Update(map[string]int8{"type": m.Type}).Error; err != nil {
  169. log.Error("syncMaterial update error(%+v)", err)
  170. httpCode(c, fmt.Sprintf("syncMaterial update error(%v)", err), ecode.RequestErr)
  171. return
  172. }
  173. }
  174. }
  175. logMaterial, _ := json.Marshal(mp)
  176. svc.SendMusicLog(c, logcli.LogClientArchiveMaterialType, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: action, Name: string(logMaterial)})
  177. //不做事务允许分类绑定异常
  178. if mp.Type == Mamdl.TypeFilter || mp.Type == Mamdl.TypeCreativeSticks {
  179. _, err = svc.BindWithCategory(c, m.ID, mp.CategoryID, mp.CategoryIndex)
  180. if err != nil {
  181. httpCode(c, fmt.Sprintf("BindWithCategory update error(%v)", err), ecode.RequestErr)
  182. return
  183. }
  184. }
  185. c.JSON(map[string]int64{
  186. "id": m.ID,
  187. }, nil)
  188. }
  189. func transferDB(c *bm.Context, form *Mamdl.Param) (material *Mamdl.Material, err error) {
  190. var extraBytes []byte
  191. material = &Mamdl.Material{}
  192. if form == nil {
  193. return material, fmt.Errorf("params is wrong")
  194. }
  195. if form.Type != Mamdl.TypeCreativeSticks && (form.Name == "") {
  196. return material, fmt.Errorf("name is need")
  197. }
  198. if form.Type == Mamdl.TypeSubTitle && (form.Max == 0) {
  199. return material, fmt.Errorf("max is need")
  200. }
  201. if form.Type != Mamdl.TypeSticksIcon && (form.Rank == 0) {
  202. return material, fmt.Errorf("rank is need")
  203. }
  204. //不需要cover
  205. if form.Type != Mamdl.TypeHotWord && form.Type != Mamdl.TypeSticksIcon && form.Type != Mamdl.TypeCooperate && (form.Cover == "" || form.DownloadURL == "") {
  206. return material, fmt.Errorf("cover and download_url is need")
  207. }
  208. if form.Cover != "" && (!strings.HasPrefix(form.Cover, "http") || !strings.Contains(form.Cover, "/bfs/")) {
  209. return material, fmt.Errorf("cover is wrong")
  210. }
  211. if form.Type == Mamdl.TypeCooperate && form.DownloadURL != "" && !strings.Contains(form.DownloadURL, "acgvideo.com/") {
  212. //acg
  213. return material, fmt.Errorf("download_url is wrong")
  214. }
  215. if form.Type != Mamdl.TypeCooperate && form.DownloadURL != "" && (!strings.HasSuffix(form.DownloadURL, ".zip") || !strings.HasPrefix(form.DownloadURL, "http") || !strings.Contains(form.DownloadURL, "/bfs/creative/")) {
  216. //bfs creative
  217. return material, fmt.Errorf("download_url is wrong")
  218. }
  219. if !Mamdl.InMaterialType(form.Type) || form.Type == Mamdl.TypeBGM {
  220. //Mamdl.TypeBGM 为迁入bgm预留
  221. return material, fmt.Errorf("type is wrong")
  222. }
  223. if !checkBuild(form.Build) {
  224. return material, fmt.Errorf("build is wrong")
  225. }
  226. switch form.Type {
  227. case Mamdl.TypeSubTitle:
  228. //字幕库
  229. extraBytes, err = json.Marshal(map[string]interface{}{
  230. "download_url": form.DownloadURL,
  231. "max": form.Max,
  232. "cover": form.Cover,
  233. })
  234. case Mamdl.TypeFont, Mamdl.TypeHotWord, Mamdl.TypeSticksIcon:
  235. //字体库,贴纸,热词,贴纸Icon
  236. extraBytes, err = json.Marshal(map[string]interface{}{
  237. "download_url": form.DownloadURL,
  238. "cover": form.Cover,
  239. })
  240. case Mamdl.TypeSticks:
  241. //贴纸
  242. extraBytes, err = json.Marshal(map[string]interface{}{
  243. "download_url": form.DownloadURL,
  244. "cover": form.Cover,
  245. "sub_type": form.SubType,
  246. "white_list": form.WhilteList,
  247. "tip": form.Tip,
  248. })
  249. case Mamdl.TypeFilter:
  250. //滤镜库
  251. if len(form.ExtraURL) > 0 && (!strings.HasSuffix(form.ExtraURL, ".zip") || !strings.HasPrefix(form.ExtraURL, "http") || !strings.Contains(form.ExtraURL, "/bfs/creative/")) {
  252. return material, fmt.Errorf("extra_url is wrong")
  253. }
  254. extraBytes, err = json.Marshal(map[string]interface{}{
  255. "download_url": form.DownloadURL,
  256. "cover": form.Cover,
  257. "extra_url": form.ExtraURL,
  258. "extra_field": form.ExtraField,
  259. "filter_type": form.FilterType,
  260. })
  261. case Mamdl.TypeCooperate:
  262. //合拍库
  263. if form.MaterialAID*form.MaterialCID*form.DemoAID == 0 || form.SubType == 0 {
  264. return material, fmt.Errorf("参数错误")
  265. }
  266. extraBytes, err = json.Marshal(map[string]interface{}{
  267. "download_url": form.DownloadURL,
  268. "cover": form.Cover,
  269. "sub_type": form.SubType,
  270. "style": form.Style,
  271. "material_aid": form.MaterialAID,
  272. "material_cid": form.MaterialCID,
  273. "demo_aid": form.DemoAID,
  274. "demo_cid": form.DemoCID,
  275. "mission_id": form.MissionID,
  276. })
  277. case Mamdl.TypeTheme:
  278. //主题库
  279. extraBytes, err = json.Marshal(map[string]interface{}{
  280. "download_url": form.DownloadURL,
  281. "cover": form.Cover,
  282. })
  283. default:
  284. extraBytes, err = json.Marshal(map[string]interface{}{
  285. "download_url": form.DownloadURL,
  286. "cover": form.Cover,
  287. })
  288. }
  289. if err != nil {
  290. return material, err
  291. }
  292. material.ID = form.ID
  293. material.Name = form.Name
  294. material.Extra = string(extraBytes)
  295. material.Type = form.Type
  296. material.Rank = form.Rank
  297. material.Platform = form.Platform
  298. material.Build = form.Build
  299. if form.CategoryID > 0 {
  300. if form.CategoryIndex < 1 {
  301. return material, fmt.Errorf("category_index is wrong")
  302. }
  303. cate, _ := svc.CategoryByID(c, form.CategoryID)
  304. if cate == nil || cate.State == Mamdl.StateOff {
  305. return material, fmt.Errorf("category_id is wrong")
  306. }
  307. }
  308. return
  309. }
  310. func stateMaterial(c *bm.Context) {
  311. var (
  312. req = c.Request.PostForm
  313. ids []int64
  314. state = parseInt(req.Get("state"))
  315. err error
  316. )
  317. idStr := req.Get("id")
  318. if ids, err = xstr.SplitInts(idStr); err != nil {
  319. log.Error("stateMaterial strconv.ParseInt(%s) error(%v)", idStr, err)
  320. httpCode(c, fmt.Sprintf("stateMaterial strconv.ParseInt error(%v)", err), ecode.RequestErr)
  321. return
  322. }
  323. if state > 2 {
  324. httpCode(c, "state参数可选值为 0 上架,1下架,2 删除", ecode.RequestErr)
  325. return
  326. }
  327. //删除时保护上架状态的
  328. if state == 2 {
  329. if err = svc.DB.Model(Mamdl.Material{}).Where("id IN (?)", ids).Where("state !=?", 0).Update(map[string]int64{"state": state}).Error; err != nil {
  330. log.Error("svc.stateMaterial error(%v)", err)
  331. httpCode(c, fmt.Sprintf("stateMaterial update state error(%v)", err), ecode.RequestErr)
  332. return
  333. }
  334. } else {
  335. if err = svc.DB.Model(Mamdl.Material{}).Where("id IN (?)", ids).Update(map[string]int64{"state": state}).Error; err != nil {
  336. log.Error("svc.stateMaterial error(%v)", err)
  337. httpCode(c, fmt.Sprintf("stateMaterial update state error(%v)", err), ecode.RequestErr)
  338. return
  339. }
  340. }
  341. c.JSON(map[string]int{
  342. "code": 0,
  343. }, nil)
  344. }
  345. func parseInt(value string) int64 {
  346. intval, err := strconv.ParseInt(value, 10, 64)
  347. if err != nil {
  348. intval = 0
  349. }
  350. return intval
  351. }
  352. func atoi(value string) (intval int) {
  353. intval, err := strconv.Atoi(value)
  354. if err != nil {
  355. intval = 0
  356. }
  357. return intval
  358. }
  359. func httpCode(c *bm.Context, message string, err error) {
  360. c.JSON(map[string]interface{}{
  361. "message": message,
  362. }, err)
  363. }
  364. func checkBuild(build string) bool {
  365. var err error
  366. if len(build) > 0 {
  367. type buildItem struct {
  368. Build int64 `json:"build"`
  369. Conditions int8 `json:"conditions"`
  370. }
  371. //比较版本号符号类型,0-等于,1-小于,2-大于,3-不等于,4-小于等于,5-大于等于
  372. //[{"conditions":0,"build":5290000},{"conditions":0,"build":5290000}]
  373. var buildExp []*buildItem
  374. if err = json.Unmarshal([]byte(build), &buildExp); err != nil {
  375. return false
  376. }
  377. }
  378. return true
  379. }
  380. func checkWhite(white string) bool {
  381. var err error
  382. if len(white) > 0 {
  383. var whiteExp []*app.WhiteExp
  384. if err = json.Unmarshal([]byte(white), &whiteExp); err != nil {
  385. return false
  386. }
  387. }
  388. return true
  389. }