rank_range.go 2.8 KB
package models

import (
	"fmt"
	"oppmg/common/log"
	"time"

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

type RankRange struct {
	Id         int64     `orm:"column(id);auto"`
	Name       string    `orm:"column(name);size(50);null" description:"名称"`
	CompanyId  int64     `orm:"column(company_id);null" description:"公司编号 表company.id"`
	RankTypeId int64     `orm:"column(rank_type_id);null" description:""`
	Type       int8      `orm:"column(type);null" description:"1:所有员工 2:指定员工 3:所有部门 4:指定部门"`
	Data       string    `orm:"column(data);size(1000);null" description:"人员范围数据(type:2,4 有值 对于人员数据/部门数据)"`
	CreateAt   time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
	UpdateAt   time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
	SortNum    int       `orm:"column(sort_num)"`
	Status     int8      `orm:"column(status)"`
}

func (t *RankRange) TableName() string {
	return "rank_range"
}

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

//1:所有员工 2:指定员工 3:所有部门 4:指定部门
const (
	RANK_RANGE_TYPE_EMPLAYEEALL   int8 = 1
	RANK_RANGE_TYPE_EMPLAYEE      int8 = 2
	RANK_RANGE_TYPE_DEPARTMENTALL int8 = 3
	RANK_RANGE_TYPE_DEPARTMENT    int8 = 4
)

// AddRankRange insert a new RankRange into database and returns
// last inserted Id on success.
func AddRankRange(m *RankRange, om ...orm.Ormer) (id int64, err error) {
	var o orm.Ormer
	if len(om) > 0 {
		o = om[0]
	} else {
		o = orm.NewOrm()
	}
	m.CreateAt = time.Now()
	m.UpdateAt = time.Now()
	id, err = o.Insert(m)
	return
}

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

// UpdateRankRangeById updates RankRange by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankRangeById(m *RankRange, cols []string, om ...orm.Ormer) (err error) {
	var o orm.Ormer
	if len(om) > 0 {
		o = om[0]
	} else {
		o = orm.NewOrm()
	}
	var num int64
	m.UpdateAt = time.Now()
	if len(cols) > 0 {
		cols = append(cols, "UpdateAt")
	}
	if num, err = o.Update(m, cols...); err == nil {
		log.Info("Number of records updated in database:%d", num)
	}
	return
}

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