achevement_score.go 2.0 KB
package models

import (
	"time"

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

type AchevementScore struct {
	Id               int64     `orm:"column(id);auto"`
	GraspScoreRemain float64   `orm:"column(grasp_score_remain);null;digits(10);decimals(1)" description:"福利池总分 ,来源achievement中的评分"`
	CompanyId        int64     `orm:"column(company_id)"`
	CreateAt         time.Time `orm:"column(create_at);type(timestamp);null"`
	UpdateAt         time.Time `orm:"column(update_at);type(timestamp);null"`
}

func (t *AchevementScore) TableName() string {
	return "achevement_score"
}

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

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

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

func GetAchevementScoreByCompanyId(companyid int64) (v *AchevementScore, err error) {
	o := orm.NewOrm()
	v = &AchevementScore{}
	ok := o.QueryTable(&AchevementScore{}).
		Filter("company_id", companyid).
		Exist()
	if !ok {
		return v, nil
	}
	err = o.QueryTable(&AchevementScore{}).
		Filter("company_id", companyid).
		One(v)
	return v, err
}

func IncreaseAchevementScore(companyid int64, addScore float64, o orm.Ormer) error {
	v := &AchevementScore{}
	ok := o.QueryTable(&AchevementScore{}).
		Filter("company_id", companyid).
		Exist()
	if ok {
		//存在
		_, err := o.QueryTable(&AchevementScore{}).
			Filter("company_id", companyid).
			Update(orm.Params{
				"grasp_score_remain": orm.ColValue(orm.ColAdd, addScore),
				"update_time":        time.Now().String(),
			})
		return err
	}
	//不存在
	v.CompanyId = companyid
	v.GraspScoreRemain = addScore
	v.CreateAt = time.Now()
	_, err := o.Insert(v)
	return err
}