package data_manage

import (
	"encoding/json"
	"eta_gn/eta_api/controllers"
	"eta_gn/eta_api/models"
	"eta_gn/eta_api/models/data_manage"
	"eta_gn/eta_api/models/data_manage/request"
	"eta_gn/eta_api/models/system"
	"eta_gn/eta_api/services/data"
	"eta_gn/eta_api/services/data/data_manage_permission"
	"eta_gn/eta_api/utils"
	"fmt"
	"sort"
)

// EdbPublicClassifyController 数据管理-公共分类模块
type EdbPublicClassifyController struct {
	controllers.BaseAuthController
}

// SimpleList
// @Title 单层公共分类列表
// @Description 单层公共分类列表
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/simple [get]
func (c *EdbPublicClassifyController) SimpleList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 公共分类来源筛选
	// 目前只有数据加工会涉及到公共分类
	classifyType := utils.EdbClassifyTypeCalculate

	// 默认查一级公共分类和一级公共分类下的指标信息,
	// 如果是 子级公共分类,查询该子级公共分类的下一级公共分类和指标信息
	// 增加标识判断是文件夹还是指标列表
	parentId, _ := c.GetInt("ParentId")

	edbPublicClassifyObj := data_manage.EdbPublicClassify{}
	rootList, err := edbPublicClassifyObj.GetEdbClassifyItemsByParentId(parentId, int8(classifyType))
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	nodeAll := make([]*data_manage.EdbClassifyItems, 0)

	var sortList data_manage.EdbClassifyItemList
	if parentId > 0 {
		// 指标类型(基础指标,预测指标)
		edbType := 0
		switch classifyType {
		case utils.EdbClassifyTypeBase:
			edbType = 1
		case utils.EdbClassifyTypeCalculate:
			edbType = 2
		}

		allEdbInfo, err := data_manage.GetEdbInfoByPublicClassifyId(parentId, 0, edbType)
		if err != nil {
			br.Msg = "获取指标数据失败"
			br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
			return
		}

		if len(allEdbInfo) > 0 {
			// 查询当前公共分类信息
			for _, v := range allEdbInfo {
				v.HaveOperaAuth = true
				button := data.GetEdbOpButton(c.SysUser, v.SysUserId, v.EdbType, utils.EDB_INFO_TYPE, v.HaveOperaAuth)
				button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
				v.Button = button
				v.Children = make([]*data_manage.EdbClassifyItems, 0)
				v.ParentId = parentId
				nodeAll = append(nodeAll, v)
			}
		}

	}
	if len(rootList) > 0 {
		// 已授权公共分类id
		permissionClassifyIdList, err := data_manage_permission.GetUserEdbClassifyPermissionList(c.SysUser.AdminId, 0)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取已授权公共分类id数据失败,Err:" + err.Error()
			return
		}

		for _, v := range rootList {
			// 公共分类设定了权限且无权限, 那么忽略掉该节点
			if v.IsJoinPermission == 1 && !utils.InArrayByInt(permissionClassifyIdList, v.ClassifyId) {
				continue
			}

			// 数据权限
			v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
			// 按钮权限
			button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
			v.Button = button
			v.Children = make([]*data_manage.EdbClassifyItems, 0)
			nodeAll = append(nodeAll, v)
		}
	}
	if len(nodeAll) > 0 {
		//根据sort值排序
		sortList = nodeAll
		sort.Sort(sortList)
	}

	language := `CN`
	// 指标显示的语言
	{
		configDetail, _ := system.GetConfigDetailByCode(c.SysUser.AdminId, system.EdbLanguageVar)
		if configDetail != nil {
			language = configDetail.ConfigValue
		} else {
			configDetail, _ = system.GetDefaultConfigDetailByCode(system.EdbLanguageVar)
			if configDetail != nil {
				language = configDetail.ConfigValue
			}
		}
	}

	// 是否允许添加一级公共分类
	canOpClassify := true
	button := data.GetEdbClassifyOpButton(c.SysUser, 0, true)
	if !button.AddButton {
		canOpClassify = false
	}

	resp := new(data_manage.EdbClassifyListResp)
	resp.AllNodes = sortList
	resp.Language = language
	resp.CanOpClassify = canOpClassify
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// ClassifyTree
// @Title 多层公共分类列表树
// @Description 多层公共分类列表树
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/tree [get]
func (c *EdbPublicClassifyController) ClassifyTree() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 公共分类来源筛选
	classifyType := utils.EdbClassifyTypeCalculate

	edbPublicClassifyObj := data_manage.EdbPublicClassify{}
	allList, err := edbPublicClassifyObj.GetAllEdbClassifyByType(int8(classifyType))
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}

	var sortList data_manage.EdbClassifyItemList
	if len(allList) > 0 {
		for k, v := range allList {
			// 数据权限
			v.HaveOperaAuth = true
			// 按钮权限
			button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
			allList[k].Button = button
		}
		allList = data.GetClassifyTreeRecursive(allList, 0)
		//根据sort值排序
		sortList = allList
		sort.Sort(sortList)
	}
	language := `CN`

	// 是否允许添加一级公共分类
	canOpClassify := true
	button := data.GetEdbClassifyOpButton(c.SysUser, 0, true)
	if !button.AddButton {
		canOpClassify = false
	}

	resp := new(data_manage.EdbClassifyListResp)
	resp.AllNodes = sortList
	resp.Language = language
	resp.CanOpClassify = canOpClassify
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// AddEdbClassify
// @Title 新增公共分类
// @Description 新增公共分类接口
// @Param	request	body data_manage.AddEdbClassifyReq true "type json string"
// @Success 200 Ret=200 保存成功
// @router /edb_public/classify/add [post]
func (c *EdbPublicClassifyController) AddClassify() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	var req data_manage.AddEdbClassifyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	if req.ClassifyName == "" {
		br.Msg = "请输入公共分类名称"
		br.IsSendEmail = false
		return
	}
	if req.ParentId < 0 {
		br.Msg = "参数错误"
		br.IsSendEmail = false
		return
	}
	// 公共分类来源筛选
	classifyType := utils.EdbClassifyTypeCalculate

	//添加指标
	_, err, errMsg := data.AddEdbPublicClassify(req.ClassifyName, req.ParentId, req.Level, uint8(classifyType), c.SysUser.AdminId, c.SysUser.RealName)
	if errMsg != `` {
		br.Msg = errMsg
		br.ErrMsg = errMsg
		if err != nil {
			br.ErrMsg = errMsg + ";Err:" + err.Error()
		} else {
			br.IsSendEmail = false
		}
		return
	}
	//count, err := data_manage.GetEdbClassifyCount(req.ClassifyName, req.ParentId)
	//if err != nil {
	//	br.Msg = "判断名称是否已存在失败"
	//	br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
	//	return
	//}
	//if count > 0 {
	//	br.Msg = "公共分类名称已存在,请重新输入"
	//	br.IsSendEmail = false
	//	return
	//}
	////获取该层级下最大的排序数
	//maxSort, err := data_manage.GetEdbClassifyMaxSort(req.ParentId, req.ClassifyType)
	//
	//classify := new(data_manage.EdbClassify)
	//classify.ClassifyType = req.ClassifyType
	//classify.ParentId = req.ParentId
	//classify.ClassifyName = req.ClassifyName
	//classify.HasData = 0
	//classify.CreateTime = time.Now()
	//classify.ModifyTime = time.Now()
	//classify.SysUserId = c.SysUser.AdminId
	//classify.SysUserRealName = c.SysUser.RealName
	//classify.Level = req.Level + 1
	//timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	//classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
	//classify.Sort = maxSort
	//
	//_, err = data_manage.AddEdbClassify(classify)
	//if err != nil {
	//	br.Msg = "保存公共分类失败"
	//	br.ErrMsg = "保存公共分类失败,Err:" + err.Error()
	//	return
	//}
	br.Ret = 200
	br.Msg = "新增成功"
	br.Success = true
	br.IsAddLog = true
}

