123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package config
- import (
- "eta/eta_mini_ht_api/models"
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
- "time"
- )
- const (
- Columns = "permission_id,name,parent_id,risk_level,sort"
- )
- // Permission 定义权限表的结构
- type Permission struct {
- Id int `gorm:"primaryKey;autoIncrement"`
- PermissionId int `gorm:"type:int"`
- Name string `gorm:"type:varchar(255)"`
- ParentId int `gorm:"default:null"`
- RiskLevel string `gorm:"type:varchar(10)"`
- Sort int `gorm:"default:null"`
- CreateTime time.Time `gorm:"default:null"`
- UpdateTime time.Time `gorm:"default:null;onUpdate:CURRENT_TIMESTAMP"`
- }
- // BeforeCreate 钩子用于在创建记录前设置 CreateTime
- func (p *Permission) BeforeCreate(tx *gorm.DB) (err error) {
- p.CreateTime = time.Now()
- return
- }
- func BatchInsertOrUpdate(list []Permission) (err error) {
- db := models.Main()
- OnConflictFunc := clause.OnConflict{
- Columns: []clause.Column{{Name: "permission_id"}},
- DoUpdates: clause.AssignmentColumns([]string{"name", "parent_id", "sort"}),
- }
- // 执行批量插入或更新操作
- err = db.Clauses(OnConflictFunc).Create(&list).Error
- return
- }
- func GetPermissionByName(name string) (permission Permission, err error) {
- db := models.Main()
- err = db.Model(&Permission{}).Select(Columns).Where("name = ?", name).First(&permission).Error
- return
- }
- func GetPermissionList() (list []Permission, err error) {
- db := models.Main()
- err = db.Model(&Permission{}).Select(Columns).Order("parent_id asc").Order("sort asc").Find(&list).Error
- return
- }
- func GetPermissionListByIds(permissionIds []int) (list []Permission, err error) {
- db := models.Main()
- //一级分类不能挂风险等级
- err = db.Model(&Permission{}).Select(Columns).Where("permission_id in ? and (risk_level !='' and risk_level is NOT NULL and parent_id !=0 )", permissionIds).Find(&list).Error
- return
- }
- func GetPermissionListWithRisk() (list []Permission, err error) {
- db := models.Main()
- //一级分类不能挂风险等级
- err = db.Model(&Permission{}).Select(Columns).Where("risk_level is NOT NULL and risk_level != '' and parent_id !=0").Find(&list).Error
- return
- }
- func GetPermissionNamesByPermissionIds(ids []int) (permissionNames []string, err error) {
- db := models.Main()
- err = db.Model(&Permission{}).Select("name").Where("permission_id in ?", ids).Scan(&permissionNames).Error
- return
- }
- func GetPermissionsByPermissionIds(ids []int) (permissions []Permission, err error) {
- db := models.Main()
- err = db.Model(&Permission{}).Select(Columns).Where("permission_id in ?", ids).Find(&permissions).Error
- return
- }
- func GetFirstPermissionsByIds(ids []int) (permissionList []Permission, err error) {
- db := models.Main()
- err = db.Model(&Permission{}).Select("permission_id, name").Where("permission_id in (select parent_id from permissions WHERE permission_id in ?)", ids).Find(&permissionList).Error
- //sql := "select permission_id, name from permission where chart_permission_id in (select parent_id from chart_permission WHERE chart_permission_id in ?)"
- //err = doSql(sql, &chartPermissionList, ids)
- return
- }
- func GetPermissionIdByName(name string) (id int, err error) {
- db := models.Main()
- //sql := "select chart_permission_id from chart_permission where permission_name =? "
- err = db.Model(&Permission{}).Select("permission_id").Where("name = ?", name).Scan(&id).Error
- return
- }
- func GetSecondPermissionsByClassifyID(classifyId int) (permissionList []Permission, err error) {
- //sql := "select chart_permission_id, permission_name from chart_permission WHERE chart_permission_id in( select DISTINCT cpskwp.chart_permission_id from chart_permission_search_key_word_mapping cpskwp where cpskwp.classify_id=? order by cpskwp.chart_permission_id)"
- //err = doSql(sql, &chartPermissionList, classifyID)
- db := models.Main()
- err = db.Model(&Permission{}).Select("permission_id, name,risk_level").Where("permission_id in ( select DISTINCT pcm.permission_id from permission_classify_mapping pcm where pcm.classify_id=? order by pcm.permission_id)", classifyId).Find(&permissionList).Error
- return
- }
- func GetFirstPermissionsByClassifyID(classifyId int) (permissionList []Permission, err error) {
- db := models.Main()
- //sql := "select chart_permission_id, permission_name,parent_id from chart_permission WHERE chart_permission_id in( select DISTINCT cp.parent_id from chart_permission_search_key_word_mapping cpskwp left JOIN chart_permission cp on cp.chart_permission_id =cpskwp.chart_permission_id where cpskwp.classify_id=? order by cp.parent_id)"
- err = db.Model(&Permission{}).Select("permission_id, name,parent_id").Where("permission_id in ( select DISTINCT ps.parent_id from permission_classify_mapping pcm left JOIN permissions ps on ps.permission_id =pcm.permission_id where pcm.classify_id=? order by ps.parent_id)", classifyId).Find(&permissionList).Error
- return
- }
- func PermissionsByPermissionId(permissionId int) (permission Permission, err error) {
- db := models.Main()
- //sql := "select chart_permission_id, permission_name,parent_id from chart_permission WHERE chart_permission_id in( select DISTINCT cp.parent_id from chart_permission_search_key_word_mapping cpskwp left JOIN chart_permission cp on cp.chart_permission_id =cpskwp.chart_permission_id where cpskwp.classify_id=? order by cp.parent_id)"
- err = db.Model(&Permission{}).Select(Columns).Where("permission_id =? ", permissionId).First(&permission).Error
- return
- }
- type PermissionWeight struct {
- PermissionId int
- Weight int
- }
|