xyxie 11 kuukautta sitten
vanhempi
commit
846430105a

+ 209 - 20
controllers/report_chapter_type.go

@@ -39,15 +39,33 @@ func (this *ReportChapterTypeController) List() {
 		br.Msg = "请选择报告类型"
 		return
 	}
-	var req services.ChapterTypeReq
-	req.ReportType = reportType
-	resp, e := services.GetReportChapterType(req)
+	cond := ` AND research_type = ?`
+	pars := make([]interface{}, 0)
+	pars = append(pars, reportType)
+	list, e := models.GetReportChapterTypePageList(cond, pars)
 	if e != nil {
 		br.Msg = "获取失败"
-		br.ErrMsg = "获取章节列表失败, Err: " + e.Error()
+		br.ErrMsg = "获取报告章节列表失败, Err: " + e.Error()
 		return
 	}
+	respList := make([]*models.ReportChapterTypeListItem, 0)
+	for i := range list {
+		respList = append(respList, &models.ReportChapterTypeListItem{
+			ReportChapterTypeId:   list[i].ReportChapterTypeId,
+			ReportChapterTypeName: list[i].ReportChapterTypeName,
+			Sort:                  list[i].Sort,
+			CreatedTime:           list[i].CreatedTime.Format(utils.FormatDateTime),
+			ResearchType:          list[i].ResearchType,
+			SelectedImage:         list[i].SelectedImage,
+			UnselectedImage:       list[i].UnselectedImage,
+			WordsImage:            list[i].YbBottomIcon, // 此处的不一样
+			EditImgUrl:            list[i].EditImgUrl,
+			IsShow:                list[i].IsShow,
+		})
+	}
 
+	resp := new(models.ReportChapterTypeListResp)
+	resp.List = respList
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
@@ -90,10 +108,84 @@ func (this *ReportChapterTypeController) Add() {
 		return
 	}
 	// 重名校验
-	e, msg := services.AddReportChapterType(&req)
+	cond := ` AND report_chapter_type_name = ? AND research_type = ?`
+	pars := make([]interface{}, 0)
+	pars = append(pars, req.ReportChapterTypeName, req.ResearchType)
+	exists, e := models.GetReportChapterTypeByCondition(cond, pars)
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取重名报告章节失败, Err:" + e.Error()
+		return
+	}
+	if exists != nil {
+		br.Msg = "章节名称已存在"
+		return
+	}
+
+	nowTime := time.Now().Local()
+	item := new(models.ReportChapterType)
+	maxSort, e := item.GetMaxSort()
 	if e != nil {
-		br.Msg = msg
-		br.ErrMsg = "新增章节失败, Err: " + e.Error()
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取章节最大排序失败, Err:" + e.Error()
+		return
+	}
+	item.ReportChapterTypeName = req.ReportChapterTypeName
+	item.Sort = maxSort + 1
+	item.Enabled = 1
+	item.CreatedTime = nowTime
+	item.LastUpdatedTime = nowTime
+	item.ResearchType = req.ResearchType
+	item.IsSet = 0
+	item.ReportChapterTypeKey = req.ReportChapterTypeName
+	item.TickerTitle = req.ReportChapterTypeName
+
+	if e = item.Create(); e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "新增报告章节失败, Err:" + e.Error()
+		return
+	}
+
+	// todo 更新章节权限
+	cond = ` and product_id=1`
+	pars = make([]interface{}, 0)
+	permissionList, e := services.GetChartPermissionList(cond, pars)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
+		return
+	}
+	permissionIdName := make(map[int]string)
+	for i := range permissionList {
+		permissionIdName[permissionList[i].ChartPermissionId] = permissionList[i].PermissionName
+	}
+
+	researchType := item.ResearchType
+	newPermissions := make([]*models.ReportChapterTypePermission, 0)       // 报告章节权限表(新)
+	newWeekPermissions := make([]*models.ChartPermissionChapterMapping, 0) // 报告章节权限表(老)
+	for i := range req.ChartPermissionIdList {
+		newPermissions = append(newPermissions, &models.ReportChapterTypePermission{
+			ReportChapterTypeId:   item.ReportChapterTypeId,
+			ReportChapterTypeName: item.ReportChapterTypeName,
+			ChartPermissionId:     req.ChartPermissionIdList[i],
+			PermissionName:        permissionIdName[req.ChartPermissionIdList[i]],
+			ResearchType:          researchType,
+			CreatedTime:           nowTime,
+		})
+		if researchType == utils.REPORT_TYPE_WEEK {
+			newWeekPermissions = append(newWeekPermissions, &models.ChartPermissionChapterMapping{
+				ChartPermissionId:   req.ChartPermissionIdList[i],
+				ReportChapterTypeId: item.ReportChapterTypeId,
+				ResearchType:        researchType,
+			})
+		}
+	}
+
+	// 设置权限
+	e = models.SetReportChapterTypePermission(item.ReportChapterTypeId, researchType, newPermissions, newWeekPermissions)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "设置章节类型权限失败, Err: " + e.Error()
 		return
 	}
 	// 清除小程序端的章节缓存
