Browse Source

fix:添加分类和权限之间的接口

zqbao 9 months ago
parent
commit
3e4a4d6dee

+ 22 - 2
controllers/chart_permission.go

@@ -190,6 +190,7 @@ func (this *ChartPermissionController) Private() {
 // @Title 获取品种权限根据分类id
 // @Description 获取品种权限根据分类id
 // @Param   ClassifyId   query   int  true       "二级分类的id"
+// @Param   Scope   query   int  true       "0:获取二级权限,1:获取一级权限,2:获取所有权限"
 // @Success 200 {object} []models.ChartPermission
 // @router /classify/detail [get]
 func (this *ChartPermissionController) DetailByClassify() {
@@ -204,18 +205,37 @@ func (this *ChartPermissionController) DetailByClassify() {
 		br.ErrMsg = "参数错误,分类id小于等于0"
 		return
 	}
+	scope, _ := this.GetInt("Scope")
 	reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(classifyId)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
 		return
 	}
-	chartPermissionList, err := models.GetChartPermissionListByIds(reportChartPermissionIds)
+	chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
 		return
 	}
+	if scope == 1 || scope == 2 {
+		fisrtPermissionIds := make([]string, 0)
+		for _, chartPermission := range chartPermissionList {
+			fisrtPermissionIds = append(fisrtPermissionIds, strconv.Itoa(chartPermission.ParentId))
+		}
+		fisrtPermissionIds = utils.Unique(fisrtPermissionIds)
+		firstPermissionList, err := models.GetChartPermissionListByStrIds(fisrtPermissionIds)
+		if err != nil {
+			br.Msg = "一级品种获取失败"
+			br.ErrMsg = "一级品种获取失败,系统错误,Err:" + err.Error()
+			return
+		}
+		if scope == 1 {
+			chartPermissionList = firstPermissionList
+		} else if scope == 2 {
+			chartPermissionList = append(chartPermissionList, firstPermissionList...)
+		}
+	}
 
 	br.Data = chartPermissionList
 	br.Msg = "权限列表获取成功"
@@ -257,7 +277,7 @@ func (this *ChartPermissionController) Detail() {
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
 		return
 	}
-	chartPermissionList, err := models.GetChartPermissionListByIds(reportChartPermissionIds)
+	chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()

+ 69 - 0
controllers/classify.go

@@ -0,0 +1,69 @@
+package controllers
+
+import (
+	"encoding/json"
+	"eta/eta_mini_bridge/models"
+	"eta/eta_mini_bridge/models/request"
+	"eta/eta_mini_bridge/utils"
+	"strconv"
+)
+
+type ClassifyController struct {
+	BaseAuthController
+}
+
+// @Title 根据分类id获取一级品种
+// @Description 根据分类id获取一级品种
+// @Param   ClassifyId   query   int  true       "二级分类ID"
+// @Success 200 {object} response.ReportDetailResp
+// @router /chart_permission/first [get]
+func (this *ClassifyController) FirstChartPermission() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	var req request.ClassifyReq
+	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,Err:" + err.Error()
+		return
+	}
+	if len(req.ClassifyIds) == 0 {
+		br.Msg = "分类id错误,获取失败"
+		return
+	}
+	classifyId, _ := this.GetInt("ClassifyId", 0)
+	if classifyId <= 0 {
+		br.Msg = "分类id错误,获取失败"
+		return
+	}
+	chartPermissionIds, err := models.GetChartPermissionIdsListByClassifyIds(req.ClassifyIds)
+	if err != nil {
+		br.Msg = "二级品种获取失败"
+		br.ErrMsg = "二级品种id获取失败,系统错误,Err:" + err.Error()
+		return
+	}
+	chartPermissionList, err := models.GetChartPermissionListByIds(chartPermissionIds)
+	if err != nil {
+		br.Msg = "二级品种获取失败"
+		br.ErrMsg = "二级品种获取失败,系统错误,Err:" + err.Error()
+		return
+	}
+	fisrtPermissionIds := make([]string, 0)
+	for _, chartPermission := range chartPermissionList {
+		fisrtPermissionIds = append(fisrtPermissionIds, strconv.Itoa(chartPermission.ParentId))
+	}
+	fisrtPermissionIds = utils.Unique(fisrtPermissionIds)
+	firstPermissionList, err := models.GetChartPermissionListByStrIds(fisrtPermissionIds)
+	if err != nil {
+		br.Msg = "一级品种获取失败"
+		br.ErrMsg = "一级品种获取失败,系统错误,Err:" + err.Error()
+		return
+	}
+
+	br.Data = firstPermissionList
+	br.Success = true
+	br.Ret = 200
+	br.Msg = "获取成功"
+}

