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/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type OrganizationApi struct {
}

var organizationService = service.ServiceGroupApp.AutoCodeServiceGroup.OrganizationService

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

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

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

// UpdateOrganization 更新Organization
// @Tags Organization
// @Summary 更新Organization
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Organization true "更新Organization"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /organization/updateOrganization [put]
func (organizationApi *OrganizationApi) UpdateOrganization(c *gin.Context) {
	var organization autocode.Organization
	_ = c.ShouldBindJSON(&organization)
	if err := organizationService.UpdateOrganization(organization); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindOrganization 用id查询Organization
// @Tags Organization
// @Summary 用id查询Organization
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.Organization true "用id查询Organization"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /organization/findOrganization [get]
func (organizationApi *OrganizationApi) FindOrganization(c *gin.Context) {
	var organization autocode.Organization
	_ = c.ShouldBindQuery(&organization)
	if err, reorganization := organizationService.GetOrganization(organization.CODE); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reorganization": reorganization}, c)
	}
}

func (organizationApi *OrganizationApi) FindOrganizationTree(c *gin.Context) {
	code, _ := c.GetQuery("code")
	treeMap := make(map[string][]autocode.Organization)
	if err, reorganization := organizationService.GetOrganizationInfoLists(); err != nil {
		response.FailWithMessage("查询失败", c)
	} else {
		for _, v := range reorganization {
			treeMap[v.PARENTCODE] = append(treeMap[v.PARENTCODE], v)
		}
		var orgList []autocode.Organization
		if code != "" {
			orgList = treeMap[code]
		} else {
			orgList = treeMap["00"]
		}
		for i := 0; i < len(orgList); i++ {
			err = organizationService.GetBaseChildrenList(&orgList[i], treeMap)
		}
		response.OkWithData(gin.H{"treeMap": orgList}, c)
	}
}

// GetOrganizationList 分页获取Organization列表
// @Tags Organization
// @Summary 分页获取Organization列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.OrganizationSearch true "分页获取Organization列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /organization/getOrganizationList [get]
func (organizationApi *OrganizationApi) GetOrganizationList(c *gin.Context) {
	var pageInfo autocodeReq.OrganizationSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := organizationService.GetOrganizationInfoList(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)
	}
}