@@ -102,6 +194,13 @@ func (this *ReportChapterTypeController) Add() {
 		_ = utils.Rc.Delete(key)
 	}
 
+	//todo 同步更新crm章节权限和章节类型
+	go func() {
+		var syncReq services.ChapterTypeSyncReq
+		syncReq.ResearchType = researchType
+		syncReq.ReportChapterTypeId = item.ReportChapterTypeId
+		_, _ = services.ReportChapterTypeSync(&syncReq)
+	}()
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "操作成功"
@@ -146,14 +245,74 @@ func (this *ReportChapterTypeController) Edit() {
 		br.Msg = "请选择报告类型"
 		return
 	}
-	ret, e, msg := services.EditReportChapterType(&req)
+	// 重名校验
+	cond := ` AND report_chapter_type_name = ? AND research_type = ?`
+	pars := make([]interface{}, 0)
+	pars = append(pars, req.ReportChapterTypeName, req.ResearchType)
+	exists, e := models.GetReportChapterTypeByCondition(cond, pars)
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取重名报告章节失败, Err:" + e.Error()
+		return
+	}
+	if exists != nil && exists.ReportChapterTypeId != req.ReportChapterTypeId {
+		br.Msg = "章节名称已存在"
+		return
+	}
+
+	item, e := models.GetReportChapterTypeById(req.ReportChapterTypeId)
 	if e != nil {
-		br.Msg = msg
-		br.ErrMsg = "编辑章节失败, Err: " + e.Error()
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取报告章节失败, Err:" + e.Error()
+		return
+	}
+	originName := item.ReportChapterTypeName
+	//更新章节权限
+	// todo 更新章节权限
+	cond = ` and product_id=1`
+	pars = make([]interface{}, 0)
+	permissionList, e := services.GetChartPermissionList(cond, pars)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
+		return
+	}
+	permissionIdName := make(map[int]string)
+	nowTime := time.Now().Local()
+	for i := range permissionList {
+		permissionIdName[permissionList[i].ChartPermissionId] = permissionList[i].PermissionName
+	}
+
+	researchType := item.ResearchType
+	newPermissions := make([]*models.ReportChapterTypePermission, 0)       // 报告章节权限表(新)
+	newWeekPermissions := make([]*models.ChartPermissionChapterMapping, 0) // 报告章节权限表(老)
+	for i := range req.ChartPermissionIdList {
+		newPermissions = append(newPermissions, &models.ReportChapterTypePermission{
+			ReportChapterTypeId:   item.ReportChapterTypeId,
+			ReportChapterTypeName: item.ReportChapterTypeName,
+			ChartPermissionId:     req.ChartPermissionIdList[i],
+			PermissionName:        permissionIdName[req.ChartPermissionIdList[i]],
+			ResearchType:          researchType,
+			CreatedTime:           nowTime,
+		})
+		if researchType == utils.REPORT_TYPE_WEEK {
+			newWeekPermissions = append(newWeekPermissions, &models.ChartPermissionChapterMapping{
+				ChartPermissionId:   req.ChartPermissionIdList[i],
+				ReportChapterTypeId: item.ReportChapterTypeId,
+				ResearchType:        researchType,
+			})
+		}
+	}
+
+	// 设置权限
+	e = models.SetReportChapterTypePermission(item.ReportChapterTypeId, researchType, newPermissions, newWeekPermissions)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "设置章节类型权限失败, Err: " + e.Error()
 		return
 	}
 	// 更新研报章节表冗余
