bulletin.go 3.5 KB
package models

import (
	"fmt"
	"oppmg/utils"
	"time"

	"github.com/astaxie/beego/orm"
)

type Bulletin struct {
	Id             int       `orm:"column(id);auto"`
	Title          string    `orm:"column(title);size(2000);null" description:"标题"`
	Content        string    `orm:"column(content);null" description:"内容"`
	Cover          string    `orm:"column(cover);size(255);null" description:"封面地址"`
	W              int       `orm:"column(w);null" description:"宽"`
	H              int       `orm:"column(h);null" description:"高"`
	Type           int8      `orm:"column(type);null" description:"公告类型(1图+跳转链接、2纯文本)"`
	Receiver       string    `orm:"column(receiver);null" description:"接收者"`
	QuestionSwitch int8      `orm:"column(question_switch);null" description:"设置问题开关 (是否有问题)"`
	CreateAt       time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
	UpdateAt       time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
	AllowClose     int8      `orm:"column(allow_close);null" description:"允许关闭公告(0允许关闭窗口,1阅读完才能关闭,2选项打勾后才能关闭)"`
	//AllowCondition int8      `orm:"column(allow_condition);null" description:"关闭条件 (1(bit 0):公告内容查看完 2(bit 1):回答完问题)"`
	CompanyId int64 `orm:"column(company_id);null" description:"公司Id"`
	Status    int8  `orm:"column(status)" description:"状态 1-下架 2-上架"`
	AllPeople int8  `orm:"column(all_people);null" description:"是否是所有人 0:否 1:是"`
}

func (t *Bulletin) TableName() string {
	return "bulletin"
}

func init() {
	orm.RegisterModel(new(Bulletin))
}

// AddBulletin insert a new Bulletin into database and returns
// last inserted Id on success.
func AddBulletin(m *Bulletin) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetBulletinById retrieves Bulletin by Id. Returns error if
// Id doesn't exist
func GetBulletinById(id int) (v *Bulletin, err error) {
	o := orm.NewOrm()
	v = &Bulletin{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateBulletin updates Bulletin by Id and returns error if
// the record to be updated doesn't exist
func UpdateBulletinById(o orm.Ormer, m *Bulletin) (err error) {
	//o := orm.NewOrm()
	v := Bulletin{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteBulletin deletes Bulletin by Id and returns error if
// the record to be deleted doesn't exist
func DeleteBulletin(id int) (err error) {
	o := orm.NewOrm()
	v := Bulletin{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&Bulletin{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

func GetBulletins(companyId int64, status int8, offset, pageSize int) (v []*Bulletin, total int, err error) {
	sql := "select * from bulletin where (status=? or ?=0) and company_id =? order by create_at desc limit ?,?"
	o := orm.NewOrm()
	if err = utils.ExecuteQueryAllWithOrmer(o, &v, sql, status, status, companyId, offset, pageSize); err != nil {
		return
	}

	sqlTotal := "select count(1) from bulletin where (status=? or ?=0) and company_id =?"
	if err = o.Raw(sqlTotal, status, status, companyId).QueryRow(&total); err != nil {
		return
	}
	return
}