Files
2022-02-09 17:35:31 +08:00

53 lines
1.4 KiB
Go

package model
import (
"SciencesServer/app/common/model"
"SciencesServer/serve/orm"
"fmt"
"strings"
)
type UserPatent struct {
*model.UserPatent
}
type UserPatentInfo struct {
model.Model
Title string `json:"title"`
ApplyAt string `json:"apply_at"`
Description string `json:"description"`
}
// Patents 专利信息
func (m *UserPatent) Patents(uid []uint64, page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*UserPatentInfo, error) {
_uids := make([]string, 0)
for _, u := range uid {
_uids = append(_uids, fmt.Sprintf("%d", u))
}
db := orm.GetDB().Table(model.NewTechnologyPatent().TableName()+" AS p").
Select("p.id", "p.title", "p.apply_at", "p.description").
Joins(fmt.Sprintf("RIGHT JOIN (SELECT patent_id FROM %s WHERE uid IN (%v) is_deleted = %d patent_id) AS u ON p.id = u.patent_id",
model.NewTechnologyPatent().TableName(), strings.Join(_uids, ","), model.DeleteStatusForNot)).
Where("u.is_deleted = ?", model.DeleteStatusForNot)
if len(where) > 0 {
for _, v := range where {
db = db.Where(v.Condition, v.Value)
}
}
out := make([]*UserPatentInfo, 0)
if err := db.Count(count).Error; err != nil {
return nil, err
}
if err := db.Order("p.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
func NewUserPatent() *UserPatent {
return &UserPatent{model.NewUserPatent()}
}