achievement.go 5.3 KB
package controllers

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

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
	}
	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
	}
	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
	err = models.UpdateAchievementById(achievementData, []string{"Status"})
	if err != nil {
		log.Error("更新achievement数据失败:%s", err)
	}
	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
}