|
- package hdl
-
- import (
- "applet/app/admin/enum"
- "applet/app/admin/lib/validate"
- "applet/app/admin/md"
- "applet/app/admin/svc"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/utils"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func PermissionGroupList(c *gin.Context) {
- roleId := c.DefaultQuery("role_id", "")
- qrcodeWithBatchRecordsDb := db.PermissionGroupDb{}
- qrcodeWithBatchRecordsDb.Set()
- groupList, err := qrcodeWithBatchRecordsDb.FindPermissionGroup()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- roleDb := db.RoleDb{}
- roleDb.Set(0)
- list, _, err := roleDb.FindPermissionGroupByRole(utils.StrToInt(roleId))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var isHasPermissionGroupId []string
- for _, v := range list {
- isHasPermissionGroupId = append(isHasPermissionGroupId, utils.IntToStr(v.PermissionGroup.Id))
- }
-
- var tempRespMap = map[string]*md.PermissionGroupListResp{}
- var tempRespMapKeys []string
- for _, v := range *groupList {
- isCheck := false
- if utils.InArr(utils.IntToStr(v.Id), isHasPermissionGroupId) {
- isCheck = true
- }
- tempRespMap[utils.IntToStr(v.Id)] = &md.PermissionGroupListResp{
- Id: v.Id,
- Name: v.Name,
- Key: v.Key,
- State: v.State,
- ParentId: v.ParentId,
- CreateAt: v.CreateAt,
- UpdateAt: v.UpdateAt,
- IsCheck: isCheck,
- }
- tempRespMapKeys = append(tempRespMapKeys, utils.IntToStr(v.Id))
- }
- for _, v := range tempRespMap {
- if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId != 0 {
- tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
- }
- }
- for _, v := range tempRespMap {
- if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId == 0 {
- tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
- }
- }
-
- var resp []*md.PermissionGroupListResp
- for _, v := range tempRespMapKeys {
- if tempRespMap[v].ParentId == 0 {
- resp = append(resp, tempRespMap[v])
- }
- }
-
- e.OutSuc(c, map[string]interface{}{
- "list": resp,
- "state": []map[string]interface{}{
- {
- "name": enum.PermissionGroupState(enum.PermissionGroupStateForNormal).String(),
- "value": enum.PermissionGroupStateForNormal,
- },
- {
- "name": enum.PermissionGroupState(enum.PermissionGroupStateForDiscard).String(),
- "value": enum.PermissionGroupStateForDiscard,
- },
- },
- }, nil)
- return
- }
-
- func RoleList(c *gin.Context) {
- roleDb := db.RoleDb{}
- roleDb.Set(0)
- roleList, err := roleDb.FindRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- adminRoleDb := db.AdminRoleDb{}
- adminRoleDb.Set()
- adminDb := db.AdminDb{}
- adminDb.Set()
- var result []*md.RoleListResp
- for _, v := range *roleList {
- var temp md.RoleListResp
- temp.Data = v
- adminRole, err1 := adminRoleDb.GetAdminRoleByRole(v.Id)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- if adminRole != nil {
- admin, err2 := adminDb.GetAdmin(adminRole.AdmId)
- if err2 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err2.Error())
- return
- }
- temp.AdminList = append(temp.AdminList, struct {
- Name string `json:"name"`
- }{
- Name: admin.Username,
- })
- }
- result = append(result, &temp)
- }
- e.OutSuc(c, map[string]interface{}{
- "list": result,
- "state": []map[string]interface{}{
- {
- "name": enum.RoleState(enum.RoleStateForNormal).String(),
- "value": enum.RoleStateForNormal,
- },
- {
- "name": enum.RoleState(enum.RoleStateForFreeze).String(),
- "value": enum.RoleStateForFreeze,
- },
- },
- }, nil)
- return
- }
-
- func AddRole(c *gin.Context) {
- var req md.AddRoleReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- roleDb := db.RoleDb{}
- roleDb.Set(0)
- now := time.Now()
- _, err = roleDb.RoleInsert(&model.Role{
- Name: req.Name,
- State: enum.RoleStateForNormal,
- Memo: req.Memo,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- func UpdateRole(c *gin.Context) {
- var req md.UpdateRoleReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- roleDb := db.RoleDb{}
- roleDb.Set(req.RoleId)
- role, err := roleDb.GetRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if role == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- role.Name = req.Name
- role.Memo = req.Memo
- _, err = roleDb.UpdateRole(role, "name", "memo")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func RoleBindPermissionGroup(c *gin.Context) {
- var req md.RoleBindPermissionGroupReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- err = svc.RoleBindPermissionGroup(req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- func UpdateRoleState(c *gin.Context) {
- var req md.UpdateRoleStateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- roleDb := db.RoleDb{}
- roleDb.Set(req.RoleId)
- role, err := roleDb.GetRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if role == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- role.State = req.State
- _, err = roleDb.UpdateRole(role, "state")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func DeleteRole(c *gin.Context) {
- id := c.Param("id")
- roleDb := db.RoleDb{}
- roleDb.Set(utils.StrToInt(id))
- role, err := roleDb.GetRole()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if role == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
-
- err = svc.DeleteRole(utils.StrToInt(id))
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- func AdminList(c *gin.Context) {
- var req md.AdminListReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- if req.Limit == 0 {
- req.Limit = 10
- }
- if req.Page == 0 {
- req.Page = 10
- }
- adminDb := db.AdminDb{}
- adminDb.Set()
- adminList, total, err := adminDb.FindAdmin(req.UserName, req.State, req.Page, req.Limit)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var result []md.AdminListResp
- for _, v := range adminList {
- permissionGroupList, _, err1 := adminDb.FindAdminRolePermissionGroup(v.AdmId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- var roleList []string
- for _, v1 := range permissionGroupList {
- roleList = append(roleList, v1.Role.Name)
- }
-
- adminEnterpriseList, _, err1 := adminDb.FindAdminWithEnterprise(v.AdmId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- var enterpriseList []map[string]interface{}
- for _, v1 := range adminEnterpriseList {
- enterpriseList = append(enterpriseList, map[string]interface{}{
- "name": v1.Enterprise.Name,
- "id": v1.Enterprise.Id,
- })
- }
-
- result = append(result, md.AdminListResp{
- AdmId: v.AdmId,
- Username: v.Username,
- State: v.State,
- IsSuperAdministrator: v.IsSuperAdministrator,
- Memo: v.Memo,
- CreateAt: v.CreateAt,
- UpdateAt: v.UpdateAt,
- RoleList: roleList,
- })
- }
-
- e.OutSuc(c, map[string]interface{}{
- "list": result,
- "total": total,
- "state": []map[string]interface{}{
- {
- "name": enum.RoleState(enum.RoleStateForNormal).String(),
- "value": enum.RoleStateForNormal,
- },
- {
- "name": enum.RoleState(enum.RoleStateForFreeze).String(),
- "value": enum.RoleStateForFreeze,
- },
- },
- }, nil)
- return
- }
-
- func UpdateAdminState(c *gin.Context) {
- var req md.UpdateAdminStateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- admDb := db.AdminDb{}
- admDb.Set()
- admin, err := admDb.GetAdmin(req.AdmId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if admin == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- admin.State = req.State
- _, err = admDb.UpdateAdmin(admin, "state")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func UpdateAdmin(c *gin.Context) {
- var req md.UpdateAdminReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- admDb := db.AdminDb{}
- admDb.Set()
- admin, err := admDb.GetAdmin(req.AdmId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if admin == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
- return
- }
- admin.Username = req.Username
- admin.Memo = req.Memo
- admin.Password = utils.Md5(req.Password)
- _, err = admDb.UpdateAdmin(admin, "username", "memo", "password")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func AddAdmin(c *gin.Context) {
- var req md.AddAdminReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- admDb := db.AdminDb{}
- admDb.Set()
-
- admId, err := admDb.CreateAdminId()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- admin := model.Admin{
- AdmId: admId,
- Username: req.Username,
- Password: utils.Md5(req.Password),
- State: enum.AdminStateForNormal,
- IsSuperAdministrator: 0,
- Memo: req.Memo,
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- _, err = admDb.AdminInsert(&admin)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func DeleteAdmin(c *gin.Context) {
- admId := c.Param("adm_id")
- admDb := db.AdminDb{}
- admDb.Set()
- err := svc.AdminDelete([]int{utils.StrToInt(admId)})
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func BindAdminRole(c *gin.Context) {
- var req md.BindAdminRoleReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- err = svc.BindAdminRole(req)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func AdminInfo(c *gin.Context) {
- admId := c.DefaultQuery("adm_id", "")
- admDb := db.AdminDb{}
- admDb.Set()
- admin, err := admDb.GetAdmin(utils.StrToInt(admId))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- admin.Password = ""
- e.OutSuc(c, map[string]interface{}{
- "info": admin,
- "state": []map[string]interface{}{
- {
- "name": enum.RoleState(enum.RoleStateForNormal).String(),
- "value": enum.RoleStateForNormal,
- },
- {
- "name": enum.RoleState(enum.RoleStateForFreeze).String(),
- "value": enum.RoleStateForFreeze,
- },
- },
- }, nil)
- return
- }
|