|
- package models
-
- import (
- "github.com/evrone/go-clean-template/modules/log"
- "time"
- )
-
- type Resource struct {
- ID int64 `xorm:"pk autoincr"`
- ResourceCode string `xorm:"notnull "`
- PublisherId string `xorm:"notnull index"`
- Name string
- Industry string
- Status string `xorm:"notnull default(0)"`
- ResourceType string
- FileType string
- Description string
- ReposAddress string
- Version string
- License string
- Keywords string
- DownloadUrl string
- DownloadCount int64
- Size int32
- PublishTime time.Time
- CreateTime time.Time `xorm:"created"`
- UpdateTime time.Time `xorm:"updated"`
- }
-
- type ResourceSearchCondition struct {
- Name string
- ResourceType string
- FileType string
- Industry string
- PublisherId string
- PublisherStartTime time.Time
- PublisherEndTime time.Time
- PageSize int
- PageNum int
- }
-
- func InsertResource(r *Resource) bool {
- count, err := x.Insert(r)
- log.Errorf("insert resource error.e=%v", err)
- return err == nil && count > 0
- }
-
- func UpdateResource(r *Resource) bool {
- count, err := x.Where("resource_code=?", r.ResourceCode).Update(r)
- printErr(err, "UpdateResource")
- return err == nil && count > 0
- }
-
- func GetResource(r *Resource) (bool, *Resource) {
- has, err := x.Get(r)
- if err != nil {
- //todo print log
- log.Errorf("get resource error. resourceCode=%s", r.ResourceCode, err)
- return false, nil
- }
- if !has {
- return true, nil
- }
- return true, r
- }
-
- func FindResource(param *Resource) []Resource {
- r := make([]Resource, 0)
- err := x.Find(&r, param)
- if err != nil {
- //todo print log
- log.Errorf("FindResource error. param=%s, error=%v", param, err)
- return nil
- }
- return r
- }
-
- func FindResourceByParamLimit(param *Resource, pageSize int) []Resource {
- r := make([]Resource, 0)
- err := x.Limit(pageSize).Find(&r, param)
- if err != nil {
- //todo print log
- log.Errorf("FindResource error. param=%s, error=%v", param, err)
- return nil
- }
- return r
- }
-
- func SearchResource(req *ResourceSearchCondition) []Resource {
- sratIndex := (req.PageNum - 1) * req.PageSize
-
- s := x.Where("1=1")
- if req.Name != "" {
- name := "%" + req.Name + "%"
- s.And("name like ? or keywords like ?", name, name)
- }
- if req.ResourceType != "" {
- s.And("resource_type = ?", req.ResourceType)
- }
- if req.PublisherId != "" {
- s.And("publisher_id = ?", req.PublisherId)
- }
- if req.Industry != "" {
- s.And("industry = ?", req.Industry)
- }
- if req.FileType != "" {
- s.And("file_type = ?", req.FileType)
- }
- if !(req.PublisherStartTime.IsZero() || req.PublisherEndTime.IsZero()) {
- s.And("between ? and ?", req.PublisherStartTime.Format("2006/01/02"), req.PublisherEndTime.Format("2006/01/02"))
- }
- result := make([]Resource, 0)
-
- s.OrderBy("publish_time desc").Limit(req.PageSize, sratIndex).Find(&result)
- return result
- }
-
- func FindResourceOrderByPublishTimeDescLimit(pageSize int) []Resource {
- resourceList := make([]Resource, 0)
- x.OrderBy("publish_time desc").Limit(pageSize).Find(&resourceList)
- return resourceList
- }
-
- func FindResourceOrderByPublishTimeLimit(pageSize int) []Resource {
- resourceList := make([]Resource, 0)
- x.OrderBy("publish_time").Limit(pageSize).Find(&resourceList)
- return resourceList
- }
-
- func FindResourceLimit(pageSize int) []Resource {
- resourceList := make([]Resource, 0)
- x.Limit(pageSize).Find(&resourceList)
- return resourceList
- }
|