package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type PlaceApi struct {
}

var placeService = service.ServiceGroupApp.AutoCodeServiceGroup.PlaceService

// CreatePlace 创建Place
// @Tags Place
// @Summary 创建Place
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Place true "创建Place"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /place/createPlace [post]
func (placeApi *PlaceApi) CreatePlace(c *gin.Context) {
	var place autocode.Place
	_ = c.ShouldBindJSON(&place)
	temp := *place.UnitMaxIntegral * 100
	place.UnitMaxIntegral = &temp
	if err := placeService.CreatePlace(place); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeletePlace 删除Place
// @Tags Place
// @Summary 删除Place
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Place true "删除Place"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /place/deletePlace [delete]
func (placeApi *PlaceApi) DeletePlace(c *gin.Context) {
	var place autocode.Place
	_ = c.ShouldBindJSON(&place)
	if err := placeService.DeletePlace(place); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeletePlaceByIds 批量删除Place
// @Tags Place
// @Summary 批量删除Place
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Place"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /place/deletePlaceByIds [delete]
func (placeApi *PlaceApi) DeletePlaceByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := placeService.DeletePlaceByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdatePlace 更新Place
// @Tags Place
// @Summary 更新Place
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Place true "更新Place"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /place/updatePlace [put]
func (placeApi *PlaceApi) UpdatePlace(c *gin.Context) {
	var place autocode.Place
	_ = c.ShouldBindJSON(&place)
	temp := *place.UnitMaxIntegral * 100
	place.UnitMaxIntegral = &temp
	if err := placeService.UpdatePlace(place); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindPlace 用id查询Place
// @Tags Place
// @Summary 用id查询Place
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.Place true "用id查询Place"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /place/findPlace [get]
func (placeApi *PlaceApi) FindPlace(c *gin.Context) {
	var place autocode.Place
	_ = c.ShouldBindQuery(&place)
	if err, replace := placeService.GetPlace(place.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		_, list := placeProService.GetPlaceProByIds(replace.ID)
		replace.ProList = list
		response.OkWithData(gin.H{"replace": replace}, c)
	}
}

// GetPlaceList 分页获取Place列表
// @Tags Place
// @Summary 分页获取Place列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.PlaceSearch true "分页获取Place列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /place/getPlaceList [get]
func (placeApi *PlaceApi) GetPlaceList(c *gin.Context) {
	var pageInfo autocodeReq.PlaceSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := placeService.GetPlaceInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
