作者 tangxvhui

赛季设置调整

@@ -287,3 +287,74 @@ func (c RankController) RankRangeInfo() { @@ -287,3 +287,74 @@ func (c RankController) RankRangeInfo() {
287 msg = protocol.NewReturnResponse(rspData, nil) 287 msg = protocol.NewReturnResponse(rspData, nil)
288 return 288 return
289 } 289 }
  290 +
  291 +//RankRangeSort ...
  292 +//@router /rank/range/sort
  293 +func (c RankController) RankRangeSort() {
  294 + var msg *protocol.ResponseMessage
  295 + defer func() {
  296 + c.ResposeJson(msg)
  297 + }()
  298 + type Parameter struct {
  299 + RankTypeId int64 `json:"rank_type_id"`
  300 + Id []int64 `json:"id"`
  301 + }
  302 + var param Parameter
  303 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  304 + log.Error("json 解析失败 err:%s", err)
  305 + msg = protocol.BadRequestParam("1")
  306 + return
  307 + }
  308 + err := serverank.RankRangeSort(param.Id)
  309 + msg = protocol.NewReturnResponse(nil, err)
  310 + return
  311 +}
  312 +
  313 +//RankItemList ...
  314 +//@router /rank/item/list
  315 +func (c RankController) RankItemList() {
  316 + var msg *protocol.ResponseMessage
  317 + defer func() {
  318 + c.ResposeJson(msg)
  319 + }()
  320 + type Parameter struct {
  321 + RankTypeId int64 `json:"rank_type_id"`
  322 + }
  323 + var param Parameter
  324 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  325 + log.Error("json 解析失败 err:%s", err)
  326 + msg = protocol.BadRequestParam("1")
  327 + return
  328 + }
  329 + companyid := c.GetCompanyId()
  330 + items := serverank.GetRankItemList(companyid, param.RankTypeId)
  331 + rspdata := map[string][]protocol.RankItemInfo{
  332 + "all": serverank.AllRankItem,
  333 + "checked": items,
  334 + }
  335 + msg = protocol.NewReturnResponse(rspdata, nil)
  336 + return
  337 +}
  338 +
  339 +//RankItemEdit ...
  340 +//@router /rank/item/edit
  341 +func (c RankController) RankItemEdit() {
  342 + var msg *protocol.ResponseMessage
  343 + defer func() {
  344 + c.ResposeJson(msg)
  345 + }()
  346 + type Parameter struct {
  347 + RankTypeId int64 `json:"rank_type_id"`
  348 + ItemKey []string `json:"item_key"`
  349 + }
  350 + var param Parameter
  351 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  352 + log.Error("json 解析失败 err:%s", err)
  353 + msg = protocol.BadRequestParam("1")
  354 + return
  355 + }
  356 + companyid := c.GetCompanyId()
  357 + err := serverank.GetRankItemEdit(companyid, param.RankTypeId, param.ItemKey)
  358 + msg = protocol.NewReturnResponse(nil, err)
  359 + return
  360 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type RankItem struct {
  11 + Id int64 `orm:"column(id);auto"`
  12 + CompanyId int64 `orm:"column(company_id);null" description:"公司编号 company.id"`
  13 + RankTypeId int64 `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
  14 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  15 + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  16 + SortNum int `orm:"column(sort_num);null" description:"序号"`
  17 + ItemName string `orm:"column(item_name);size(50);null" description:"评比项名称"`
  18 + ItemKey string `orm:"column(item_key);size(50);null" description:"评比项键值(排行榜排序使用)"`
  19 +}
  20 +
  21 +func (t *RankItem) TableName() string {
  22 + return "rank_item"
  23 +}
  24 +
  25 +func init() {
  26 + orm.RegisterModel(new(RankItem))
  27 +}
  28 +
  29 +// AddRankItem insert a new NewRankItem into database and returns
  30 +// last inserted Id on success.
  31 +func AddRankItem(m *RankItem) (id int64, err error) {
  32 + o := orm.NewOrm()
  33 + id, err = o.Insert(m)
  34 + return
  35 +}
  36 +
  37 +// GetRankItemById retrieves NewRankItem by Id. Returns error if
  38 +// Id doesn't exist
  39 +func GetRankItemById(id int64) (v *RankItem, err error) {
  40 + o := orm.NewOrm()
  41 + v = &RankItem{Id: id}
  42 + if err = o.Read(v); err == nil {
  43 + return v, nil
  44 + }
  45 + return nil, err
  46 +}
  47 +
  48 +// UpdateRankItem updates RankItem by Id and returns error if
  49 +// the record to be updated doesn't exist
  50 +func UpdateRankItemById(m *RankItem) (err error) {
  51 + o := orm.NewOrm()
  52 + v := RankItem{Id: m.Id}
  53 + // ascertain id exists in the database
  54 + if err = o.Read(&v); err == nil {
  55 + var num int64
  56 + if num, err = o.Update(m); err == nil {
  57 + fmt.Println("Number of records updated in database:", num)
  58 + }
  59 + }
  60 + return
  61 +}
  62 +
  63 +func GetRankItemByCompanyid(companyid int64, rankTypeId int64) ([]RankItem, error) {
  64 + var (
  65 + data []RankItem
  66 + err error
  67 + )
  68 + o := orm.NewOrm()
  69 + _, err = o.QueryTable(&RankItem{}).
  70 + Filter("company_id", companyid).
  71 + Filter("rank_yype_id", rankTypeId).
  72 + All(&data)
  73 + if err == orm.ErrNoRows {
  74 + return data, nil
  75 + }
  76 + return data, err
  77 +}
@@ -16,6 +16,7 @@ type RankRange struct { @@ -16,6 +16,7 @@ type RankRange struct {
16 Data string `orm:"column(data);size(1000);null" description:"人员范围数据(type:2,4 有值 对于人员数据/部门数据)"` 16 Data string `orm:"column(data);size(1000);null" description:"人员范围数据(type:2,4 有值 对于人员数据/部门数据)"`
17 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"` 17 CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
18 UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"` 18 UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
  19 + SortNum int `orm:"column(sort_num)"`
19 } 20 }
20 21
21 func (t *RankRange) TableName() string { 22 func (t *RankRange) TableName() string {
@@ -40,3 +40,18 @@ type RankRangeRelation struct { @@ -40,3 +40,18 @@ type RankRangeRelation struct {
40 Id int64 `json:"id"` 40 Id int64 `json:"id"`
41 Name string `json:"name"` 41 Name string `json:"name"`
42 } 42 }
  43 +
  44 +//排行榜评比项
  45 +type RankItemInfo struct {
  46 + ItemName string `json:"item_name"`
  47 + ItemKey string `json:"item_key"`
  48 + SortNum int `json:"sort_num"`
  49 +}
  50 +
  51 +type RankItemAll []RankItemInfo
  52 +
  53 +func (a RankItemAll) Len() int { return len(a) }
  54 +func (a RankItemAll) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  55 +func (a RankItemAll) Less(i, j int) bool {
  56 + return a[i].SortNum < a[j].SortNum
  57 +}
@@ -114,6 +114,9 @@ func init() { @@ -114,6 +114,9 @@ func init() {
114 beego.NSRouter("/range/edit", &controllers.RankController{}, "post:RankRangeEdit"), 114 beego.NSRouter("/range/edit", &controllers.RankController{}, "post:RankRangeEdit"),
115 beego.NSRouter("/range/add", &controllers.RankController{}, "post:RankRangeAdd"), 115 beego.NSRouter("/range/add", &controllers.RankController{}, "post:RankRangeAdd"),
116 beego.NSRouter("/range/info", &controllers.RankController{}, "post:RankRangeInfo"), 116 beego.NSRouter("/range/info", &controllers.RankController{}, "post:RankRangeInfo"),
  117 + beego.NSRouter("/range/sort", &controllers.RankController{}, "post:RankRangeSort"),
  118 + beego.NSRouter("/item/list", &controllers.RankController{}, "post:RankItemList"),
  119 + beego.NSRouter("/item/edit", &controllers.RankController{}, "post:RankItemEdit"),
117 ), 120 ),
118 ) 121 )
119 122
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "oppmg/models" 6 "oppmg/models"
7 "oppmg/protocol" 7 "oppmg/protocol"
8 "oppmg/utils" 8 "oppmg/utils"
  9 + "sort"