+ 5 - 5
controllers/report.go

@@ -240,7 +240,7 @@ func (this *ReportController) Detail() {
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
 		return
 	}
-	chartPermissionList, err := models.GetChartPermissionListByIds(reportChartPermissionIds)
+	chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
@@ -415,7 +415,7 @@ func (this *ReportController) Today() {
 	}
 	classifyIds = utils.Unique(classifyIds)
 	// 获取二级分类和二级品种权限的映射
-	chartPermissionMapping, err := models.GetChartPermissionIdsListByClassifyIds(classifyIds)
+	chartPermissionMapping, err := models.GetChartPermissionListByClassifyStrIds(classifyIds)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
@@ -428,7 +428,7 @@ func (this *ReportController) Today() {
 		chartPermissionIds = append(chartPermissionIds, strconv.Itoa(v.ChartPermissionId))
 	}
 	// 获取二级品种的权限,并建立映射
-	chartPermissionList2, err := models.GetChartPermissionListByIds(chartPermissionIds)
+	chartPermissionList2, err := models.GetChartPermissionListByStrIds(chartPermissionIds)
 	if err != nil {
 		br.Msg = "获取研报二级品种权限失败"
 		br.ErrMsg = "获取研报二级品种权限失败,Err:" + err.Error()
@@ -512,7 +512,7 @@ func (this *ReportController) RecentList() {
 	}
 	classifyIds = utils.Unique(classifyIds)
 	// 获取二级分类和二级品种权限的映射
-	chartPermissionMapping, err := models.GetChartPermissionIdsListByClassifyIds(classifyIds)
+	chartPermissionMapping, err := models.GetChartPermissionListByClassifyStrIds(classifyIds)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
@@ -525,7 +525,7 @@ func (this *ReportController) RecentList() {
 		chartPermissionIds = append(chartPermissionIds, strconv.Itoa(v.ChartPermissionId))
 	}
 	// 获取二级品种的权限,并建立映射
-	chartPermissionList2, err := models.GetChartPermissionListByIds(chartPermissionIds)
+	chartPermissionList2, err := models.GetChartPermissionListByStrIds(chartPermissionIds)
 	if err != nil {
 		br.Msg = "获取研报二级品种权限失败"
 		br.ErrMsg = "获取研报二级品种权限失败,Err:" + err.Error()

+ 1 - 1
controllers/wechat.go

@@ -58,7 +58,7 @@ func (this *WeChatController) SendTemplateMsg() {
 		br.Msg = "该研报所在分类未关联品种权限,不能推送"
 		return
 	}
-	chartPermissionList, err := models.GetChartPermissionListByIds(reportChartPermissionIdStrs)
+	chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIdStrs)
 	if err != nil {
 		br.Msg = "获取研报权限失败"
 		br.ErrMsg = "获取研报权限失败,Err:" + err.Error()

+ 20 - 2
models/chart_permission.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"eta/eta_mini_bridge/utils"
 	"fmt"
 	"strings"
 	"time"
@@ -66,8 +67,8 @@ func GetChildChartPermissionListById(id int) (items []*ChartPermission, err erro
 	return
 }
 
-// GetChartPermissionListByIds 根据品种ids获取品种权限列表
-func GetChartPermissionListByIds(chartPermissionIds []string) (items []*ChartPermission, err error) {
+// GetChartPermissionListByStrIds 根据品种ids获取品种权限列表
+func GetChartPermissionListByStrIds(chartPermissionIds []string) (items []*ChartPermission, err error) {
 	// 没有品种权限就返回空
 	if len(chartPermissionIds) == 0 {
 		return
@@ -83,6 +84,23 @@ func GetChartPermissionListByIds(chartPermissionIds []string) (items []*ChartPer
 	return
 }
 
+// GetChartPermissionListByIds 根据品种ids获取品种权限列表
+func GetChartPermissionListByIds(chartPermissionIds []int) (items []*ChartPermission, err error) {
+	// 没有品种权限就返回空
+	if len(chartPermissionIds) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `SELECT * FROM chart_permission WHERE enabled=1 AND product_id=1 `
+	if len(chartPermissionIds) > 0 {
+		sql += fmt.Sprintf(" AND chart_permission_id IN (%s) ", utils.GetOrmReplaceHolder(len(chartPermissionIds)))
+	}
+	sql += ` ORDER BY sort ASC `
+
+	_, err = o.Raw(sql, chartPermissionIds).QueryRows(&items)
+	return
+}
+
 // GetChartPermissionById 根据品种id获取品种权限
 func GetChartPermissionById(chartPermissionId int) (item *ChartPermission, err error) {
 	o := orm.NewOrmUsingDB("rddp")

+ 28 - 1
models/chart_permission_search_key_word_mapping.go

@@ -32,7 +32,21 @@ func GetChartPermissionIdsListByClassifyId(classifyId int) (chartPermissionIds [
 	return
 }
 
-func GetChartPermissionIdsListByClassifyIds(classifyIds []string) (items []*ChartPermissionSearchKeyWordMapping, err error) {
+func GetChartPermissionIdsListByClassifyIds(classifyId []int) (chartPermissionIds []int, err error) {
+	if len(classifyId) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` SELECT chart_permission_id
+		FROM chart_permission_search_key_word_mapping	
+		WHERE classify_id = (%s) `
+	sql = fmt.Sprintf(sql, utils.GetOrmReplaceHolder(len(classifyId)))
+
+	_, err = o.Raw(sql, classifyId).QueryRows(&chartPermissionIds)
+	return
+}
+
+func GetChartPermissionListByClassifyStrIds(classifyIds []string) (items []*ChartPermissionSearchKeyWordMapping, err error) {
 	if len(classifyIds) == 0 {
 		return
 	}
@@ -47,6 +61,19 @@ func GetChartPermissionIdsListByClassifyIds(classifyIds []string) (items []*Char
 	return
 }
 
+func GetChartPermissionListByClassifyIds(classifyIds []int) (items []*ChartPermissionSearchKeyWordMapping, err error) {
+	if len(classifyIds) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` SELECT * FROM chart_permission_search_key_word_mapping	WHERE 1=1 `
+	if len(classifyIds) > 0 {
+		sql += fmt.Sprintf(" AND classify_id in (%s) ", utils.GetOrmReplaceHolder(len(classifyIds)))
+	}
+	_, err = o.Raw(sql, classifyIds).QueryRows(&items)
+	return
+}
+
 func GetClassifyIdsListByIds(chartPermissionIds []int) (classifyIds []int, err error) {
 	if len(chartPermissionIds) == 0 {
 		return

+ 5 - 0
models/request/classify.go

@@ -0,0 +1,5 @@
+package request
+
+type ClassifyReq struct {
+	ClassifyIds []int `description:"二级分类id"`
+}

+ 9 - 0
routers/commentsRouter.go

@@ -106,6 +106,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ClassifyController"] = append(beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ClassifyController"],
+        beego.ControllerComments{
+            Method: "FirstChartPermission",
+            Router: `/chart_permission/first`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ReportController"] = append(beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ReportController"],
         beego.ControllerComments{
             Method: "Today",

+ 5 - 0
routers/router.go

@@ -43,6 +43,11 @@ func init() {
 				&controllers.WeChatController{},
 			),
 		),
+		web.NSNamespace("/classify",
+			web.NSInclude(
+				&controllers.ClassifyController{},
+			),
+		),
 	)
 	web.AddNamespace(ns)
 }

+ 0 - 32
services/user_permission.go

@@ -1,32 +0,0 @@
-package services
-
-import (
-	"errors"
-	"eta/eta_mini_bridge/utils"
-	"strconv"
-)
-
-func CheckUserPermission(userId int) (status int, err error) {
-	if userId > 0 {
-		user, err := GetUserItemByUserId(userId)
-		if err != nil {
-			status = 40001
-			if err.Error() == utils.ErrNoRow() {
-				err = errors.New("用户信息不存在:userId:" + strconv.Itoa(userId))
-				return status, err
-			}
-			err = errors.New("获取用户信息失败:userId:" + strconv.Itoa(userId) + ";Err:" + err.Error())
-			return status, err
-		}
-		if user == nil {
-			status = 40001
-			err = errors.New("获取用户信息失败:userId:" + strconv.Itoa(userId))
-			return status, err
-		}
-
-	} else {
-		status = 40001
-		err = errors.New("用户id错误")
-	}
-	return
-}