123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- 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": "提交成功",
- })
- }
-
- }
|