package admin

import (
	"strconv"
	"strings"
	"ulink-admin/frame"
	"ulink-admin/modules/system/models/model"
	"ulink-admin/modules/system/models/req"
	"ulink-admin/modules/system/service"
	"ulink-admin/pkg/jwt/admin"
	"ulink-admin/pkg/library/tree/tree_dept"
)

type DeptApi struct {
	DeptService *service.DeptService `inject:""`
}

// TreeSelect 查询部门部门树
// @Summary 加载对应角色部门列表树接口
// @Description 加载对应角色部门列表树接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param id path   int true "id" id
// @Security ApiKeyAuth
// @Success 200 {object} resp.Response{data=map[INode]interface{}} "返回部门查询"
// @Router /dept/teeselect [get]
func (a DeptApi) TreeSelect(c *frame.Context) {

	query := req.DeptQuery{}
	c.ValidteError(c.ShouldBind(&query), &query)
	treeSelect := a.DeptService.TreeSelect(query)
	var list tree_dept.DeptList = *treeSelect
	c.Ok(list.GetTree())
}

// RoleDeptTreeSelect 加载对应角色部门列表树
// @Summary 加载对应角色部门列表树接口
// @Description 加载对应角色部门列表树接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param id path   int true "id" id
// @Security ApiKeyAuth
// @Success 200 {object} resp.Response{data=map[INode]interface{}} "返回部门查询"
// @Router /dept/roleDeptTreeselect [get]
func (a DeptApi) RoleDeptTreeSelect(c *frame.Context) {
	m := make(map[string]interface{})
	param := c.Param("id")
	id, _ := strconv.ParseInt(param, 10, 64)
	checkedKeys := a.DeptService.SelectDeptListByRoleId(id)
	m["checkedKeys"] = checkedKeys
	treeSelect := a.DeptService.TreeSelect(req.DeptQuery{})
	var list tree_dept.DeptList = *treeSelect
	tree := list.GetTree()
	m["depts"] = tree
	c.Ok(m)
}

// Find 查询部门列表
// @Summary 分页查询部门数据接口
// @Description 分页查询部门数据接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param object query req.DeptQuery false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} resp.Response{data=dept.Page} "分页获取部门列表,返回包括列表,总数,页码,每页数量"
// @Router /dept/List [get]
func (a DeptApi) Find(c *frame.Context) {
	query := req.DeptQuery{}
	c.ValidteError(c.ShouldBind(&query), &query)
	c.Ok(a.DeptService.GetList(query))
}

// ExcludeChild 查询部门列表(排除节点)
// @Summary 分页查询部门数据接口 排除节点
// @Description 分页查询部门数据接口 排除节点
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param object query req.DeptQuery false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} resp.Response{data=model.SysDept} "分页获取部门列表,返回包括列表,总数,页码,每页数量"
// @Router /dept/list/exclude [get]
func (a DeptApi) ExcludeChild(c *frame.Context) {
	var params struct {
		Id int64 `uri:"id" binding:"required"  msg:"id不存在"` //ids
	}
	c.ValidteError(c.ShouldBindUri(&params), &params)
	list := a.DeptService.GetList(req.DeptQuery{})
	var depts = *list
	deptList := make([]model.SysDept, 0)
	for _, dept := range depts {
		if dept.Id == params.Id || strings.Contains(dept.Ancestors, strconv.FormatInt(params.Id, 10)) {
			continue
		}
		deptList = append(deptList, dept)
	}
	c.Ok(deptList)
}

// GetInfo 根据部门编号获取详细信息
// @Summary 部门详情查询接口
// @Description 部门详情查询接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param id path   int true "id" id
// @Security ApiKeyAuth
// @Success 200 {object} resp.Response{data=model.SysDept} "返回部门详情查询"
// @Router /dept [get]
func (a DeptApi) GetInfo(c *frame.Context) {
	var params struct {
		Id int64 `uri:"id" binding:"required"  msg:"id不存在"` //ids
	}
	c.ValidteError(c.ShouldBindUri(&params), &params)
	c.Ok(a.DeptService.GetDeptById(params.Id))
}

// Add 添加部门
// @Summary 新增部门接口
// @Description 新增部门接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param  data body model.SysDept true "部门实体对象"
// @Success 200 {object} resp.Response{msg=string} "操作状态"
// @Router /dept/add [post]
func (a DeptApi) Add(c *frame.Context) {
	dept := model.SysDept{}
	c.ValidteError(c.ShouldBind(&dept), &dept)
	//校验部门名称是否唯一
	unique := a.DeptService.CheckDeptNameUnique(dept)
	if unique {
		frame.Throw(frame.BUSINESS_CODE, "新增部门'"+dept.DeptName+"'失败,部门名称已存在")
		return
	}
	info := a.DeptService.GetDeptById(dept.ParentId)
	if info == nil {
		frame.Throw(frame.BUSINESS_CODE, "部门查询异常'")
		return
	}
	if info.Status == 1 {
		frame.Throw(frame.BUSINESS_CODE, "部门停用,不允许新增")
		return
	}
	dept.Ancestors = info.Ancestors + "," + strconv.FormatInt(dept.ParentId, 10)
	dept.CreateBy = admin.GetUserInfo(c).UserName
	a.DeptService.Insert(dept)
}

// Edit 修改部门数据
// @Summary 修改部门接口
// @Description 新增部门接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param  data body model.SysDept true "部门实体对象"
// @Success 200 {object} resp.Response{msg=string} "操作状态"
// @Router /dept/edit [put]
func (a DeptApi) Edit(c *frame.Context) {
	dept := model.SysDept{}
	c.ValidteError(c.ShouldBind(&dept), &dept)
	a.DeptService.Update(dept)
}

// Delete 删除部门
// @Summary 删除部门接口
// @Description 删除部门接口
// @Tags 部门相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 令牌"
// @Param id path   int true "id" id
// @Success 200 {object} resp.Response{msg=string} "操作状态"
// @Router /dept/remove [delete]
func (a DeptApi) Delete(c *frame.Context) {
	var params struct {
		Id int64 `uri:"id" binding:"required"  msg:"id不存在"` //ids
	}
	c.ValidteError(c.ShouldBindUri(&params), &params)
	//是否存在部门子节点
	if a.DeptService.HasChildByDeptId(params.Id) > 0 {
		frame.Throw(frame.BUSINESS_CODE, "存在下级部门,不允许删除")
		return
	}
	if a.DeptService.CheckDeptExistUser(params.Id) > 0 {
		frame.Throw(frame.BUSINESS_CODE, "部门存在用户,不允许删除")
		return
	}
	a.DeptService.Remove(params.Id)
}