material.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "icloudapp.cn/tools/service/entity"
  6. "icloudapp.cn/tools/service/model"
  7. )
  8. type Material struct {
  9. Base
  10. ctx context.Context
  11. query *model.MMaterial
  12. }
  13. // NewMaterial 实例化素材对象
  14. func NewMaterial(ctx context.Context) *Material {
  15. material := &Material{ctx: ctx}
  16. material.query = model.Material
  17. return material
  18. }
  19. // Info 通过ID获取素材信息
  20. func (m *Material) Info(id int64) (*entity.Material, error) {
  21. return m.query.WithContext(m.ctx).Where(m.query.ID.Eq(id)).Take()
  22. }
  23. // Infos 通过素材ID批量获取素材信息
  24. func (m *Material) Infos(ids ...int64) ([]*entity.Material, error) {
  25. return m.query.WithContext(m.ctx).Where(m.query.ID.In(ids...)).Find()
  26. }
  27. func (m *Material) InfoByUidAndHash(uid int64, hash string) (*entity.Material, error) {
  28. return m.query.WithContext(m.ctx).Where(m.query.UID.Eq(uid), m.query.Hash.Eq(hash)).Take()
  29. }
  30. func (m *Material) Add(material *entity.Material) (*entity.Material, error) {
  31. exist, err := m.InfoByUidAndHash(material.UID, material.Hash)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if exist.UID > 0 {
  36. //如果是被删除了,就更新此数据
  37. if exist.Status < 1 {
  38. exist.Status = 1
  39. affected, err := m.query.WithContext(m.ctx).Where(m.query.ID.Eq(exist.ID)).Update(m.query.Status, -1)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if affected.RowsAffected == 0 {
  44. return nil, errors.New("更新数据失败")
  45. }
  46. return exist, nil
  47. }
  48. return nil, errors.New("文件" + material.Hash + "已存在")
  49. }
  50. err = m.query.WithContext(m.ctx).Create(material)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return material, nil
  55. }
  56. func (m *Material) Count(uid int64) int64 {
  57. var count int64 = 0
  58. var err error
  59. if uid > 0 {
  60. count, err = m.query.WithContext(m.ctx).Where(m.query.UID.Eq(uid)).Count()
  61. } else {
  62. count, err = m.query.WithContext(m.ctx).Count()
  63. }
  64. if err != nil {
  65. return 0
  66. }
  67. return count
  68. }
  69. func (m *Material) Lists(uid int64, page, pageSize int) (Page, []*entity.Material) {
  70. count := m.Count(uid)
  71. pages := m.InitPages(count, page, pageSize)
  72. if count == 0 {
  73. return pages, nil
  74. }
  75. var lists []*entity.Material
  76. var err error
  77. if uid > 0 {
  78. lists, err = m.query.WithContext(m.ctx).Where(m.query.UID.Eq(uid)).Offset(pages.LimitStart).Limit(pageSize).Where(m.query.UID.Eq(uid)).Find()
  79. } else {
  80. lists, err = m.query.WithContext(m.ctx).Offset(pages.LimitStart).Limit(pageSize).Find()
  81. }
  82. if err != nil {
  83. return pages, nil
  84. }
  85. return pages, lists
  86. }
  87. func (m *Material) Materials(uid int64) []*entity.Material {
  88. materials, _ := m.query.WithContext(m.ctx).Select(m.query.ID, m.query.UID, m.query.Name, m.query.File, m.query.Type).Where(m.query.UID.Eq(uid), m.query.Status.Eq(1)).Find()
  89. return materials
  90. }
  91. func (m *Material) Remove(ID int64) (bool, error) {
  92. exist, err := m.Info(ID)
  93. if err != nil {
  94. return false, err
  95. }
  96. if exist.UID == 0 {
  97. return false, errors.New("未找到相关记录")
  98. }
  99. //如果是被删除了,就更新此数据
  100. info, err := m.query.WithContext(m.ctx).Where(m.query.ID.Eq(ID)).Updates(map[string]interface{}{"Status": -1})
  101. if err != nil {
  102. return false, err
  103. }
  104. if info.RowsAffected == 0 {
  105. return false, errors.New("更新数据失败")
  106. }
  107. return true, nil
  108. }