package controllers

import (
	"encoding/json"
	"eta/eta_api/models"
	"eta/eta_api/services"
	"eta/eta_api/utils"
)

// ChartPermissionController 品种列表
type ChartPermissionController struct {
	BaseAuthController
}

// List
// @Title 品种列表
// @Description 品种列表
// @Param   Keyword  query  string  false  "关键词"
// @Success 200 Ret=200 操作成功
// @router /list [get]
func (this *ChartPermissionController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	list, e := services.GetChartPermissionList()
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
		return
	}

	// 品种树
	resp := make([]*models.ChartPermissionItem, 0)
	childMap := make(map[int][]*models.ChartPermissionItem)

	for _, v := range list {
		t := new(models.ChartPermissionItem)
		t.PermissionId = v.ChartPermissionId
		t.PermissionName = v.PermissionName
		t.ParentId = v.ParentId
		t.IsPublic = v.IsPublic
		t.Enabled = v.Enabled
		t.Sort = v.Sort
		t.CreateTime = v.CreatedTime.Format(utils.FormatDateTime)
		t.Child = make([]*models.ChartPermissionItem, 0)
		if v.ParentId == 0 {
			resp = append(resp, t)
			continue
		}
		if v.ParentId > 0 {
			if childMap[v.ParentId] == nil {
				childMap[v.ParentId] = make([]*models.ChartPermissionItem, 0)
			}
			childMap[v.ParentId] = append(childMap[v.ParentId], t)
		}
	}
	for _, r := range resp {
		r.Child = childMap[r.PermissionId]
	}

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

// Add
// @Title 新增品种列表
// @Description 新增品种列表
// @Param  request body models.PermissionAddReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /add [post]
func (this *ChartPermissionController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req models.PermissionAddReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	if req.PermissionName == "" {
		br.Msg = "参数有误"
		return
	}
	var req2 services.ChartPermissionAddReq
	req2.PermissionName = req.PermissionName
	req2.ParentId = req.ParentId
	req2.IsPublic = req.IsPublic
	req2.Enabled = req.Enabled
	e, msg := services.AddChartPermission(&req2)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "新增品种失败, Err: " + e.Error()
		return
	}

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

// Edit
// @Title 编辑品种
// @Description 编辑品种
// @Param	request	body models.PermissionEditReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /edit [post]
func (this *ChartPermissionController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req models.PermissionEditReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	if req.PermissionId == 0 {
		br.Msg = "请选择要编辑的品种"
		return
	}

	if req.PermissionName == "" {
		br.Msg = "请输入品种名称"
		return
	}
	var req2 services.ChartPermissionEditReq
	req2.ChartPermissionId = req.PermissionId
	req2.PermissionName = req.PermissionName
	req2.IsPublic = req.IsPublic
	req2.Enabled = req.Enabled
	req2.PublicPermissionIds = req.PublicPermissionIds
	e, msg := services.EditChartPermission(&req2)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "编辑品种失败, Err: " + e.Error()
		return
	}

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

// Move
// @Title 移动品种
// @Description 移动品种
// @Param	request	body models.PermissionMoveReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /move [post]
func (this *ChartPermissionController) Move() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req models.PermissionMoveReq
	// todo 限制修改一级品种
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}

	if req.PermissionId == 0 {
		br.Msg = "请选择要移动的品种"
		return
	}
	var req2 services.ChartPermissionMoveReq
	req2.ChartPermissionId = req.PermissionId
	req2.PrevChartPermissionId = req.PrevPermissionId
	req2.NextChartPermissionId = req.NextPermissionId
	e, msg := services.MoveChartPermission(&req2)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "移动品种失败, Err: " + e.Error()
		return
	}

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