package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type UnitPlaceApi struct {
}

var unitPlaceService = service.ServiceGroupApp.AutoCodeServiceGroup.UnitPlaceService

// CreateUnitPlace 创建UnitPlace
// @Tags UnitPlace
// @Summary 创建UnitPlace
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.UnitPlace true "创建UnitPlace"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /unitPlace/createUnitPlace [post]
func (unitPlaceApi *UnitPlaceApi) CreateUnitPlace(c *gin.Context) {
	var unitPlace autocode.UnitPlace
	_ = c.ShouldBindJSON(&unitPlace)
	if err := unitPlaceService.CreateUnitPlace(unitPlace); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteUnitPlace 删除UnitPlace
// @Tags UnitPlace
// @Summary 删除UnitPlace
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.UnitPlace true "删除UnitPlace"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /unitPlace/deleteUnitPlace [delete]
func (unitPlaceApi *UnitPlaceApi) DeleteUnitPlace(c *gin.Context) {
	var unitPlace autocode.UnitPlace
	_ = c.ShouldBindJSON(&unitPlace)
	if err := unitPlaceService.DeleteUnitPlace(unitPlace); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteUnitPlaceByIds 批量删除UnitPlace
// @Tags UnitPlace
// @Summary 批量删除UnitPlace
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除UnitPlace"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /unitPlace/deleteUnitPlaceByIds [delete]
func (unitPlaceApi *UnitPlaceApi) DeleteUnitPlaceByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := unitPlaceService.DeleteUnitPlaceByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateUnitPlace 更新UnitPlace
// @Tags UnitPlace
// @Summary 更新UnitPlace
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.UnitPlace true "更新UnitPlace"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /unitPlace/updateUnitPlace [put]
func (unitPlaceApi *UnitPlaceApi) UpdateUnitPlace(c *gin.Context) {
	var unitPlace autocode.UnitPlaceVo
	_ = c.ShouldBindJSON(&unitPlace)
	_, unitPlaces := unitPlaceService.GetUnitPlaces(unitPlace.UnitId)
	newUser := make(map[string]struct{}, len(unitPlace.PlaceIds))
	for _, item := range unitPlace.PlaceIds {
		newUser[item] = struct{}{}
	}
	// 反未包含删除
	for _, unitTemp := range unitPlaces {
		if _, ok := newUser[unitTemp.PlaceId]; ok {
			continue
		} else {
			unitPlaceService.DeleteUnitPlace(unitTemp)
		}
	}

	//原有的map
	source := make(map[string]struct{}, len(unitPlaces))
	for _, item := range unitPlaces {
		source[item.PlaceId] = struct{}{}
	}
	for _, place := range unitPlace.PlaceIds {
		if _, ok := source[place]; ok {
			continue
		} else {
			// 未包含的添加
			unitPlaceService.CreateUnitPlace(autocode.UnitPlace{UnitId: unitPlace.UnitId, PlaceId: place})
		}
	}

	//计算总分满分
	_, unitPlacesNow := unitPlaceService.GetUnitPlaces(unitPlace.UnitId)
	placeIds := make([]string, 0)
	for _, places := range unitPlacesNow {
		placeIds = append(placeIds, places.PlaceId)
	}
	sum := placeService.FindPlaceSum(placeIds)

	unitService.UpdateUnitMaxIntegral(unitPlace.UnitId, sum)

	response.OkWithMessage("更新成功", c)
}

// FindUnitPlace 用id查询UnitPlace
// @Tags UnitPlace
// @Summary 用id查询UnitPlace
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.UnitPlace true "用id查询UnitPlace"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /unitPlace/findUnitPlace [get]
func (unitPlaceApi *UnitPlaceApi) FindUnitPlace(c *gin.Context) {
	var unitPlace autocode.UnitPlace
	_ = c.ShouldBindQuery(&unitPlace)
	if err, reunitPlace := unitPlaceService.GetUnitPlace(unitPlace.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reunitPlace": reunitPlace}, c)
	}
}

// GetUnitPlaceList 分页获取UnitPlace列表
// @Tags UnitPlace
// @Summary 分页获取UnitPlace列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.UnitPlaceSearch true "分页获取UnitPlace列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /unitPlace/getUnitPlaceList [get]
func (unitPlaceApi *UnitPlaceApi) GetUnitPlaceList(c *gin.Context) {
	var pageInfo autocodeReq.UnitPlaceSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := unitPlaceService.GetUnitPlaceInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
