123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package controllers
- import (
- "encoding/json"
- "hongze/hz_eta_api/models"
- "hongze/hz_eta_api/models/system"
- "hongze/hz_eta_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_ETA_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_ETA_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_ETA_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
- }
|