-	if ret.OriginReportChapterTypeName != req.ReportChapterTypeName {
+	if originName != req.ReportChapterTypeName {
 		go func() {
 			_ = models.UpdateReportChapterTypeNameByTypeId(req.ReportChapterTypeId, req.ReportChapterTypeName)
 		}()
@@ -165,6 +324,12 @@ func (this *ReportChapterTypeController) Edit() {
 		_ = utils.Rc.Delete(key)
 	}
 
+	go func() {
+		var syncReq services.ChapterTypeSyncReq
+		syncReq.ResearchType = researchType
+		syncReq.ReportChapterTypeId = item.ReportChapterTypeId
+		_, _ = services.ReportChapterTypeSync(&syncReq)
+	}()
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "操作成功"
@@ -282,11 +447,12 @@ func (this *ReportChapterTypeController) AuthSetting() {
 		br.Msg = "章节不存在或已被删除"
 		return
 	}
-	// todo 测试品种在报告中的使用
-	permissionList, e := services.GetChartPermissionList("", []interface{}{})
+	cond := ` and product_id=1`
+	pars := make([]interface{}, 0)
+	permissionList, e := services.GetChartPermissionList(cond, pars)
 	if e != nil {
-		br.Msg = "操作失败"
-		br.ErrMsg = "获取权限列表失败, Err: " + e.Error()
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
 		return
 	}
 	permissionIdName := make(map[int]string)
@@ -425,7 +591,10 @@ func (this *ReportChapterTypeController) Move() {
 		br.ErrMsg = "移动品种失败, Err: " + e.Error()
 		return
 	}
-
+	go func() {
+		var syncReq services.ChapterTypeSyncReq
+		_, _ = services.ReportChapterTypeSync(&syncReq)
+	}()
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "操作成功"
@@ -468,13 +637,33 @@ func (this *ReportChapterTypeController) SetEnabled() {
 		br.Msg = "请选择正确的启用禁用状态"
 		return
 	}
-	e := services.SetEnabledReportChapterType(&req)
-	if e != nil {
+	ob := new(models.ReportChapterType)
+	item, err := models.GetReportChapterTypeById(req.ReportChapterTypeId)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			br.Msg = "章节不存在"
+			br.ErrMsg = "章节不存在, Err:" + err.Error()
+			return
+		}
+		br.Msg = "获取章节信息失败"
+		br.ErrMsg = "获取章节信息失败, Err:" + err.Error()
+		return
+	}
+	if item == nil {
+		br.Msg = "章节不存在"
+		return
+	}
+	//设置分类启用、禁用状态
+	err = ob.SetEnabled(req.ReportChapterTypeId, req.Enabled)
+	if err != nil {
 		br.Msg = "操作失败"
-		br.ErrMsg = "设置报告章节状态失败, Err: " + e.Error()
+		br.ErrMsg = "设置报告章节状态失败, Err: " + err.Error()
 		return
 	}
-
+	go func() {
+		var syncReq services.ChapterTypeSyncReq
+		_, _ = services.ReportChapterTypeSync(&syncReq)
+	}()
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "操作成功"

+ 68 - 15
models/report_chapter_type.go

@@ -2,6 +2,7 @@ package models
 
 import (
 	"eta/eta_api/utils"
+	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"time"
 )
@@ -31,19 +32,19 @@ type ReportChapterType struct {
 	YbBottomIcon           string    `description:"研报小程序详情底部icon"`
 }
 
-func (item *ReportChapterType) Create() (err error) {
+func (r *ReportChapterType) Create() (err error) {
 	o := orm.NewOrmUsingDB("weekly")
-	id, err := o.Insert(item)
+	id, err := o.Insert(r)
 	if err != nil {
 		return
 	}
-	item.ReportChapterTypeId = int(id)
+	r.ReportChapterTypeId = int(id)
 	return
 }
 
-func (item *ReportChapterType) Update(cols []string) (err error) {
+func (r *ReportChapterType) Update(cols []string) (err error) {
 	o := orm.NewOrmUsingDB("weekly")
-	_, err = o.Update(item, cols...)
+	_, err = o.Update(r, cols...)
 	return
 }
 
@@ -80,7 +81,7 @@ func DeleteReportChapterType(typeId int, reportType string) (err error) {
 
 // GetReportChapterTypeById 获取章节类型
 func GetReportChapterTypeById(reportChapterTypeId int) (item *ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE report_chapter_type_id = ? `
 	err = o.Raw(sql, reportChapterTypeId).QueryRow(&item)
 	return
@@ -88,7 +89,7 @@ func GetReportChapterTypeById(reportChapterTypeId int) (item *ReportChapterType,
 
 // GetReportChapterTypeList 获取章节类型列表
 func GetReportChapterTypeList() (list []*ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE enabled = 1 `
 	_, err = o.Raw(sql).QueryRows(&list)
 	return
@@ -96,7 +97,7 @@ func GetReportChapterTypeList() (list []*ReportChapterType, err error) {
 
 // GetReportChapterTypeListByResearchType 通过报告类型获取章节类型列表
 func GetReportChapterTypeListByResearchType(researchType string) (list []*ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE research_type = ? AND enabled = 1`
 	_, err = o.Raw(sql, researchType).QueryRows(&list)
 	return
@@ -104,7 +105,7 @@ func GetReportChapterTypeListByResearchType(researchType string) (list []*Report
 
 // GetAllReportChapterTypeListByResearchType 通过报告类型获取章节类型列表
 func GetAllReportChapterTypeListByResearchType(researchType string) (list []*ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE research_type = ?`
 	_, err = o.Raw(sql, researchType).QueryRows(&list)
 	return
@@ -112,7 +113,7 @@ func GetAllReportChapterTypeListByResearchType(researchType string) (list []*Rep
 
 // GetAllReportChapterTypeList 通过传入的条件获取所有的章节类型列表
 func GetAllReportChapterTypeList(condition string, pars []interface{}) (list []*ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE 1=1 `
 	sql += condition
 	_, err = o.Raw(sql, pars).QueryRows(&list)
@@ -121,7 +122,7 @@ func GetAllReportChapterTypeList(condition string, pars []interface{}) (list []*
 
 // GetEnableReportChapterTypeList 获取未暂停的章节类型列表
 func GetEnableReportChapterTypeList(researchType string) (list []*ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT
 				*
 			FROM
@@ -312,13 +313,13 @@ func GetReportChapterTypeCount(condition string, pars []interface{}) (count int,
 }
 
 // GetReportChapterTypePageList 获取章节类型列表
-func GetReportChapterTypePageList(condition string, pars []interface{}, startSize, pageSize int) (list []*ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+func GetReportChapterTypePageList(condition string, pars []interface{}) (list []*ReportChapterType, err error) {
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE 1 = 1 `
 	sql += condition
 	sql += ` ORDER BY sort ASC, created_time DESC`
 	sql += ` LIMIT ?,?`
-	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
+	_, err = o.Raw(sql, pars).QueryRows(&list)
 	return
 }
 
@@ -388,10 +389,62 @@ type ReportChapterTypeEnabledReq struct {
 
 // GetReportChapterTypeByCondition 获取章节类型
 func GetReportChapterTypeByCondition(condition string, pars []interface{}) (item *ReportChapterType, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type WHERE 1 = 1 `
 	sql += condition
 	sql += ` LIMIT 1`
 	err = o.Raw(sql, pars).QueryRow(&item)
 	return
 }
+
+// GetMaxSort 获取最大的排序值
+func (r *ReportChapterType) GetMaxSort() (maxSort int, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `SELECT max(sort) AS sort FROM report_chapter_type`
+	err = o.Raw(sql).QueryRow(&maxSort)
+	return
+}
+
+// GetMaxSortByResearchType 获取最大的排序值
+func (r *ReportChapterType) GetMaxSortByResearchType(researchType string) (maxSort int, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `SELECT max(sort) AS sort FROM report_chapter_type WHERE research_type = ?`
+	err = o.Raw(sql, researchType).QueryRow(&maxSort)
+	return
+}
+
+// UpdateReportChapterTypeSortByResearchType 根据父类id更新排序
+func UpdateReportChapterTypeSortByResearchType(researchType string, reportChapterTypeId, nowSort int, updateSort string) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` update report_chapter_type set sort = ` + updateSort + ` WHERE research_type=? AND sort > ?`
+	if reportChapterTypeId > 0 {
+		sql += ` or ( report_chapter_type_id > ` + fmt.Sprint(reportChapterTypeId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
+	}
+
+	_, err = o.Raw(sql, researchType, nowSort).Exec()
+	return
+}
+
+// GetFirstReportChapterTypeByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
+func (r *ReportChapterType) GetFirstReportChapterTypeByResearchType(researchType string) (item *ReportChapterType, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` SELECT * FROM report_chapter_type WHERE 1 = 1 AND research_type = ? ORDER BY sort ASC, report_chapter_type_id ASC LIMIT 1`
+	err = o.Raw(sql, researchType).QueryRow(&item)
+	return
+}
+
+// GetReportChapterTypeById 获取章节类型
+func (r *ReportChapterType) GetReportChapterTypeById(reportChapterTypeId int) (item *ReportChapterType, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` SELECT * FROM report_chapter_type WHERE report_chapter_type_id = ?`
+	err = o.Raw(sql, reportChapterTypeId).QueryRow(&item)
+	return
+}
+
+// SetEnabled 更新启动禁用
+func (r *ReportChapterType) SetEnabled(id, enabled int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` update report_chapter_type set enabled = ? WHERE report_chapter_type_id = ?`
+	_, err = o.Raw(sql, enabled, id).Exec()
+	return
+}

+ 2 - 2
models/report_chapter_type_permission.go

@@ -18,7 +18,7 @@ type ReportChapterTypePermission struct {
 
 // GetChapterTypePermissionByTypeIdAndResearchType 根据章节类型ID及研报类型获取章节类型权限列表
 func GetChapterTypePermissionByTypeIdAndResearchType(typeId int, researchType string) (list []*ReportChapterTypePermission, err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	sql := ` SELECT * FROM report_chapter_type_permission WHERE report_chapter_type_id = ? AND research_type = ? ORDER BY chart_permission_id ASC `
 	_, err = o.Raw(sql, typeId, researchType).QueryRows(&list)
 	return
@@ -26,7 +26,7 @@ func GetChapterTypePermissionByTypeIdAndResearchType(typeId int, researchType st
 
 // SetReportChapterTypePermission 设置报告章节类型权限
 func SetReportChapterTypePermission(chapterTypeId int, researchType string, newPermissions []*ReportChapterTypePermission, newWeekPermissions []*ChartPermissionChapterMapping) (err error) {
-	o := orm.NewOrmUsingDB("weekly")
+	o := orm.NewOrmUsingDB("rddp")
 	to, err := o.Begin()
 	if err != nil {
 		return

+ 126 - 136
services/report_chapter_type.go

@@ -1,163 +1,153 @@
 package services
 
 import (
-	"encoding/json"
 	"eta/eta_api/models"
 	"eta/eta_api/utils"
 	"fmt"
+	"time"
 )
 
-type ChapterTypeReq struct {
-	ReportType string
-}
-type ChapterTypeResp struct {
-	Code   int                              `json:"code" description:"状态码"`
-	Msg    string                           `json:"msg" description:"提示信息"`
-	Data   models.ReportChapterTypeListResp `json:"data" description:"返回数据"`
-	ErrMsg string                           `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
-}
+// MoveReportChapterType 移动报告章节
+func MoveReportChapterType(req *models.ReportChapterTypeMoveReq) (err error, errMsg string) {
+	ob := new(models.ReportChapterType)
+	reportChapterTypeId := req.ReportChapterTypeId
+	prevReportChapterTypeId := req.PrevReportChapterTypeId
+	nextReportChapterTypeId := req.NextReportChapterTypeId
 
-func GetReportChapterType(pars ChapterTypeReq) (res models.ReportChapterTypeListResp, err error) {
-	if utils.CrmEtaServerUrl == "" {
-		return
-	}
-	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/list")
-	b, err := crmEtaPost(url, pars)
-	if err != nil {
-		err = fmt.Errorf("url:%s err: %s", url, err.Error())
-		return
-	}
-	result := new(ChapterTypeResp)
-	if e := json.Unmarshal(b, &result); e != nil {
-		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
-		return
-	}
-	utils.FileLog.Info("%s", string(b))
-	if result.Code != 200 {
-		err = fmt.Errorf("result: %s", string(b))
-		return
-	}
-	res = result.Data
-	return
-}
+	//如果有传入 上一个兄弟节点分类id
+	var (
+		reportChapterType     *models.ReportChapterType
+		prevReportChapterType *models.ReportChapterType
+		nextReportChapterType *models.ReportChapterType
 
-type ChapterTypeBaseResp struct {
-	Code   int    `json:"code" description:"状态码"`
-	Msg    string `json:"msg" description:"提示信息"`
-	ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
-}
+		prevSort int
+		nextSort int
+	)
 
-func AddReportChapterType(pars *models.ReportChapterTypeAddReq) (err error, errMsg string) {
-	if utils.CrmEtaServerUrl == "" {
-		return
-	}
-	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/add")
-	b, err := crmEtaPost(url, pars)
+	// 移动对象为分类, 判断权限
+	reportChapterType, err = ob.GetReportChapterTypeById(reportChapterTypeId)
 	if err != nil {
-		errMsg = "新增品种失败"
-		err = fmt.Errorf("url:%s err: %s", url, err.Error())
+		if err.Error() == utils.ErrNoRow() {
+			errMsg = "当前报告章节不存在"
+			err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
+			return
+		}
+		errMsg = "移动失败"
+		err = fmt.Errorf("获取章节信息失败,Err:" + err.Error())
 		return
-	}
-	result := new(ChapterTypeBaseResp)
-	if e := json.Unmarshal(b, &result); e != nil {
-		errMsg = "新增品种失败"
-		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
+	} else if reportChapterType.ReportChapterTypeId == 0 {
+		errMsg = "当前报告章节不存在"
+		err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
 		return
 	}
-	utils.FileLog.Info("%s", string(b))
-	if result.Code != 200 {
-		err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
-		errMsg = result.Msg
-		return
+
+	researchType := reportChapterType.ResearchType
+	if prevReportChapterTypeId > 0 {
+		prevReportChapterType, err = ob.GetReportChapterTypeById(prevReportChapterTypeId)
+		if err != nil {
+			errMsg = "移动失败"
+			err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
+			return
+		}
+		prevSort = prevReportChapterType.Sort
 	}
+
+	if nextReportChapterTypeId > 0 {
+		//下一个兄弟节点
+		nextReportChapterType, err = ob.GetReportChapterTypeById(nextReportChapterTypeId)
+		if err != nil {
+			errMsg = "移动失败"
+			err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
+			return
+		}
+		nextSort = nextReportChapterType.Sort
+	}
+
+	err, errMsg = moveReportChapterType(reportChapterType, prevReportChapterType, nextReportChapterType, researchType, prevSort, nextSort)
 	return
 }
 
-// ReportChapterTypeEditRespData 编辑章节返回
-type ReportChapterTypeEditRespData struct {
-	OriginReportChapterTypeName string `description:"报告章节类型原来的名称"`
-}
+// moveReportChapterType 移动指标分类
+func moveReportChapterType(reportChapterType, prevReportChapterType, nextReportChapterType *models.ReportChapterType, researchType string, prevSort, nextSort int) (err error, errMsg string) {
+	ob := new(models.ReportChapterType)
+	updateCol := make([]string, 0)
 
-type ReportChapterTypeEditResp struct {
-	Code   int                           `json:"code" description:"状态码"`
-	Msg    string                        `json:"msg" description:"提示信息"`
-	Data   ReportChapterTypeEditRespData `json:"data" description:"返回数据"`
-	ErrMsg string                        `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
-}
+	if prevSort > 0 {
+		//如果是移动在两个兄弟节点之间
+		if nextSort > 0 {
+			//下一个兄弟节点
+			//如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
+			if prevSort == nextSort || prevSort == reportChapterType.Sort {
+				//变更兄弟节点的排序
+				updateSortStr := `sort + 2`
 
-func EditReportChapterType(pars *models.ReportChapterTypeEditReq) (ret ReportChapterTypeEditRespData, err error, errMsg string) {
-	if utils.CrmEtaServerUrl == "" {
-		return
-	}
-	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/edit")
-	b, err := crmEtaPost(url, pars)
-	if err != nil {
-		errMsg = "更新品种失败"
-		err = fmt.Errorf("url:%s err: %s", url, err.Error())
-		return
-	}
-	result := new(ReportChapterTypeEditResp)
-	if e := json.Unmarshal(b, &result); e != nil {
-		errMsg = "更新品种失败"
-		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
-		return
-	}
-	utils.FileLog.Info("%s", string(b))
-	if result.Code != 200 {
-		err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
-		errMsg = result.Msg
-		return
-	}
-	ret = result.Data
-	return
-}
+				//变更分类
+				if prevReportChapterType != nil {
+					_ = models.UpdateReportChapterTypeSortByResearchType(researchType, prevReportChapterType.ReportChapterTypeId, prevReportChapterType.Sort, updateSortStr)
+				} else {
+					_ = models.UpdateReportChapterTypeSortByResearchType(researchType, 0, prevSort, updateSortStr)
+				}
 
-func MoveReportChapterType(pars *models.ReportChapterTypeMoveReq) (err error, errMsg string) {
-	if utils.CrmEtaServerUrl == "" {
-		return
-	}
-	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/move")
-	b, err := crmEtaPost(url, pars)
-	if err != nil {
-		errMsg = "移动品种失败"
-		err = fmt.Errorf("url:%s err: %s", url, err.Error())
-		return
-	}
-	result := new(ChapterTypeBaseResp)
-	if e := json.Unmarshal(b, &result); e != nil {
-		errMsg = "移动品种失败"
-		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
-		return
-	}
-	utils.FileLog.Info("%s", string(b))
-	if result.Code != 200 {
-		err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
-		errMsg = result.Msg
-		return
-	}
-	return
-}
+			} else {
+				//如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
+				if nextSort-prevSort == 1 {
+					//变更兄弟节点的排序
+					updateSortStr := `sort + 1`
 
-// SetEnabledReportChapterType 设置报告分类权限
-func SetEnabledReportChapterType(req *models.ReportChapterTypeEnabledReq) (err error) {
-	if utils.CrmEtaServerUrl == "" {
-		return
-	}
-	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/enabled/set")
-	b, err := crmEtaPost(url, req)
-	if err != nil {
-		err = fmt.Errorf("url:%s err: %s", url, err.Error())
-		return
-	}
-	result := new(ChapterTypeBaseResp)
-	if e := json.Unmarshal(b, &result); e != nil {
-		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
-		return
+					//变更分类
+					if prevReportChapterType != nil {
+						_ = models.UpdateReportChapterTypeSortByResearchType(researchType, prevReportChapterType.ReportChapterTypeId, prevSort, updateSortStr)
+					} else {
+						_ = models.UpdateReportChapterTypeSortByResearchType(researchType, 0, prevSort, updateSortStr)
+					}
+
+				}
+			}
+		}
+
+		reportChapterType.Sort = prevSort + 1
+		reportChapterType.LastUpdatedTime = time.Now()
+		updateCol = append(updateCol, "Sort", "LastUpdatedTime")
+	} else if prevReportChapterType == nil && nextReportChapterType == nil && researchType != "" {
+		//处理只拖动到目录里,默认放到目录底部的情况
+		var maxSort int
+		maxSort, err = ob.GetMaxSortByResearchType(researchType)
+		if err != nil {
+			errMsg = "移动失败"
+			err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
+			return
+		}
+		reportChapterType.Sort = maxSort + 1 //那就是排在组内最后一位
+		reportChapterType.LastUpdatedTime = time.Now()
+		updateCol = append(updateCol, "Sort", "LastUpdatedTime")
+	} else {
+		// 拖动到父级分类的第一位
+		firstReportChapterType, tmpErr := ob.GetFirstReportChapterTypeByResearchType(researchType)
+		if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
+			errMsg = "移动失败"
+			err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
+			return
+		}
+
+		//如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
+		if firstReportChapterType != nil && firstReportChapterType.ReportChapterTypeId != 0 && firstReportChapterType.Sort == 0 {
+			updateSortStr := ` sort + 1 `
+			_ = models.UpdateReportChapterTypeSortByResearchType(researchType, firstReportChapterType.ReportChapterTypeId-1, 0, updateSortStr)
+		}
+
+		reportChapterType.Sort = 0 //那就是排在第一位
+		reportChapterType.LastUpdatedTime = time.Now()
+		updateCol = append(updateCol, "Sort", "LastUpdatedTime")
 	}
-	utils.FileLog.Info("%s", string(b))
-	if result.Code != 200 {
-		err = fmt.Errorf("result: %s", string(b))
-		return
+
+	//更新
+	if len(updateCol) > 0 {
+		err = reportChapterType.Update(updateCol)
+		if err != nil {
+			errMsg = "移动失败"
+			err = fmt.Errorf("修改失败,Err:" + err.Error())
+			return
+		}
 	}
 	return
 }

+ 51 - 0
services/report_chapter_type_sync.go

@@ -0,0 +1,51 @@
+package services
+
+import (
+	"encoding/json"
+	"eta/eta_api/services/alarm_msg"
+	"eta/eta_api/utils"
+	"fmt"
+)
+
+type ChapterTypeSyncReq struct {
+	ReportChapterTypeId int    `description:"报告章节类型id"`
+	ResearchType        string `description:"研报类型"`
+}
+
+type ChapterTypeBaseResp struct {
+	Code   int    `json:"code" description:"状态码"`
+	Msg    string `json:"msg" description:"提示信息"`
+	ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
+}
+
+func ReportChapterTypeSync(pars *ChapterTypeSyncReq) (err error, errMsg string) {
+	defer func() {
+		if err != nil {
+			utils.FileLog.Info("同步章节权限数据失败, Err: " + err.Error() + errMsg)
+			alarm_msg.SendAlarmMsg("同步章节权限数据失败,Err:"+err.Error(), 3)
+		}
+	}()
+	if utils.CrmEtaServerUrl == "" {
+		return
+	}
+	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/sync")
+	b, err := crmEtaPost(url, pars)
+	if err != nil {
+		errMsg = "操作失败"
+		err = fmt.Errorf("url:%s err: %s", url, err.Error())
+		return
+	}
+	result := new(ChapterTypeBaseResp)
+	if e := json.Unmarshal(b, &result); e != nil {
+		errMsg = "操作失败"
+		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
+		return
+	}
+	utils.FileLog.Info("%s", string(b))
+	if result.Code != 200 {
+		err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
+		errMsg = result.Msg
+		return
+	}
+	return
+}