// EditEdbClassify
// @Title 修改公共分类
// @Description 修改公共分类接口
// @Param	request	body data_manage.EditEdbClassifyReq true "type json string"
// @Success 200 Ret=200 修改成功
// @router /edb_public/classify/edit [post]
func (c *EdbPublicClassifyController) EditEdbClassify() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	var req data_manage.EditEdbClassifyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	if req.ClassifyName == "" {
		br.Msg = "请输入公共分类名称"
		br.IsSendEmail = false
		return
	}

	if req.ClassifyId < 0 {
		br.Msg = "参数错误"
		br.IsSendEmail = false
		return
	}

	err, errMsg := data.EditEdbPublicClassify(req.ClassifyId, req.ClassifyName, c.SysUser)
	if errMsg != `` {
		br.Msg = errMsg
		br.ErrMsg = errMsg
		if err != nil {
			br.ErrMsg = errMsg + ";Err:" + err.Error()
		} else {
			br.IsSendEmail = false
		}
		return
	}
	br.Ret = 200
	br.Msg = "修改成功"
	br.Success = true
	br.IsAddLog = true
}

// DeleteEdbClassifyCheck
// @Title 删除检测接口
// @Description 删除检测接口
// @Param	request	body request.PublicClassifyDeleteCheckReq true "type json string"
// @Success 200 Ret=200 检测成功
// @router /edb_public/classify/delete/check [post]
func (c *EdbPublicClassifyController) DeleteClassifyCheck() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	var req request.PublicClassifyDeleteCheckReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	if req.ClassifyId < 0 {
		br.Msg = "参数错误"
		br.IsSendEmail = false
		return
	}
	deleteStatus, tipsMsg, _, tableList, err, errMsg := data.DeleteEdbPublicClassifyCheck(req.ClassifyId, c.SysUser)
	if errMsg != `` {
		br.Msg = errMsg
		br.ErrMsg = errMsg
		if err != nil {
			br.ErrMsg = err.Error()
		} else {
			br.IsSendEmail = false
		}
		return
	}
	if c.Lang == "en" {
		if utils.ViperConfig.InConfig(tipsMsg) {
			tipsMsg = utils.ViperConfig.GetString(tipsMsg)
		}
	}

	//var deleteStatus int
	//var tipsMsg string
	////删除公共分类
	//if req.ClassifyId > 0 && req.EdbInfoId == 0 {
	//	//判断公共分类下,是否含有指标
	//	count, err := data_manage.GetEdbInfoCountByClassifyId(req.ClassifyId)
	//	if err != nil {
	//		br.Msg = "删除失败"
	//		br.ErrMsg = "公共分类下是否含有指标失败,Err:" + err.Error()
	//		return
	//	}
	//
	//	if count > 0 {
	//		deleteStatus = 1
	//		tipsMsg = "若目录关联指标不可删除"
	//	}
	//}
	//
	//if deleteStatus != 1 && req.EdbInfoId == 0 {
	//	classifyCount, err := data_manage.GetClassifyCountByClassifyId(req.ClassifyId)
	//	if err != nil && !utils.IsErrNoRow(err) {
	//		br.Msg = "删除失败"
	//		br.ErrMsg = "公共分类下是否含有指标失败,Err:" + err.Error()
	//		return
	//	}
	//	if classifyCount > 0 {
	//		deleteStatus = 2
	//		tipsMsg = "确认删除当前目录及包含的子目录吗"
	//	}
	//}
	//
	////删除指标
	//if req.EdbInfoId > 0 {
	//	//判断指标是否用于作图,如果用于作图,则不可删除
	//	chartCount, err := data_manage.GetChartEdbMappingCount(req.EdbInfoId)
	//	if err != nil && !utils.IsErrNoRow(err) {
	//		br.Msg = "删除失败"
	//		br.ErrMsg = "判断指标是否被用于作图失败,Err:" + err.Error()
	//		return
	//	}
	//	if chartCount > 0 {
	//		deleteStatus = 3
	//		tipsMsg = "当前指标已用作画图,不可删除"
	//	}
	//	//判断指标是否用于计算
	//	{
	//		calculateCount, err := data_manage.GetEdbInfoCalculateMappingCount(req.EdbInfoId)
	//		if err != nil && !utils.IsErrNoRow(err) {
	//			br.Msg = "删除失败"
	//			br.ErrMsg = "判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + err.Error()
	//			return
	//		}
	//		if calculateCount > 0 {
	//			deleteStatus = 4
	//			tipsMsg = "当前指标已用作,指标运算,不可删除"
	//		}
	//	}
	//}

	resp := new(data_manage.ClassifyDeleteCheckResp)
	resp.DeleteStatus = deleteStatus
	resp.TipsMsg = tipsMsg
	resp.TableList = tableList
	br.Ret = 200
	br.Msg = "检测成功"
	br.Success = true
	br.Data = resp
}