9 "time" 10 "time"
10 11
11 "github.com/astaxie/beego/orm" 12 "github.com/astaxie/beego/orm"
@@ -398,5 +399,142 @@ func GetRankRangeInfo(id int64) protocol.ResponseRankRangeInfo { @@ -398,5 +399,142 @@ func GetRankRangeInfo(id int64) protocol.ResponseRankRangeInfo {
398 } 399 }
399 } 400 }
400 return rspdata 401 return rspdata
  402 +}
  403 +
  404 +func RankRangeSort(ids []int64) error {
  405 + var updateData []*models.RankRange
  406 + for i := range ids {
  407 + m := &models.RankRange{
  408 + Id: ids[i],
  409 + SortNum: i,
  410 + }
  411 + updateData = append(updateData, m)
  412 + }
  413 + o := orm.NewOrm()
  414 + o.Begin()
  415 + for i := range updateData {
  416 + err := models.UpdateRankRangeById(updateData[i], []string{"SortNum"}, o)
  417 + if err != nil {
  418 + o.Rollback()
  419 + log.Error("更新rank_range数据失败:%s", err)
  420 + return protocol.NewErrWithMessage("1")
  421 + }
  422 + }
  423 + o.Commit()
  424 + return nil
  425 +}
  426 +
  427 +var AllRankItem = []protocol.RankItemInfo{
  428 + protocol.RankItemInfo{
  429 + ItemName: "总分",
  430 + ItemKey: "zongfen",
  431 + },
  432 + protocol.RankItemInfo{
  433 + ItemName: "发现",
  434 + ItemKey: "faxian",
  435 + },
  436 + protocol.RankItemInfo{
  437 + ItemName: "把握",
  438 + ItemKey: "bawo",
  439 + },
  440 + protocol.RankItemInfo{
  441 + ItemName: "发现数量",
  442 + ItemKey: "faxianshuliang",
  443 + },
  444 + protocol.RankItemInfo{
  445 + ItemName: "评论数量",
  446 + ItemKey: "pinglunshuliang",
  447 + },
  448 +}
  449 +
  450 +func GetRankItemList(companyid int64, rankTypeId int64) []protocol.RankItemInfo {
  451 + var (
  452 + rankitemData []models.RankItem
  453 + err error
  454 + )
  455 + rspData := make([]protocol.RankItemInfo, 0)
  456 + rankitemData, err = models.GetRankItemByCompanyid(companyid, rankTypeId)
  457 + if err != nil {
  458 + log.Error("获取rank_item数据失败:%s", err)
  459 + return rspData
  460 + }
  461 + for i := range rankitemData {
  462 + for ii := range AllRankItem {
  463 + if rankitemData[i].ItemKey == AllRankItem[ii].ItemKey {
  464 + m := protocol.RankItemInfo{
  465 + ItemKey: rankitemData[i].ItemKey,
  466 + ItemName: rankitemData[i].ItemName,
  467 + SortNum: rankitemData[i].SortNum,
  468 + }
  469 + rspData = append(rspData, m)
  470 + }
  471 + }
  472 + }
  473 + sort.Sort(protocol.RankItemAll(rspData))
  474 + return rspData
  475 +}
