-
Notifications
You must be signed in to change notification settings - Fork 38
/
adlist.go
64 lines (60 loc) · 1.6 KB
/
adlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package taglibs
import (
"github.com/CloudyKit/jet"
"github.com/go-xorm/xorm"
"github.com/xiusin/pine"
"github.com/xiusin/pinecms/src/application/controllers"
"github.com/xiusin/pinecms/src/application/models/tables"
"github.com/xiusin/pinecms/src/common/helper"
"reflect"
"strconv"
"strings"
"time"
)
/**
广告标签
{{yield adlist(id=3, pos="首页banner图")}}
返回一组广告
*/
func AdList(args jet.Arguments) reflect.Value {
if !checkArgType(&args) {
return defaultArrReturnVal
}
id := args.Get(0)
var ids []string
if isNumber(id) {
ids = append(ids, strconv.Itoa(int(getNumber(id))))
} else if id.String() != "" {
ids = strings.Split(id.String(), ",")
}
order := args.Get(2).String()
if order == "" {
order = "listorder desc"
}
orm := pine.Make(controllers.ServiceXorm).(*xorm.Engine).OrderBy(order)
// 获取广告位信息
var pos = args.Get(1)
switch pos.Type().String() {
case "string":
if len(pos.String()) > 0 {
advPos := &tables.AdvertSpace{}
exists ,_ := pine.Make(controllers.ServiceXorm).(*xorm.Engine).Table(advPos).Where("name = ?", pos.String()).Get(advPos)
if !exists {
return reflect.ValueOf([]tables.Advert{})
}
orm.Where("space_id = ?", advPos.Id)
}
default:
if pid := getNumber(pos); pid > 0 {
orm.Where("space_id = ?", pid)
}
}
if len(ids) > 0 {
orm.In("id", ids)
}
now := time.Now().In(helper.GetLocation()).Format(helper.TimeFormat)
orm.Where("status = 1").Where("start_time <= ?", now).Where("end_time >= ?", now).Select("id, name, image, link_url")
var advs = []tables.Advert{}
orm.Find(&advs)
return reflect.ValueOf(advs)
}