123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package dept
- import (
- "errors"
- "strings"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/redis"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- )
- const cacheKey = "dept_list_cache"
- var Dept models.Dept
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Dept, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return Dept.GetMulti(where, fields, retVal)
- }
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(Dept.TableName(), where)
- }
- func Add(form form.DeptAdd) (int64, error) {
- if CheckDeptDuplicate(form.Name, form.Pid) {
- return 0, errors.New("department already exists")
- }
- mapDept := map[string]interface{}{
- "name": form.Name,
- "pid": form.Pid,
- "attribute": form.Attribute,
- }
- deptID, err := db.InsertOne(Dept.TableName(), mapDept)
- redis.Del(cacheKey)
- if err != nil {
- return 0, nil
- }
- return deptID, nil
- }
- func EditByID(form form.DeptAdd, id int) error {
- deptInfo, err := GetInfoByID(id)
- if deptInfo == nil {
- return errors.New("invalid department id")
- }
- if deptInfo.Name != form.Name && CheckDeptDuplicate(form.Name, form.Pid) {
- return errors.New("dept already exists")
- }
- mapDept := map[string]interface{}{
- "name": form.Name,
- "pid": form.Pid,
- "attribute": form.Attribute,
- }
- _, err = db.Update(Dept.TableName(), map[string]interface{}{"id": id}, mapDept)
- redis.Del(cacheKey)
- return err
- }
- func CheckDeptDuplicate(name string, pid int) bool {
- deptInfo, err := GetOne(map[string]interface{}{"name": name, "pid": pid}, nil, nil)
- return deptInfo != nil && err == nil
- }
- func DeleteByID(id int) error {
- authInfo, _ := GetInfoByID(id)
- if authInfo == nil {
- return errors.New("invalid department id")
- }
- _, err := db.Delete(Dept.TableName(), map[string]interface{}{"id": id})
- redis.Del(cacheKey)
- return err
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Dept, error) {
- return Dept.GetOne(where, fields, retVal)
- }
- func GetInfoByID(id int) (*models.Dept, error) {
- return GetOne(map[string]interface{}{"id": id}, nil, nil)
- }
- func GetListByCache() ([]*models.Dept, error) {
- res, err := redis.Get(cacheKey)
- var deptList []*models.Dept
- if err != nil {
- deptList, err = GetList(nil, nil, app.Page{}, nil)
- if err != nil {
- return nil, err
- }
- redis.Set(cacheKey, deptList, 3600)
- } else {
- utils.JsonDecode(res).To(&deptList)
- }
- return deptList, nil
- }
- func GetSubDepts(deptID int) []*models.Dept {
- deptList, err := GetListByCache()
- if err != nil {
- return nil
- }
- list := make([]*models.Dept, 0)
- for _, v := range deptList {
- if v.ID == deptID {
- list = append(list, v)
- } else if v.Pid == deptID {
- list = append(list, GetSubDepts(v.ID)...)
- }
- }
- return list
- }
- func GetSubDeptIds(deptID int, deptIds []int) []int {
- deptList, err := GetListByCache()
- if err != nil {
- return []int{}
- }
- for _, v := range deptList {
- if v.Pid == deptID {
- deptIds = GetSubDeptIds(v.ID, append(deptIds, v.ID))
- }
- }
- return deptIds
- }
- func GetParentDeptIds(deptID int, parentIds []int) []int {
- deptList, err := GetListByCache()
- if err != nil {
- return []int{}
- }
- for _, v := range deptList {
- if v.ID == deptID && v.Pid > 0 {
- parentIds = GetParentDeptIds(v.Pid, append(parentIds, v.Pid))
- }
- }
- return parentIds
- }
- func GetDeptFullName(deptID int, sep string) string {
- if deptID == 0 {
- return ""
- }
- deptIds := GetParentDeptIds(deptID, []int{deptID})
- deptList, err := GetListByCache()
- if err != nil {
- return ""
- }
- deptMap := make(map[int]string, 0)
- for _, v := range deptList {
- deptMap[v.ID] = v.Name
- }
- deptNames := make([]string, 0)
- for i := len(deptIds) - 1; i >= 0; i-- {
- deptNames = append(deptNames, deptMap[deptIds[i]])
- }
- return strings.Join(deptNames, sep)
- }
|