package controllers

import (
	"encoding/json"
	"eta/eta_mini_crm_ht/models"
	"eta/eta_mini_crm_ht/models/request"
	"eta/eta_mini_crm_ht/models/response"
	"eta/eta_mini_crm_ht/services"

	"github.com/beego/beego/v2/client/orm"
)

type SysDepartmentController struct {
	BaseAuthController
}

// @Title 获取部门列表
// @Description 获取部门列表接口
// @Success 200 {object} system.SysDepartmentListResp
// @router /list [get]
func (this *SysDepartmentController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	departmentList, err := models.GetSysDepartmentList()
	if err != nil {
		br.Msg = "获取部门列表失败"
		br.ErrMsg = "获取部门列表失败，Err" + err.Error()
		return
	}
	list := services.GetSysDepartmentTree(departmentList, 0)

	resp := new(response.SysDepartmentListResp)
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 新增部门
// @Description 新增部门接口
// @Param	request	body system.SysDepartmentAddReq true "type json string"
// @Success 200 新增成功
// @router /add [post]
func (this *SysDepartmentController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req request.SysDepartmentAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if len(req.SysDepartmentNames) <= 0 {
		br.Msg = "部门名称不能为空"
		return
	}
	for _, dep := range req.SysDepartmentNames {
		count, err := models.GetSysDepartmentCountByParentId(req.SysDepartmentId, dep)
		if err != nil {
			br.Msg = "获取数据失败"
			br.ErrMsg = "获取数据失败,Err:" + err.Error()
			return
		}
		if count <= 0 {
			sysDepartment := &models.SysDepartment{}
			sysDepartment.SysDepartmentName = dep
			sysDepartment.Level = req.Level
			sysDepartment.ParentId = req.SysDepartmentId
			err = sysDepartment.Add()
			if err != nil {
				br.Msg = "新增失败"
				br.ErrMsg = "新增失败，Err:" + err.Error()
				return
			}
		}
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
}

// @Title 编辑部门
// @Description 编辑部门接口
// @Param	request	body request.SysDepartmentEditReq true "type json string"
// @Success 200 编辑成功
// @router /edit [post]
func (this *SysDepartmentController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req request.SysDepartmentEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.SysDepartmentId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误，SysDepartmentId 小于等于0 "
		return
	}
	if req.SysDepartmentName == "" {
		br.Msg = "名称不能为空"
		return
	}
	count, err := models.GetSysDepartmentCountById(req.SysDepartmentId)
	if err != nil && err != orm.ErrNoRows {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	if count <= 0 {
		br.Msg = "分组不存在，请刷新重试"
		return
	}
	oldSysDepartment, err := models.GetSysDepartmentById(req.SysDepartmentId)
	if err != nil && err != orm.ErrNoRows {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}

	newSysDepartment, err := models.GetSysDepartmentByName(req.SysDepartmentName)
	if err != nil && err != orm.ErrNoRows {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	if newSysDepartment != nil && newSysDepartment.SysDepartmentId != oldSysDepartment.SysDepartmentId {
		br.Msg = "分组名称已存在，请重新输入"
		return
	}
	sysDepartment := &models.SysDepartment{}
	sysDepartment.SysDepartmentId = req.SysDepartmentId
	sysDepartment.SysDepartmentName = req.SysDepartmentName
	err = sysDepartment.Update([]string{"sys_department_name"})
	if err != nil {
		br.Msg = "编辑失败，系统错误"
		br.Msg = "编辑失败，系统错误，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "编辑成功"
}

// @Title 删除分组
// @Description 删除分组接口
// @Param	request	body request.SysDepartmentDeleteReq true "type json string"
// @Success 200 删除成功
// @router /delete [post]
func (this *SysDepartmentController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req request.SysDepartmentDeleteReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.SysDepartmentId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,SysDepartmentId 小于等于0 "
		return
	}
	if req.Level <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,Level 小于等于0 "
		return
	}
	err = services.DeleteSysDepartmentById(req.SysDepartmentId, req.Level)
	if err != nil {
		br.Msg = "删除失败"
		br.ErrMsg = "删除失败，系统异常，Err:" + err.Error()
		return
	}

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

// SetSort
// @Title 分组排序
// @Description  分组排序
// @Param	request	body request.SysDepartmentSortReq true "type json string"
// @Success 200 修改成功
// @router /set_sort [post]
func (this *SysDepartmentController) SetSort() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req request.SysDepartmentSortReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	// 一级排序
	if len(req.DepartmentIds1) > 0 {
		err = models.UpdateDepartmentSortByIds(req.DepartmentIds1)
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "批量更新部门排序失败, Err: " + err.Error()
			return
		}
	}
	// 二级级排序
	if len(req.DepartmentIds2) > 0 {
		err = models.UpdateDepartmentSortByIds(req.DepartmentIds2)
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "批量更新部门排序失败, Err: " + err.Error()
			return
		}
	}
	// 三级级排序
	if len(req.DepartmentIds3) > 0 {
		err = models.UpdateDepartmentSortByIds(req.DepartmentIds3)
		if err != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "批量更新部门排序失败, Err: " + err.Error()
			return
		}
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
