sys_user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. package controllers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta/eta_mini_crm/models"
  6. "eta/eta_mini_crm/models/request"
  7. "eta/eta_mini_crm/models/response"
  8. "eta/eta_mini_crm/services"
  9. "eta/eta_mini_crm/utils"
  10. "fmt"
  11. "strings"
  12. "time"
  13. "github.com/beego/beego/v2/client/orm"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. )
  16. type SysUserController struct {
  17. BaseAuthController
  18. }
  19. // Add
  20. // @Title 系统用户添加
  21. // @Description 系统用户添加
  22. // @Param request body request.UserLoginReq true "type json string"
  23. // @Success 200 {object} models.LoginResp
  24. // @router /add [post]
  25. func (this *SysUserController) Add() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. var req request.SysUserInfoReq
  32. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  33. if err != nil {
  34. br.Msg = "参数解析异常!"
  35. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  36. return
  37. }
  38. if req.SysUserName == "" {
  39. br.Msg = "请输入账号"
  40. return
  41. }
  42. if req.Password == "" {
  43. br.Msg = "请输入密码"
  44. return
  45. }
  46. if req.RealName == "" {
  47. br.Msg = "请输入姓名"
  48. return
  49. }
  50. // 手机号和邮箱必填一个
  51. req.Phone = strings.TrimSpace(req.Phone)
  52. req.Email = strings.TrimSpace(req.Email)
  53. if req.Phone == "" && req.Email == "" {
  54. br.Msg = "至少输入一个手机号或邮箱"
  55. return
  56. }
  57. if req.Phone != "" {
  58. if req.AreaCode == "86" {
  59. if !utils.ValidateMobileFormatat(req.Phone) {
  60. br.Msg = "手机号格式有误, 请检查"
  61. return
  62. }
  63. }
  64. }
  65. if req.Email != "" {
  66. if !utils.ValidateEmailFormatat(req.Phone) {
  67. br.Msg = "邮箱格式有误, 请检查"
  68. return
  69. }
  70. }
  71. if req.SysDepartmentId <= 0 {
  72. br.Msg = "请选择部门"
  73. return
  74. }
  75. _, err = models.GetSysDepartmentById(req.SysDepartmentId)
  76. if err != nil {
  77. if err == orm.ErrNoRows {
  78. br.Msg = "所选部门不存在"
  79. return
  80. }
  81. br.Msg = "获取数据失败"
  82. br.ErrMsg = "获取部门数据失败,Err:" + err.Error()
  83. return
  84. }
  85. sysRole, err := models.GetSysRoleById(req.SysRoleId)
  86. if err != nil {
  87. if err == orm.ErrNoRows {
  88. br.Msg = "所选角色不存在"
  89. return
  90. }
  91. br.Msg = "获取数据失败"
  92. br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
  93. return
  94. }
  95. // 校验系统用户的密码
  96. var pwd string
  97. {
  98. pwdByte, err := base64.StdEncoding.DecodeString(req.Password)
  99. if err != nil {
  100. br.Msg = "解析数据失败"
  101. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  102. return
  103. }
  104. originPwd := string(pwdByte)
  105. if !utils.CheckPwd(originPwd) {
  106. br.Msg = "密码格式错误,请重新输入"
  107. return
  108. }
  109. pwd = utils.MD5(originPwd)
  110. }
  111. count, err := models.GetSysUserCountBySysUserName(req.SysUserName)
  112. if err != nil && err != orm.ErrNoRows {
  113. br.Msg = "添加用户失败,系统错误"
  114. br.ErrMsg = "添加用户失败,系统错误 Err:" + err.Error()
  115. return
  116. }
  117. if count > 0 {
  118. br.Msg = "添加用户失败,用户已存在"
  119. return
  120. }
  121. sysUser := &models.SysUser{}
  122. sysUser.SysUserName = req.SysUserName
  123. sysUser.SysRealName = req.RealName
  124. sysUser.Password = pwd
  125. sysUser.AreaCode = req.AreaCode
  126. sysUser.Phone = req.Phone
  127. sysUser.Email = req.Email
  128. sysUser.SysDepartmentId = req.SysDepartmentId
  129. sysUser.SysRoleId = req.SysRoleId
  130. sysUser.SysRoleName = sysRole.SysRoleName
  131. sysUser.Province = req.Province
  132. sysUser.City = req.City
  133. sysUser.IsEnabled = req.IsEnabled
  134. sysUser.CreateTime = time.Now()
  135. sysUser.ModifyTime = time.Now()
  136. depPathIds, err := services.GetSysDepartmentPathIdsById(req.SysDepartmentId)
  137. if err != nil {
  138. br.Msg = "编辑角色失败"
  139. br.ErrMsg = "获得部门路径失败,Err:" + err.Error()
  140. return
  141. }
  142. err = models.SaveSysUser(sysUser, depPathIds)
  143. if err != nil {
  144. br.Msg = "用户添加失败"
  145. br.ErrMsg = "用户添加失败,Err:" + err.Error()
  146. return
  147. }
  148. br.Ret = 200
  149. br.Success = true
  150. br.IsAddLog = true
  151. br.Msg = "添加成功"
  152. }
  153. // Detail
  154. // @Title 系统用户详情信息
  155. // @Description 用户详情信息
  156. // @Param SysUserId query int true "系统用户id"
  157. // @Success 200 {object} models.LoginResp
  158. // @router /detail [get]
  159. func (this *SysUserController) Detail() {
  160. br := new(models.BaseResponse).Init()
  161. defer func() {
  162. this.Data["json"] = br
  163. this.ServeJSON()
  164. }()
  165. sysUserId, err := this.GetInt("SysUserId")
  166. if err != nil {
  167. br.Msg = "参数解析错误"
  168. return
  169. }
  170. if sysUserId <= 0 {
  171. br.Msg = "用户参数错误"
  172. br.ErrMsg = fmt.Sprintf("用户参数错误 <%d>", sysUserId)
  173. return
  174. }
  175. sysUser, err := models.GetSysUserById(sysUserId)
  176. if err != nil {
  177. br.Msg = "获取用户失败,用户已删除"
  178. br.ErrMsg = "获取用户失败,用户已删除,Err" + err.Error()
  179. return
  180. }
  181. sysUser.Password = ""
  182. sysUser.SysDepartmentId = 0
  183. br.Data = sysUser
  184. br.Ret = 200
  185. br.Success = true
  186. br.Msg = "获取成功"
  187. }
  188. // Edit
  189. // @Title 系统用户编辑
  190. // @Description 系统用户编辑
  191. // @Param request body UserLoginReq true "type json string"
  192. // @Success 200 {object} models.LoginResp
  193. // @router /edit [post]
  194. func (this *SysUserController) Edit() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. this.Data["json"] = br
  198. this.ServeJSON()
  199. }()
  200. var req request.SysUserInfoReq
  201. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  202. if err != nil {
  203. br.Msg = "参数解析异常!"
  204. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  205. return
  206. }
  207. if req.SysUserName == "" {
  208. br.Msg = "请输入用户名"
  209. return
  210. }
  211. if req.SysUserId <= 0 {
  212. br.Msg = "参数错误"
  213. br.ErrMsg = fmt.Sprintf("参数错误,sysUserId<%d>", req.SysUserId)
  214. return
  215. }
  216. if req.SysRoleId <= 0 {
  217. br.Msg = "请选择角色"
  218. br.ErrMsg = "角色ID小于等于0"
  219. return
  220. }
  221. sysUser, err := models.GetSysUserById(req.SysUserId)
  222. if err != nil {
  223. if err == orm.ErrNoRows {
  224. br.Msg = "用户不存在,请刷新页面"
  225. return
  226. }
  227. br.Msg = "获取数据失败"
  228. br.ErrMsg = "获取用户数据失败,Err:" + err.Error() + ";sysUserId:" + fmt.Sprint(req.SysUserId)
  229. return
  230. }
  231. item, err := models.GetSysUserBySysUserName(req.SysUserName)
  232. if err != nil && err != orm.ErrNoRows {
  233. br.Msg = "获取数据失败"
  234. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";sysUserName:" + req.SysUserName
  235. return
  236. }
  237. if item != nil && item.SysUserId != req.SysUserId {
  238. br.Msg = "账号名称已存在,请重新输入"
  239. return
  240. }
  241. req.Phone = strings.TrimSpace(req.Phone)
  242. req.Email = strings.TrimSpace(req.Email)
  243. if req.Phone == "" && req.Email == "" {
  244. br.Msg = "至少输入一个手机号或邮箱"
  245. return
  246. }
  247. if req.Phone != "" {
  248. if req.AreaCode == "86" {
  249. if !utils.ValidateMobileFormatat(req.Phone) {
  250. br.Msg = "手机号格式有误, 请检查"
  251. return
  252. }
  253. }
  254. }
  255. if req.Email != "" {
  256. if !utils.ValidateEmailFormatat(req.Email) {
  257. br.Msg = "邮箱格式有误, 请检查"
  258. return
  259. }
  260. }
  261. var roleName string
  262. roleItem, err := models.GetSysRoleById(req.SysRoleId)
  263. if err != nil {
  264. if err == orm.ErrNoRows {
  265. br.Msg = "角色不存在,请重新选择"
  266. br.ErrMsg = "角色不存在"
  267. return
  268. }
  269. br.Msg = "获取数据失败"
  270. br.ErrMsg = "获取角色数据失败,Err:" + err.Error()
  271. return
  272. }
  273. if roleItem != nil {
  274. roleName = roleItem.SysRoleName
  275. }
  276. sysUser.SysUserName = req.SysUserName
  277. sysUser.SysRealName = req.RealName
  278. sysUser.AreaCode = req.AreaCode
  279. sysUser.Phone = req.Phone
  280. sysUser.Email = req.Email
  281. sysUser.SysRoleId = req.SysRoleId
  282. sysUser.SysRoleName = roleName
  283. sysUser.Province = req.Province
  284. sysUser.City = req.City
  285. sysUser.IsEnabled = req.IsEnabled
  286. sysUser.ModifyTime = time.Now()
  287. err = sysUser.Save()
  288. if err != nil {
  289. br.Msg = "编辑角色失败"
  290. br.ErrMsg = "编辑角色失败,Err:" + err.Error()
  291. return
  292. }
  293. br.Ret = 200
  294. br.Success = true
  295. br.Msg = "编辑成功"
  296. }
  297. // List
  298. // @Title 系统用户列表
  299. // @Description 系统用户列表
  300. // @Param PageSize query int true "每页数据条数"
  301. // @Param CurrentIndex query int true "当前页页码,从1开始"
  302. // @Param DepartmentId query int true "部门ID"
  303. // @Param RoleId query int true "角色ID"
  304. // @Param KeyWord query string true "搜索关键词"
  305. // @Success 200 {object} models.LoginResp
  306. // @router /list [get]
  307. func (this *SysUserController) List() {
  308. br := new(models.BaseResponse).Init()
  309. defer func() {
  310. this.Data["json"] = br
  311. this.ServeJSON()
  312. }()
  313. pageSize, _ := this.GetInt("PageSize")
  314. currentIndex, _ := this.GetInt("CurrentIndex")
  315. roleId, _ := this.GetInt("RoleId")
  316. departmentId, _ := this.GetInt("DepartmentId")
  317. keyWord := this.GetString("KeyWord")
  318. var condition string
  319. var pars []interface{}
  320. if roleId > 0 {
  321. condition += ` AND sys_role_id=? `
  322. pars = append(pars, roleId)
  323. }
  324. if departmentId > 0 {
  325. condition += ` AND (sys_department_id1=? OR sys_department_id2=? OR sys_department_id3=?) `
  326. pars = append(pars, departmentId, departmentId, departmentId)
  327. }
  328. if keyWord != "" {
  329. condition += ` AND (sys_real_name LIKE ? OR sys_user_name LIKE ? OR phone LIKE ? OR email LIKE ?) `
  330. pars = utils.GetLikeKeywordPars(pars, keyWord, 4)
  331. }
  332. var startSize int
  333. if pageSize <= 0 {
  334. pageSize = utils.PageSize20
  335. } else if pageSize > utils.PageSize100 {
  336. pageSize = utils.PageSize100
  337. }
  338. if currentIndex <= 0 {
  339. currentIndex = 1
  340. }
  341. startSize = utils.StartIndex(currentIndex, pageSize)
  342. total, err := models.GetSysUserCount(condition, pars)
  343. if err != nil {
  344. br.Msg = "获取失败"
  345. br.ErrMsg = "获取失败,Err:" + err.Error()
  346. return
  347. }
  348. list, err := models.GetSysUserList(condition, pars, startSize, pageSize)
  349. if err != nil {
  350. br.Msg = "获取失败"
  351. br.ErrMsg = "获取失败,Err:" + err.Error()
  352. return
  353. }
  354. fmt.Println(list)
  355. sysUserList := make([]models.SysUserView, len(list))
  356. for i, user := range list {
  357. sysUserList[i].SysUserId = user.SysUserId
  358. sysUserList[i].SysUserName = user.SysUserName
  359. sysUserList[i].SysRealName = user.SysRealName
  360. sysUserList[i].Email = user.Email
  361. sysUserList[i].Phone = user.Phone
  362. sysUserList[i].AreaCode = user.AreaCode
  363. sysUserList[i].SysRoleId = user.SysRoleId
  364. sysUserList[i].SysRoleName = user.SysRoleName
  365. sysUserList[i].SysDepartmentId = user.SysDepartmentId
  366. var path string
  367. if user.SysDepartmentName1 != "" {
  368. path = user.SysDepartmentName1
  369. }
  370. if user.SysDepartmentName2 != "" {
  371. path += "/" + user.SysDepartmentName2
  372. }
  373. if user.SysDepartmentName3 != "" {
  374. path += "/" + user.SysDepartmentName3
  375. }
  376. sysUserList[i].SysDepartmentName = path
  377. sysUserList[i].SysRoleId = user.SysRoleId
  378. sysUserList[i].Province = user.Province
  379. sysUserList[i].City = user.City
  380. sysUserList[i].IsEnabled = user.IsEnabled
  381. sysUserList[i].CreateTime = user.CreateTime
  382. sysUserList[i].ModifyTime = user.ModifyTime
  383. }
  384. page := paging.GetPaging(currentIndex, pageSize, total)
  385. resp := new(response.SysUserListResp)
  386. resp.List = sysUserList
  387. resp.Paging = page
  388. br.Ret = 200
  389. br.Success = true
  390. br.Msg = "获取成功"
  391. br.Data = resp
  392. }
  393. // ResetPass
  394. // @Title 重置密码
  395. // @Description 重置密码
  396. // @Param request body system.SysUserResetPassReq true "type json string"
  397. // @Success 200 编辑成功
  398. // @router /reset_pass [post]
  399. func (this *SysUserController) ResetPass() {
  400. br := new(models.BaseResponse).Init()
  401. defer func() {
  402. this.Data["json"] = br
  403. this.ServeJSON()
  404. }()
  405. var req request.ResetPasswordReq
  406. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  407. if err != nil {
  408. br.Msg = "参数解析异常!"
  409. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  410. return
  411. }
  412. if req.SysUserId <= 0 {
  413. br.Msg = "参数有误"
  414. br.ErrMsg = "参数有误,SysUserNameId"
  415. return
  416. }
  417. req.Password = strings.TrimSpace(req.Password)
  418. req.RePassword = strings.TrimSpace(req.RePassword)
  419. if req.Password == "" {
  420. br.Msg = "密码不能为空"
  421. return
  422. }
  423. if req.Password != req.RePassword {
  424. br.Msg = "两次密码输入不一致"
  425. return
  426. }
  427. sysUser, err := models.GetSysUserById(req.SysUserId)
  428. if err != nil {
  429. if err == orm.ErrNoRows {
  430. br.Msg = "用户已被删除, 请刷新页面"
  431. return
  432. }
  433. br.Msg = "获取数据失败"
  434. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";SysUserId:" + fmt.Sprint(req.SysUserId)
  435. return
  436. }
  437. b, err := base64.StdEncoding.DecodeString(req.Password)
  438. if err != nil {
  439. br.Msg = "解析数据失败"
  440. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  441. return
  442. }
  443. pwd := string(b)
  444. if !utils.CheckPwd(pwd) {
  445. br.Msg = "密码格式不对,必须包含8位及以上,包含数字、大写字母、小写字母、特殊字符中的三个类型"
  446. return
  447. }
  448. pwd = utils.MD5(pwd)
  449. sysUser.Password = pwd
  450. sysUser.IsEnabled = true
  451. sysUser.ModifyTime = time.Now()
  452. err = sysUser.Save()
  453. if err != nil {
  454. br.Msg = "密码修改失败"
  455. br.ErrMsg = "密码修改失败,系统错误,Err:" + err.Error()
  456. return
  457. }
  458. br.Ret = 200
  459. br.Msg = "重置密码成功"
  460. br.Success = true
  461. }
  462. // ResetMyPass
  463. // @Title 重置密码
  464. // @Description 重置密码
  465. // @Param request body system.SysUserResetPassReq true "type json string"
  466. // @Success 200 编辑成功
  467. // @router /reset_my_pass [post]
  468. func (this *SysUserController) ResetMyPass() {
  469. br := new(models.BaseResponse).Init()
  470. defer func() {
  471. this.Data["json"] = br
  472. this.ServeJSON()
  473. }()
  474. var req request.ResetMyPasswordReq
  475. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  476. if err != nil {
  477. br.Msg = "参数解析异常!"
  478. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  479. return
  480. }
  481. if req.Password != req.RePassword {
  482. br.Msg = "两次密码输入不一致"
  483. return
  484. }
  485. sysUser := this.SysUser
  486. // 校验系统用户的密码
  487. var pwd string
  488. {
  489. pwdByte, err := base64.StdEncoding.DecodeString(req.Password)
  490. if err != nil {
  491. br.Msg = "解析数据失败"
  492. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  493. return
  494. }
  495. originPwd := string(pwdByte)
  496. if !utils.CheckPwd(originPwd) {
  497. br.Msg = "密码格式错误,请重新输入"
  498. return
  499. }
  500. pwd = utils.MD5(originPwd)
  501. originPwdByte, err := base64.StdEncoding.DecodeString(req.OriginPassword)
  502. if err != nil {
  503. br.Msg = "解析数据失败"
  504. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  505. return
  506. }
  507. stringPwd := string(originPwdByte)
  508. if sysUser.Password != utils.MD5(stringPwd) {
  509. br.Msg = "原密码错误"
  510. return
  511. }
  512. }
  513. sysUser.Password = pwd
  514. sysUser.ModifyTime = time.Now()
  515. err = sysUser.Update([]string{"password", "modify_time"})
  516. if err != nil {
  517. br.Msg = "密码修改失败"
  518. br.ErrMsg = "密码修改失败,系统错误,Err:" + err.Error()
  519. return
  520. }
  521. //将用户对应的token给过期
  522. services.LogoutSysUser(sysUser.SysUserId)
  523. br.Msg = "重置密码成功"
  524. br.Success = true
  525. br.Ret = 200
  526. }
  527. // @Title 开启/禁用系统用户
  528. // @Description 开启/禁用系统用户接口
  529. // @Param request body system.SysuserEditReq true "type json string"
  530. // @Success 200 操作成功
  531. // @router /editEnabled [post]
  532. func (this *SysUserController) EditEnabled() {
  533. br := new(models.BaseResponse).Init()
  534. defer func() {
  535. this.Data["json"] = br
  536. this.ServeJSON()
  537. }()
  538. var req request.SysUserEditEnabledReq
  539. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  540. if err != nil {
  541. br.Msg = "参数解析异常!"
  542. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  543. return
  544. }
  545. sysUser, err := models.GetSysUserById(req.SysUserId)
  546. if err != nil {
  547. br.Msg = "获取数据失败"
  548. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  549. return
  550. }
  551. if sysUser.SysRoleName == "admin" && sysUser.SysUserName == "admin" {
  552. br.Msg = "禁止对admin使用<禁用>功能"
  553. return
  554. }
  555. // 修改系统用户禁用状态
  556. sysUser.IsEnabled = req.IsEnabled
  557. err = sysUser.Update([]string{"is_enabled"})
  558. if err != nil {
  559. br.Msg = "修改失败"
  560. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  561. return
  562. }
  563. //用户被禁用的情况下,需要将他对应的token给过期
  564. if sysUser.IsEnabled && !req.IsEnabled {
  565. services.LogoutSysUser(req.SysUserId)
  566. }
  567. br.Ret = 200
  568. br.Success = true
  569. br.IsAddLog = true
  570. br.Msg = "操作成功"
  571. }
  572. // MoveToDepartment
  573. // @Title 移动分组
  574. // @Description 移动分组
  575. // @Param request body system.SysUserMoveReq true "type json string"
  576. // @Success 200 编辑成功
  577. // @router /moveToDepartment [post]
  578. func (this *SysUserController) MoveToDepartment() {
  579. br := new(models.BaseResponse).Init()
  580. defer func() {
  581. this.Data["json"] = br
  582. this.ServeJSON()
  583. }()
  584. var req request.MoveToDepartmentReq
  585. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  586. if err != nil {
  587. br.Msg = "参数解析异常!"
  588. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  589. return
  590. }
  591. count, err := models.GetSysUserCountById(req.SysUserId)
  592. if err != nil {
  593. br.Msg = "移动分组失败,系统错误"
  594. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  595. return
  596. }
  597. if count <= 0 {
  598. br.Msg = "用户已被删除, 请刷新页面"
  599. return
  600. }
  601. count, err = models.GetSysDepartmentCountById(req.SysDepartmentId)
  602. if err != nil {
  603. br.Msg = "移动分组失败,系统错误"
  604. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  605. return
  606. }
  607. if count <= 0 {
  608. br.Msg = "分组已被删除, 请刷新页面"
  609. return
  610. }
  611. depPathIds, err := services.GetSysDepartmentPathIdsById(req.SysDepartmentId)
  612. if err != nil {
  613. br.Msg = "移动分组失败,系统错误"
  614. br.ErrMsg = "获得分组路径失败,系统错误,Err:" + err.Error()
  615. return
  616. }
  617. sysUser := &models.SysUser{}
  618. sysUser.SysUserId = req.SysUserId
  619. sysUser.SysDepartmentId1 = depPathIds[0]
  620. sysUser.SysDepartmentId2 = depPathIds[1]
  621. sysUser.SysDepartmentId3 = depPathIds[2]
  622. if depPathIds[0] != 0 {
  623. sysUser.SysDepartmentId = depPathIds[0]
  624. }
  625. if depPathIds[1] != 0 {
  626. sysUser.SysDepartmentId = depPathIds[1]
  627. }
  628. if depPathIds[2] != 0 {
  629. sysUser.SysDepartmentId = depPathIds[2]
  630. }
  631. err = sysUser.Update([]string{"sys_department_id", "sys_department_id1", "sys_department_id2", "sys_department_id3"})
  632. // userDepMapping := &models.SysUserDepartmentMapping{}
  633. // userDepMapping.SysUserId = req.SysUserId
  634. // userDepMapping.SysDepartmentId1 = depPathIds[0]
  635. // userDepMapping.SysDepartmentId2 = depPathIds[1]
  636. // userDepMapping.SysDepartmentId3 = depPathIds[2]
  637. // err = userDepMapping.Save()
  638. if err != nil {
  639. br.Msg = "移动分组失败,系统错误"
  640. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  641. return
  642. }
  643. br.Ret = 200
  644. br.Success = true
  645. br.Msg = "移动分组成功"
  646. }