作者 tangxvhui

赛季设置调整

... ... @@ -287,3 +287,74 @@ func (c RankController) RankRangeInfo() {
msg = protocol.NewReturnResponse(rspData, nil)
return
}
//RankRangeSort ...
//@router /rank/range/sort
func (c RankController) RankRangeSort() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
RankTypeId int64 `json:"rank_type_id"`
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
}
err := serverank.RankRangeSort(param.Id)
msg = protocol.NewReturnResponse(nil, err)
return
}
//RankItemList ...
//@router /rank/item/list
func (c RankController) RankItemList() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
RankTypeId int64 `json:"rank_type_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()
items := serverank.GetRankItemList(companyid, param.RankTypeId)
rspdata := map[string][]protocol.RankItemInfo{
"all": serverank.AllRankItem,
"checked": items,
}
msg = protocol.NewReturnResponse(rspdata, nil)
return
}
//RankItemEdit ...
//@router /rank/item/edit
func (c RankController) RankItemEdit() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
RankTypeId int64 `json:"rank_type_id"`
ItemKey []string `json:"item_key"`
}
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.GetRankItemEdit(companyid, param.RankTypeId, param.ItemKey)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type RankItem struct {
Id int64 `orm:"column(id);auto"`
CompanyId int64 `orm:"column(company_id);null" description:"公司编号 company.id"`
RankTypeId int64 `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
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);null" description:"序号"`
ItemName string `orm:"column(item_name);size(50);null" description:"评比项名称"`
ItemKey string `orm:"column(item_key);size(50);null" description:"评比项键值(排行榜排序使用)"`
}
func (t *RankItem) TableName() string {
return "rank_item"
}
func init() {
orm.RegisterModel(new(RankItem))
}
// AddRankItem insert a new NewRankItem into database and returns
// last inserted Id on success.
func AddRankItem(m *RankItem) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetRankItemById retrieves NewRankItem by Id. Returns error if
// Id doesn't exist
func GetRankItemById(id int64) (v *RankItem, err error) {
o := orm.NewOrm()
v = &RankItem{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRankItem updates RankItem by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankItemById(m *RankItem) (err error) {
o := orm.NewOrm()
v := RankItem{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
}
func GetRankItemByCompanyid(companyid int64, rankTypeId int64) ([]RankItem, error) {
var (
data []RankItem
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&RankItem{}).
Filter("company_id", companyid).
Filter("rank_yype_id", rankTypeId).
All(&data)
if err == orm.ErrNoRows {
return data, nil
}
return data, err
}
... ...
... ... @@ -16,6 +16,7 @@ type RankRange struct {
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)"`
}
func (t *RankRange) TableName() string {
... ...
... ... @@ -40,3 +40,18 @@ type RankRangeRelation struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
//排行榜评比项
type RankItemInfo struct {
ItemName string `json:"item_name"`
ItemKey string `json:"item_key"`
SortNum int `json:"sort_num"`
}
type RankItemAll []RankItemInfo
func (a RankItemAll) Len() int { return len(a) }
func (a RankItemAll) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RankItemAll) Less(i, j int) bool {
return a[i].SortNum < a[j].SortNum
}
... ...
... ... @@ -114,6 +114,9 @@ func init() {
beego.NSRouter("/range/edit", &controllers.RankController{}, "post:RankRangeEdit"),
beego.NSRouter("/range/add", &controllers.RankController{}, "post:RankRangeAdd"),
beego.NSRouter("/range/info", &controllers.RankController{}, "post:RankRangeInfo"),
beego.NSRouter("/range/sort", &controllers.RankController{}, "post:RankRangeSort"),
beego.NSRouter("/item/list", &controllers.RankController{}, "post:RankItemList"),
beego.NSRouter("/item/edit", &controllers.RankController{}, "post:RankItemEdit"),
),
)
... ...
... ... @@ -6,6 +6,7 @@ import (
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"sort"
"time"
"github.com/astaxie/beego/orm"
... ... @@ -398,5 +399,142 @@ func GetRankRangeInfo(id int64) protocol.ResponseRankRangeInfo {
}
}
return rspdata
}
func RankRangeSort(ids []int64) error {
var updateData []*models.RankRange
for i := range ids {
m := &models.RankRange{
Id: ids[i],
SortNum: i,
}
updateData = append(updateData, m)
}
o := orm.NewOrm()
o.Begin()
for i := range updateData {
err := models.UpdateRankRangeById(updateData[i], []string{"SortNum"}, o)
if err != nil {
o.Rollback()
log.Error("更新rank_range数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
}
o.Commit()
return nil
}
var AllRankItem = []protocol.RankItemInfo{
protocol.RankItemInfo{
ItemName: "总分",
ItemKey: "zongfen",
},
protocol.RankItemInfo{
ItemName: "发现",
ItemKey: "faxian",
},
protocol.RankItemInfo{
ItemName: "把握",
ItemKey: "bawo",
},
protocol.RankItemInfo{
ItemName: "发现数量",
ItemKey: "faxianshuliang",
},
protocol.RankItemInfo{
ItemName: "评论数量",
ItemKey: "pinglunshuliang",
},
}
func GetRankItemList(companyid int64, rankTypeId int64) []protocol.RankItemInfo {
var (
rankitemData []models.RankItem
err error
)
rspData := make([]protocol.RankItemInfo, 0)
rankitemData, err = models.GetRankItemByCompanyid(companyid, rankTypeId)
if err != nil {
log.Error("获取rank_item数据失败:%s", err)
return rspData
}
for i := range rankitemData {
for ii := range AllRankItem {
if rankitemData[i].ItemKey == AllRankItem[ii].ItemKey {
m := protocol.RankItemInfo{
ItemKey: rankitemData[i].ItemKey,
ItemName: rankitemData[i].ItemName,
SortNum: rankitemData[i].SortNum,
}
rspData = append(rspData, m)
}
}
}
sort.Sort(protocol.RankItemAll(rspData))
return rspData
}
func GetRankItemEdit(companyid int64, rankTypeid int64, itemKey []string) error {
var (
rankitemData []models.RankItem
err error
)
rankitemData, err = models.GetRankItemByCompanyid(companyid, rankTypeid)
if err != nil {
log.Error("获取rank_item数据失败:%s", err)
return nil
}
var (
newItems []models.RankItem
)
for i := range itemKey {
for ii := range AllRankItem {
if AllRankItem[ii].ItemKey == itemKey[i] {
m := models.RankItem{
CompanyId: companyid,
RankTypeId: rankTypeid,
ItemName: AllRankItem[ii].ItemName,
SortNum: i,
}
newItems = append(newItems, m)
break
}
}
}
var (
delIds []int64
addItems []models.RankItem
updateItems []models.RankItem
)
for i := range rankitemData {
var has bool = false
for ii := range newItems {
if rankitemData[i].ItemKey == newItems[ii].ItemKey {
has = true
newItems[ii].Id = rankitemData[i].Id
updateItems = append(updateItems, newItems[ii])
break
}
}
if !has {
delIds = append(delIds, rankitemData[i].Id)
}
}
for i := range newItems {
var has bool = false
for ii := range rankitemData {
if rankitemData[i].ItemKey == newItems[ii].ItemKey {
has = true
break
}
}
if !has {
addItems = append(addItems, newItems[i])
}
}
o := orm.NewOrm()
o.Begin()
o.Commit()
return nil
}
... ...