|
|
package rank
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"sort"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
//GetRankList 获取榜单类型列表
|
|
|
func GetRankList(companyid int64) []protocol.ResponseRankType {
|
|
|
var (
|
|
|
rspData = make([]protocol.ResponseRankType, 0)
|
|
|
err error
|
|
|
)
|
|
|
sqlString := `SELECT id,name,enable_status,type FROM rank_type 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, rankTypeId int64, companyid int64) (protocol.ResponseRankSeasonList, error) {
|
|
|
var (
|
|
|
err error
|
|
|
listdata []protocol.RankSeasonItem
|
|
|
cnt int
|
|
|
)
|
|
|
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 = ? AND rank_type_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 = ? AND rank_type_id=? `
|
|
|
err = utils.ExecuteQueryOne(&cnt, countsql, companyid, rankTypeId)
|
|
|
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, rankTypeId)
|
|
|
if err != nil {
|
|
|
log.Error("EXCUTE SQL ERR:%s", err)
|
|
|
return rspData, nil
|
|
|
}
|
|
|
rspData.TotalPage = cnt
|
|
|
rspData.List = listdata
|
|
|
return rspData, err
|
|
|
}
|
|
|
|
|
|
func AddRankPeriod(rankTypeId int64, beginTime int64, endTime int64, name string, companyid int64) error {
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
m := &models.RankPeriod{
|
|
|
CompanyId: companyid,
|
|
|
SeasonName: name,
|
|
|
RankTypeId: rankTypeId,
|
|
|
BeginTime: time.Unix(beginTime, 0).Local(),
|
|
|
EndTime: time.Unix(endTime, 0).Local(),
|
|
|
Status: 0,
|
|
|
}
|
|
|
|
|
|
_, err = models.AddRankPeriod(m)
|
|
|
if err != nil {
|
|
|
log.Error("添加赛季失败;%s", err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//RankPeriodCheckTime 设置赛季时检查时间范围的合法性
|
|
|
func RankPeriodCheckTime(rankTypeId int64, beginTime int64, endTime int64, idNot int64) bool {
|
|
|
sql := `SELECT count(*) FROM rank_period
|
|
|
WHERE rank_type_id = %d
|
|
|
AND id <> %d
|
|
|
AND
|
|
|
(
|
|
|
(UNIX_TIMESTAMP(begin_time) BETWEEN %d AND %d)
|
|
|
OR
|
|
|
(UNIX_TIMESTAMP(end_time) BETWEEN %d AND %d)
|
|
|
OR
|
|
|
(%d BETWEEN UNIX_TIMESTAMP(begin_time) AND UNIX_TIMESTAMP(end_time))
|
|
|
OR
|
|
|
(%d BETWEEN UNIX_TIMESTAMP(begin_time) AND UNIX_TIMESTAMP(end_time))
|
|
|
)
|
|
|
LIMIT 1 `
|
|
|
sql = fmt.Sprintf(sql, rankTypeId, idNot, beginTime, endTime, beginTime, endTime, beginTime, endTime)
|
|
|
var cnt int
|
|
|
err := utils.ExecuteQueryOne(&cnt, sql)
|
|
|
if err != nil {
|
|
|
log.Error("SQL Execute err:%s", err)
|
|
|
return false
|
|
|
}
|
|
|
if cnt > 0 {
|
|
|
return false
|
|
|
}
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
func EditRankPeriod(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, []string{"SeasonName", "BeginTime", "EndTime"})
|
|
|
if err != nil {
|
|
|
log.Error("更新赛季失败;%s", err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//获取赛季参与人列表
|
|
|
func GetRankRangeList(companyid int64, rankTypeId int64) []protocol.RankRangeListItem {
|
|
|
var (
|
|
|
err error
|
|
|
rspData = make([]protocol.RankRangeListItem, 0)
|
|
|
)
|
|
|
datasql := `SELECT a.id ,a.name,a.type,a.status,a.sort_num FROM rank_range AS a WHERE company_id=? AND rank_type_id=?`
|
|
|
err = utils.ExecuteQueryAll(&rspData, datasql, companyid, rankTypeId)
|
|
|
if err != nil {
|
|
|
log.Error("获取参与人列表失败:%s", err)
|
|
|
}
|
|
|
for i := range rspData {
|
|
|
var nameList []string
|
|
|
relations := getRangeDataRelationLsit(rspData[i].Id, rspData[i].RangeType)
|
|
|
for ii := range relations {
|
|
|
nameList = append(nameList, relations[ii].Name)
|
|
|
}
|
|
|
rspData[i].RangeData = strings.Join(nameList, ",")
|
|
|
}
|
|
|
sort.Sort(protocol.RankRangeList(rspData))
|
|
|
return rspData
|
|
|
}
|
|
|
|
|
|
//getRangeDataLsit 获取range_data的数据
|
|
|
func getRangeDataRelationLsit(rankRangeId int64, rangeType int8) []protocol.RankRangeRelation {
|
|
|
rankRangeData, _ := models.GetRankRangeDataByRangeId(rankRangeId)
|
|
|
var relationIds []int64
|
|
|
for i := range rankRangeData {
|
|
|
relationIds = append(relationIds, rankRangeData[i].RelationId)
|
|
|
}
|
|
|
datalist := make([]protocol.RankRangeRelation, 0)
|
|
|
if len(relationIds) == 0 {
|
|
|
return datalist
|
|
|
}
|
|
|
switch rangeType {
|
|
|
case models.RANK_RANGE_TYPE_DEPARTMENT:
|
|
|
//部门
|
|
|
departments, err := models.GetDepartmentByIds(relationIds)
|
|
|
if err != nil {
|
|
|
log.Error("获取部门数据失败")
|
|
|
}
|
|
|
for i := range departments {
|
|
|
m := protocol.RankRangeRelation{
|
|
|
Id: departments[i].Id,
|
|
|
Name: departments[i].Name,
|
|
|
}
|
|
|
datalist = append(datalist, m)
|
|
|
}
|
|
|
case models.RANK_RANGE_TYPE_EMPLAYEE:
|
|
|
//员工
|
|
|
users, err := models.GetUserCompanyByIds(relationIds)
|
|
|
if err != nil {
|
|
|
log.Error("获取员工数据失败")
|
|
|
}
|
|
|
for i := range users {
|
|
|
m := protocol.RankRangeRelation{
|
|
|
Id: users[i].Id,
|
|
|
Name: users[i].NickName,
|
|
|
}
|
|
|
datalist = append(datalist, m)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return datalist
|
|
|
}
|
|
|
|
|
|
func RankRangeNameOnlyOne(rankTypeId int64, rankRangeId int64, name string) bool {
|
|
|
sql := `SELECT count(*) FROM rank_range WHERE rank_type_id=? AND name=? AND id<>?`
|
|
|
var (
|
|
|
err error
|
|
|
cnt int
|
|
|
)
|
|
|
err = utils.ExecuteQueryOne(&cnt, sql, rankTypeId, name, rankRangeId)
|
|
|
if err != nil {
|
|
|
return false
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
func AddRankRange(rankTypeId int64, 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,
|
|
|
RankTypeId: rankTypeId,
|
|
|
Type: rangetype,
|
|
|
SortNum: 9999,
|
|
|
}
|
|
|
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")
|
|
|
}
|
|
|
|
|
|
//获取部门或人员数据
|
|
|
newRelationId := getDataForRankRnageRelation(companyid, rangetype, relationId)
|
|
|
for i := range newRelationId {
|
|
|
m := models.RankRangeData{
|
|
|
RankRangeId: rankRange.Id,
|
|
|
RangeType: rangetype,
|
|
|
RelationId: newRelationId[i],
|
|
|
RankTypeId: rankTypeId,
|
|
|
}
|
|
|
rankRangeDatas = append(rankRangeDatas, m)
|
|
|
}
|
|
|
|
|
|
if len(rankRangeDatas) > 0 {
|
|
|
//TODO 去除其他分组的重复项
|
|
|
err = deleteRankRangeDataByRelation(rankTypeId, rangetype, newRelationId, o)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
_, 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 getDataForRankRnageRelation(companyid int64, rangeType int8, relationId []int64) (newRelationId []int64) {
|
|
|
o := orm.NewOrm()
|
|
|
var (
|
|
|
err error
|
|
|
)
|
|
|
switch rangeType {
|
|
|
case models.RANK_RANGE_TYPE_EMPLAYEE:
|
|
|
var (
|
|
|
users []models.UserCompany
|
|
|
)
|
|
|
qs := o.QueryTable(&models.UserCompany{}).
|
|
|
Filter("delete_at", 0).
|
|
|
Filter("company_id", companyid).
|
|
|
Filter("enable", 1)
|
|
|
if len(relationId) > 0 {
|
|
|
qs = qs.Filter("id__in", relationId)
|
|
|
}
|
|
|
_, err = qs.All(&users, "Id")
|
|
|
if err == nil {
|
|
|
for _, v := range users {
|
|
|
newRelationId = append(newRelationId, v.Id)
|
|
|
}
|
|
|
}
|
|
|
case models.RANK_RANGE_TYPE_DEPARTMENT:
|
|
|
var (
|
|
|
departments []models.Department
|
|
|
)
|
|
|
qs := o.QueryTable(&models.Department{}).
|
|
|
Filter("delete_at", 0).
|
|
|
Filter("company_id", companyid)
|
|
|
if len(relationId) > 0 {
|
|
|
qs = qs.Filter("id__in", relationId)
|
|
|
}
|
|
|
_, err = qs.All(&departments, "Id")
|
|
|
if err == nil {
|
|
|
for _, v := range departments {
|
|
|
newRelationId = append(newRelationId, v.Id)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return newRelationId
|
|
|
}
|
|
|
|
|
|
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")
|
|
|
}
|
|
|
ok := RankRangeNameOnlyOne(rankRange.RankTypeId, rankRange.Id, name)
|
|
|
if !ok {
|
|
|
return protocol.NewErrWithMessage("10125")
|
|
|
}
|
|
|
rankRangeDatas, err = models.GetRankRangeDataByRangeId(rankRange.Id)
|
|
|
if err != nil {
|
|
|
log.Error("获取rank_range_data数据失败;%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
var (
|
|
|
oldids []int64
|
|
|
)
|
|
|
for i := range rankRangeDatas {
|
|
|
oldids = append(oldids, rankRangeDatas[i].Id)
|
|
|
}
|
|
|
var (
|
|
|
addRangeData []models.RankRangeData
|
|
|
)
|
|
|
newRelationId := getDataForRankRnageRelation(companyid, rangetype, relationId)
|
|
|
for i := range newRelationId {
|
|
|
m := models.RankRangeData{
|
|
|
RankRangeId: rankRange.Id,
|
|
|
RangeType: rangetype,
|
|
|
RelationId: newRelationId[i],
|
|
|
RankTypeId: rankRange.RankTypeId,
|
|
|
}
|
|
|
addRangeData = append(addRangeData, m)
|
|
|
}
|
|
|
rankRange.Name = name
|
|
|
rankRange.Type = rangetype
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
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(oldids) > 0 {
|
|
|
err = models.DeleteRankRangeData(oldids, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("删除rank_range_data数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
}
|
|
|
if len(newRelationId) > 0 {
|
|
|
//TODO 去除其他分组的重复项
|
|
|
err = deleteRankRangeDataByRelation(rankRange.RankTypeId, rangetype, newRelationId, o)
|
|
|
if err != nil {
|
|
|
log.Error(err.Error())
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
_, 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 deleteRankRangeDataByRelation(rankTypeId int64, rangeType int8, relationId []int64, om orm.Ormer) error {
|
|
|
_, err := om.QueryTable(&models.RankRangeData{}).
|
|
|
Filter("rank_type_id", rankTypeId).
|
|
|
Filter("range_type", rangeType).
|
|
|
Filter("relation_id__in", relationId).Delete()
|
|
|
if err != nil {
|
|
|
log.Error("删除rank_range_data数据失败:%s", err)
|
|
|
return err
|
|
|
}
|
|
|
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:
|
|
|
//部门
|
|
|
departments, err := models.GetDepartmentByIds(relationIds)
|
|
|
if err != nil {
|
|
|
log.Error("获取部门数据失败")
|
|
|
}
|
|
|
for i := range departments {
|
|
|
m := protocol.RankRangeRelation{
|
|
|
Id: departments[i].Id,
|
|
|
Name: departments[i].Name,
|
|
|
}
|
|
|
rspdata.Relation = append(rspdata.Relation, m)
|
|
|
}
|
|
|
case models.RANK_RANGE_TYPE_EMPLAYEE:
|
|
|
//员工
|
|
|
users, err := models.GetUserCompanyByIds(relationIds)
|
|
|
if err != nil {
|
|
|
log.Error("获取员工数据失败")
|
|
|
}
|
|
|
for i := range users {
|
|
|
m := protocol.RankRangeRelation{
|
|
|
Id: users[i].Id,
|
|
|
Name: users[i].NickName,
|
|
|
}
|
|
|
rspdata.Relation = append(rspdata.Relation, m)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
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: "total_score",
|
|
|
},
|
|
|
protocol.RankItemInfo{
|
|
|
ItemName: "发现",
|
|
|
ItemKey: "discovery_score",
|
|
|
},
|
|
|
protocol.RankItemInfo{
|
|
|
ItemName: "把握",
|
|
|
ItemKey: "grasp_score",
|
|
|
},
|
|
|
protocol.RankItemInfo{
|
|
|
ItemName: "发现数量",
|
|
|
ItemKey: "discovery_total",
|
|
|
},
|
|
|
protocol.RankItemInfo{
|
|
|
ItemName: "评论数量",
|
|
|
ItemKey: "comment_total",
|
|
|
},
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
ItemKey: itemKey[i],
|
|
|
}
|
|
|
newItems = append(newItems, m)
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
var (
|
|
|
delItemIds []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 {
|
|
|
delItemIds = append(delItemIds, rankitemData[i].Id)
|
|
|
}
|
|
|
}
|
|
|
for i := range newItems {
|
|
|
var has bool = false
|
|
|
for ii := range rankitemData {
|
|
|
if rankitemData[ii].ItemKey == newItems[i].ItemKey {
|
|
|
has = true
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
if !has {
|
|
|
addItems = append(addItems, newItems[i])
|
|
|
}
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//添加
|
|
|
if len(addItems) > 0 {
|
|
|
_, err = models.AddRankItem(addItems, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("添加rank_item记录失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
}
|
|
|
//更新
|
|
|
for i := range updateItems {
|
|
|
err = models.UpdateRankItemById(&updateItems[i], []string{"SortNum", "ItemName", "ItemKey"}, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("更新rank_item记录失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
}
|
|
|
//删除
|
|
|
if len(delItemIds) > 0 {
|
|
|
err = models.DeleteRanKItemByIds(delItemIds, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Error("删除rank_item记录失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func ForbidRankRange(id int64, companyid int64) error {
|
|
|
var (
|
|
|
err error
|
|
|
rankRange *models.RankRange
|
|
|
)
|
|
|
rankRange, err = models.GetRankRangeById(id)
|
|
|
if err != nil {
|
|
|
log.Error("获取rank_range数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
rankRange.Status = 1
|
|
|
err = models.UpdateRankRangeById(rankRange, []string{"Status"})
|
|
|
if err != nil {
|
|
|
log.Error("更新rank_range数据失败:%s", err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func AllowRankRange(id int64, companyid int64) error {
|
|
|
var (
|
|
|
err error
|
|
|
rankRange *models.RankRange
|
|
|
)
|
|
|
rankRange, err = models.GetRankRangeById(id)
|
|
|
if err != nil {
|
|
|
log.Error("获取rank_range数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
rankRange.Status = 0
|
|
|
err = models.UpdateRankRangeById(rankRange, []string{"Status"})
|
|
|
if err != nil {
|
|
|
log.Error("更新rank_range数据失败:%s", err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func RankRangeMove(fromId int64, toId int64, relationId []int64, companyid int64) error {
|
|
|
var (
|
|
|
fromRankRange *models.RankRange
|
|
|
toRankRange *models.RankRange
|
|
|
err error
|
|
|
)
|
|
|
fromRankRange, err = models.GetRankRangeById(fromId)
|
|
|
if err != nil {
|
|
|
log.Error("获取rank_range 数据失败;%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if fromRankRange.CompanyId != companyid {
|
|
|
log.Error("rank_range的公司不匹配;%s")
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
toRankRange, err = models.GetRankRangeById(toId)
|
|
|
if err != nil {
|
|
|
log.Error("获取rank_range 数据失败;%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if toRankRange.CompanyId != companyid {
|
|
|
log.Error("rank_range的公司不匹配;%s")
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
|
|
|
typeIn := map[int8]int{
|
|
|
models.RANK_RANGE_TYPE_DEPARTMENT: 1,
|
|
|
models.RANK_RANGE_TYPE_DEPARTMENTALL: 1,
|
|
|
models.RANK_RANGE_TYPE_EMPLAYEE: 2,
|
|
|
models.RANK_RANGE_TYPE_EMPLAYEEALL: 2,
|
|
|
}
|
|
|
var (
|
|
|
fromTypeFlag int
|
|
|
ToTypeFlag int
|
|
|
ok bool
|
|
|
)
|
|
|
if fromTypeFlag, ok = typeIn[fromRankRange.Type]; !ok {
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if ToTypeFlag, ok = typeIn[toRankRange.Type]; !ok {
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if fromTypeFlag != ToTypeFlag {
|
|
|
log.Error("RankRange类型不一致")
|
|
|
return protocol.NewErrWithMessage("10102")
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
addNew []models.RankRangeData
|
|
|
rangeType int8
|
|
|
)
|
|
|
if typeIn[toRankRange.Type] == 1 {
|
|
|
rangeType = models.RANK_RANGE_TYPE_DEPARTMENT
|
|
|
} else {
|
|
|
rangeType = models.RANK_RANGE_TYPE_EMPLAYEE
|
|
|
}
|
|
|
|
|
|
for i := range relationId {
|
|
|
m := models.RankRangeData{
|
|
|
RankRangeId: toRankRange.Id,
|
|
|
RangeType: rangeType,
|
|
|
RelationId: relationId[i],
|
|
|
}
|
|
|
addNew = append(addNew, m)
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
_, err = o.QueryTable(&models.RankRangeData{}).
|
|
|
Filter("rank_range_id", fromId).
|
|
|
Filter("relation_id__in", relationId).
|
|
|
Delete()
|
|
|
if err != nil {
|
|
|
log.Error("删除rank_range_data数据失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
cnt, err := o.QueryTable(&models.RankRangeData{}).Filter("rank_range_id", fromId).Count()
|
|
|
if err != nil {
|
|
|
log.Error("统计rank_range_data 数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if cnt == 0 {
|
|
|
if fromTypeFlag == 1 {
|
|
|
fromRankRange.Type = models.RANK_RANGE_TYPE_DEPARTMENTALL
|
|
|
} else {
|
|
|
fromRankRange.Type = models.RANK_RANGE_TYPE_EMPLAYEEALL
|
|
|
}
|
|
|
err = models.UpdateRankRangeById(fromRankRange, []string{"Type"}, o)
|
|
|
if err != nil {
|
|
|
log.Error("更新rank_range数据失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
}
|
|
|
_, err = models.AddRankRangeDataMulti(addNew, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加rank_range_data数据失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if ToTypeFlag == 1 {
|
|
|
toRankRange.Type = models.RANK_RANGE_TYPE_DEPARTMENT
|
|
|
} else {
|
|
|
toRankRange.Type = models.RANK_RANGE_TYPE_EMPLAYEE
|
|
|
}
|
|
|
err = models.UpdateRankRangeById(toRankRange, []string{"Type"}, o)
|
|
|
if err != nil {
|
|
|
log.Error("更新rank_range数据失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if err = removeRanks(o, companyid, fromRankRange.RankTypeId, fromRankRange.Id, relationId); err != nil {
|
|
|
log.Error("更新rank数据失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// func SetAutoCreateRank
|
|
|
//排行榜批量转移用户时 ,把当前进行的排行榜分数置为无效
|
|
|
func removeRanks(o orm.Ormer, companyId, rankTypeId, rankRangeId int64, relationIds []int64) (err error) {
|
|
|
var sqlRemoveRanks = fmt.Sprintf(`
|
|
|
update rank set enable_status=0,update_at=NOW()
|
|
|
where company_id=? and rank_type_id=? and rank_range_id=? and rank_period_id in
|
|
|
(
|
|
|
select id from rank_period where company_id=? and rank_type_id=? and status=1
|
|
|
) and relation_id in (%v)
|
|
|
`, utils.JoinInt64s(relationIds, ","))
|
|
|
if _, err = o.Raw(sqlRemoveRanks, companyId, rankTypeId, rankRangeId,
|
|
|
companyId, rankTypeId,
|
|
|
).Exec(); err != nil {
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
} |
...
|
...
|
|