package backstage_api

// 品牌信息

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"net/http"
	"youngmini_server/app/model/backstage_model"
	"youngmini_server/app/service/backstage_service"
	"youngmini_server/library/response"
)

var Brand = brandApi{}

type brandApi struct {
}

// CreatBrand  创建品牌信息接口
func (brand *brandApi) CreatBrand(r *ghttp.Request) {
	var apiReq *backstage_model.BrandReq
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Brand.CreatBrand(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "品牌创建成功")
}

// DeleteBrand 根据ID删除品牌信息
func (brand *brandApi) DeleteBrand(r *ghttp.Request) {
	var apiReq *backstage_model.BrandDelete
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err := backstage_service.Brand.DeleteBrand(apiReq.Id)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "删除成功")
}

// FindByIdBrand  根据ID查询品牌信息
func (brand *brandApi) FindByIdBrand(r *ghttp.Request) {
	var apiReq *backstage_model.BrandDelete
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	res, err := backstage_service.Brand.FindByIdBrand(apiReq.Id)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	var result *backstage_model.BrandResp
	err = gconv.Structs(res, &result)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, "数据查询失败，请重试")
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", result)
}

// FindAll  品牌查询所有信息接口
func (brand *brandApi) FindAll(r *ghttp.Request) {
	var apiReq *backstage_model.BrandFind
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	res, err := backstage_service.Brand.FindAllBrand(apiReq.IndexPage, apiReq.PageSize)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	var result []*backstage_model.BrandResp
	err = gconv.Structs(res, &result)
	if err != nil {
		response.JsonExit(r, http.StatusInternalServerError, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "数据返回成功", g.Map{
		"result": result,
	})
}

// UpdateBrand 更新数据
func (brand *brandApi) UpdateBrand(r *ghttp.Request) {
	var apiReq *backstage_model.BrandResp
	err := r.ParseForm(&apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	err = backstage_service.Brand.UpdateBrand(apiReq)
	if err != nil {
		response.JsonExit(r, http.StatusBadRequest, err.Error())
	}
	response.JsonExit(r, http.StatusOK, "更新成功")
}
