1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package services
- import (
- "Medical_ERP/common/global"
- db "Medical_ERP/common/initialize"
- "Medical_ERP/common/redis"
- "Medical_ERP/dto"
- "Medical_ERP/models"
- "github.com/beego/beego/v2/core/logs"
- "time"
- )
- type RoleApi struct {
- }
- func (svc *RoleApi) UpdateRoleApi(d *dto.RoleApiUpdateReq) error {
- var err error
- tx := db.DB.Begin()
- // 删除之前的角色关联的api
- result := db.DB.Where("role_key = ?", d.RoleKey).Delete(&RoleApi{})
- if result.RowsAffected == 1 {
- tx.Rollback()
- logs.Error("delete api list error, %s", result.Error)
- return global.UpdateFailedErr
- }
- var roleApiList []models.RoleApi
- // 添加新增的角色关联的api
- for _, v := range d.ApiList {
- roleApiObj := models.RoleApi{}
- roleApiObj.SetApi(v.ServiceId, v.Name, v.Path, v.Action, d.RoleKey)
- roleApiObj.SetCreateBy(d.UpdateBy)
- roleApiList = append(roleApiList, roleApiObj)
- }
- // 批量插入
- err = tx.Create(&roleApiList).Error
- if err != nil {
- tx.Rollback()
- logs.Error("save api list error, %s", err)
- return global.UpdateFailedErr
- }
- tx.Commit()
- // 更新redis
- redis.SetJson(global.RedisPrefixRoleApi, roleApiList, 24*time.Hour)
- return nil
- }
- func (svc *RoleApi) GetRoleApi(roleKey string) ([]models.RoleApi, error) {
- var roleApiList []models.RoleApi
- err := redis.GetJson(global.RedisPrefixRoleApi+roleKey, &roleApiList)
- if err != nil {
- logs.Error("get role api list from redis error, %s", err)
- }
- err = db.DB.Where("role_key = ?", roleKey).Find(&roleApiList).Error
- if err != nil {
- logs.Error("get role api list error, %s", err)
- return roleApiList, err
- }
- // 更新redis
- redis.SetJson(global.RedisPrefixRoleApi+roleKey, roleApiList, 24*time.Hour)
- return roleApiList, err
- }
|