package twi

import (
	"courseSign/server/global"
    "courseSign/server/model/twi"
    "courseSign/server/model/common/request"
    twiReq "courseSign/server/model/twi/request"
    "courseSign/server/model/common/response"
    "courseSign/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
    "courseSign/server/utils"
)

type TwiActTemplateApi struct {
}

var twiActTemplateService = service.ServiceGroupApp.TwiServiceGroup.TwiActTemplateService


// CreateTwiActTemplate 创建TwiActTemplate
// @Tags TwiActTemplate
// @Summary 创建TwiActTemplate
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body twi.TwiActTemplate true "创建TwiActTemplate"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /twiActTemplate/createTwiActTemplate [post]
func (twiActTemplateApi *TwiActTemplateApi) CreateTwiActTemplate(c *gin.Context) {
	var twiActTemplate twi.TwiActTemplate
	err := c.ShouldBindJSON(&twiActTemplate)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    verify := utils.Rules{
        "ActId":{utils.NotEmpty()},
        "ActType":{utils.NotEmpty()},
        "ActContent":{utils.NotEmpty()},
    }
	if err := utils.Verify(twiActTemplate, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := twiActTemplateService.CreateTwiActTemplate(twiActTemplate); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteTwiActTemplate 删除TwiActTemplate
// @Tags TwiActTemplate
// @Summary 删除TwiActTemplate
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body twi.TwiActTemplate true "删除TwiActTemplate"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /twiActTemplate/deleteTwiActTemplate [delete]
func (twiActTemplateApi *TwiActTemplateApi) DeleteTwiActTemplate(c *gin.Context) {
	var twiActTemplate twi.TwiActTemplate
	err := c.ShouldBindJSON(&twiActTemplate)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := twiActTemplateService.DeleteTwiActTemplate(twiActTemplate); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteTwiActTemplateByIds 批量删除TwiActTemplate
// @Tags TwiActTemplate
// @Summary 批量删除TwiActTemplate
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除TwiActTemplate"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /twiActTemplate/deleteTwiActTemplateByIds [delete]
func (twiActTemplateApi *TwiActTemplateApi) DeleteTwiActTemplateByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := twiActTemplateService.DeleteTwiActTemplateByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateTwiActTemplate 更新TwiActTemplate
// @Tags TwiActTemplate
// @Summary 更新TwiActTemplate
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body twi.TwiActTemplate true "更新TwiActTemplate"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /twiActTemplate/updateTwiActTemplate [put]
func (twiActTemplateApi *TwiActTemplateApi) UpdateTwiActTemplate(c *gin.Context) {
	var twiActTemplate twi.TwiActTemplate
	err := c.ShouldBindJSON(&twiActTemplate)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
      verify := utils.Rules{
          "ActId":{utils.NotEmpty()},
          "ActType":{utils.NotEmpty()},
          "ActContent":{utils.NotEmpty()},
      }
    if err := utils.Verify(twiActTemplate, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := twiActTemplateService.UpdateTwiActTemplate(twiActTemplate); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindTwiActTemplate 用id查询TwiActTemplate
// @Tags TwiActTemplate
// @Summary 用id查询TwiActTemplate
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query twi.TwiActTemplate true "用id查询TwiActTemplate"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /twiActTemplate/findTwiActTemplate [get]
func (twiActTemplateApi *TwiActTemplateApi) FindTwiActTemplate(c *gin.Context) {
	var twiActTemplate twi.TwiActTemplate
	err := c.ShouldBindQuery(&twiActTemplate)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if retwiActTemplate, err := twiActTemplateService.GetTwiActTemplate(twiActTemplate.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"retwiActTemplate": retwiActTemplate}, c)
	}
}

// GetTwiActTemplateList 分页获取TwiActTemplate列表
// @Tags TwiActTemplate
// @Summary 分页获取TwiActTemplate列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query twiReq.TwiActTemplateSearch true "分页获取TwiActTemplate列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /twiActTemplate/getTwiActTemplateList [get]
func (twiActTemplateApi *TwiActTemplateApi) GetTwiActTemplateList(c *gin.Context) {
	var pageInfo twiReq.TwiActTemplateSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := twiActTemplateService.GetTwiActTemplateInfoList(pageInfo); 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)
    }
}
