123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package service
- import (
- "context"
- "errors"
- "icloudapp.cn/tools/service/entity"
- "icloudapp.cn/tools/service/model"
- )
- type Material struct {
- Base
- ctx context.Context
- query *model.MMaterial
- }
- func NewMaterial(ctx context.Context) *Material {
- material := &Material{ctx: ctx}
- material.query = model.Material
- return material
- }
- func (m *Material) Info(id int64) (*entity.Material, error) {
- return m.query.WithContext(m.ctx).Where(m.query.ID.Eq(id)).Take()
- }
- func (m *Material) Infos(ids ...int64) ([]*entity.Material, error) {
- return m.query.WithContext(m.ctx).Where(m.query.ID.In(ids...)).Find()
- }
- func (m *Material) InfoByUidAndHash(uid int64, hash string) (*entity.Material, error) {
- return m.query.WithContext(m.ctx).Where(m.query.UID.Eq(uid), m.query.Hash.Eq(hash)).Take()
- }
- func (m *Material) Add(material *entity.Material) (*entity.Material, error) {
- exist, err := m.InfoByUidAndHash(material.UID, material.Hash)
- if err != nil {
- return nil, err
- }
- if exist.UID > 0 {
-
- if exist.Status < 1 {
- exist.Status = 1
- affected, err := m.query.WithContext(m.ctx).Where(m.query.ID.Eq(exist.ID)).Update(m.query.Status, -1)
- if err != nil {
- return nil, err
- }
- if affected.RowsAffected == 0 {
- return nil, errors.New("更新数据失败")
- }
- return exist, nil
- }
- return nil, errors.New("文件" + material.Hash + "已存在")
- }
- err = m.query.WithContext(m.ctx).Create(material)
- if err != nil {
- return nil, err
- }
- return material, nil
- }
- func (m *Material) Count(uid int64) int64 {
- var count int64 = 0
- var err error
- if uid > 0 {
- count, err = m.query.WithContext(m.ctx).Where(m.query.UID.Eq(uid)).Count()
- } else {
- count, err = m.query.WithContext(m.ctx).Count()
- }
- if err != nil {
- return 0
- }
- return count
- }
- func (m *Material) Lists(uid int64, page, pageSize int) (Page, []*entity.Material) {
- count := m.Count(uid)
- pages := m.InitPages(count, page, pageSize)
- if count == 0 {
- return pages, nil
- }
- var lists []*entity.Material
- var err error
- if uid > 0 {
- 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()
- } else {
- lists, err = m.query.WithContext(m.ctx).Offset(pages.LimitStart).Limit(pageSize).Find()
- }
- if err != nil {
- return pages, nil
- }
- return pages, lists
- }
- func (m *Material) Materials(uid int64) []*entity.Material {
- 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()
- return materials
- }
- func (m *Material) Remove(ID int64) (bool, error) {
- exist, err := m.Info(ID)
- if err != nil {
- return false, err
- }
- if exist.UID == 0 {
- return false, errors.New("未找到相关记录")
- }
-
- info, err := m.query.WithContext(m.ctx).Where(m.query.ID.Eq(ID)).Updates(map[string]interface{}{"Status": -1})
- if err != nil {
- return false, err
- }
- if info.RowsAffected == 0 {
- return false, errors.New("更新数据失败")
- }
- return true, nil
- }
|