manual_user.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package eta_trial
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-playground/validator/v10"
  6. "hongze/hz_crm_eta/controller/resp"
  7. "hongze/hz_crm_eta/global"
  8. edbTrialModel "hongze/hz_crm_eta/models/eta_trial"
  9. "time"
  10. )
  11. // CreateManualAuth
  12. // @Description 新增手工录入权限
  13. // @Success 200 {string} string "操作成功"
  14. // @Router /eta_trial/manual_user/add_auth [post]
  15. func (a *EtaTrialController) CreateManualAuth(c *gin.Context) {
  16. var req edbTrialModel.CreateManualAuthReq
  17. err := c.Bind(&req)
  18. if err != nil {
  19. errs, ok := err.(validator.ValidationErrors)
  20. if !ok {
  21. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  22. return
  23. }
  24. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  25. return
  26. }
  27. if req.AdminId <= 0 {
  28. resp.FailData("参数有误", fmt.Sprintf("CreateManualAuth参数有误, AdminId: %d", req.AdminId), c)
  29. return
  30. }
  31. // 从ETA试用平台的edb库中读取手工数据分类
  32. classifyIds := make([]int, 0)
  33. {
  34. classifyOb := new(edbTrialModel.EdbdataClassify)
  35. cond := `is_show = 1`
  36. pars := make([]interface{}, 0)
  37. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, "")
  38. if e != nil {
  39. resp.FailData("操作失败", "获取手工数据分类失败, Err: "+e.Error(), c)
  40. return
  41. }
  42. for _, v := range list {
  43. classifyIds = append(classifyIds, v.ClassifyId)
  44. }
  45. }
  46. // 新增用户及权限
  47. manualUser := new(edbTrialModel.ManualUser)
  48. manualUser.AdminId = req.AdminId
  49. manualUser.AdminRealName = req.AdminName
  50. manualUser.CreateTime = time.Now().Local()
  51. if e := manualUser.Create(); e != nil {
  52. resp.FailData("操作失败", "新增权限用户失败, Err: "+e.Error(), c)
  53. return
  54. }
  55. userClassifies := make([]*edbTrialModel.ManualUserClassify, 0)
  56. for _, v := range classifyIds {
  57. userClassifies = append(userClassifies, &edbTrialModel.ManualUserClassify{
  58. AdminId: req.AdminId,
  59. ClassifyId: v,
  60. CreateTime: time.Now().Local(),
  61. })
  62. }
  63. userClassifyOb := new(edbTrialModel.ManualUserClassify)
  64. if e := userClassifyOb.BatchCreate(userClassifies); e != nil {
  65. resp.FailData("操作失败", "批量新增权限用户分类失败, Err: "+e.Error(), c)
  66. return
  67. }
  68. resp.OkData("操作成功", "", c)
  69. }