作者 tangxvhui

榜单设置

package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
serverank "oppmg/services/rank"
)
type RankController struct {
BaseController
}
//GetRankType ..
//@router /rank/list
func (c RankController) GetRankType() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
companyid := c.GetCompanyId()
rspData := serverank.GetRankList(companyid)
msg = protocol.NewReturnResponse(rspData, nil)
return
}
//EditRankType ...
//@router /rank/edit
func (c RankController) EditRankType() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := serverank.UpdateRankType(param.Id, param.Name, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
//EditRankType ...
//@router /rank/forbid_allow
func (c RankController) RankTypeForbidAllow() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Id int64 `json:"id"`
status int `json:"status"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
var err error
companyid := c.GetCompanyId()
switch param.status {
case 1:
err = serverank.ForbidRank(param.Id, companyid)
case 2:
err = serverank.AllowRank(param.Id, companyid)
}
msg = protocol.NewReturnResponse(nil, err)
return
}
//RankSeasonList ...
//@router /rank/season/list
func (c RankController) RankSeasonList() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestPageInfo
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
rspData, err := serverank.RankSeasonList(param.PageIndex, param.PageSize, companyid)
msg = protocol.NewReturnResponse(rspData, err)
return
}
//RankSeasonAdd ...
//@router /rank/season/add
func (c RankController) RankSeasonAdd() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Name string `json:"name"`
BeginTime string `json:"begin_time"`
EndTime string `json:"end_time"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
var (
beginTime int64
endTime int64
)
companyid := c.GetCompanyId()
err := serverank.AddRankSeason(beginTime, endTime, param.Name, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
//RankSeasonEdit ...
//@router /rank/season/edit
func (c RankController) RankSeasonEdit() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Id int64 `json:"id"`
Name string `json:"name"`
BeginTime string `json:"begin_time"`
EndTime string `json:"end_time"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
var (
beginTime int64
endTime int64
)
companyid := c.GetCompanyId()
err := serverank.EditRankSeason(param.Id, beginTime, endTime, param.Name, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type RankPeriod struct {
Id int64 `orm:"column(id);auto" description:"主键"`
CompanyId int64 `orm:"column(company_id);null" description:"公司编号 表company.id"`
SeasonName string `orm:"column(season_name);size(50);null" description:"赛季名称"`
BeginTime time.Time `orm:"column(begin_time);type(timestamp);null" description:"开始时间"`
EndTime time.Time `orm:"column(end_time);type(timestamp);null" description:"结束时间"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
Status int8 `orm:"column(status);null" description:"状态 0:未开始 1:开始 2:结束 "`
RankRangeBack string `orm:"column(rank_range_back)" description:"赛季结束时,备份赛季参与人设置"`
RankItemBack string `orm:"column(rank_item_back)" description:"赛季结束时,备份赛季评比项设置"`
}
func (t *RankPeriod) TableName() string {
return "rank_period"
}
func init() {
orm.RegisterModel(new(RankPeriod))
}
//状态 0:未开始 1:开始 2:结束
const (
RANKPERIOD_STATUS_NOT int8 = 0
RANKPERIOD_STATUS_BEGIN int8 = 1
RANKPERIOD_STATUS_END int8 = 2
)
// AddNewRankPeriod insert a new RankPeriod into database and returns
// last inserted Id on success.
func AddRankPeriod(m *RankPeriod) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetNewRankPeriodById retrieves RankPeriod by Id. Returns error if
// Id doesn't exist
func GetRankPeriodById(id int64) (v *RankPeriod, err error) {
o := orm.NewOrm()
v = &RankPeriod{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateNewRankPeriod updates RankPeriod by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankPeriodById(m *RankPeriod) (err error) {
o := orm.NewOrm()
v := RankPeriod{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
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type NewRankRange struct {
Id int `orm:"column(id);auto"`
Name string `orm:"column(name);size(50);null" description:"名称"`
CompanyId int `orm:"column(company_id);null" description:"公司编号 表company.id"`
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:"更新时间"`
}
func (t *NewRankRange) TableName() string {
return "rank_range"
}
func init() {
orm.RegisterModel(new(NewRankRange))
}
// AddNewRankRange insert a new NewRankRange into database and returns
// last inserted Id on success.
func AddNewRankRange(m *NewRankRange) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetNewRankRangeById retrieves NewRankRange by Id. Returns error if
// Id doesn't exist
func GetNewRankRangeById(id int) (v *NewRankRange, err error) {
o := orm.NewOrm()
v = &NewRankRange{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllNewRankRange retrieves all NewRankRange matches certain condition. Returns empty list if
// no records exist
func GetAllNewRankRange(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(NewRankRange))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []NewRankRange
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdateNewRankRange updates NewRankRange by Id and returns error if
// the record to be updated doesn't exist
func UpdateNewRankRangeById(m *NewRankRange) (err error) {
o := orm.NewOrm()
v := NewRankRange{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
}
// DeleteNewRankRange deletes NewRankRange by Id and returns error if
// the record to be deleted doesn't exist
func DeleteNewRankRange(id int) (err error) {
o := orm.NewOrm()
v := NewRankRange{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&NewRankRange{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type RankType struct {
Id int64 `orm:"column(id);pk"`
CompanyId int64 `orm:"column(company_id)"`
Name string `orm:"column(name)"`
EnableStatus int8 `orm:"column(enable_status)"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdataAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
Type int8 `orm:"column(type)"`
}
func (t *RankType) TableName() string {
return "rank_type"
}
func init() {
orm.RegisterModel(new(RankType))
}
//榜单类型
const (
RANK_TYPE_SEASON int8 = 1 //季赛榜
RANK_TYPE_YEAR int8 = 2 //年榜
)
//榜单状态
const (
RANK_STATUS_YES int8 = 1 //启用
RANK_STATUS_NO int8 = 2 //禁用
)
// AddRank insert a new Rank into database and returns
// last inserted Id on success.
func AddRank(m *RankType) (id int64, err error) {
o := orm.NewOrm()
nowTime := time.Now()
m.CreateAt = nowTime
m.UpdataAt = nowTime
id, err = o.Insert(m)
return
}
// GetRankById retrieves Rank by Id. Returns error if
// Id doesn't exist
func GetRankById(id int64) (v *RankType, err error) {
o := orm.NewOrm()
v = &RankType{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRank updates Rank by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankById(m *RankType, cols []string, om ...orm.Ormer) (err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
m.UpdataAt = time.Now()
if len(cols) > 0 {
cols = append(cols, "UpdateAt")
}
var num int64
if num, err = o.Update(m, cols...); err == nil {
fmt.Println("Number of records updated in database:", num)
}
return
}
... ...
package protocol
//获取公司的榜单类型列表
type ResponseRankType struct {
Id int `json:"id" orm:"column(id)"`
Name string `json:"name" orm:"column(name)"`
Status int8 `json:"status" orm:"column(status)"`
RankType int8 `json:"rank_type" orm:"column(rank_type)"`
}
// ResponseRankSeasonList 赛季列表
type ResponseRankSeasonList struct {
ResponsePageInfo
List []RankSeasonItem `json:"lists"`
}
type RankSeasonItem struct {
Id int64 `json:"id" orm:"column(id)"`
SeasonName string `json:"season_name" orm:"column(season_name)"`
BeginTime string `json:"begin_time" orm:"column(begin_time)"`
EndTime string `json:"end_time" orm:"column(end_time)"`
Status int8 `json:"status" orm:"column(status)"`
}
... ...
... ... @@ -103,6 +103,14 @@ func init() {
beego.NSRouter("/info", &controllers.AuditController{}, "post:AuditInfo"),
beego.NSRouter("/allow_forbid", &controllers.AuditController{}, "post:AllowForbidAudit"),
),
beego.NSNamespace("/rank",
beego.NSRouter("/list", &controllers.RankController{}, "post:GetRankType"),
beego.NSRouter("/edit", &controllers.RankController{}, "post:EditRankType"),
beego.NSRouter("/forbid_allow", &controllers.RankController{}, "post:RankTypeForbidAllow"),
beego.NSRouter("/season/add", &controllers.RankController{}, "post:RankSeasonAdd"),
beego.NSRouter("/season/list", &controllers.RankController{}, "post:RankSeasonList"),
beego.NSRouter("/season/edit", &controllers.RankController{}, "post:RankSeasonEdit"),
),
)
nsAuth := beego.NewNamespace("/auth",
... ...
... ... @@ -98,33 +98,7 @@ func buildSqlForAuditList(usercompanyid int64, companyid int64, userid int64) st
unionsql = sql1 + " UNION " + sql2
return fmt.Sprintf(allsql, unionsql)
}
// //获取权限
// if ok := redisdata.ExistUserPermission(userid); !ok {
// //尝试重数据库获取
// log.Debug("从数据库读取权限")
// permissionMap, err := serverabc.GetUserPermission(usercompanyid, serverabc.M_SYSTEM_OPPORTUNITY)
// if err != nil {
// log.Debug("从数据库未获得对应权限 :%s", err)
// unionsql = sql1 + " UNION " + sql2
// return fmt.Sprintf(allsql, unionsql)
// }
// if v, ok := permissionMap[serverabc.M_SYSTEM_OPPORTUNITY]; !ok {
// unionsql = sql1 + " UNION " + sql2
// return fmt.Sprintf(allsql, unionsql)
// } else {
// permissionObject = v
// }
// } else {
// //使用缓存
// log.Debug("从缓存读取权限")
// permissionObject, err = redisdata.GetUserPermission(userid, serverabc.M_SYSTEM_OPPORTUNITY)
// if err != nil {
// log.Debug("从缓存未获得对应权限 :%s", err)
// unionsql = sql1 + " UNION " + sql2
// return fmt.Sprintf(allsql, unionsql)
// }
// }
// -------------------------
var (
usrPermission *serverabc.OptionOpportunity
ok bool
... ...
package rank
import (
"fmt"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"time"
)
//GetRankList 获取榜单类型列表
func GetRankList(companyid int64) []protocol.ResponseRankType {
var (
rspData = make([]protocol.ResponseRankType, 0)
err error
)
sqlString := `SELECT id,name,status,rank_type FROM rank WHERE company_id=?`
err = utils.ExecuteQueryAll(&rspData, sqlString, companyid)
if err != nil {
log.Error("获取榜单列表失败,err%s", err)
return rspData
}
if len(rspData) >= 2 {
return rspData
}
if len(rspData) < 2 {
AddDefaultRankType(companyid)
}
err = utils.ExecuteQueryAll(&rspData, sqlString, companyid)
if err != nil {
log.Error("获取榜单列表失败,err%s", err)
return rspData
}
return rspData
}
func AddDefaultRankType(companyid int64) []*models.RankType {
var (
rankA *models.RankType
rankB *models.RankType
err error
)
rankA = &models.RankType{
CompanyId: companyid,
Name: "季赛榜",
Type: models.RANK_TYPE_SEASON,
EnableStatus: models.RANK_STATUS_YES,
}
rankB = &models.RankType{
CompanyId: companyid,
Name: "年榜",
Type: models.RANK_TYPE_YEAR,
EnableStatus: models.RANK_STATUS_YES,
}
_, err = models.AddRank(rankA)
if err != nil {
log.Error("添加榜单失败:AddRank,err:%s", err)
}
_, err = models.AddRank(rankB)
if err != nil {
log.Error("添加榜单失败:AddRank,err:%s", err)
}
ranks := []*models.RankType{
rankA, rankB,
}
return ranks
}
func UpdateRankType(id int64, name string, companyid int64) error {
var (
err error
ranktype *models.RankType
)
ranktype, err = models.GetRankById(id)
if err != nil {
log.Error("获取榜单类型数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if ranktype.CompanyId != companyid {
log.Error("数据无法与公司id对应")
return protocol.NewErrWithMessage("1")
}
ranktype.Name = name
err = models.UpdateRankById(ranktype, []string{"Name"})
if err != nil {
log.Error("更新榜单数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
//ForbidRank 禁用榜单
func ForbidRank(id int64, companyid int64) error {
var (
err error
ranktype *models.RankType
)
ranktype, err = models.GetRankById(id)
if err != nil {
log.Error("获取榜单类型数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if ranktype.CompanyId != companyid {
log.Error("数据无法与公司id对应")
return protocol.NewErrWithMessage("1")
}
ranktype.EnableStatus = models.RANK_STATUS_NO
err = models.UpdateRankById(ranktype, []string{"EnableStatus"})
if err != nil {
log.Error("更新榜单数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
//AllowRank 启用榜单
func AllowRank(id int64, companyid int64) error {
var (
err error
ranktype *models.RankType
)
ranktype, err = models.GetRankById(id)
if err != nil {
log.Error("获取榜单类型数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if ranktype.CompanyId != companyid {
log.Error("数据无法与公司id对应")
return protocol.NewErrWithMessage("1")
}
ranktype.EnableStatus = models.RANK_STATUS_YES
err = models.UpdateRankById(ranktype, []string{"EnableStatus"})
if err != nil {
log.Error("更新榜单数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
//RankSeasonList ..获取赛季列表
func RankSeasonList(pageindex int, pagesize int, companyid int64) (protocol.ResponseRankSeasonList, error) {
var (
err error
listdata []protocol.RankSeasonItem
cnt int64
)
rspData := protocol.ResponseRankSeasonList{
ResponsePageInfo: protocol.ResponsePageInfo{
TotalPage: 0, CurrentPage: pageindex,
},
List: make([]protocol.RankSeasonItem, 0),
}
pageStart := (pageindex - 1) * pagesize
datasql := `SELECT id,season_name,begin_time,end_time,status FROM rank_period WHERE company_id = ?`
datasql = fmt.Sprintf("%s ORDER BY begin_time DESC limit %d,%d", datasql, pageStart, pagesize)
countsql := `SELECT COUNT(*) FROM rank_period WHERE company_id = ?`
err = utils.ExecuteQueryOne(&cnt, countsql, companyid)
if err != nil {
log.Error("EXCUTE SQL ERR:%s", err)
return rspData, nil
}
if cnt <= 0 {
return rspData, nil
}
err = utils.ExecuteQueryAll(&listdata, datasql, companyid)
if err != nil {
log.Error("EXCUTE SQL ERR:%s", err)
return rspData, nil
}
return rspData, err
}
func AddRankSeason(beginTime int64, endTime int64, name string, companyid int64) error {
var (
err error
)
m := &models.RankPeriod{
CompanyId: companyid,
SeasonName: name,
BeginTime: time.Unix(beginTime, 0),
EndTime: time.Unix(endTime, 0),
Status: 0,
}
_, err = models.AddRankPeriod(m)
if err != nil {
log.Error("添加赛季失败;%s", err)
}
return nil
}
func EditRankSeason(id int64, beginTime int64, endTime int64, name string, companyid int64) error {
var (
err error
m *models.RankPeriod
)
m, err = models.GetRankPeriodById(id)
if err != nil {
log.Error("获取赛季失败;%s", err)
return protocol.NewErrWithMessage("1")
}
if m.CompanyId != companyid {
log.Error("公司不匹配")
return protocol.NewErrWithMessage("1")
}
m.SeasonName = name
m.BeginTime = time.Unix(beginTime, 0)
m.EndTime = time.Unix(endTime, 0)
err = models.UpdateRankPeriodById(m)
if err != nil {
log.Error("更新赛季失败;%s", err)
}
return nil
}
//获取赛季参与人列表
func GetRankRangeList() {
}
... ...