package admin

import (
	"github.com/gin-gonic/gin"
	"server/dao"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
)

type ConstructorApi struct{}

func (ca *ConstructorApi) QueryConstructorList(c *gin.Context) {
	var info request.SearchProject
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryConstructorList ====== " + err.Error())
		return
	}
	list, total, err := constructorService.QueryConstructorList(info)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryConstructorList ====== " + err.Error())
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     info.PageInfo.Page,
		PageSize: info.PageInfo.PageSize,
	}, "获取成功", c)
}

func (ca *ConstructorApi) QueryConstructor(c *gin.Context) {
	constructors, err := constructorService.QueryConstructor()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateConstructor ====== " + err.Error())
		return
	}
	response.OkWithData(constructors, c)
}

func (ca *ConstructorApi) CreateConstructor(c *gin.Context) {
	var constructor dao.Constructor
	err := c.ShouldBindJSON(&constructor)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateConstructor ====== " + err.Error())
		return
	}
	err = constructorService.CreateConstructor(constructor)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateConstructor ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (ca *ConstructorApi) UpdateConstructor(c *gin.Context) {
	var constructor dao.Constructor
	err := c.ShouldBindJSON(&constructor)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateConstructor ====== " + err.Error())
		return
	}
	err = constructorService.UpdateConstructor(constructor)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateConstructor ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (ca *ConstructorApi) DeleteConstructor(c *gin.Context) {
	var constructor dao.Constructor
	err := c.ShouldBindJSON(&constructor)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteConstructor ====== " + err.Error())
		return
	}
	err = constructorService.DeleteConstructor(constructor)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteConstructor ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}
