作者 tangxvhui

赛季配置

... ... @@ -3,6 +3,7 @@ package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
serverank "oppmg/services/rank"
)
... ... @@ -152,3 +153,128 @@ func (c RankController) RankSeasonEdit() {
msg = protocol.NewReturnResponse(nil, err)
return
}
//RankRangeList ...
//@router /rank/range/list
func (c RankController) RankRangeList() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
companyid := c.GetCompanyId()
rspData := serverank.GetRankRangeList(companyid)
msg = protocol.NewReturnResponse(rspData, nil)
return
}
//RankRangeAdd ...
//@router /rank/range/add
func (c RankController) RankRangeAdd() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Name string `json:"name"`
RangeType int8 `json:"range_type"`
RelationId []int64 `json:"relation_id"`
}
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
}
switch param.RangeType {
case 1:
//员工
if len(param.RelationId) == 0 {
param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEEALL
} else {
param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEE
}
case 2:
//部门
if len(param.RelationId) == 0 {
param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENTALL
} else {
param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENT
}
default:
log.Error("param.RangeType err")
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := serverank.AddRankRange(param.Name, param.RangeType, param.RelationId, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
//RankRangeEdit ...
//@router /rank/range/edit
func (c RankController) RankRangeEdit() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Id int64 `json:"id"`
Name string `json:"name"`
RangeType int8 `json:"range_type"`
RelationId []int64 `json:"relation_id"`
}
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
}
switch param.RangeType {
case 1:
//员工
if len(param.RelationId) == 0 {
param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEEALL
} else {
param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEE
}
case 2:
//部门
if len(param.RelationId) == 0 {
param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENTALL
} else {
param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENT
}
default:
log.Error("param.RangeType err")
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := serverank.EditRankRange(param.Id, param.Name, param.RangeType, param.RelationId, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
//RankRangeInfo ...
//@router /rank/range/info
func (c RankController) RankRangeInfo() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Id int64 `json:"id"`
}
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()
rspData := serverank.GetRankRangeInfo(param.Id)
msg = protocol.NewReturnResponse(rspData, nil)
return
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"oppmg/common/log"
"time"
"github.com/astaxie/beego/orm"
)
type NewRankRange struct {
Id int `orm:"column(id);auto"`
type RankRange struct {
Id int64 `orm:"column(id);auto"`
Name string `orm:"column(name);size(50);null" description:"名称"`
CompanyId int `orm:"column(company_id);null" description:"公司编号 表company.id"`
CompanyId int64 `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 {
func (t *RankRange) TableName() string {
return "rank_range"
}
func init() {
orm.RegisterModel(new(NewRankRange))
orm.RegisterModel(new(RankRange))
}
// AddNewRankRange insert a new NewRankRange into database and returns
//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 AddNewRankRange(m *NewRankRange) (id int64, err error) {
o := orm.NewOrm()
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
}
// GetNewRankRangeById retrieves NewRankRange by Id. Returns error if
// GetRankRangeById retrieves RankRange by Id. Returns error if
// Id doesn't exist
func GetNewRankRangeById(id int) (v *NewRankRange, err error) {
func GetRankRangeById(id int64) (v *RankRange, err error) {
o := orm.NewOrm()
v = &NewRankRange{Id: id}
v = &RankRange{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")
}
// 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 {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
o = orm.NewOrm()
}
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
var num int64
m.UpdateAt = time.Now()
if len(cols) > 0 {
cols = append(cols, "UpdateAt")
}
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)
}
if num, err = o.Update(m, cols...); err == nil {
log.Info("Number of records updated in database:%d", num)
}
return
}
// DeleteNewRankRange deletes NewRankRange by Id and returns error if
// DeleteRankRange deletes RankRange by Id and returns error if
// the record to be deleted doesn't exist
func DeleteNewRankRange(id int) (err error) {
func DeleteRankRange(id int64) (err error) {
o := orm.NewOrm()
v := NewRankRange{Id: id}
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(&NewRankRange{Id: id}); err == nil {
if num, err = o.Delete(&RankRange{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
... ...
package models
import (
"github.com/astaxie/beego/orm"
)
type RankRangeData struct {
Id int64 `orm:"column(id);auto"`
RankRangeId int64 `orm:"column(rank_range_id)"`
RangeType int8 `orm:"column(range_type);" description:"类型"`
RelationId int64 `orm:"column(relation_id);"`
}
func (t *RankRangeData) TableName() string {
return "rank_range_data"
}
func init() {
orm.RegisterModel(new(RankRangeData))
}
// AddRankRangeData insert a new RankRangeData into database and returns
// last inserted Id on success.
func AddRankRangeDataMulti(m []RankRangeData, om ...orm.Ormer) (successNums int64, err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
successNums, err = o.InsertMulti(20, m)
return
}
// GetRankRangeDataById retrieves RankRangeData by Id. Returns error if
// Id doesn't exist
func GetRankRangeDataById(id int64) (v *RankRangeData, err error) {
o := orm.NewOrm()
v = &RankRangeData{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// DeleteRankRangeData deletes RankRangeData by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankRangeData(id []int64, om orm.Ormer) (err error) {
_, err = om.QueryTable(&RankRangeData{}).
Filter("id__in", id).
Delete()
return err
}
func GetRankRangeDataByRangeId(id int64) ([]RankRangeData, error) {
var (
data = make([]RankRangeData, 0)
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&RankRangeData{}).
Filter("rank_range_id", id).
All(&data)
if err == orm.ErrNoRows {
return data, nil
}
if err != nil {
return nil, err
}
return data, nil
}
... ...
... ... @@ -20,3 +20,23 @@ type RankSeasonItem struct {
EndTime string `json:"end_time" orm:"column(end_time)"`
Status int8 `json:"status" orm:"column(status)"`
}
//RankRangeItem 赛季参与人列表项
type RankRangeItem struct {
Id int64 `json:"id" orm:"column(id)"`
Name string `json:"name" orm:"column(name)"`
RangeType int8 `json:"range_type" orm:"column(type)"`
}
//ResponseRankRangeInfo 赛季参与人详情
type ResponseRankRangeInfo struct {
Id int64 `json:"id"`
Name string `json:"name"`
RangeType int8 `json:"range_type"`
Relation []RankRangeRelation `json:"relation"`
}
type RankRangeRelation struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
... ...
... ... @@ -110,6 +110,10 @@ func init() {
beego.NSRouter("/season/add", &controllers.RankController{}, "post:RankSeasonAdd"),
beego.NSRouter("/season/list", &controllers.RankController{}, "post:RankSeasonList"),
beego.NSRouter("/season/edit", &controllers.RankController{}, "post:RankSeasonEdit"),
beego.NSRouter("/range/list", &controllers.RankController{}, "post:RankRangeList"),
beego.NSRouter("/range/edit", &controllers.RankController{}, "post:RankRangeEdit"),
beego.NSRouter("/range/add", &controllers.RankController{}, "post:RankRangeAdd"),
beego.NSRouter("/range/info", &controllers.RankController{}, "post:RankRangeInfo"),
),
)
... ...
... ... @@ -7,6 +7,8 @@ import (
"oppmg/protocol"
"oppmg/utils"
"time"
"github.com/astaxie/beego/orm"
)
//GetRankList 获取榜单类型列表
... ... @@ -216,6 +218,163 @@ func EditRankSeason(id int64, beginTime int64, endTime int64, name string, compa
}
//获取赛季参与人列表
func GetRankRangeList() {
func GetRankRangeList(companyid int64) []protocol.RankRangeItem {
var (
err error
rspData = make([]protocol.RankRangeItem, 0)
)
datasql := `SELECT a.id ,a.name,a.type FROM rank_range AS a WHERE company_id=?`
err = utils.ExecuteQueryAll(rspData, datasql, companyid)
if err != nil {
log.Error("获取参与人列表失败:%s", err)
}
return rspData
}
func AddRankRange(name string, rangetype int8, relationId []int64, companyid int64) error {
var (
rankRangeDatas []models.RankRangeData
rankRange *models.RankRange
err error
)
rankRange = &models.RankRange{
Name: name,
CompanyId: companyid,
Type: rangetype,
}
o := orm.NewOrm()
o.Begin()
_, err = models.AddRankRange(rankRange, o)
if err != nil {
o.Rollback()
log.Error("添加rank_range记录失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//TODO 去除其他分组的重复项
for i := range relationId {
m := models.RankRangeData{
RankRangeId: rankRange.Id,
RangeType: rangetype,
RelationId: relationId[i],
}
rankRangeDatas = append(rankRangeDatas, m)
}
_, err = models.AddRankRangeDataMulti(rankRangeDatas, o)
if err != nil {
o.Rollback()
log.Error("添加rank_range_data记录失败;%s", err)
return protocol.NewErrWithMessage("1")
}
o.Commit()
return nil
}
func EditRankRange(id int64, name string, rangetype int8, relationId []int64, companyid int64) error {
var (
rankRangeDatas []models.RankRangeData
rankRange *models.RankRange
err error
)
rankRange, err = models.GetRankRangeById(id)
if err != nil {
log.Error("获取rank_range数据失败;%s", err)
return protocol.NewErrWithMessage("1")
}
if rankRange.CompanyId != companyid {
log.Error("公司不匹配")
return protocol.NewErrWithMessage("1")
}
rankRangeDatas, err = models.GetRankRangeDataByRangeId(rankRange.Id)
if err != nil {
log.Error("获取rank_range_data数据失败;%s", err)
return protocol.NewErrWithMessage("1")
}
var (
oldids []int64
delids []int64
addids []int64
)
if rankRange.Type != rangetype {
//当rangeType不同时
delids = oldids
addids = relationId
} else {
for i := range rankRangeDatas {
oldids = append(oldids, rankRangeDatas[i].RelationId)
}
delids = utils.ArrayInt64Diff(oldids, relationId)
addids = utils.ArrayInt64Diff(relationId, oldids)
}
var (
addRangeData []models.RankRangeData
)
for i := range addids {
r := models.RankRangeData{
RankRangeId: id,
RangeType: rangetype,
RelationId: addids[i],
}
addRangeData = append(addRangeData, r)
}
rankRange.Name = name
rankRange.Type = rangetype
o := orm.NewOrm()
o.Begin()
//TODO 去除其他分组的重复项
err = models.UpdateRankRangeById(rankRange, []string{"Name", "Type"}, o)
if err != nil {
o.Rollback()
log.Error("添加rank_range数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if len(delids) > 0 {
err = models.DeleteRankRangeData(delids, o)
if err != nil {
o.Rollback()
log.Error("删除rank_range_data数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
}
if len(addids) > 0 {
_, err = models.AddRankRangeDataMulti(addRangeData, o)
if err != nil {
o.Rollback()
log.Error("添加rank_range_data数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
}
o.Commit()
return nil
}
func GetRankRangeInfo(id int64) protocol.ResponseRankRangeInfo {
var (
rankRange *models.RankRange
rankRangeData []models.RankRangeData
err error
)
rspdata := protocol.ResponseRankRangeInfo{
Relation: make([]protocol.RankRangeRelation, 0),
}
rankRange, err = models.GetRankRangeById(id)
if err != nil {
log.Error("获取rank_range数据失败:%s", err)
return rspdata
}
rspdata.Id = rankRange.Id
rspdata.Name = rankRange.Name
rspdata.RangeType = rankRange.Type
rankRangeData, _ = models.GetRankRangeDataByRangeId(id)
var relationIds []int64
for i := range rankRangeData {
relationIds = append(relationIds, rankRangeData[i].RelationId)
}
switch rankRange.Type {
case models.RANK_RANGE_TYPE_DEPARTMENT, models.RANK_RANGE_TYPE_DEPARTMENTALL:
//员工
case models.RANK_RANGE_TYPE_EMPLAYEE, models.RANK_RANGE_TYPE_EMPLAYEEALL:
//部门
}
return rspdata
}
... ...