529 lines
13 KiB
Go
529 lines
13 KiB
Go
package controller
|
|
|
|
import (
|
|
"case-admin-api/api/dao"
|
|
"case-admin-api/api/dto"
|
|
"case-admin-api/api/model"
|
|
"case-admin-api/api/requests"
|
|
"case-admin-api/api/responses"
|
|
"case-admin-api/api/service"
|
|
"case-admin-api/global"
|
|
"case-admin-api/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"strconv"
|
|
)
|
|
|
|
type ProjectPlatform struct{}
|
|
|
|
// GetProjectPlatformPage 获取列表-分页
|
|
func (b *ProjectPlatform) GetProjectPlatformPage(c *gin.Context) {
|
|
projectPlatformRequest := requests.ProjectPlatformRequest{}
|
|
req := projectPlatformRequest.GetProjectPlatformPage
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
// 获取数据
|
|
projectPlatformDao := dao.ProjectPlatformDao{}
|
|
projectPlatforms, total, err := projectPlatformDao.GetProjectPlatformPageSearch(req, req.Page, req.PageSize)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetProjectPlatformListDto(projectPlatforms)
|
|
|
|
result := make(map[string]interface{})
|
|
result["page"] = req.Page
|
|
result["page_size"] = req.PageSize
|
|
result["total"] = total
|
|
result["data"] = g
|
|
responses.OkWithData(result, c)
|
|
}
|
|
|
|
// GetProjectPlatform 获取详情
|
|
func (b *ProjectPlatform) GetProjectPlatform(c *gin.Context) {
|
|
id := c.Param("project_platform_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
projectPlatformId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
projectPlatformDao := dao.ProjectPlatformDao{}
|
|
projectPlatform, err := projectPlatformDao.GetProjectPlatformById(projectPlatformId)
|
|
if err != nil {
|
|
responses.FailWithMessage("数据异常", c)
|
|
return
|
|
}
|
|
|
|
platformDao := dao.PlatformDao{}
|
|
platform, err := platformDao.GetPlatformById(projectPlatform.PlatformId)
|
|
if err != nil {
|
|
responses.FailWithMessage("数据异常", c)
|
|
return
|
|
}
|
|
|
|
g := dto.GetProjectPlatformDto(projectPlatform)
|
|
|
|
// 加载数据-平台名称
|
|
g.LoadPlatFormName(platform)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// PutProjectPlatformStatus 操作状态
|
|
func (b *ProjectPlatform) PutProjectPlatformStatus(c *gin.Context) {
|
|
projectPlatformRequest := requests.ProjectPlatformRequest{}
|
|
req := projectPlatformRequest.PutProjectPlatformStatus
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("project_platform_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
projectPlatformId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
projectPlatformDao := dao.ProjectPlatformDao{}
|
|
projectPlatform, err := projectPlatformDao.GetProjectPlatformById(projectPlatformId)
|
|
if err != nil {
|
|
responses.FailWithMessage("数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 检测状态
|
|
if projectPlatform.Status == req.Status {
|
|
responses.Ok(c)
|
|
return
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
projectPlatformData := make(map[string]interface{})
|
|
projectPlatformData["status"] = req.Status
|
|
err = projectPlatformDao.EditProjectPlatformById(tx, projectPlatformId, projectPlatformData)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// AddProjectPlatform 新增
|
|
func (b *ProjectPlatform) AddProjectPlatform(c *gin.Context) {
|
|
projectPlatformRequest := requests.ProjectPlatformRequest{}
|
|
req := projectPlatformRequest.AddProjectPlatform
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
// 开启福利参数判断
|
|
if req.IsWelfare == 1 {
|
|
if req.ReadDuration == 0 || req.SingleCaseScore == 0 || req.CompleteRead == 0 {
|
|
responses.FailWithMessage("请填入对应福利配置", c)
|
|
return
|
|
}
|
|
|
|
if req.CompleteRead > req.SingleCaseScore {
|
|
responses.FailWithMessage("完成阅读积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if req.CompleteReadTime > req.SingleCaseScore {
|
|
responses.FailWithMessage("完成阅读时间积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if req.FirstHighQuality > req.SingleCaseScore {
|
|
responses.FailWithMessage("首次优质留言积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if req.OnceMoreHighQuality > req.SingleCaseScore {
|
|
responses.FailWithMessage("再次优质留言积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if (req.CompleteRead + req.CompleteReadTime + req.FirstHighQuality + req.OnceMoreHighQuality) > req.SingleCaseScore {
|
|
responses.FailWithMessage("可获取积分之和应小于或等于总积分", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 开启白名单参数判断
|
|
if req.IsWhite == 1 {
|
|
if req.WhiteType == 0 {
|
|
responses.FailWithMessage("请选择需要开启的白名单类型", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 检测项目
|
|
// 将 id 转换为 int64 类型
|
|
projectId, err := strconv.ParseInt(req.ProjectId, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
projectDao := dao.ProjectDao{}
|
|
project, err := projectDao.GetProjectById(projectId)
|
|
if err != nil {
|
|
responses.FailWithMessage("项目数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 检测状态
|
|
if project.ProjectStatus == 0 {
|
|
responses.FailWithMessage("项目无效,无法添加", c)
|
|
return
|
|
}
|
|
|
|
// 检测平台
|
|
// 将 id 转换为 int64 类型
|
|
platformId, err := strconv.ParseInt(req.PlatformId, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
platformDao := dao.PlatformDao{}
|
|
platform, err := platformDao.GetPlatformById(platformId)
|
|
if err != nil {
|
|
responses.FailWithMessage("平台数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 检测状态
|
|
if platform.PlatformStatus == 0 {
|
|
responses.FailWithMessage("平台无效,无法添加", c)
|
|
return
|
|
}
|
|
|
|
// 检测重复
|
|
projectPlatformDao := dao.ProjectPlatformDao{}
|
|
maps := make(map[string]interface{})
|
|
maps["project_id"] = req.ProjectId
|
|
maps["platform_id"] = req.PlatformId
|
|
projectPlatform, _ := projectPlatformDao.GetProjectPlatform(maps)
|
|
if projectPlatform != nil {
|
|
responses.FailWithMessage("重复添加", c)
|
|
return
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
// 新增项目平台
|
|
projectPlatform = &model.ProjectPlatform{
|
|
ProjectId: project.ProjectId,
|
|
PlatformId: platformId,
|
|
Status: req.Status,
|
|
ReadDuration: req.ReadDuration,
|
|
IsWelfare: req.IsWelfare,
|
|
SingleCaseScore: req.SingleCaseScore,
|
|
CompleteRead: req.CompleteRead,
|
|
CompleteReadTime: req.CompleteReadTime,
|
|
FirstHighQuality: req.FirstHighQuality,
|
|
OnceMoreHighQuality: req.OnceMoreHighQuality,
|
|
IsWhite: req.IsWhite,
|
|
WhiteType: req.WhiteType,
|
|
}
|
|
|
|
projectPlatform, err = projectPlatformDao.AddProjectPlatform(tx, projectPlatform)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 获取项目下所有病例
|
|
caseDao := dao.CaseDao{}
|
|
maps = make(map[string]interface{})
|
|
maps["project_id"] = project.ProjectId
|
|
results, _ := caseDao.GetCaseList(maps)
|
|
if len(results) > 0 {
|
|
// 新增病例-平台
|
|
casePlatformDao := dao.CasePlatformDao{}
|
|
for _, result := range results {
|
|
casePlatform := &model.CasePlatform{
|
|
CaseId: result.CaseId,
|
|
PlatformId: platformId,
|
|
IssuedScore: 0,
|
|
JoinNum: 0,
|
|
JoinWhiteNum: 0,
|
|
MessageNum: 0,
|
|
}
|
|
|
|
_, err := casePlatformDao.AddCasePlatform(tx, casePlatform)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// PutProjectPlatform 修改
|
|
func (b *ProjectPlatform) PutProjectPlatform(c *gin.Context) {
|
|
projectPlatformRequest := requests.ProjectPlatformRequest{}
|
|
req := projectPlatformRequest.PutProjectPlatform
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
id := c.Param("project_platform_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
projectPlatformId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
projectPlatformDao := dao.ProjectPlatformDao{}
|
|
projectPlatform, err := projectPlatformDao.GetProjectPlatformById(projectPlatformId)
|
|
if err != nil {
|
|
responses.FailWithMessage("数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 开启福利参数判断
|
|
if req.IsWelfare == 1 {
|
|
if req.ReadDuration == 0 || req.SingleCaseScore == 0 || req.CompleteRead == 0 {
|
|
responses.FailWithMessage("请填入对应福利配置", c)
|
|
return
|
|
}
|
|
|
|
if req.CompleteRead > req.SingleCaseScore {
|
|
responses.FailWithMessage("完成阅读积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if req.CompleteReadTime > req.SingleCaseScore {
|
|
responses.FailWithMessage("完成阅读时间积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if req.FirstHighQuality > req.SingleCaseScore {
|
|
responses.FailWithMessage("首次优质留言积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if req.OnceMoreHighQuality > req.SingleCaseScore {
|
|
responses.FailWithMessage("再次优质留言积分应小于总积分", c)
|
|
return
|
|
}
|
|
|
|
if (req.CompleteRead + req.CompleteReadTime + req.FirstHighQuality + req.OnceMoreHighQuality) > req.SingleCaseScore {
|
|
responses.FailWithMessage("可获取积分之和应小于或等于总积分", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 开启白名单参数判断
|
|
if req.IsWhite == 1 {
|
|
if req.WhiteType == 0 {
|
|
responses.FailWithMessage("请选择需要开启的白名单类型", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 检测项目
|
|
// 将 id 转换为 int64 类型
|
|
projectId, err := strconv.ParseInt(req.ProjectId, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
projectDao := dao.ProjectDao{}
|
|
project, err := projectDao.GetProjectById(projectId)
|
|
if err != nil {
|
|
responses.FailWithMessage("项目数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 检测状态
|
|
if project.ProjectStatus == 0 {
|
|
responses.FailWithMessage("项目无效,无法添加", c)
|
|
return
|
|
}
|
|
|
|
if project.ProjectId != projectPlatform.ProjectId {
|
|
responses.FailWithMessage("平台未绑定改项目,无法操作", c)
|
|
return
|
|
}
|
|
|
|
platformId, err := strconv.ParseInt(req.PlatformId, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 修改值
|
|
projectPlatformData := make(map[string]interface{})
|
|
|
|
// 平台名称
|
|
if platformId != projectPlatform.PlatformId {
|
|
responses.FailWithMessage("关联平台不可更改", c)
|
|
return
|
|
}
|
|
|
|
// 状态
|
|
if req.Status != projectPlatform.Status {
|
|
projectPlatformData["status"] = req.Status
|
|
}
|
|
|
|
// 是否开启福利
|
|
if req.IsWelfare != projectPlatform.IsWelfare {
|
|
projectPlatformData["is_welfare"] = req.IsWelfare
|
|
}
|
|
|
|
// 阅读时长(秒)
|
|
if req.ReadDuration != projectPlatform.ReadDuration {
|
|
projectPlatformData["read_duration"] = req.ReadDuration
|
|
}
|
|
|
|
// 单个病例总积分
|
|
if req.SingleCaseScore != projectPlatform.SingleCaseScore {
|
|
projectPlatformData["single_case_score"] = req.SingleCaseScore
|
|
}
|
|
|
|
// 完成阅读积分
|
|
if req.CompleteRead != projectPlatform.CompleteRead {
|
|
projectPlatformData["complete_read"] = req.CompleteRead
|
|
}
|
|
|
|
// 完成阅读时间积分
|
|
if req.CompleteReadTime != projectPlatform.CompleteReadTime {
|
|
projectPlatformData["complete_read_time"] = req.CompleteReadTime
|
|
}
|
|
|
|
// 首次优质留言积分
|
|
if req.FirstHighQuality != projectPlatform.FirstHighQuality {
|
|
projectPlatformData["first_high_quality"] = req.FirstHighQuality
|
|
}
|
|
|
|
// 再次优质留言积分
|
|
if req.OnceMoreHighQuality != projectPlatform.OnceMoreHighQuality {
|
|
projectPlatformData["once_more_high_quality"] = req.OnceMoreHighQuality
|
|
}
|
|
|
|
// 是否开启白名单
|
|
if req.IsWhite != projectPlatform.IsWhite {
|
|
projectPlatformData["is_white"] = req.IsWhite
|
|
}
|
|
|
|
// 白名单类型
|
|
if req.WhiteType != projectPlatform.WhiteType {
|
|
projectPlatformData["white_type"] = req.WhiteType
|
|
}
|
|
|
|
if len(projectPlatformData) > 0 {
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
err = projectPlatformDao.EditProjectPlatformById(tx, platformId, projectPlatformData)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
// 白名单类型变更时,删除原白名单数据
|
|
if req.WhiteType != projectPlatform.WhiteType {
|
|
projectPlatformWhiteService := service.ProjectPlatformWhiteService{}
|
|
err = projectPlatformWhiteService.DeleteProjectPlatformWhite(tx, projectPlatform.WhiteType, platformId)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
tx.Commit()
|
|
}
|
|
|
|
responses.Ok(c)
|
|
}
|