|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- 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
- adminRoles, err1 := adminRoleDb.FindAdminRoleByRoleId(v.Id)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- for _, adminRole := range *adminRoles {
- 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)
- }
-
- 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()
- isHasAdmin, err := admDb.GetAdminByUserName(req.Username)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if isHasAdmin != nil {
- e.OutErr(c, e.ERR, "当前用户名已存在,请勿重复添加")
- return
- }
-
- 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
- }
|