// DeleteEdbClassify
// @Title 删除公共分类/指标
// @Description 删除公共分类/指标接口
// @Param	request	body request.PublicClassifyDeleteCheckReq true "type json string"
// @Success 200 Ret=200 删除成功
// @router /edb_public/classify/delete [post]
func (c *EdbPublicClassifyController) DeleteClassify() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.PublicClassifyDeleteCheckReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	if req.ClassifyId < 0 {
		br.Msg = "参数错误"
		br.IsSendEmail = false
		return
	}

	nextItem, _, err, errMsg := data.DeleteEdbPublicClassify(req.ClassifyId, sysUser, string(c.Ctx.Input.RequestBody), c.Ctx.Input.URI())
	if errMsg != `` {
		br.Msg = errMsg
		br.ErrMsg = errMsg
		if err != nil {
			br.ErrMsg = err.Error()
		} else {
			br.IsSendEmail = false
		}
		return
	}

	resp := data_manage.AddEdbInfoResp{}
	if nextItem != nil {
		resp = data_manage.AddEdbInfoResp{
			EdbInfoId:  nextItem.EdbInfoId,
			UniqueCode: nextItem.UniqueCode,
		}
	}

	br.Ret = 200
	br.Msg = "删除成功"
	br.Success = true
	br.IsAddLog = true
	br.Data = resp
}

