package admin_api_controllers import ( "XT_Admin_Api/enums" "XT_Admin_Api/models" "XT_Admin_Api/models/admin_models" "XT_Admin_Api/service" "XT_Admin_Api/utils" "encoding/json" "github.com/jinzhu/gorm" "reflect" "time" ) type SystemApiController struct { AdminBaseAPIAuthController } func (this *SystemApiController) GetUserList() { list, _ := service.GetAllAdmin() this.ServeSuccessJSON(map[string]interface{}{ "list": list, }) } func (this *SystemApiController) CreateAdminUser() { name := this.GetString("name") mobile := this.GetString("mobile") password := this.GetString("password") admin := &admin_models.AdminAccount{ Account: mobile, Pwd: password, Name: name, Status: 1, IsSuperAdmin: 2, Ctime: time.Now().Unix(), Mtime: time.Now().Unix(), } admins, _ := service.FindUserInfoByAccount(mobile) if admins.ID > 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException) return } err := service.CreateAdmin(admin) if err == nil { this.ServeSuccessJSON(map[string]interface{}{ "admin": admin, }) } } func (this *SystemApiController) ModifyAdminUser() { id, _ := this.GetInt64("id") name := this.GetString("name") mobile := this.GetString("mobile") password := this.GetString("password") status, _ := this.GetInt64("status") admins, _ := service.FindAdminById(id) admin := &admin_models.AdminAccount{ ID: admins.ID, Account: mobile, Pwd: password, Name: name, Status: status, IsSuperAdmin: 2, Ctime: admins.Ctime, Mtime: time.Now().Unix(), } info, _ := service.FindUserInfoByAccount(mobile) if info.ID > 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException) return } err := service.UpdateAdmin(admin) if err == nil { this.ServeSuccessJSON(map[string]interface{}{ "admin": admin, }) } } func (this *SystemApiController) DeleteAdminUser() { id, _ := this.GetInt64("id") err := service.DeleteAdmin(id) if err == nil { this.ServeSuccessJSON(map[string]interface{}{ "msg": "删除成功", }) } } func (this *SystemApiController) GetUserInitData() { org, err := service.FindAllOrg() if err == nil { this.ServeSuccessJSON(map[string]interface{}{ "org": org, }) } } func (this *SystemApiController) GetFollowOrg() { id, _ := this.GetInt64("admin_user_id") FollowOrgs, err := service.FindFollowOrg(id) unFollow, _ := service.FindUnFollowOrgByIds(id) if err == nil { this.ServeSuccessJSON(map[string]interface{}{ "follows": FollowOrgs, "unFollow": unFollow, }) } } func (this *SystemApiController) GetAdminUserById() { id, _ := this.GetInt64("id") account, err := service.FindAdminById(id) if err == nil { this.ServeSuccessJSON(map[string]interface{}{ "account": account, }) } } func (this *SystemApiController) PostFollowInfo() { time := time.Now().Unix() var orgFollow []*models.OrgFollow dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" { thisFollow, _ := dataBody["Follow"].([]interface{}) if len(thisFollow) > 0 { for _, item := range thisFollow { items := item.(map[string]interface{}) if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" { utils.ErrorLog("org_id") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } org_id := int64(items["org_id"].(float64)) if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" { utils.ErrorLog("admin_user_id") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } admin_user_id := int64(items["admin_user_id"].(float64)) follow := &models.OrgFollow{ Mtime: time, Status: 1, Ctime: time, OrgId: org_id, AdminUserId: admin_user_id, } orgFollow = append(orgFollow, follow) } } } //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据 for index, follow := range orgFollow { info, err := service.FindFollowRecordByID(follow) if err == nil { //存在,则修改删除状态 orgFollow = append(orgFollow[:index], orgFollow[index+1:]...) if info.Status == 0 { follow := &models.OrgFollow{ ID: info.ID, Mtime: info.Mtime, Status: 1, Ctime: info.Ctime, OrgId: info.OrgId, AdminUserId: info.AdminUserId, } service.UpdateFollow(follow) } } } if len(orgFollow) > 0 { errs := service.CreateFollowInfo(orgFollow) if errs == nil { this.ServeSuccessJSON(map[string]interface{}{ "msg": "提交成功", }) } } else { this.ServeSuccessJSON(map[string]interface{}{ "msg": "提交成功", }) } } func (this *SystemApiController) CancelFollowInfo() { time := time.Now().Unix() var orgFollow []*models.OrgFollow dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" { thisFollow, _ := dataBody["Follow"].([]interface{}) if len(thisFollow) > 0 { for _, item := range thisFollow { items := item.(map[string]interface{}) if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" { utils.ErrorLog("org_id") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } org_id := int64(items["org_id"].(float64)) if items["follow_id"] == nil || reflect.TypeOf(items["follow_id"]).String() != "float64" { utils.ErrorLog("follow_id") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } follow_id := int64(items["follow_id"].(float64)) if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" { utils.ErrorLog("admin_user_id") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } admin_user_id := int64(items["admin_user_id"].(float64)) follow := &models.OrgFollow{ ID: follow_id, Mtime: time, Status: 0, Ctime: time, OrgId: org_id, AdminUserId: admin_user_id, } orgFollow = append(orgFollow, follow) } } } for _, follow := range orgFollow { service.UpdateFollow(follow) } this.ServeSuccessJSON(map[string]interface{}{ "follow": orgFollow, }) } func (this *SystemApiController) PostUnFollow() { org_id, _ := this.GetInt64("org_id") admin_user_id, _ := this.GetInt64("admin_user_id") info, _ := service.FindFollowInfoById(org_id, admin_user_id) follow := &models.OrgFollow{ ID: info.ID, Mtime: time.Now().Unix(), Status: 0, Ctime: info.Ctime, OrgId: org_id, AdminUserId: info.AdminUserId, } service.UpdateFollow(follow) } func (this *SystemApiController) ModifyFollowInfo() { org_id, _ := this.GetInt64("org_id") time := time.Now().Unix() var orgFollow []*models.OrgFollow dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" { thisFollow, _ := dataBody["Follow"].([]interface{}) if len(thisFollow) > 0 { for _, item := range thisFollow { items := item.(map[string]interface{}) if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" { utils.ErrorLog("admin_user_id") this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } admin_user_id := int64(items["admin_user_id"].(float64)) follow := &models.OrgFollow{ Mtime: time, Status: 1, Ctime: time, OrgId: org_id, AdminUserId: admin_user_id, } orgFollow = append(orgFollow, follow) } } } //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据 for index, follow := range orgFollow { info, err := service.FindFollowRecordByID(follow) if err == gorm.ErrRecordNotFound { //不存在,过滤掉该数据 orgFollow = append(orgFollow[:index], orgFollow[index+1:]...) } else if err == nil { //存在,则修改删除状态 follow := &models.OrgFollow{ ID: info.ID, Mtime: info.Mtime, Status: 1, Ctime: info.Ctime, OrgId: info.OrgId, AdminUserId: info.AdminUserId, } service.UpdateFollow(follow) } } if len(orgFollow) > 0 { errs := service.CreateFollowInfo(orgFollow) if errs == nil { this.ServeSuccessJSON(map[string]interface{}{ "msg": "提交成功", }) } } else { this.ServeSuccessJSON(map[string]interface{}{ "msg": "提交成功", }) } }