sys_user.go 19 KB

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