position.go 2.2 KB
package models

import (
	"fmt"
	"time"

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

type Position struct {
	Id           int       `orm:"column(id);auto" description:"职位表id"`
	CompanyId    int64     `orm:"column(company_id)" description:"表company.id 公司编号"`
	Name         string    `orm:"column(name);size(100)" description:"职位名称"`
	ParentId     int       `orm:"column(parent_id)" description:"父级id"`
	Relation     string    `orm:"column(relation);size(400)" description:"父子级关系树"`
	CreateAt     time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
	UpdateAt     time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
	EnableStatus string    `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"`
}

func (t *Position) TableName() string {
	return "position"
}

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

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

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

// UpdatePosition updates Position by Id and returns error if
// the record to be updated doesn't exist
func UpdatePositionById(m *Position) (err error) {
	o := orm.NewOrm()
	v := Position{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
}

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