238 lines
5.7 KiB
Go
238 lines
5.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"server/models"
|
|
"server/pkg/identity"
|
|
"server/pkg/jwtutil"
|
|
"server/services"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
func employeeFileMenuIDs() map[uint64]bool {
|
|
ids := map[uint64]bool{}
|
|
if models.Orm == nil {
|
|
return ids
|
|
}
|
|
var rows []models.SystemMenu
|
|
if _, err := models.Orm.QueryTable(new(models.SystemMenu)).
|
|
Filter("status", 1).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows); err != nil {
|
|
return ids
|
|
}
|
|
|
|
pidOf := make(map[uint64]int64, len(rows))
|
|
hit := map[uint64]bool{}
|
|
for _, m := range rows {
|
|
pidOf[m.ID] = m.Pid
|
|
hay := strings.ToLower(m.Title + " " + derefString(m.Path) + " " + derefString(m.ComponentPath))
|
|
if strings.Contains(hay, "employeefile") || strings.Contains(hay, "employee-file") ||
|
|
strings.Contains(m.Title, "员工档案") || strings.Contains(m.Title, "人事档案") {
|
|
hit[m.ID] = true
|
|
}
|
|
}
|
|
if len(hit) == 0 {
|
|
return ids
|
|
}
|
|
|
|
for id := range hit {
|
|
cur := id
|
|
for i := 0; i < 32; i++ {
|
|
if _, ok := pidOf[cur]; !ok {
|
|
break
|
|
}
|
|
ids[cur] = true
|
|
pid := pidOf[cur]
|
|
if pid <= 0 {
|
|
break
|
|
}
|
|
cur = uint64(pid)
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func canViewAllEmployeeFiles(claims *jwtutil.Claims) bool {
|
|
if claims == nil || claims.TenantId <= 0 {
|
|
return false
|
|
}
|
|
if claims.UserType == "platform" {
|
|
return true
|
|
}
|
|
if isTenantAdmin(claims.UserID, claims.TenantId) {
|
|
return true
|
|
}
|
|
|
|
var tu models.SystemTenantUser
|
|
if models.Orm.QueryTable(new(models.SystemTenantUser)).
|
|
Filter("uid", tenantUIDOf(claims)).
|
|
Filter("tid", claims.TenantId).
|
|
Filter("delete_time__isnull", true).One(&tu) != nil {
|
|
return false
|
|
}
|
|
if tu.GroupID == 0 {
|
|
return true
|
|
}
|
|
var role models.AdminRole
|
|
if models.Orm.QueryTable(new(models.AdminRole)).
|
|
Filter("id", tu.GroupID).
|
|
Filter("cid", 2).One(&role) != nil {
|
|
return false
|
|
}
|
|
if strings.TrimSpace(role.Name) == "租户管理员" {
|
|
return true
|
|
}
|
|
rights := ""
|
|
if role.Rights != nil {
|
|
rights = strings.TrimSpace(*role.Rights)
|
|
}
|
|
if rights == "" || rights == "[]" || rights == "null" {
|
|
return true
|
|
}
|
|
|
|
allowed := services.ParseMenuRights(rights)
|
|
if len(allowed) == 0 {
|
|
return true
|
|
}
|
|
for id := range employeeFileMenuIDs() {
|
|
if allowed[id] {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func tenantUIDOf(claims *jwtutil.Claims) uint64 {
|
|
return services.ResolveTenantUID(uint64(claims.TenantId), uint64(claims.UserID))
|
|
}
|
|
|
|
func (c *AppOaController) EmployeeFileAccess() {
|
|
claims, err := c.oaClaims()
|
|
if err != nil {
|
|
code := identity.Status(err)
|
|
c.oaErr(code, code, err.Error())
|
|
return
|
|
}
|
|
c.oaOk(map[string]interface{}{"can_view_all": canViewAllEmployeeFiles(claims)})
|
|
}
|
|
|
|
func (c *AppOaController) EmployeeFileList() {
|
|
claims, err := c.oaClaims()
|
|
if err != nil {
|
|
code := identity.Status(err)
|
|
c.oaErr(code, code, err.Error())
|
|
return
|
|
}
|
|
if !canViewAllEmployeeFiles(claims) {
|
|
c.oaErr(403, 403, "您没有查看全部员工档案的权限")
|
|
return
|
|
}
|
|
tid := claims.TenantId
|
|
|
|
page, _ := c.GetInt("page", 1)
|
|
pageSize, _ := c.GetInt("pageSize", 20)
|
|
keyword := strings.TrimSpace(c.GetString("keyword"))
|
|
statusRaw := strings.TrimSpace(c.GetString("status"))
|
|
orgID, _ := c.GetUint64("org_id")
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 20
|
|
}
|
|
|
|
web := &BackendEmployeeFileController{}
|
|
qs := web.efQuery(tid)
|
|
|
|
if orgID > 0 {
|
|
empIDs := web.efEmployeeIDsInOrgs(tid, web.efSubtreeOrgIDs(tid, orgID))
|
|
if len(empIDs) == 0 {
|
|
c.oaOk(map[string]interface{}{"list": []employeeFileDTO{}, "total": 0, "page": page, "pageSize": pageSize})
|
|
return
|
|
}
|
|
qs = qs.Filter("employee_id__in", empIDs)
|
|
}
|
|
if statusRaw != "" {
|
|
status, convErr := strconv.Atoi(statusRaw)
|
|
if convErr != nil || status < 1 || status > 3 {
|
|
c.oaErr(400, 400, "在职状态参数无效")
|
|
return
|
|
}
|
|
qs = qs.Filter("employment_status", int8(status))
|
|
}
|
|
if keyword != "" {
|
|
empIDs := web.efEmployeeIDsByKeyword(tid, keyword)
|
|
cond := orm.NewCondition().
|
|
Or("file_no__icontains", keyword).
|
|
Or("id_card__icontains", keyword)
|
|
if len(empIDs) > 0 {
|
|
cond = cond.Or("employee_id__in", empIDs)
|
|
}
|
|
qs = qs.SetCond(orm.NewCondition().AndCond(cond))
|
|
}
|
|
|
|
total, _ := qs.Count()
|
|
var files []models.BackendEmployeeFile
|
|
if _, err := qs.OrderBy("-id").Limit(pageSize).Offset((page - 1) * pageSize).All(&files); err != nil && err != orm.ErrNoRows {
|
|
c.oaErr(500, 500, "查询失败")
|
|
return
|
|
}
|
|
|
|
c.oaOk(map[string]interface{}{
|
|
"list": web.efAssembleDTOList(tid, files),
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": pageSize,
|
|
})
|
|
}
|
|
|
|
func (c *AppOaController) EmployeeFileDetail() {
|
|
claims, err := c.oaClaims()
|
|
if err != nil {
|
|
code := identity.Status(err)
|
|
c.oaErr(code, code, err.Error())
|
|
return
|
|
}
|
|
tid := claims.TenantId
|
|
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
c.oaErr(400, 400, "无效的档案ID")
|
|
return
|
|
}
|
|
|
|
web := &BackendEmployeeFileController{}
|
|
var file models.BackendEmployeeFile
|
|
if web.efQuery(tid).Filter("id", id).One(&file) != nil {
|
|
c.oaErr(404, 404, "档案不存在")
|
|
return
|
|
}
|
|
|
|
if !canViewAllEmployeeFiles(claims) {
|
|
emp, ok := c.oaMyEmployee(claims)
|
|
if !ok || uint64(emp.ID) != file.EmployeeID {
|
|
c.oaErr(403, 403, "您没有查看他人档案的权限")
|
|
return
|
|
}
|
|
}
|
|
|
|
var records []models.BackendEmployeeFileRecord
|
|
if _, err := models.Orm.QueryTable(new(models.BackendEmployeeFileRecord)).
|
|
Filter("tid", tid).
|
|
Filter("is_deleted", 0).
|
|
Filter("employee_id", file.EmployeeID).
|
|
OrderBy("sort", "id").
|
|
All(&records); err != nil && err != orm.ErrNoRows {
|
|
c.oaErr(500, 500, "查询档案记录失败")
|
|
return
|
|
}
|
|
if records == nil {
|
|
records = []models.BackendEmployeeFileRecord{}
|
|
}
|
|
dtos := web.efAssembleDTOList(tid, []models.BackendEmployeeFile{file})
|
|
c.oaOk(map[string]interface{}{"file": dtos[0], "records": records})
|
|
} |