package apis

import (
	"fmt"
	oDto "go-admin/app/operate/service/dto"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/mobile/service"
	"go-admin/app/mobile/service/dto"
	"go-admin/app/operate/models"
	oService "go-admin/app/operate/service"
	"go-admin/common/actions"
)

type OrgNews struct {
	api.Api
}

// GetPage <手机端>首页
// @Summary <手机端>首页
// @Description <手机端>首页
// @Tags <手机端>首页
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgNewsGetPageReq true "data"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-news [post]
// @Security Bearer
func (e OrgNews) GetPage(c *gin.Context) {
	req := dto.OrgNewsGetPageReq{}
	s := service.OrgNews{}

	err := c.Bind(&req)
	err = e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)

	reqOrgAd := oDto.OrgAdGetPageReq{}
	sOrgAd := oService.OrgAd{}
	e.MakeContext(c).MakeOrm().MakeService(&sOrgAd.Service)
	reqOrgAd.Status = "1"
	listOrgAd := make([]models.OrgAd, 0)
	var countOrgAd int64

	err = sOrgAd.GetPage(&reqOrgAd, p, &listOrgAd, &countOrgAd)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取广告 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	list := make([]dto.OrgNews, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取新闻 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	var reply dto.OrgNewsGetPageReply
	reply.OrgAd = listOrgAd
	reply.OrgNews = list

	e.PageOK(reply, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取新闻详情
// @Summary 获取新闻详情
// @Description 获取新闻详情
// @Tags <手机端>首页
// @Param id path string false "id"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-news/{id} [get]
// @Security Bearer
func (e OrgNews) Get(c *gin.Context) {
	req := dto.OrgNewsGetReq{}
	s := service.OrgNews{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.OrgNews

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取新闻失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}
