Browse Source

Merge branch 'bzq/306_report_classify_cf' of eta_gn_server/eta_api into debug

baoziqiang 4 months ago
parent
commit
3f57b659c2
6 changed files with 178 additions and 13 deletions
  1. 22 2
      controllers/classify.go
  2. 2 1
      controllers/report_v2.go
  3. 42 3
      models/classify.go
  4. 81 0
      models/classify_visible.go
  5. 3 2
      models/report.go
  6. 28 5
      services/classify.go

+ 22 - 2
controllers/classify.go

@@ -76,7 +76,7 @@ func (this *ClassifyController) Add() {
 	}
 
 	// 新增分类
-	err, errMsg, isSentEmail := services.AddReportClassify(req.ClassifyName, req.ParentId, req.ClassifyType, req.IsRemind, req.RemindTime)
+	err, errMsg, isSentEmail := services.AddReportClassify(req.ClassifyName, req.ParentId, req.ClassifyType, req.IsRemind, req.RemindTime, req.VisibleUserIds)
 	if err != nil {
 		br.Msg = "添加失败"
 		if errMsg != "" {
@@ -325,7 +325,7 @@ func (this *ClassifyController) Edit() {
 	}
 
 	// 修改分类
-	err, errMsg, isSentEmail := services.EditReportClassify(req.ClassifyId, req.ClassifyName, req.IsRemind, req.RemindTime)
+	err, errMsg, isSentEmail := services.EditReportClassify(req.ClassifyId, req.ClassifyName, req.IsRemind, req.RemindTime, req.VisibleUserIds)
 	if err != nil {
 		br.Msg = "修改失败"
 		if errMsg != "" {
@@ -576,6 +576,26 @@ func (this *ClassifyController) ListClassify() {
 	}
 	list := originList
 
+	visibleUsers, err := models.GetClassifyVisibleAll()
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+	visibleUserMap := make(map[int][]int)
+
+	for _, v := range visibleUsers {
+		if m, ok := visibleUserMap[v.ClassifyId]; !ok {
+			visibleUserMap[v.ClassifyId] = make([]int, 0)
+			visibleUserMap[v.ClassifyId] = append(visibleUserMap[v.ClassifyId], v.AdminId)
+		} else {
+			visibleUserMap[v.ClassifyId] = append(m, v.AdminId)
+		}
+	}
+
+	for i, v := range list {
+		list[i].VisiableUsers = visibleUserMap[v.Id]
+	}
 	// 指定分类类型(上级中的分类类型可能与最下层的不一致,但是要把上级也一起取出来, 这需求...=_=!)
 	if classifyType > 0 {
 		list = make([]*models.ClassifyList, 0)

+ 2 - 1
controllers/report_v2.go

@@ -12,11 +12,12 @@ import (
 	"eta_gn/eta_api/services/data"
 	"eta_gn/eta_api/utils"
 	"fmt"
-	"github.com/rdlucklib/rdluck_tools/paging"
 	"html"
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
 // ListReport

+ 42 - 3
models/classify.go

@@ -4,9 +4,10 @@ import (
 	"eta_gn/eta_api/global"
 	"eta_gn/eta_api/utils"
 	"fmt"
-	"github.com/rdlucklib/rdluck_tools/paging"
 	"strings"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
 type Classify struct {
@@ -56,6 +57,7 @@ type ClassifyAddReq struct {
 	ClassifyName          string `description:"分类名称"`
 	ParentId              int    `description:"父级分类id,没有父级分类传0"`
 	ChartPermissionIdList []int  `description:"权限id数组"`
+	VisibleUserIds        []int  `description:"可见用户id数组"`
 	ClassifyType          int    `description:"分类类型:1-研报;2-PPT"`
 	IsRemind              int    `description:"是否开启提醒:0-关闭;1-开启"`
 	RemindTime            string `description:"提醒时间:可选00:00-23:59"`
@@ -74,8 +76,44 @@ func GetClassifyById(classifyId int) (item *Classify, err error) {
 }
 
 // 添加分类
-func AddClassify(item *Classify) (err error) {
-	err = global.DmSQL["rddp"].Create(item).Error
+func AddClassify(item *Classify, parentId int, visibleUserIds []int) (err error) {
+	tx := global.DmSQL["rddp"].Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+			return
+		}
+		tx.Commit()
+	}()
+	err = tx.Create(item).Error
+	if err != nil {
+		return
+	}
+	// 添加可见用户
+	if len(visibleUserIds) > 0 {
+		addMappingList := make([]ClassifyVisible, 0)
+		now := time.Now()
+		for _, visibleUserId := range visibleUserIds {
+			addMapping := ClassifyVisible{
+				ClassifyId: item.Id,
+				AdminId:    visibleUserId,
+				CreateTime: now,
+			}
+			addMappingList = append(addMappingList, addMapping)
+		}
+		err = tx.CreateInBatches(addMappingList, utils.MultiAddNum).Error
+		if err != nil {
+			return
+		}
+	}
+	// 删除父级分类可见权限
+	if parentId > 0 {
+		sql := `DELETE FROM classify_visible WHERE classify_id=? `
+		err = tx.Exec(sql, parentId).Error
+		if err != nil {
+			return
+		}
+	}
 	return
 }
 
@@ -159,6 +197,7 @@ type ClassifyList struct {
 	ClassifyMenuList      []*ClassifyMenu `gorm:"-"`
 	ChartPermissionIdList []int           `gorm:"-"`                                   //`description:"绑定的权限ID"`
 	LevelPath             string          `gorm:"column:level_path" json:"level_path"` //`description:"分类的层级路径,英文逗号分隔"`
+	VisiableUsers         []int           `gorm:"-"`
 }
 
 type ClassifyItem struct {

+ 81 - 0
models/classify_visible.go

@@ -0,0 +1,81 @@
+package models
+
+import (
+	"eta_gn/eta_api/global"
+	"eta_gn/eta_api/utils"
+	"time"
+)
+
+type ClassifyVisible struct {
+	ClassifyVisibleId int       `gorm:"column:classify_visible_id;primary_key"`
+	ClassifyId        int       `gorm:"column:classify_id"`
+	AdminId           int       `gorm:"column:admin_id"`
+	CreateTime        time.Time `gorm:"column:create_time"`
+}
+
+func (ClassifyVisible) TableName() string {
+	return "classify_visible"
+}
+
+func GetClassifyVisibleAll() (items []*ClassifyVisible, err error) {
+	sql := "SELECT * FROM classify_visible"
+	err = global.DmSQL["rddp"].Raw(sql).Find(&items).Error
+	return
+}
+
+func ExtendClassifyVisible(parentId, classifyId int, adminIds []int) (err error) {
+	tx := global.DmSQL["rddp"].Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+			return
+		}
+		tx.Commit()
+	}()
+	err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", parentId).Delete(&ClassifyVisible{}).Error
+	insertList := make([]ClassifyVisible, 0, len(adminIds))
+	for _, adminId := range adminIds {
+		classifyVisible := ClassifyVisible{
+			ClassifyId: classifyId,
+			AdminId:    adminId,
+			CreateTime: time.Now(),
+		}
+		insertList = append(insertList, classifyVisible)
+	}
+	err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error
+	return
+}
+
+func UpdateClassifyVisible(classifyId int, adminId []int) (err error) {
+	tx := global.DmSQL["rddp"].Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+			return
+		}
+		tx.Commit()
+	}()
+	err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Delete(&ClassifyVisible{}).Error
+	if err != nil {
+		return
+	}
+	if len(adminId) > 0 {
+		insertList := make([]ClassifyVisible, 0, len(adminId))
+		for _, admin := range adminId {
+			classifyVisible := ClassifyVisible{
+				ClassifyId: classifyId,
+				AdminId:    admin,
+				CreateTime: time.Now(),
+			}
+			insertList = append(insertList, classifyVisible)
+		}
+		err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error
+	}
+	return
+}
+
+func GetClassifyVisibleUserIdByClassifyId(classifyId int) (classifyVisibles []int, err error) {
+	db := global.DmSQL["rddp"]
+	err = db.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Pluck("admin_id", &classifyVisibles).Error
+	return
+}

+ 3 - 2
models/report.go

@@ -6,10 +6,11 @@ import (
 	"eta_gn/eta_api/global"
 	"eta_gn/eta_api/utils"
 	"fmt"
-	"github.com/rdlucklib/rdluck_tools/paging"
-	"gorm.io/gorm"
 	"strings"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
+	"gorm.io/gorm"
 )
 
 // 报告状态

+ 28 - 5
services/classify.go

@@ -193,7 +193,7 @@ func moveReportClassify(classifyInfo, prevClassify, nextClassify *models.Classif
 // @return err error
 // @return errMsg string
 // @return isSendEmail bool
-func AddReportClassify(classifyName string, parentId int, classifyType, isRemind int, remindTime string) (err error, errMsg string, isSendEmail bool) {
+func AddReportClassify(classifyName string, parentId int, classifyType, isRemind int, remindTime string, visibleUserIds []int) (err error, errMsg string, isSendEmail bool) {
 	isSendEmail = true
 	errMsg = `添加失败`
 	item, err := models.GetClassifyByName(classifyName, parentId)
@@ -276,8 +276,7 @@ func AddReportClassify(classifyName string, parentId int, classifyType, isRemind
 	classify.ClassifyType = classifyType
 	classify.IsRemind = isRemind
 	classify.RemindTime = remindTime
-
-	err = models.AddClassify(classify)
+	err = models.AddClassify(classify, parentId, visibleUserIds)
 	if err != nil {
 		return
 	}
@@ -297,8 +296,12 @@ func AddReportClassify(classifyName string, parentId int, classifyType, isRemind
 
 			// 继承父级分类审批流
 			//go inheritReportApproveFlow(parentClassifyItem, classify)
-
 			moveReportByAddClassify(parentClassifyItem, classify)
+			// err = models.UpdateClassifyVisible(classify.Id, visibleUserIds)
+			// if err != nil {
+			// 	errMsg = "添加分类可见权限失败"
+			// 	return
+			// }
 		}
 	}
 
@@ -317,6 +320,21 @@ func AddReportClassify(classifyName string, parentId int, classifyType, isRemind
 	return
 }
 
+// inheritParentClassifyVisibleUser 继承父类的可见用户
+// func inheritParentClassifyVisibleUser(parentClassifyId, currClassifyId int) (err error) {
+// 	parentUserIds, err := models.GetClassifyVisibleUserIdByClassifyId(parentClassifyId)
+// 	if err != nil {
+// 		return
+// 	}
+// 	currUserIds, err := models.GetClassifyVisibleUserIdByClassifyId(currClassifyId)
+// 	if err != nil {
+// 		return
+// 	}
+// 	parentUserIds = append(parentUserIds, currUserIds...)
+// 	err = models.ExtendClassifyVisible(parentClassifyId, currClassifyId, parentUserIds)
+// 	return
+// }
+
 // checkClassifyApprove
 // @Description: 判断分类是否存在待操作的审批单
 // @author: Roc
@@ -660,7 +678,7 @@ func inheritReportApproveFlow(parentClassifyItem, currClassifyItem *models.Class
 // @return err error
 // @return errMsg string
 // @return isSendEmail bool
-func EditReportClassify(classifyId int, classifyName string, isRemind int, remindTime string) (err error, errMsg string, isSendEmail bool) {
+func EditReportClassify(classifyId int, classifyName string, isRemind int, remindTime string, visibleUserIds []int) (err error, errMsg string, isSendEmail bool) {
 	isSendEmail = true
 	errMsg = `修改失败`
 
@@ -713,6 +731,11 @@ func EditReportClassify(classifyId int, classifyName string, isRemind int, remin
 	if err != nil {
 		return
 	}
+	err = models.UpdateClassifyVisible(item.Id, visibleUserIds)
+	if err != nil {
+		errMsg = "更新可见权限失败"
+		return
+	}
 	return
 }