// ListV2
// @Title 公共分类列表
// @Description 公共分类列表接口
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/list [get]
func (c *EdbPublicClassifyController) ListV2() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 公共分类来源筛选
	classifyType, _ := c.GetInt("ClassifyType", utils.EdbClassifyTypeBase) // 默认指标库的
	if classifyType != utils.EdbClassifyTypeBase && classifyType != utils.EdbClassifyTypeCalculate {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("指标公共分类类型有误, ClassifyType: %d", classifyType)
		return
	}
	edbType := utils.EdbTypeBase
	if classifyType == utils.EdbClassifyTypeCalculate {
		edbType = utils.EdbTypeCalculate
	}

	rootList, err := data_manage.GetEdbClassifyByParentId(0, int8(classifyType), c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	//classifyAll, err := data_manage.GetEdbClassifyAll()
	classifyAll, err := data_manage.GetChildEdbClassifyByClassifyType(classifyType, c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	// 公共分类map
	classifyMap := make(map[int]*data_manage.EdbClassifyItems)
	for _, v := range classifyAll {
		classifyMap[v.ClassifyId] = v
	}

	// 已授权公共分类id
	permissionClassifyIdList, err := data_manage_permission.GetUserEdbClassifyPermissionList(c.SysUser.AdminId, 0)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取已授权公共分类id数据失败,Err:" + err.Error()
		return
	}

	// 获取当前账号的不可见指标
	obj := data_manage.EdbInfoNoPermissionAdmin{}
	confList, err := obj.GetAllListByAdminId(c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
		return
	}
	noPermissionEdbInfoIdMap := make(map[int]bool)
	for _, v := range confList {
		noPermissionEdbInfoIdMap[v.EdbInfoId] = true
	}
	//allEdbInfo, err := data_manage.GetEdbInfoAll(utils.EDB_INFO_TYPE)
	allEdbInfo, err := data_manage.GetEdbInfoByTypes(utils.EDB_INFO_TYPE, edbType)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	edbInfoMap := make(map[int][]*data_manage.EdbClassifyItems)
	if len(allEdbInfo) > 0 {
		// 获取所有有权限的指标和公共分类
		permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(c.SysUser.AdminId, 0, 0)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取所有有权限的指标和公共分类失败,Err:" + err.Error()
			return
		}
		for _, v := range allEdbInfo {
			// 如果指标不可见,那么就不返回该指标
			if _, ok := noPermissionEdbInfoIdMap[v.EdbInfoId]; ok {
				continue
			}
			// 数据权限
			if classifyInfo, ok := classifyMap[v.ClassifyId]; ok {
				v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
			}

			button := data.GetEdbOpButton(c.SysUser, v.SysUserId, v.EdbType, utils.EDB_INFO_TYPE, v.HaveOperaAuth)
			button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
			v.Button = button
			edbInfoMap[v.ClassifyId] = append(edbInfoMap[v.ClassifyId], v)
		}
	}
	rootChildMap := make(map[int][]*data_manage.EdbClassifyItems)
	for _, v := range classifyAll {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
		v.Button = button

		rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
		if existItems, ok := edbInfoMap[v.ClassifyId]; ok {
			v.Children = existItems
		} else {
			items := make([]*data_manage.EdbClassifyItems, 0)
			v.Children = items
		}
	}
	nodeAll := make([]*data_manage.EdbClassifyItems, 0)
	for _, v := range rootList {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		v.Button = button

		if existItems, ok := rootChildMap[v.ClassifyId]; ok {
			v.Children = existItems
		} else {
			items := make([]*data_manage.EdbClassifyItems, 0)
			v.Children = items
		}
		nodeAll = append(nodeAll, v)
	}
	resp := new(data_manage.EdbClassifyListResp)
	resp.AllNodes = nodeAll
	resp.CanOpClassify = true
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// Items
// @Title 获取所有公共分类接口-不包含指标
// @Description 获取所有公共分类接口-不包含指标
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/items [get]
func (c *EdbPublicClassifyController) Items() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 公共分类来源筛选
	classifyType, _ := c.GetInt("ClassifyType", utils.EdbClassifyTypeBase) // 默认指标库的
	if classifyType != utils.EdbClassifyTypeBase && classifyType != utils.EdbClassifyTypeCalculate {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("指标公共分类类型有误, ClassifyType: %d", classifyType)
		return
	}
	//edbType := utils.EdbTypeBase
	//if classifyType == utils.EdbClassifyTypeCalculate {
	//	edbType = utils.EdbTypeCalculate
	//}

	rootList, err := data_manage.GetEdbClassifyByParentId(0, int8(classifyType), c.SysUser.AdminId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}

	//classifyAll, err := data_manage.GetEdbClassifyAll()
	classifyAll, err := data_manage.GetChildEdbClassifyByClassifyType(classifyType, c.SysUser.AdminId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}

	nodeAll := make([]*data_manage.EdbClassifyItems, 0)
	for k := range rootList {
		rootNode := rootList[k]
		data.EdbClassifyItemsMakeTree(classifyAll, rootNode)
		nodeAll = append(nodeAll, rootNode)
	}
	resp := new(data_manage.EdbClassifyListResp)
	resp.AllNodes = nodeAll
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// EdbClassifyMove
// @Title 公共分类移动接口
// @Description 公共分类移动接口
// @Success 200 {object} data_manage.MoveEdbClassifyReq
// @router /edb_classify/move [post]
func (c *EdbPublicClassifyController) EdbClassifyMove() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}

	var req data_manage.MoveEdbClassifyReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	if req.ClassifyId <= 0 && req.EdbInfoId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "请选择拖动目标,公共分类目录或者指标"
		return
	}

	// 公共分类来源筛选
	classifyType := req.ClassifyType // 默认指标库的
	if classifyType != utils.EdbClassifyTypeBase && classifyType != utils.EdbClassifyTypeCalculate {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("指标公共分类类型有误, ClassifyType: %d", classifyType)
		return
	}

	err, errMsg := data.MoveEdbClassify(req, sysUser, uint8(classifyType))
	if errMsg != `` {
		br.Msg = errMsg
		br.ErrMsg = errMsg
		if err != nil {
			br.ErrMsg = err.Error()
		} else {
			br.IsSendEmail = false
		}
		return
	}
	////判断公共分类是否存在
	//edbClassifyInfo, err := data_manage.GetEdbClassifyById(req.ClassifyId)
	//if err != nil {
	//	br.Msg = "移动失败"
	//	br.ErrMsg = "获取公共分类信息失败,Err:" + err.Error()
	//	return
	//}
	//updateCol := make([]string, 0)
	//
	////判断上级id是否一致,如果不一致的话,那么需要移动该公共分类层级
	//if edbClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
	//	parentEdbClassifyInfo, err := data_manage.GetEdbClassifyById(req.ParentClassifyId)
	//	if err != nil {
	//		br.Msg = "移动失败"
	//		br.ErrMsg = "获取上级公共分类信息失败,Err:" + err.Error()
	//		return
	//	}
	//	edbClassifyInfo.ParentId = parentEdbClassifyInfo.ClassifyId
	//	edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
	//	edbClassifyInfo.ModifyTime = time.Now()
	//	updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
	//}
	//
	////如果有传入 上一个兄弟节点公共分类id
	//if req.PrevClassifyId > 0 {
	//	prevClassify, err := data_manage.GetEdbClassifyById(req.PrevClassifyId)
	//	if err != nil {
	//		br.Msg = "移动失败"
	//		br.ErrMsg = "获取上一个兄弟节点公共分类信息失败,Err:" + err.Error()
	//		return
	//	}
	//
	//	//如果是移动在两个兄弟节点之间
	//	if req.NextClassifyId > 0 {
	//		//下一个兄弟节点
	//		nextClassify, err := data_manage.GetEdbClassifyById(req.NextClassifyId)
	//		if err != nil {
	//			br.Msg = "移动失败"
	//			br.ErrMsg = "获取下一个兄弟节点公共分类信息失败,Err:" + err.Error()
	//			return
	//		}
	//		//如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
	//		if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == edbClassifyInfo.Sort {
	//			//变更兄弟节点的排序
	//			updateSortStr := `sort + 2`
	//			_ = data_manage.UpdateEdbClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
	//		} else {
	//			//如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
	//			if nextClassify.Sort-prevClassify.Sort == 1 {
	//				//变更兄弟节点的排序
	//				updateSortStr := `sort + 1`
	//				_ = data_manage.UpdateEdbClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
	//			}
	//		}
	//	}
	//
	//	edbClassifyInfo.Sort = prevClassify.Sort + 1
	//	edbClassifyInfo.ModifyTime = time.Now()
	//	updateCol = append(updateCol, "Sort", "ModifyTime")
	//} else {
	//	firstClassify, err := data_manage.GetFirstEdbClassifyByParentId(edbClassifyInfo.ParentId)
	//	if err != nil && !utils.IsErrNoRow(err) {
	//		br.Msg = "移动失败"
	//		br.ErrMsg = "获取获取当前父级公共分类下的排序第一条的公共分类信息失败,Err:" + err.Error()
	//		return
	//	}
	//
	//	//如果该公共分类下存在其他公共分类,且第一个其他公共分类的排序等于0,那么需要调整排序
	//	if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
	//		updateSortStr := ` sort + 1 `
	//		_ = data_manage.UpdateEdbClassifySortByParentId(firstClassify.ParentId, firstClassify.ClassifyId-1, 0, updateSortStr)
	//	}
	//
	//	edbClassifyInfo.Sort = 0 //那就是排在第一位
	//	edbClassifyInfo.ModifyTime = time.Now()
	//	updateCol = append(updateCol, "Sort", "ModifyTime")
	//}
	//
	////更新
	//if len(updateCol) > 0 {
	//	err = edbClassifyInfo.Update(updateCol)
	//	if err != nil {
	//		br.Msg = "移动失败"
	//		br.ErrMsg = "修改失败,Err:" + err.Error()
	//		return
	//	}
	//}
	br.Ret = 200
	br.Success = true
	br.Msg = "移动成功"
}

