package data_manage_permission

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	data_manage_permission2 "eta/eta_api/models/data_manage/data_manage_permission"
	"eta/eta_api/services/data/data_manage_permission"
	"eta/eta_api/utils"
)

// 资产的含义：指标/图表/表格

// DataMangePermissionController 数据权限管理
type DataMangePermissionController struct {
	controllers.BaseAuthController
}

// SetEdbChartPermission
// @Title 指标/图表/表格权限设置接口
// @Description 指标/图表/表格权限设置接口
// @Param	request	body data_manage.SetEdbChartPermissionReq true "type json string"
// @Success 200 {object} data_manage.ChartListResp
// @router /edb_chart/permission/set [post]
func (c *DataMangePermissionController) SetEdbChartPermission() {
	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_permission2.SetEdbChartPermissionReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.Source <= 0 || req.Source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	if req.Source == 6 && (req.SubSource <= utils.EXCEL_DEFAULT || req.SubSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	if !req.IsSelectAll && len(req.DataIdList) <= 0 {
		br.Msg = "请勾选数据项"
		br.IsSendEmail = false
		return
	}

	err, errMsg := data_manage_permission.SetEdbChartPermission(req.Source, req.SubSource, req.UserId, req.UserList, req.IsSelectAll, req.DataIdList, req.NoDataIdList, req.Keyword, req.ClassifyId, sysUser.AdminId)
	if err != nil {
		//br.Success = true
		br.Msg = "设置失败"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = "设置失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.IsAddLog = true
	br.Success = true
	br.Msg = "设置成功"
}

// SetPermissionEdbChartClassifyIsPermission
// @Title 指标/图表/表格分类是否涉密设置接口
// @Description 指标/图表/表格分类是否涉密设置接口
// @Param	request	body data_manage.MoveEdbChartReq true "type json string"
// @Success 200 {object} data_manage.ChartListResp
// @router /edb_chart/classify/permission/is_permission/set [post]
func (c *DataMangePermissionController) SetPermissionEdbChartClassifyIsPermission() {
	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_permission2.SetDataIsPermissionReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.Source <= 0 || req.Source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	if req.Source == 6 && (req.SubSource <= utils.EXCEL_DEFAULT || req.SubSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	err, errMsg := data_manage_permission.SetDataClassifyIsPermission(req.Source, req.SubSource, req.ClassifyIdList, sysUser.AdminId)
	if err != nil {
		//br.Success = true
		br.Msg = "设置失败"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = "设置失败,Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.IsAddLog = true
	br.Success = true
	br.Msg = "设置成功"
}

// SetEdbChartClassifyPermission
// @Title 指标/图表/表格分类权限设置接口
// @Description 指标/图表/表格分类权限设置接口
// @Param	request	body data_manage.SetEdbChartClassifyPermissionReq true "type json string"
// @Success 200 {object} data_manage.ChartListResp
// @router /edb_chart/classify/permission/set [post]
func (c *DataMangePermissionController) SetEdbChartClassifyPermission() {
	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_permission2.SetEdbChartClassifyPermissionReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.Source <= 0 || req.Source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	if req.Source == 6 && (req.SubSource <= utils.EXCEL_DEFAULT || req.SubSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	if len(req.UserList) <= 0 {
		br.Msg = "请选择用户"
		br.IsSendEmail = false
		return
	}

	err, errMsg := data_manage_permission.SetEdbChartClassifyPermission(req.Source, req.SubSource, req.UserList, req.ClassifyIdList, sysUser.AdminId)
	if err != nil {
		//br.Success = true
		br.Msg = "设置失败"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = "设置失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.IsAddLog = true
	br.Success = true
	br.Msg = "设置成功"
}

// GetEdbChartPermission
// @Title 指标/图表/表格权限设置接口
// @Description 指标/图表/表格权限设置接口
// @Param   Source   query   int  false       "来源 ：1：手工数据指标 2：钢联化工数据库 3：ETA指标库 4：ETA预测指标 5：图库 6：ETA表格"
// @Param   SubSource   query   int  false       "子来源 ：目前作用于ETA表格，2024-3-26 14:12:09"
// @Param   DataId   query   int  false       "资产id"
// @Success 200 {object} data_manage.ChartListResp
// @router /edb_chart/permission/show_by_data [get]
func (c *DataMangePermissionController) GetEdbChartPermission() {
	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
	}

	source, _ := c.GetInt("Source")
	if source <= 0 || source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	subSource, _ := c.GetInt("SubSource")
	if source == 6 && (subSource <= utils.EXCEL_DEFAULT || subSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	dataId, _ := c.GetInt("DataId")
	// 用户判断
	if dataId <= 0 {
		br.Msg = "请选择数据"
		br.IsSendEmail = false
		return
	}

	// 获取已经配置了的资产id列表
	idList, err := data_manage_permission.GetUserIdListPermissionByDataId(source, subSource, dataId)
	if err != nil {
		//br.Success = true
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = idList
}

// GetEdbChartClassifyPermission
// @Title 获取指标/图表/表格分类权限设置接口
// @Description 获取指标/图表/表格分类权限设置接口
// @Param   Source   query   int  false       "来源 ：1：手工数据指标 2：钢联化工数据库 3：ETA指标库 4：ETA预测指标 5：图库 6：ETA表格"
// @Param   SubSource   query   int  false       "子来源 ：目前作用于ETA表格，2024-3-26 14:12:09"
// @Param   UserId   query   int  false       "用户id"
// @Success 200 {object} data_manage.ChartListResp
// @router /edb_chart/classify/permission/show_by_user [get]
func (c *DataMangePermissionController) GetEdbChartClassifyPermission() {
	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
	}

	source, _ := c.GetInt("Source")
	if source <= 0 || source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	subSource, _ := c.GetInt("SubSource")
	if source == 6 && (subSource <= utils.EXCEL_DEFAULT || subSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	userId, _ := c.GetInt("UserId")

	// 来源判断
	if source <= 0 || source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	if source == 6 && (subSource <= utils.EXCEL_DEFAULT || subSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	// 用户判断
	if userId <= 0 {
		br.Msg = "请选择用户"
		br.IsSendEmail = false
		return
	}

	// 获取已经配置了的分类id列表
	idList, err := data_manage_permission.GetEdbChartClassifyIdListPermissionByUserId(source, subSource, userId)
	if err != nil {
		//br.Success = true
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = idList
}

// GetEdbChartNoPermission
// @Title 根据资产id获取其分类没有权限的用户id列表接口
// @Description 根据资产id获取其分类没有权限的用户id列表接口
// @Param   Source   query   int  false       "来源 ：1：手工数据指标 2：钢联化工数据库 3：ETA指标库 4：ETA预测指标 5：图库 6：ETA表格"
// @Param   SubSource   query   int  false       "子来源 ：目前作用于ETA表格，2024-3-26 14:12:09"
// @Param   DataId   query   int  false       "资产id"
// @Success 200 {object} data_manage.ChartListResp
// @router /edb_chart/no_classify_permission/show_by_data [get]
func (c *DataMangePermissionController) GetEdbChartNoPermission() {
	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
	}

	source, _ := c.GetInt("Source")
	if source <= 0 || source > 6 {
		br.Msg = "错误的来源"
		br.IsSendEmail = false
		return
	}

	// 子来源（目前作用于ETA表格，2024-3-26 14:12:09）
	subSource, _ := c.GetInt("SubSource")
	if source == 6 && (subSource <= utils.EXCEL_DEFAULT || subSource > utils.BALANCE_TABLE) {
		br.Msg = "错误的子来源"
		br.IsSendEmail = false
		return
	}

	dataId, _ := c.GetInt("DataId")

	// 用户判断
	if dataId <= 0 {
		br.Msg = "请选择数据"
		br.IsSendEmail = false
		return
	}

	// 获取已经配置了的资产id列表
	idList, err := data_manage_permission.GetUserIdListNoPermissionByDataId(source, subSource, dataId)
	if err != nil {
		//br.Success = true
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = idList
}
