package twi

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

type TwiActListApi struct {
}

var twiActListService = service.ServiceGroupApp.TwiServiceGroup.TwiActListService


// CreateTwiActList 创建TwiActList
// @Tags TwiActList
// @Summary 创建TwiActList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body twi.TwiActList true "创建TwiActList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /twiActList/createTwiActList [post]
func (twiActListApi *TwiActListApi) CreateTwiActList(c *gin.Context) {
	var twiActList twi.TwiActList
	err := c.ShouldBindJSON(&twiActList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    verify := utils.Rules{
        "ActTitle":{utils.NotEmpty()},
        "StartTime":{utils.NotEmpty()},
        "EndTime":{utils.NotEmpty()},
        "Type":{utils.NotEmpty()},
        "Chain":{utils.NotEmpty()},
        "RewardType":{utils.NotEmpty()},
        "RewardAmount":{utils.NotEmpty()},
    }
	if err := utils.Verify(twiActList, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
    	twiActList.RewardAmountRemain = twiActList.RewardAmount
    	twiActList.RewardPersonRemain = twiActList.RewardPerson
	if err := twiActListService.CreateTwiActList(twiActList); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

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

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

// UpdateTwiActList 更新TwiActList
// @Tags TwiActList
// @Summary 更新TwiActList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body twi.TwiActList true "更新TwiActList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /twiActList/updateTwiActList [put]
func (twiActListApi *TwiActListApi) UpdateTwiActList(c *gin.Context) {
	var twiActList twi.TwiActList
	err := c.ShouldBindJSON(&twiActList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
      verify := utils.Rules{
          "ActTitle":{utils.NotEmpty()},
          "StartTime":{utils.NotEmpty()},
          "EndTime":{utils.NotEmpty()},
          "Type":{utils.NotEmpty()},
          "Chain":{utils.NotEmpty()},
          "RewardType":{utils.NotEmpty()},
      }
    if err := utils.Verify(twiActList, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := twiActListService.UpdateTwiActList(twiActList); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindTwiActList 用id查询TwiActList
// @Tags TwiActList
// @Summary 用id查询TwiActList
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query twi.TwiActList true "用id查询TwiActList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /twiActList/findTwiActList [get]
func (twiActListApi *TwiActListApi) FindTwiActList(c *gin.Context) {
	var twiActList twi.TwiActList
	err := c.ShouldBindQuery(&twiActList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if retwiActList, err := twiActListService.GetTwiActList(twiActList.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"retwiActList": retwiActList}, c)
	}
}

// FindTwiActListWeb 用id查询TwiActList
// @Tags TwiActList
// @Summary 用id查询TwiActList
// @accept application/json
// @Produce application/json
// @Param data query twi.TwiActList true "用id查询TwiActList"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /twiActList/info [get]
func (twiActListApi *TwiActListApi) FindTwiActListWeb(c *gin.Context) {
	var twiActList twi.TwiActList
	err := c.ShouldBindQuery(&twiActList)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if retwiActList, err := twiActListService.GetTwiActList(twiActList.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
		return
	} else {
		actTemps,err := twiActTemplateService.GetTwiActTemplateByAid(retwiActList.ID)
		if err != nil {
			global.GVA_LOG.Error("查询失败!", zap.Error(err))
			response.FailWithMessage("查询失败", c)
			return
		}
		project,err := twiProjectService.GetTwiProject(uint(*retwiActList.ProjectId))
		if err != nil {
			global.GVA_LOG.Error("查询失败!", zap.Error(err))
			response.FailWithMessage("查询失败", c)
			return
		}
		response.OkWithData(gin.H{"data": twi.TwiActInfo{Activity: retwiActList,Project: project,TemplateList: actTemps}}, c)
	}
}

// GetTwiActListListWeb 分页获取TwiActList列表
// @Tags GetTwiActListListWeb
// @Summary 分页获取TwiActList列表
// @accept application/json
// @Produce application/json
// @Param data query twiReq.TwiActListSearch true "分页获取TwiActList列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /twiActList/list [get]
func (twiActListApi *TwiActListApi) GetTwiActListListWeb(c *gin.Context) {
	var pageInfo twiReq.TwiActListSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := twiActListService.GetTwiActListInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	} else {
		newList := make([]twi.TwiActInfo,0)
		for _ ,v := range list {
			if v.ProjectId == nil {
				response.FailWithMessage("活动异常", c)
				return
			}
			nv := new(twi.TwiActInfo)
			project,err := twiProjectService.GetTwiProject(uint(*v.ProjectId))
			if err != nil {
				global.GVA_LOG.Error("查询失败!", zap.Error(err))
				response.FailWithMessage("查询失败", c)
				return
			}
			persons,count,err := twiActFinishDetailService.GetPersonsByActId(int(v.ID))
			if err != nil {
				global.GVA_LOG.Error("查询失败!", zap.Error(err))
				response.FailWithMessage("查询失败", c)
				return
			}
			v.Count = int(count)
			nv.Project = project
			nv.Activity = v
			nv.Persons = persons
			newList = append(newList,*nv)
		}
		response.OkWithDetailed(response.PageResult{
			List:     newList,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetTwiActListList 分页获取TwiActList列表
// @Tags TwiActList
// @Summary 分页获取TwiActList列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query twiReq.TwiActListSearch true "分页获取TwiActList列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /twiActList/getTwiActListList [get]
func (twiActListApi *TwiActListApi) GetTwiActListList(c *gin.Context) {
	var pageInfo twiReq.TwiActListSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := twiActListService.GetTwiActListInfoList(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)
    }
}
