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"
	typeManageReq "log-server/model/typeManage/request"
	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)
	//推送状态和启用状态默认赋予1
	if responsiblePerson.PushStatus == 0 {
		responsiblePerson.PushStatus = 1
	}
	if responsiblePerson.State == 0 {
		responsiblePerson.State = 1
	}

	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)
	//获取托管状态， 1是托管， -1不需要托管
	//不需要托管
	//if person.ManagedStatus == -1 {
	//	person.Custodians = ""
	//}
	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)
	}
}

// GetResponsiblePersonAll 查询所有
func (r *ResponsiblePersonApi) GetResponsiblePersonAll(c *gin.Context) {

	if list, err := responsiblePersonService.GetResponsiblePersonAll(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//响应结果
		response.OkWithDetailed(response.PageResult{
			List: list,
		}, "获取成功", c)
	}
}

func (r *ResponsiblePersonApi) SelectResponsiblePerson(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, err := responsiblePersonService.SelectResponsiblePersonList(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(list, "获取成功", c)
	}
}

//更改负责人启用状态
func (r *ResponsiblePersonApi) StatusChange(c *gin.Context) {
	var paramsInfo typeManageReq.UpdateResponsibleStatusRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if paramsInfo.State == 0 {
		response.FailWithMessage("状态值不能为空", c)
		return
	}
	if paramsInfo.Id == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	if err := responsiblePersonService.UpdateResponsibleStatus(paramsInfo); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

//更改负责人推送状态
func (r *ResponsiblePersonApi) PushStatusChange(c *gin.Context) {
	var paramsInfo typeManageReq.UpdatePushStatusRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if paramsInfo.PushStatus == 0 {
		response.FailWithMessage("状态值不能为空", c)
		return
	}
	if paramsInfo.Id == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	if err := responsiblePersonService.UpdatePushStatus(paramsInfo); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}