sys_user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. pwd = utils.MD5(originPwd)
  516. originPwdByte, err := base64.StdEncoding.DecodeString(req.OriginPassword)
  517. if err != nil {
  518. br.Msg = "解析数据失败"
  519. br.ErrMsg = "解析数据失败,Err:" + err.Error()
  520. return
  521. }
  522. stringPwd := string(originPwdByte)
  523. if sysUser.Password != utils.MD5(stringPwd) {
  524. br.Msg = "原密码错误"
  525. return
  526. }
  527. }
  528. sysUser.Password = pwd
  529. sysUser.ModifyTime = time.Now()
  530. err = sysUser.Update([]string{"password", "modify_time"})
  531. if err != nil {
  532. br.Msg = "密码修改失败"
  533. br.ErrMsg = "密码修改失败,系统错误,Err:" + err.Error()
  534. return
  535. }
  536. //将用户对应的token给过期
  537. services.LogoutSysUser(sysUser.SysUserId)
  538. br.Msg = "重置密码成功"
  539. br.Success = true
  540. br.Ret = 200
  541. }
  542. // @Title 开启/禁用系统用户
  543. // @Description 开启/禁用系统用户接口
  544. // @Param request body system.SysuserEditReq true "type json string"
  545. // @Success 200 操作成功
  546. // @router /editEnabled [post]
  547. func (this *SysUserController) EditEnabled() {
  548. br := new(models.BaseResponse).Init()
  549. defer func() {
  550. this.Data["json"] = br
  551. this.ServeJSON()
  552. }()
  553. var req request.SysUserEditEnabledReq
  554. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  555. if err != nil {
  556. br.Msg = "参数解析异常!"
  557. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  558. return
  559. }
  560. sysUser, err := models.GetSysUserById(req.SysUserId)
  561. if err != nil {
  562. br.Msg = "获取数据失败"
  563. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  564. return
  565. }
  566. if sysUser.SysRoleName == "admin" && sysUser.SysUserName == "admin" {
  567. br.Msg = "禁止对admin使用<禁用>功能"
  568. return
  569. }
  570. // 修改系统用户禁用状态
  571. sysUser.IsEnabled = req.IsEnabled
  572. err = sysUser.Update([]string{"is_enabled"})
  573. if err != nil {
  574. br.Msg = "修改失败"
  575. br.ErrMsg = "修改系统用户数据失败,Err:" + err.Error()
  576. return
  577. }
  578. //用户被禁用的情况下,需要将他对应的token给过期
  579. if sysUser.IsEnabled && !req.IsEnabled {
  580. services.LogoutSysUser(req.SysUserId)
  581. }
  582. br.Ret = 200
  583. br.Success = true
  584. br.Msg = "操作成功"
  585. }
  586. // MoveToDepartment
  587. // @Title 移动分组
  588. // @Description 移动分组
  589. // @Param request body system.SysUserMoveReq true "type json string"
  590. // @Success 200 编辑成功
  591. // @router /moveToDepartment [post]
  592. func (this *SysUserController) MoveToDepartment() {
  593. br := new(models.BaseResponse).Init()
  594. defer func() {
  595. this.Data["json"] = br
  596. this.ServeJSON()
  597. }()
  598. var req request.MoveToDepartmentReq
  599. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  600. if err != nil {
  601. br.Msg = "参数解析异常!"
  602. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  603. return
  604. }
  605. count, err := models.GetSysUserCountById(req.SysUserId)
  606. if err != nil {
  607. br.Msg = "移动分组失败,系统错误"
  608. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  609. return
  610. }
  611. if count <= 0 {
  612. br.Msg = "用户已被删除, 请刷新页面"
  613. return
  614. }
  615. count, err = models.GetSysDepartmentCountById(req.SysDepartmentId)
  616. if err != nil {
  617. br.Msg = "移动分组失败,系统错误"
  618. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  619. return
  620. }
  621. if count <= 0 {
  622. br.Msg = "分组已被删除, 请刷新页面"
  623. return
  624. }
  625. depPathIds, err := services.GetSysDepartmentPathIdsById(req.SysDepartmentId)
  626. if err != nil {
  627. br.Msg = "移动分组失败,系统错误"
  628. br.ErrMsg = "获得分组路径失败,系统错误,Err:" + err.Error()
  629. return
  630. }
  631. sysUser := &models.SysUser{}
  632. sysUser.SysUserId = req.SysUserId
  633. sysUser.SysDepartmentId1 = depPathIds[0]
  634. sysUser.SysDepartmentId2 = depPathIds[1]
  635. sysUser.SysDepartmentId3 = depPathIds[2]
  636. sysUser.ModifyTime = time.Now()
  637. if depPathIds[0] != 0 {
  638. sysUser.SysDepartmentId = depPathIds[0]
  639. }
  640. if depPathIds[1] != 0 {
  641. sysUser.SysDepartmentId = depPathIds[1]
  642. }
  643. if depPathIds[2] != 0 {
  644. sysUser.SysDepartmentId = depPathIds[2]
  645. }
  646. err = sysUser.Update([]string{"sys_department_id", "sys_department_id1", "sys_department_id2", "sys_department_id3", "modify_time"})
  647. // userDepMapping := &models.SysUserDepartmentMapping{}
  648. // userDepMapping.SysUserId = req.SysUserId
  649. // userDepMapping.SysDepartmentId1 = depPathIds[0]
  650. // userDepMapping.SysDepartmentId2 = depPathIds[1]
  651. // userDepMapping.SysDepartmentId3 = depPathIds[2]
  652. // err = userDepMapping.Save()
  653. if err != nil {
  654. br.Msg = "移动分组失败,系统错误"
  655. br.ErrMsg = "移动分组失败,系统错误,Err:" + err.Error()
  656. return
  657. }
  658. br.Ret = 200
  659. br.Success = true
  660. br.Msg = "移动分组成功"
  661. }