material.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dao
  2. import (
  3. "context"
  4. "github.com/jinzhu/gorm"
  5. "go-common/app/admin/main/creative/model/material"
  6. "go-common/library/log"
  7. )
  8. // CategoryByID .
  9. func (d *Dao) CategoryByID(c context.Context, id int64) (cate *material.Category, err error) {
  10. cate = &material.Category{}
  11. if err = d.DB.Where("id=?", id).First(&cate).Error; err != nil {
  12. log.Error("d.CategoryByID.Find error(%v)", err)
  13. return
  14. }
  15. return
  16. }
  17. // BindWithCategory .
  18. func (d *Dao) BindWithCategory(c context.Context, MaterialID, CategoryID, index int64) (id int64, err error) {
  19. var state int
  20. cate := &material.WithCategory{}
  21. if err = d.DB.Where("material_id=?", MaterialID).First(&cate).Error; err != nil && err != gorm.ErrRecordNotFound {
  22. log.Error("d.BindWithCategory.Find error(%v)", err)
  23. return
  24. }
  25. cate.CategoryID = CategoryID
  26. cate.MaterialID = MaterialID
  27. cate.Index = index
  28. if err != nil && err == gorm.ErrRecordNotFound {
  29. //添加关联
  30. if CategoryID == 0 {
  31. return
  32. }
  33. if err = d.DB.Create(cate).Error; err != nil {
  34. log.Error("BindWithCategory Create error(%+v)", err)
  35. return
  36. }
  37. } else {
  38. if CategoryID == 0 {
  39. //删除关联
  40. state = material.StateOff
  41. } else {
  42. state = material.StateOn
  43. }
  44. if err = d.DB.Model(&material.WithCategory{}).Where("id=?", cate.ID).Update(cate).Update(map[string]int{"state": state}).Error; err != nil {
  45. log.Error("dao BindWithCategory error(%v)", err)
  46. return
  47. }
  48. }
  49. id = cate.ID
  50. err = nil
  51. return
  52. }