401 476
  477 +func GetRankItemEdit(companyid int64, rankTypeid int64, itemKey []string) error {
  478 + var (
  479 + rankitemData []models.RankItem
  480 + err error
  481 + )
  482 + rankitemData, err = models.GetRankItemByCompanyid(companyid, rankTypeid)
  483 + if err != nil {
  484 + log.Error("获取rank_item数据失败:%s", err)
  485 + return nil
  486 + }
  487 + var (
  488 + newItems []models.RankItem
  489 + )
  490 + for i := range itemKey {
  491 + for ii := range AllRankItem {
  492 + if AllRankItem[ii].ItemKey == itemKey[i] {
  493 + m := models.RankItem{
  494 + CompanyId: companyid,
  495 + RankTypeId: rankTypeid,
  496 + ItemName: AllRankItem[ii].ItemName,
  497 + SortNum: i,
  498 + }
  499 + newItems = append(newItems, m)
  500 + break
  501 + }
  502 + }
  503 + }
  504 + var (
  505 + delIds []int64
  506 + addItems []models.RankItem
  507 + updateItems []models.RankItem
  508 + )
  509 +
  510 + for i := range rankitemData {
  511 + var has bool = false
  512 + for ii := range newItems {
  513 + if rankitemData[i].ItemKey == newItems[ii].ItemKey {
  514 + has = true
  515 + newItems[ii].Id = rankitemData[i].Id
  516 + updateItems = append(updateItems, newItems[ii])
  517 + break
  518 + }
  519 + }
  520 + if !has {
  521 + delIds = append(delIds, rankitemData[i].Id)
  522 + }
  523 + }
  524 + for i := range newItems {
  525 + var has bool = false
  526 + for ii := range rankitemData {
  527 + if rankitemData[i].ItemKey == newItems[ii].ItemKey {
  528 + has = true
  529 + break
  530 + }
  531 + }
  532 + if !has {
  533 + addItems = append(addItems, newItems[i])
  534 + }
  535 + }
  536 + o := orm.NewOrm()
  537 + o.Begin()
  538 + o.Commit()
  539 + return nil
402 } 540 }