package typeManage

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/request"
	"log-server/model/common/response"
	"log-server/model/typeManage"
	typeManageRes "log-server/model/typeManage/response"
	"log-server/utils"
)

type ResponsiblePersonApi struct {}

//增加负责人
func (r *ResponsiblePersonApi) CreateResponsiblePerson(c *gin.Context)  {
	var responsiblePerson typeManage.ResponsiblePerson
	_ = c.ShouldBindJSON(&responsiblePerson)
	if err := utils.Verify(responsiblePerson, utils.ResponsiblePersonVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := responsiblePersonService.CreateResponsiblePerson(responsiblePerson);err != nil{
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

//删除单个负责人
func (r *ResponsiblePersonApi) DeleteResponsiblePerson(c *gin.Context)  {
	var person typeManage.ResponsiblePerson
	_ = c.ShouldBindJSON(&person)
	//数据校验
	if err := utils.Verify(person, utils.ResponsiblePersonVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := responsiblePersonService.DeleteResponsiblePerson(person);err != nil{
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除
func (r *ResponsiblePersonApi) DeleteResponsiblePersonsByIds(c *gin.Context)  {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := responsiblePersonService.DeleteResponsiblePersonsByIds(ids);err != nil{
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}

}

//更新负责人信息
func (r *ResponsiblePersonApi) UpdateResponsiblePerson(c *gin.Context)  {
	var person typeManage.ResponsiblePerson
	_ = c.ShouldBindJSON(&person)
	if err := utils.Verify(person, utils.ResponsiblePersonVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := responsiblePersonService.UpdateResponsiblePerson(person);err != nil{
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
	} else {
		response.OkWithMessage("修改成功", c)
	}

}

//获取单个负责人信息
func (r *ResponsiblePersonApi) GetResponsiblePersonById(c *gin.Context)  {
	var id request.GetById
	_ = c.ShouldBindJSON(&id)
	//数据校验
	if err := utils.Verify(id, utils.IdVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if person, err := responsiblePersonService.GetResponsiblePersonById(id.ID);err != nil{
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(typeManageRes.ResponsiblePersonResponse{ResponsiblePerson: person}, "获取成功", c)
	}
}

//条件查询所有负责人
func (r *ResponsiblePersonApi) GetResponsiblePerson(c *gin.Context)  {
	var pageInfo typeManage.SearchResponsiblePersonParams
	_ = c.ShouldBindJSON(&pageInfo)
	if err := utils.Verify(pageInfo, utils.ResponsiblePersonVerify);err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := responsiblePersonService.GetResponsiblePerson(pageInfo.ResponsiblePerson, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc);err != nil{
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//响应结果
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}