// ItemsV2
// @Title 公共分类列表
// @Description 公共分类列表接口
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/items/v2 [get]
func (c *EdbPublicClassifyController) ItemsV2() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 公共分类来源筛选
	classifyType, _ := c.GetInt("ClassifyType", utils.EdbClassifyTypeBase) // 默认指标库的
	if classifyType != utils.EdbClassifyTypeBase && classifyType != utils.EdbClassifyTypeCalculate {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("指标公共分类类型有误, ClassifyType: %d", classifyType)
		return
	}
	//edbType := utils.EdbTypeBase
	//if classifyType == utils.EdbClassifyTypeCalculate {
	//	edbType = utils.EdbTypeCalculate
	//}

	rootList, err := data_manage.GetEdbClassifyByParentId(0, int8(classifyType), c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	//classifyAll, err := data_manage.GetEdbClassifyAll()
	classifyAll, err := data_manage.GetChildEdbClassifyByClassifyType(classifyType, c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	// 已授权公共分类id
	permissionClassifyIdList, err := data_manage_permission.GetUserEdbClassifyPermissionList(c.SysUser.AdminId, 0)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取已授权公共分类id数据失败,Err:" + err.Error()
		return
	}
	//allEdbInfo, err := data_manage.GetEdbInfoAll(0)
	//if err != nil && !utils.IsErrNoRow(err) {
	//	br.Msg = "获取失败"
	//	br.ErrMsg = "获取数据失败,Err:" + err.Error()
	//	return
	//}
	//edbInfoMap := make(map[int][]*data_manage.EdbClassifyItems)
	//for _, v := range allEdbInfo {
	//	button := data.GetEdbOpButton(c.SysUser, v.SysUserId)
	//	button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
	//	v.Button = button
	//	edbInfoMap[v.ClassifyId] = append(edbInfoMap[v.ClassifyId], v)
	//}
	rootChildMap := make(map[int][]*data_manage.EdbClassifyItems)
	for _, v := range classifyAll {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
		v.Button = button

		rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
		//if existItems, ok := edbInfoMap[v.ClassifyId]; ok {
		//	v.Children = existItems
		//} else {
		//	items := make([]*data_manage.EdbClassifyItems, 0)
		//	v.Children = items
		//}
	}
	nodeAll := make([]*data_manage.EdbClassifyItems, 0)
	for _, v := range rootList {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		v.Button = button

		if existItems, ok := rootChildMap[v.ClassifyId]; ok {
			v.Children = existItems
		} else {
			items := make([]*data_manage.EdbClassifyItems, 0)
			v.Children = items
		}
		nodeAll = append(nodeAll, v)
	}
	language := `CN`
	// 指标显示的语言
	{
		configDetail, _ := system.GetConfigDetailByCode(c.SysUser.AdminId, system.EdbLanguageVar)
		if configDetail != nil {
			language = configDetail.ConfigValue
		} else {
			configDetail, _ = system.GetDefaultConfigDetailByCode(system.EdbLanguageVar)
			if configDetail != nil {
				language = configDetail.ConfigValue
			}
		}
	}

	// 是否允许添加一级公共分类
	canOpClassify := true
	//button := data.GetEdbClassifyOpButton(c.SysUser, 0)
	//if !button.AddButton {
	//	canOpClassify = false
	//}

	resp := data_manage.EdbClassifyListResp{
		AllNodes:      nodeAll,
		CanOpClassify: canOpClassify,
		Language:      language,
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// ClassifyEdbInfoList
// @Title 获取公共分类下指标接口
// @Description 获取公共分类下指标接口
// @Param   ClassifyId   query   int  true       "公共分类id"
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/edb_info/list [get]
func (c *EdbPublicClassifyController) ClassifyEdbInfoList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	classifyId, _ := c.GetInt("ClassifyId")
	if classifyId <= 0 {
		br.Msg = "参数错误,请刷新页面"
		return
	}

	classifyInfo, err := data_manage.GetEdbClassifyById(classifyId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取公共分类信息失败,Err:" + err.Error()
		return
	}

	// 获取当前账号的不可见指标
	obj := data_manage.EdbInfoNoPermissionAdmin{}
	confList, err := obj.GetAllListByAdminId(c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
		return
	}
	noPermissionEdbInfoIdMap := make(map[int]bool)
	for _, v := range confList {
		noPermissionEdbInfoIdMap[v.EdbInfoId] = true
	}

	// 指标类型(基础指标,预测指标)
	edbType := 0
	switch classifyInfo.ClassifyType {
	case utils.EdbClassifyTypeBase:
		edbType = 1
	case utils.EdbClassifyTypeCalculate:
		edbType = 2
	}
	// 无权限指标 和 无权限指标公共分类id
	noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(c.SysUser.AdminId, utils.EDB_INFO_TYPE, edbType)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
		return
	}

	allEdbInfo, err := data_manage.GetEdbInfoByClassifyId(classifyId, utils.EDB_INFO_TYPE, edbType, 0, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}

	list := make([]*data_manage.EdbClassifyItems, 0)

	if len(allEdbInfo) > 0 {
		// 获取所有有权限的指标和公共分类
		permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(c.SysUser.AdminId, 0, 0)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取所有有权限的指标和公共分类失败,Err:" + err.Error()
			return
		}
		for _, v := range allEdbInfo {
			// 如果指标不可见,那么就不返回该指标
			if _, ok := noPermissionEdbInfoIdMap[v.EdbInfoId]; ok {
				continue
			}
			v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
			button := data.GetEdbOpButton(c.SysUser, v.SysUserId, v.EdbType, utils.EDB_INFO_TYPE, v.HaveOperaAuth)
			button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
			v.Button = button

			list = append(list, v)
		}
	}

	resp := new(data_manage.ClassifyEdbInfoListResp)
	resp.EdbInfoList = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// ItemsV3
