123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package models
- import (
- "eta_gn/eta_api/global"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strings"
- "time"
- )
- type EnPermission struct {
- EnPermissionId int `gorm:"column:en_permission_id;primaryKey;autoIncrement" description:"英文权限ID"`
- EnPermissionName string `gorm:"column:en_permission_name" description:"英文权限名称"`
- CnPermissionName string `gorm:"column:cn_permission_name" description:"对应的中文权限名称"`
- ParentId int `gorm:"column:parent_id" description:"父级ID"`
- Sort int `gorm:"column:sort" description:"排序"`
- CreateTime time.Time `gorm:"column:create_time;autoCreateTime" description:"创建时间"`
- ModifyTime time.Time `gorm:"column:modify_time;autoUpdateTime" description:"修改时间"`
- Enabled int `gorm:"column:enabled" description:"是否可用,1可用,0禁用"`
- }
- var EnPermissionColumns = struct {
- EnPermissionId string
- EnPermissionName string
- CnPermissionName string
- ParentId string
- Sort string
- CreateTime string
- ModifyTime string
- Enabled string
- }{
- EnPermissionId: "en_permission_id",
- EnPermissionName: "en_permission_name",
- CnPermissionName: "cn_permission_name",
- ParentId: "parent_id",
- Sort: "sort",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- Enabled: "enabled",
- }
- func (m *EnPermission) TableName() string {
- return "en_permission"
- }
- func (m *EnPermission) Create() (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- //id, err := o.Insert(m)
- //if err != nil {
- // return
- //}
- //m.EnPermissionId = int(id)
- err = global.DmSQL["rddp"].Create(m).Error
- return
- }
- func (m *EnPermission) Update(cols []string) (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- //_, err = o.Update(m, cols...)
- err = global.DmSQL["rddp"].Select(cols).Updates(m).Error
- return
- }
- func (m *EnPermission) Del() (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnPermissionColumns.EnPermissionId)
- //_, err = o.Raw(sql, m.EnPermissionId).Exec()
- err = global.DmSQL["rddp"].Exec(sql, m.EnPermissionId).Error
- return
- }
- func (m *EnPermission) GetItemById(id int) (item *EnPermission, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnPermissionColumns.EnPermissionId)
- //err = o.Raw(sql, id).QueryRow(&item)
- err = global.DmSQL["rddp"].Raw(sql, id).First(&item).Error
- return
- }
- func (m *EnPermission) GetItemByCondition(condition string, pars []interface{}) (item *EnPermission, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 `, m.TableName())
- sql += condition
- sql += ` LIMIT 1`
- //err = o.Raw(sql, pars).QueryRow(&item)
- err = global.DmSQL["rddp"].Raw(sql, pars).First(&item).Error
- return
- }
- func (m *EnPermission) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- //err = o.Raw(sql, pars).QueryRow(&count)
- err = global.DmSQL["rddp"].Raw(sql, pars).Scan(&count).Error
- return
- }
- func (m *EnPermission) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EnPermission, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- //_, err = o.Raw(sql, pars).QueryRows(&items)
- err = global.DmSQL["rddp"].Raw(sql, pars).Find(&items).Error
- return
- }
- func (m *EnPermission) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*EnPermission, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
- //if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
- if err = global.DmSQL["rddp"].Raw(totalSql, pars).Scan(&total).Error; err != nil {
- return
- }
- sql += ` LIMIT ?,?`
- //_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- err = global.DmSQL["rddp"].Raw(sql, pars, startSize, pageSize).Find(&items).Error
- return
- }
- type EnPermissionAddReq struct {
- PermissionName string `description:"品种权限名称"`
- CnPermissionName string `description:"对应的中文权限名称"`
- ParentId int `description:"父级ID"`
- Enabled int `description:"是否可用,1可用,0禁用"`
- }
- type EnPermissionEditReq struct {
- PermissionId int `description:"英文品种权限ID"`
- PermissionName string `description:"品种权限名称"`
- CnPermissionName string `description:"对应的中文权限名称"`
- ParentId int `description:"父级ID"`
- }
- type EnPermissionEnabledReq struct {
- PermissionId int `description:"英文品种权限ID"`
- Enabled int `description:"是否可用,1可用,0禁用"`
- }
- type EnPermissionRemoveReq struct {
- PermissionId int `description:"英文品种权限ID"`
- }
- type EnPermissionPageListResp struct {
- List []*EnPermissionItem
- Paging *paging.PagingItem `description:"分页数据"`
- }
- type EnPermissionItem struct {
- PermissionId int `description:"英文品种权限ID"`
- PermissionName string `description:"品种权限名称"`
- CnPermissionName string `description:"对应的中文权限名称"`
- ParentId int `description:"父级ID"`
- Sort int `description:"排序"`
- CreateTime string `description:"创建时间"`
- Child []*EnPermissionItem
- Enabled int `description:"是否可用,1可用,0禁用"`
- }
- type EnPermissionMoveReq struct {
- PermissionId int `description:"品种id"`
- PrevPermissionId int `description:"上一个兄弟节点品种id"`
- NextPermissionId int `description:"下一个兄弟节点品种id"`
- }
- func GetEnPermissionUnionList(condition string, pars []interface{}) (items []*EnPermission, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`
- SELECT
- *
- FROM
- (
- SELECT * FROM en_permission WHERE 1=1 %s
- UNION
- SELECT * FROM en_permission WHERE en_permission_id IN (
- SELECT parent_id FROM en_permission WHERE parent_id > 0 %s
- )
- ) AS t
- ORDER BY
- parent_id ASC,
- sort ASC,
- create_time ASC`, condition, condition)
- //_, err = o.Raw(sql, pars, pars).QueryRows(&items)
- err = global.DmSQL["rddp"].Raw(sql, pars, pars).Find(&items).Error
- return
- }
- func ClearEnPermissionsByPermissionId(permissionId int) (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- //tx, e := o.Begin()
- tx := global.DmSQL["rddp"].Begin()
- //if e != nil {
- // err = fmt.Errorf("orm begin err: %s", e.Error())
- // return
- //}
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- sql := `DELETE FROM en_classify_permission WHERE en_permission_id = ?`
- //_, e = tx.Raw(sql, permissionId).Exec()
- e := tx.Exec(sql, permissionId).Error
- if e != nil {
- err = fmt.Errorf("delete en_classify_permission err: %s", e.Error())
- return
- }
- sql = `DELETE FROM en_company_permission WHERE en_permission_id = ?`
- e = tx.Exec(sql, permissionId).Error
- if e != nil {
- err = fmt.Errorf("delete en_company_permission err: %s", e.Error())
- return
- }
- return
- }
- // UpdateEnPermissionSortByParentId 根据父类id更新排序
- func UpdateEnPermissionSortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := ` update en_permission set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
- if permissionId > 0 {
- sql += ` or ( en_permission_id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
- }
- //_, err = o.Raw(sql, parentId, nowSort).Exec()
- err = global.DmSQL["rddp"].Exec(sql, parentId, nowSort).Error
- return
- }
- // GetMaxSortByParentId 获取最大的排序值
- func (m *EnPermission) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT max(%s) AS sort FROM %s WHERE %s = ? `, EnPermissionColumns.Sort, m.TableName(), EnPermissionColumns.ParentId)
- //err = o.Raw(sql, parentId).QueryRow(&maxSort)
- err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&maxSort).Error
- return
- }
- // GetMaxSort 获取最大的排序值
- func (m *EnPermission) GetMaxSort() (maxSort int, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT max(%s) AS sort FROM %s`, EnPermissionColumns.Sort, m.TableName())
- //err = o.Raw(sql).QueryRow(&maxSort)
- err = global.DmSQL["rddp"].Raw(sql).Scan(&maxSort).Error
- return
- }
- // GetFirstEnPermissionByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
- func (m *EnPermission) GetFirstEnPermissionByParentId(parentId int) (item *EnPermission, err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? order by sort asc, en_permission_id asc limit 1`, m.TableName(), EnPermissionColumns.ParentId)
- //err = o.Raw(sql, parentId).QueryRow(&item)
- err = global.DmSQL["rddp"].Raw(sql, parentId).First(&item).Error
- return
- }
- // SetEnabled 更新启动禁用
- func (m *EnPermission) SetEnabled(parentId, enabled int) (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ?`, m.TableName(), EnPermissionColumns.Enabled, EnPermissionColumns.ParentId)
- //_, err = o.Raw(sql, enabled, parentId).Exec()
- err = global.DmSQL["rddp"].Exec(sql, enabled, parentId).Error
- return
- }
- // SetEnabledByPermissionId 更新启动禁用
- func (m *EnPermission) SetEnabledByPermissionId(permissionId, enabled int) (err error) {
- //o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ?`, m.TableName(), EnPermissionColumns.Enabled, EnPermissionColumns.EnPermissionId)
- //_, err = o.Raw(sql, enabled, permissionId).Exec()
- err = global.DmSQL["rddp"].Exec(sql, enabled, permissionId).Error
- return
- }
|