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 UnitUserApi struct {
}

var unitUserService = service.ServiceGroupApp.AutoCodeServiceGroup.UnitUserService

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

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

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

// UpdateUnitUser 更新UnitUser
// @Tags UnitUser
// @Summary 更新UnitUser
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.UnitUser true "更新UnitUser"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /unitUser/updateUnitUser [put]
func (unitUserApi *UnitUserApi) UpdateUnitUser(c *gin.Context) {
	var unitUser autocode.UnitUserVo
	_ = c.ShouldBindJSON(&unitUser)
	_, unitUsers := unitUserService.GetUnitUsers(unitUser.UnitId)
	newUser := make(map[string]struct{}, len(unitUser.UUID))
	for _, item := range unitUser.UUID {
		newUser[item] = struct{}{}
	}
	// 反未包含删除
	for _, unitTemp := range unitUsers {
		if _, ok := newUser[unitTemp.UUID]; ok {
			continue
		} else {
			unitUserService.DeleteUnitUser(unitTemp)
		}
	}

	//原有的map
	source := make(map[string]struct{}, len(unitUsers))
	for _, item := range unitUsers {
		source[item.UUID] = struct{}{}
	}
	for _, uuid := range unitUser.UUID {
		if _, ok := source[uuid]; ok {
			continue
		} else {
			// 未包含的添加
			unitUserService.CreateUnitUser(autocode.UnitUser{UnitId: unitUser.UnitId, UUID: uuid})
		}
	}
	response.OkWithMessage("更新成功", c)
}

// FindUnitUser 用id查询UnitUser
// @Tags UnitUser
// @Summary 用id查询UnitUser
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.UnitUser true "用id查询UnitUser"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /unitUser/findUnitUser [get]
func (unitUserApi *UnitUserApi) FindUnitUser(c *gin.Context) {
	var unitUser autocode.UnitUser
	_ = c.ShouldBindQuery(&unitUser)
	if err, reunitUser := unitUserService.GetUnitUser(unitUser.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reunitUser": reunitUser}, c)
	}
}

// GetUnitUserList 分页获取UnitUser列表
// @Tags UnitUser
// @Summary 分页获取UnitUser列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.UnitUserSearch true "分页获取UnitUser列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /unitUser/getUnitUserList [get]
func (unitUserApi *UnitUserApi) GetUnitUserList(c *gin.Context) {
	var pageInfo autocodeReq.UnitUserSearch
	_ = c.ShouldBindQuery(&pageInfo)
	_, list := unitUserService.GetUnitUsers(pageInfo.UnitId)
	temp := make([]string, 0)
	for _, item := range list {
		temp = append(temp, item.UUID)
	}
	response.OkWithData(temp, c)
}