// @Title 公共分类列表
// @Description 公共分类列表接口
// @Success 200 {object} data_manage.EdbClassifyListResp
// @router /edb_public/classify/items/v3 [get]
func (c *EdbPublicClassifyController) ItemsV3() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	// 公共分类来源筛选
	classifyType, _ := c.GetInt("ClassifyType", utils.EdbClassifyTypeBase) // 默认指标库的
	if classifyType != utils.EdbClassifyTypeBase && classifyType != utils.EdbClassifyTypeCalculate {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("指标公共分类类型有误, ClassifyType: %d", classifyType)
		return
	}
	//edbType := utils.EdbTypeBase
	//if classifyType == utils.EdbClassifyTypeCalculate {
	//	edbType = utils.EdbTypeCalculate
	//}

	// TODO:9级改造
	rootList, err := data_manage.GetEdbClassifyByParentId(0, int8(classifyType), c.SysUser.AdminId)
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	rootTwoList, err := data_manage.GetEdbClassifyByParentIdTwo(int8(classifyType))
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	classifyAll, err := data_manage.GetEdbClassifyAllV2(int8(classifyType))
	if err != nil && !utils.IsErrNoRow(err) {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	// 已授权公共分类id
	permissionClassifyIdList, err := data_manage_permission.GetUserEdbClassifyPermissionList(c.SysUser.AdminId, 0)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取已授权公共分类id数据失败,Err:" + err.Error()
		return
	}

	rootTwoMap := make(map[int][]*data_manage.EdbClassifyItems)
	for _, v := range rootTwoList {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		v.Button = button

		rootTwoMap[v.ParentId] = append(rootTwoMap[v.ParentId], v)
	}
	rootTwoChildMap := make(map[int][]*data_manage.EdbClassifyItems)
	for _, v := range classifyAll {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		v.Button = button
		if v.Level == 3 {
			rootTwoChildMap[v.ParentId] = append(rootTwoChildMap[v.ParentId], v)
		}
	}

	nodeAll := make([]*data_manage.EdbClassifyItems, 0)
	for _, v := range rootList {
		// 数据权限
		v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
		// 按钮权限
		button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
		v.Button = button

		if existItems, ok := rootTwoMap[v.ClassifyId]; ok {
			v.Children = existItems
			for _, item := range existItems {
				// 数据权限
				v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
				// 按钮权限
				button := data.GetEdbClassifyOpButton(c.SysUser, item.SysUserId, v.HaveOperaAuth)
				item.Button = button

				if existItems, ok := rootTwoChildMap[item.ClassifyId]; ok {
					for _, existItem := range existItems {
						// 数据权限
						v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ClassifyId, permissionClassifyIdList)
						// 按钮权限
						button := data.GetEdbClassifyOpButton(c.SysUser, v.SysUserId, v.HaveOperaAuth)
						button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
						existItem.Button = button
					}
					item.Children = existItems
				} else {
					items := make([]*data_manage.EdbClassifyItems, 0)
					item.Children = items
				}
			}
		} else {
			items := make([]*data_manage.EdbClassifyItems, 0)
			v.Children = items
		}
		nodeAll = append(nodeAll, v)
	}
	language := `CN`
	// 指标显示的语言
	{
		configDetail, _ := system.GetConfigDetailByCode(c.SysUser.AdminId, system.EdbLanguageVar)
		if configDetail != nil {
			language = configDetail.ConfigValue
		} else {
			configDetail, _ = system.GetDefaultConfigDetailByCode(system.EdbLanguageVar)
			if configDetail != nil {
				language = configDetail.ConfigValue
			}
		}
	}

	// 是否允许添加一级公共分类
	canOpClassify := true
	button := data.GetEdbClassifyOpButton(c.SysUser, 0, true)
	if !button.AddButton {
		canOpClassify = false
	}

	resp := new(data_manage.EdbClassifyListResp)
	resp.AllNodes = nodeAll
	resp.Language = language
	resp.CanOpClassify = canOpClassify
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}