achievement.go 7.4 KB
package controllers

import (
	"encoding/json"
	"fmt"
	"oppmg/common/log"
	"oppmg/models"
	"oppmg/protocol"
	serveachievement "oppmg/services/achievement"

	"github.com/astaxie/beego/orm"
)

type AchievementController struct {
	BaseController
}

//AchievementList 成果列表
//@router /achievement/list
func (c AchievementController) AchievementList() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		protocol.RequestPageInfo
		Status int `json:"status"`
	}
	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 := serveachievement.GetAchievementList(param.PageIndex, param.PageSize, companyid, param.Status)
	msg = protocol.NewPageDataResponse(rspdata, nil)
	return
}

//AddAchievement 添加成果列表
//@router /achievement/add
func (c AchievementController) AddAchievement() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()

	var param protocol.RequestAddAchievement
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if len(param.ChanceData) == 0 {
		log.Error("len(param.ChanceData) == 0 ")
		msg = protocol.BadRequestParam("10109")
		return
	}
	if param.ChanceTypeId == 0 {
		log.Error(" param.ChanceTypeId == 0 ")
		msg = protocol.BadRequestParam("10108")
		return
	}
	if param.GraspScore < 0 || param.GraspScore > 100 {
		log.Error(" param.GraspScore < 0 || param.GraspScore > 100 ")
		msg = protocol.BadRequestParam("10121")
		return
	}
	if param.UserGraspScore < 0 || param.UserGraspScore > 100 {
		log.Error("param.UserGraspScore < 0 || param.UserGraspScore > 100")
		msg = protocol.BadRequestParam("10122")
	}
	var providerScore float64
	for _, v := range param.Provider {
		providerScore += v.UserGraspScore
	}
	remainScore := param.GraspScore - param.UserGraspScore - providerScore
	if remainScore < 0 {
		log.Error("分配的总分不可大于把握分")
		msg = protocol.BadRequestParam("10122")
		return
	}

	companyid := c.GetCompanyId()
	err := serveachievement.AddAchievement(&param, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//EditAchievement 编辑成果
//@router /achievement/edit
func (c AchievementController) EditAchievement() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestEditAchievement
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if len(param.ChanceData) == 0 {
		log.Error("len(param.ChanceData) == 0 ")
		msg = protocol.BadRequestParam("10109")
		return
	}
	if param.ChanceTypeId == 0 {
		log.Error(" param.ChanceTypeId == 0 ")
		msg = protocol.BadRequestParam("10108")
		return
	}
	if param.GraspScore < 0 || param.GraspScore > 100 {
		log.Error(" param.GraspScore < 0 || param.GraspScore > 100 ")
		msg = protocol.BadRequestParam("10121")
		return
	}
	if param.UserGraspScore < 0 || param.UserGraspScore > 100 {
		log.Error("param.UserGraspScore < 0 || param.UserGraspScore > 100")
		msg = protocol.BadRequestParam("10122")
	}
	var providerScore float64
	for _, v := range param.Provider {
		providerScore += v.UserGraspScore
	}
	remainScore := param.GraspScore - param.UserGraspScore - providerScore
	if remainScore < 0 {
		log.Error("分配的总分不可大于把握分")
		msg = protocol.BadRequestParam("10122")
		return
	}
	companyid := c.GetCompanyId()
	err := serveachievement.EditAchievement(&param, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//EditAchievement 成果详情
//@router /achievement/info
func (c AchievementController) AchievementInfo() {
	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 := serveachievement.GetAchievementInfo(param.Id, companyid)
	msg = protocol.NewReturnResponse(rspData, nil)
	return
}

//EditAchievement 删除成果
//@router /achievement/delete
func (c AchievementController) DeleteAchievement() {
	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()
	var (
		err             error
		achievementData *models.Achievement
	)
	achievementData, err = models.GetAchievementById(param.Id)
	if err != nil {
		log.Error("获取achievement数据失败:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if achievementData.CompanyId != companyid {
		log.Error("achievement数据公司不匹配")
		msg = protocol.BadRequestParam("1")
		return
	}
	achievementData.Status = models.ACHIEVEMENT_STATUS_DEL
	o := orm.NewOrm()
	o.Begin()
	err = models.UpdateAchievementById(achievementData, []string{"Status"})
	if err != nil {
		o.Rollback()
		log.Error("更新achievement数据失败:%s", err)
	}
	err = models.IncreaseAchevementScore(companyid, -achievementData.GraspScoreRemain, o)
	if err != nil {
		o.Rollback()
		log.Error("更新福利池失败数据失败:%s", err)
	}
	o.Commit()
	msg = protocol.NewReturnResponse(nil, nil)
	return
}

//EditAchievement 显示隐藏成果
//@router /achievement/forbid_allow
func (c AchievementController) ForbidAllowAchievement() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Id     int64 `json:"id"`
		Status int8  `json:"status"` //[1:显示][2:隐藏]
	}
	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
	}
	if param.Status != 1 && param.Status != 2 {
		msg = protocol.BadRequestParam("1")
		return
	}
	companyid := c.GetCompanyId()
	var (
		err             error
		achievementData *models.Achievement
	)
	achievementData, err = models.GetAchievementById(param.Id)
	if err != nil {
		log.Error("获取achievement数据失败:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if achievementData.CompanyId != companyid {
		log.Error("achievement数据公司不匹配")
		msg = protocol.BadRequestParam("1")
		return
	}

	achievementData.Status = param.Status
	err = models.UpdateAchievementById(achievementData, []string{"Status"})
	if err != nil {
		log.Error("更新achievement数据失败:%s", err)
	}
	msg = protocol.NewReturnResponse(nil, nil)
	return
}

//AchievementScoreRemain 获取福利池分数
//@router /achievement/remain/score
func (c AchievementController) AchievementScoreRemain() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	companyid := c.GetCompanyId()
	scoreData, err := models.GetAchevementScoreByCompanyId(companyid)
	rspData := map[string]string{
		"score": "0",
	}
	if err == nil {
		rspData["score"] = fmt.Sprintf("%.1f", scoreData.GraspScoreRemain)
	}
	msg = protocol.NewReturnResponse(rspData, nil)
	return
}