package controllers

import (
	"encoding/json"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/utils"
	"time"
)

// 系统设置
type SysDepartmentController struct {
	BaseAuthController
}

// @Title 新增部门
// @Description 新增部门接口
// @Param	request	body system.SysDepartmentAddReq true "type json string"
// @Success 200 新增成功
// @router /department/add [post]
func (this *SysDepartmentController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysDepartmentAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.DepartmentName == "" {
		br.Msg = "部门名称不能为空"
		return
	}
	count, err := system.GetSysDepartmentCount(req.DepartmentName)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败"
		return
	}
	if count > 0 {
		br.Msg = "部门名称已存在,请重新输入"
		return
	}
	item := new(system.SysDepartment)
	item.DepartmentName = req.DepartmentName
	item.CreateTime = time.Now()
	departmentId, err := system.AddSysDepartment(item)
	if err != nil {
		br.Msg = "新增失败"
		br.ErrMsg = "新增失败，Err:" + err.Error()
		return
	}

	// 同步部门缓存
	var syncData system.SyncDepartmentData
	syncData.Source = utils.SOURCE_CRM_FLAG
	syncData.DepartmentId = int(departmentId)
	_ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)

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

// @Title 修改部门
// @Description 修改部门接口
// @Param	request	body system.SysDepartmentEditReq true "type json string"
// @Success 200 修改成功
// @router /department/edit [post]
func (this *SysDepartmentController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.SysDepartmentEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.DepartmentId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,DepartmentId 小于等于0 "
		return
	}
	if req.DepartmentName == "" {
		br.Msg = "部门名称不能为空"
		return
	}
	item, err := system.GetSysDepartmentByName(req.DepartmentName)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	if item != nil {
		if item.DepartmentId != req.DepartmentId {
			br.Msg = "名称已存在，请重新输入"
			return
		}
	}
	err = system.ModifySysDepartment(req.DepartmentName, req.DepartmentId)
	if err != nil {
		br.Msg = "修改失败"
		br.ErrMsg = "修改失败，Err:" + err.Error()
		return
	}

	// 同步部门缓存
	var syncData system.SyncDepartmentData
	syncData.Source = utils.SOURCE_CRM_FLAG
	syncData.DepartmentId = req.DepartmentId
	_ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)

	br.Ret = 200
	br.Success = true
	br.Msg = "修改成功"
}

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

	// 同步部门缓存
	var syncData system.SyncDepartmentData
	syncData.Source = utils.SOURCE_CRM_FLAG
	syncData.DepartmentId = req.DepartmentId
	_ = utils.Rc.LPush(utils.CACHE_SYNC_DEPARTMENT, syncData)

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

// @Title 获取部门列表
// @Description 获取部门列表接口
// @Success 200 {object} system.SysDepartmentListResp
// @router /department/list [get]
func (this *SysDepartmentController) ListDepartment() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		return
	}
	list, err := system.GetDepartmentList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	lenList := len(list)
	for i := 0; i < lenList; i++ {
		departmentId := list[i].DepartmentId
		groupList, err := system.GetSysGroupByDepartmentId(departmentId)
		if err != nil {
			br.Msg = "获取数据失败"
			br.ErrMsg = "获取数据失败，Err:" + err.Error()
			return
		}
		list[i].Child = groupList
		list[i].IsDepartment = true
		for i2, sysGroup := range groupList {
			teamList, err := system.GetSysTeamByDepartmentId(sysGroup.GroupId)
			if err != nil {
				br.Msg = "获取数据失败"
				br.ErrMsg = "获取数据失败，Err:" + err.Error()
				return
			}
			groupList[i2].Child = teamList
			groupList[i2].IsGroup = true
		}
	}
	resp := new(system.SysDepartmentListResp)
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
