package pressreleases

import (
	"errors"

	"github.com/gin-gonic/gin"

	"fonchain-fiee/api/pressreleases"
	"fonchain-fiee/pkg/model/login"
	"fonchain-fiee/pkg/service"
)

func Create(ctx *gin.Context) {
	var req pressreleases.CreateReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.PressReleasesProvider.Create(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Delete(ctx *gin.Context) {
	var req pressreleases.DeleteReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.Delete(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Display(ctx *gin.Context) {
	var req pressreleases.DisplayReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if req.LangType == "" {
		service.Error(ctx, errors.New("语言类型为空"))
		return
	}
	resp, err := service.PressReleasesProvider.Display(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func SortAndStatus(ctx *gin.Context) {
	var req pressreleases.SortAndStatusReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.SortAndStatus(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Info(ctx *gin.Context) {
	var req pressreleases.InfoReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.Info(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func List(ctx *gin.Context) {
	var req pressreleases.ListReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.PressReleasesProvider.List(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Edit(ctx *gin.Context) {
	var req pressreleases.EditReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.PressReleasesProvider.Edit(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Get(ctx *gin.Context) {
	var req pressreleases.GetReq
	uuid := ctx.Query("uuid")
	if uuid == "" {
		service.Error(ctx, errors.New("uuid 参数不能为空"))
		return
	}
	langType := ctx.Query("langType")
	if langType == "" {
		service.Error(ctx, errors.New("langType 参数不能为空"))
		return
	}
	req.Uuid = uuid
	req.LangType = langType
	resp, err := service.PressReleasesProvider.Get(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}
