sys_user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 := services.GetUserList(condition, pars, startSize, pageSize)
  349. if err != nil {
  350. br.Msg = "获取失败"
  351. br.ErrMsg = "获取失败,Err:" + err.Error()
  352. return
  353. }
  354. sysUserList := make([]models.SysUserView, len(list))
  355. for i, user := range list {
  356. sysUserList[i].SysUserId = user.SysUserId
  357. sysUserList[i].SysUserName = user.SysUserName
  358. sysUserList[i].SysRealName = user.SysRealName
  359. sysUserList[i].Email = user.Email
  360. sysUserList[i].Phone = user.Phone
  361. sysUserList[i].AreaCode = user.AreaCode
  362. sysUserList[i].SysRoleId = user.SysRoleId
  363. sysUserList[i].SysRoleName = user.SysRoleName
  364. sysUserList[i].SysDepartmentId = user.SysDepartmentId
  365. var path string
  366. if user.SysDepartmentName1 != "" {
  367. path = user.SysDepartmentName1
  368. }
  369. if user.SysDepartmentName2 != "" {
  370. path += "/" + user.SysDepartmentName2
  371. }
  372. if user.SysDepartmentName3 != "" {
  373. path += "/" + user.SysDepartmentName3
  374. }
  375. sysUserList[i].SysDepartmentName = path
  376. sysUserList[i].SysRoleId = user.SysRoleId
  377. sysUserList[i].Province = user.Province
  378. sysUserList[i].City = user.City
  379. sysUserList[i].IsEnabled = user.IsEnabled
  380. sysUserList[i].CreateTime = user.CreateTime
  381. sysUserList[i].ModifyTime = user.ModifyTime
  382. }
  383. page := paging.GetPaging(currentIndex, pageSize, total)
  384. resp := new(response.SysUserListResp)
  385. resp.List = sysUserList
  386. resp.Paging = page
  387. br.Ret = 200
  388. br.Success = true
  389. br.Msg = "获取成功"
  390. br.Data = resp
  391. }
  392. // ResetPass
  393. // @Title 重置密码
  394. // @Description 重置密码
  395. // @Param request body system.SysUserResetPassReq true "type json string"
  396. // @Success 200 编辑成功
  397. // @router /reset_pass [post]
  398. func (this *SysUserController) ResetPass() {
  399. br := new(models.BaseResponse).Init()
  400. defer func() {
  401. this.Data["json"] = br
  402. this.ServeJSON()
  403. }()
  404. var req request.ResetPasswordReq
  405. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  406. if err != nil {
  407. br.Msg = "参数解析异常!"
  408. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  409. return
  410. }
  411. if req.SysUserId <= 0 {
  412. br.Msg = "参数有误"
  413. br.ErrMsg = "参数有误,SysUserNameId"
  414. return
  415. }
  416. req.Password = strings.TrimSpace(req.Password)
  417. req.RePassword = strings.TrimSpace(req.RePassword)
  418. if req.Password == "" {
  419. br.Msg = "密码不能为空"
  420. return
  421. }
  422. if req.Password != req.RePassword {
  423. br.Msg = "两次密码输入不一致"
  424. return
  425. }
  426. sysUser, err := models.GetSysUserById(req.SysUserId)
  427. if err != nil {
  428. if err == orm.ErrNoRows {
  429. br.Msg = "用户已被删除, 请刷新页面"
  430. return
  431. }
  432. br.Msg = "获取数据失败"
  433. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error() + ";SysUserId:" + fmt.Sprint(req.SysUserId)
  434. return
  435. }
  436. b, err := base64.StdEncoding.DecodeString(req.Password)
  437. if err != nil {
  438. br.Msg = "解析数据失败"
  439. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  440. return
  441. }
  442. pwd := string(b)
  443. if !utils.CheckPwd(pwd) {
  444. br.Msg = "密码格式不对,必须包含8位及以上,包含数字、大写字母、小写字母、特殊字符中的三个类型"
  445. return
  446. }
  447. pwd = utils.MD5(pwd)
  448. sysUser.Password = pwd
  449. sysUser.IsEnabled = true
  450. sysUser.ModifyTime = time.Now()
  451. err = sysUser.Save()
  452. if err != nil {
  453. br.Msg = "密码修改失败"
  454. br.ErrMsg = "密码修改失败,系统错误,Err:" + err.Error()
  455. return
  456. }
  457. br.Ret = 200
  458. br.Msg = "重置密码成功"
  459. br.Success = true
  460. }
  461. // ResetMyPass
  462. // @Title 重置密码
  463. // @Description 重置密码
  464. // @Param request body system.SysUserResetPassReq true "type json string"
  465. // @Success 200 编辑成功
  466. // @router /reset_my_pass [post]
  467. func (this *SysUserController) ResetMyPass() {
  468. br := new(models.BaseResponse).Init()
  469. defer func() {
  470. this.Data["json"] = br
  471. this.ServeJSON()
  472. }()
  473. var req request.ResetMyPasswordReq
  474. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  475. if err != nil {
  476. br.Msg = "参数解析异常!"
  477. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  478. return
  479. }
  480. if req.Password != req.RePassword {
  481. br.Msg = "两次密码输入不一致"
  482. return
  483. }
  484. sysUser := this.SysUser
  485. // 校验系统用户的密码
  486. var pwd string
  487. {
  488. pwdByte, err := base64.StdEncoding.DecodeString(req.Password)
  489. if err != nil {
  490. br.Msg = "解析数据失败"
  491. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  492. return
  493. }
  494. originPwd := string(pwdByte)
  495. if !utils.CheckPwd(originPwd) {
  496. br.Msg = "密码格式错误,请重新输入"
  497. return
  498. }
  499. pwd = utils.MD5(originPwd)
  500. originPwdByte, err := base64.StdEncoding.DecodeString(req.OriginPassword)
  501. if err != nil {
  502. br.Msg = "解析数据失败"
  503. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  504. return
  505. }
  506. stringPwd := string(originPwdByte)
  507. if sysUser.Password != utils.MD5(stringPwd) {
  508. br.Msg = "原密码错误"
  509. return
  510. }
  511. }
  512. sysUser.Password = pwd
  513. sysUser.ModifyTime = time.Now()
  514. err = sysUser.Update([]string{"password", "modify_time"})
  515. if err != nil {
  516. br.Msg = "密码修改失败"
  517. br.ErrMsg = "密码修改失败,系统错误,Err:" + err.Error()
  518. return
  519. }
  520. //将用户对应的token给过期
  521. services.LogoutSysUser(sysUser.SysUserId)
  522. br.Msg = "重置密码成功"
  523. br.Success = true
  524. br.Ret = 200
  525. }
  526. // @Title 开启/禁用系统用户
  527. // @Description 开启/禁用系统用户接口
  528. // @Param request body system.SysuserEditReq true "type json string"
  529. // @Success 200 操作成功
  530. // @router /editEnabled [post]
  531. func (this *SysUserController) EditEnabled() {
  532. br := new(models.BaseResponse).Init()
  533. defer func() {
  534. this.Data["json"] = br
  535. this.ServeJSON()
  536. }()
  537. var req request.SysUserEditEnabledReq
  538. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  539. if err != nil {
  540. br.Msg = "参数解析异常!"
  541. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  542. return
  543. }
  544. sysUser, err := models.GetSysUserById(req.SysUserId)
  545. if err != nil {
  546. br.Msg = "获取数据失败"
  547. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  548. return
  549. }
  550. if sysUser.SysRoleName == "admin" && sysUser.SysUserName == "admin" {
  551. br.Msg = "禁止对admin使用<禁用>功能"
  552. return
  553. }
  554. // 修改系统用户禁用状态
  555. sysUser.IsEnabled = req.IsEnabled
  556. err = sysUser.Update([]string{"is_enabled"})
  557. if err != nil {
  558. br.Msg = "修改失败"
  559. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  560. return
  561. }
  562. //用户被禁用的情况下,需要将他对应的token给过期
  563. if sysUser.IsEnabled && !req.IsEnabled {
  564. services.LogoutSysUser(req.SysUserId)
  565. }
  566. br.Ret = 200
  567. br.Success = true
  568. br.IsAddLog = true
  569. br.Msg = "操作成功"
  570. }
  571. // MoveToDepartment
  572. // @Title 移动分组
  573. // @Description 移动分组
  574. // @Param request body system.SysUserMoveReq true "type json string"
  575. // @Success 200 编辑成功
  576. // @router /moveToDepartment [post]
  577. func (this *SysUserController) MoveToDepartment() {
  578. br := new(models.BaseResponse).Init()
  579. defer func() {
  580. this.Data["json"] = br
  581. this.ServeJSON()
  582. }()
  583. var req request.MoveToDepartmentReq
  584. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  585. if err != nil {
  586. br.Msg = "参数解析异常!"
  587. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  588. return
  589. }
  590. count, err := models.GetSysUserCountById(req.SysUserId)
  591. if err != nil {
  592. br.Msg = "移动分组失败,系统错误"
  593. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  594. return
  595. }
  596. if count <= 0 {
  597. br.Msg = "用户已被删除, 请刷新页面"
  598. return
  599. }
  600. count, err = models.GetSysDepartmentCountById(req.SysDepartmentId)
  601. if err != nil {
  602. br.Msg = "移动分组失败,系统错误"
  603. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  604. return
  605. }
  606. if count <= 0 {
  607. br.Msg = "分组已被删除, 请刷新页面"
  608. return
  609. }
  610. depPathIds, err := services.GetSysDepartmentPathIdsById(req.SysDepartmentId)
  611. if err != nil {
  612. br.Msg = "移动分组失败,系统错误"
  613. br.ErrMsg = "获得分组路径失败,系统错误,Err:" + err.Error()
  614. return
  615. }
  616. sysUser := &models.SysUser{}
  617. sysUser.SysUserId = req.SysUserId
  618. sysUser.SysDepartmentId1 = depPathIds[0]
  619. sysUser.SysDepartmentId2 = depPathIds[1]
  620. sysUser.SysDepartmentId3 = depPathIds[2]
  621. if depPathIds[0] != 0 {
  622. sysUser.SysDepartmentId = depPathIds[0]
  623. }
  624. if depPathIds[1] != 0 {
  625. sysUser.SysDepartmentId = depPathIds[1]
  626. }
  627. if depPathIds[2] != 0 {
  628. sysUser.SysDepartmentId = depPathIds[2]
  629. }
  630. err = sysUser.Update([]string{"sys_department_id", "sys_department_id1", "sys_department_id2", "sys_department_id3"})
  631. // userDepMapping := &models.SysUserDepartmentMapping{}
  632. // userDepMapping.SysUserId = req.SysUserId
  633. // userDepMapping.SysDepartmentId1 = depPathIds[0]
  634. // userDepMapping.SysDepartmentId2 = depPathIds[1]
  635. // userDepMapping.SysDepartmentId3 = depPathIds[2]
  636. // err = userDepMapping.Save()
  637. if err != nil {
  638. br.Msg = "移动分组失败,系统错误"
  639. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  640. return
  641. }
  642. br.Ret = 200
  643. br.Success = true
  644. br.Msg = "移动分组成功"
  645. }