作者 yangfu

榜单编辑修改

@@ -383,7 +383,7 @@ func AddRankRange(rankTypeId int64, name string, rangetype int8, relationId []in @@ -383,7 +383,7 @@ func AddRankRange(rankTypeId int64, name string, rangetype int8, relationId []in
383 log.Error("添加rank_range_data记录失败;%s", err) 383 log.Error("添加rank_range_data记录失败;%s", err)
384 return protocol.NewErrWithMessage("1") 384 return protocol.NewErrWithMessage("1")
385 } 385 }
386 - if err = removeAllRanks(o, companyid, rankTypeId, newRelationId); err != nil { 386 + if err = removeAllRanks(o, companyid, rankTypeId, 0, newRelationId); err != nil {
387 log.Error("更新rank数据失败:%s", err) 387 log.Error("更新rank数据失败:%s", err)
388 o.Rollback() 388 o.Rollback()
389 return protocol.NewErrWithMessage("1") 389 return protocol.NewErrWithMessage("1")
@@ -513,19 +513,21 @@ func EditRankRange(id int64, name string, rangetype int8, relationId []int64, co @@ -513,19 +513,21 @@ func EditRankRange(id int64, name string, rangetype int8, relationId []int64, co
513 return protocol.NewErrWithMessage("1") 513 return protocol.NewErrWithMessage("1")
514 } 514 }
515 } 515 }
516 -  
517 - if err = removeRanksNotIn(o, companyid, rankRange.RankTypeId, rankRange.Id, relationId); err != nil {  
518 - log.Error("更新rank数据失败:%s", err)  
519 - o.Rollback()  
520 - return protocol.NewErrWithMessage("1") 516 + if len(relationId) == 0 {
  517 + //移除当前范围所有榜单数据
  518 + if err = removeAllRanks(o, companyid, rankRange.RankTypeId, rankRange.Id, []int64{}); err != nil {
  519 + log.Error("更新rank数据失败:%s", err)
  520 + o.Rollback()
  521 + return protocol.NewErrWithMessage("1")
  522 + }
  523 + } else {
  524 + //移除不在当前范围的榜单数据
  525 + if err = removeRanksOutRange(o, companyid, rankRange.RankTypeId, rankRange.Id, relationId); err != nil {
  526 + log.Error("更新rank数据失败:%s", err)
  527 + o.Rollback()
  528 + return protocol.NewErrWithMessage("1")
  529 + }
521 } 530 }
522 - //if len(relationId)>0{  
523 - // if err = updateRanks(o, companyid, rankRange.RankTypeId, rankRange.Id, relationId,1); err != nil {  
524 - // log.Error("更新rank数据失败:%s", err)  
525 - // o.Rollback()  
526 - // return protocol.NewErrWithMessage("1")  
527 - // }  
528 - //}  
529 o.Commit() 531 o.Commit()
530 return nil 532 return nil
531 } 533 }
@@ -937,20 +939,22 @@ where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id in @@ -937,20 +939,22 @@ where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id in
937 } 939 }
938 return 940 return
939 } 941 }
940 -func removeRanksNotIn(o orm.Ormer, companyId, rankTypeId, rankRangeId int64, relationIds []int64) (err error) { 942 +
  943 +//移除参与范围外 其他参与范围的已统计版本记录
  944 +//@rankRangeId 当前参与范围编号
  945 +func removeRanksOutRange(o orm.Ormer, companyId, rankTypeId, rankRangeId int64, relationIds []int64) (err error) {
941 var sqlRemoveRanks = fmt.Sprintf(` 946 var sqlRemoveRanks = fmt.Sprintf(`
942 update rank set enable_status=0,update_at=NOW() 947 update rank set enable_status=0,update_at=NOW()
943 -where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id in 948 +where company_id=%v and rank_type_id=%v and rank_range_id<>%v and enable_status=1 and rank_period_id in
944 ( 949 (
945 - select id from rank_period where company_id=? and rank_type_id=? and status=1 950 + select id from rank_period where company_id=%v and rank_type_id=%v and status=1
946 ) 951 )
947 -`) 952 +`, companyId, rankTypeId, rankRangeId,
  953 + companyId, rankTypeId)
948 if len(relationIds) > 0 { 954 if len(relationIds) > 0 {
949 - sqlRemoveRanks += fmt.Sprintf(" and relation_id not in (%v)", utils.JoinInt64s(relationIds, ",")) 955 + sqlRemoveRanks += fmt.Sprintf(" and relation_id in (%v)", utils.JoinInt64s(relationIds, ","))
950 } 956 }
951 - if _, err = o.Raw(sqlRemoveRanks, companyId, rankTypeId, rankRangeId,  
952 - companyId, rankTypeId,  
953 - ).Exec(); err != nil { 957 + if err = utils.ExecuteSQLWithOrmer(o, sqlRemoveRanks); err != nil {
954 return 958 return
955 } 959 }
956 return 960 return
@@ -958,21 +962,21 @@ where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id in @@ -958,21 +962,21 @@ where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id in
958 962
959 //移除已存在的榜单记录(榜单周期内) 963 //移除已存在的榜单记录(榜单周期内)
960 //同时只会存在在一个排行榜单里面 964 //同时只会存在在一个排行榜单里面
961 -func removeAllRanks(o orm.Ormer, companyId, rankTypeId int64, relationIds []int64) (err error) {  
962 - if len(relationIds) == 0 {  
963 - return nil  
964 - } 965 +func removeAllRanks(o orm.Ormer, companyId, rankTypeId int64, rankRangeId int64, relationIds []int64) (err error) {
965 var sqlRemoveRanks = fmt.Sprintf(` 966 var sqlRemoveRanks = fmt.Sprintf(`
966 update rank set enable_status=0,update_at=NOW() 967 update rank set enable_status=0,update_at=NOW()
967 -where company_id=? and rank_type_id=? and enable_status =1 and rank_period_id in 968 +where company_id=%v and rank_type_id=%v and enable_status =1 and rank_period_id in
968 ( 969 (
969 - select id from rank_period where company_id=? and rank_type_id=? and status=1 970 + select id from rank_period where company_id=%v and rank_type_id=%v and status=1
970 ) 971 )
971 -`) 972 +`, companyId, rankTypeId, companyId, rankTypeId)
972 if len(relationIds) > 0 { 973 if len(relationIds) > 0 {
973 sqlRemoveRanks += fmt.Sprintf(" and relation_id in (%v)", utils.JoinInt64s(relationIds, ",")) 974 sqlRemoveRanks += fmt.Sprintf(" and relation_id in (%v)", utils.JoinInt64s(relationIds, ","))
974 } 975 }
975 - if err = utils.ExecuteSQLWithOrmer(o, sqlRemoveRanks, companyId, rankTypeId, companyId, rankTypeId); err != nil { 976 + if rankRangeId > 0 {
  977 + sqlRemoveRanks += fmt.Sprintf(" and rank_range_id =%v", rankRangeId)
  978 + }
  979 + if err = utils.ExecuteSQLWithOrmer(o, sqlRemoveRanks); err != nil {
976 return 980 return
977 } 981 }
978 return 982 return