Browse Source

同步报告章节

xyxie 11 months ago
parent
commit
75daa7388c

+ 0 - 12
controller/crm/chart_permission.go

@@ -68,18 +68,6 @@ func (cp *ChartPermissionController) SyncClassifyChartPermission(c *gin.Context)
 // @Success 200 新增成功
 // @router /crm/chart_permission/sync [post]
 func (this *ChartPermissionController) Sync(c *gin.Context) {
-	var req crm.PermissionEnabledReq
-	err := c.Bind(&req)
-	if err != nil {
-		errs, ok := err.(validator.ValidationErrors)
-		if !ok {
-			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
-			return
-		}
-		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
-		return
-	}
-
 	e, msg := crmService.SyncChartPermission()
 	if e != nil {
 		resp.FailData(msg, e.Error(), c)

+ 8 - 367
controller/crm/report_chapter_type.go

@@ -7,88 +7,18 @@ import (
 	"hongze/hz_crm_eta/global"
 	"hongze/hz_crm_eta/models/crm"
 	crmService "hongze/hz_crm_eta/services/crm"
-	"hongze/hz_crm_eta/utils"
-	"time"
 )
 
 type ReportChapterTypeController struct{}
 
-// List
-// @Title 报告章节列表
-// @Description 报告章节列表
-// @Param   ReportType  query  string  true  "报告类型: day-晨报; week-周报"
-// @Success 200 {object} models.ReportChapterTypePageListResp
-// @router /chapter_type/list [get]
-func (this *ReportChapterTypeController) List(c *gin.Context) {
-	var req crm.ChapterTypeReq
-	err := c.Bind(&req)
-	if err != nil {
-		errs, ok := err.(validator.ValidationErrors)
-		if !ok {
-			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
-			return
-		}
-		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
-		return
-	}
-	reportType := req.ReportType
-	typeArr := []string{utils.REPORT_TYPE_DAY, utils.REPORT_TYPE_WEEK}
-	if !utils.InArrayByStr(typeArr, reportType) {
-		resp.Fail("请选择报告类型", c)
-		return
-	}
-
-	cond := ` research_type = ?`
-	pars := make([]interface{}, 0)
-	pars = append(pars, reportType)
-	list, e := crm.GetReportChapterTypePageList(cond, pars)
-	if e != nil {
-		resp.FailData("获取失败", "获取报告章节列表失败, Err:"+e.Error(), c)
-		return
-	}
-
-	mappingList, e := crm.GetChapterTypePermissionByTypeIdAndResearchType(reportType)
-	if e != nil {
-		resp.FailData("获取章节类型权限列表失败", "获取章节类型权限列表失败, Err:"+e.Error(), c)
-		return
-	}
-	mappingMap := make(map[int][]int)
-	for _, v := range mappingList {
-		mappingMap[v.ReportChapterTypeId] = append(mappingMap[v.ReportChapterTypeId], v.ChartPermissionId)
-	}
-
-	respList := make([]*crm.ReportChapterTypeListItem, 0)
-	for i := range list {
-		permissionIds, _ := mappingMap[list[i].ReportChapterTypeId]
-		respList = append(respList, &crm.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,
-			Enabled:               list[i].Enabled,
-			ChartPermissionIdList: permissionIds,
-		})
-	}
-
-	data := new(crm.ReportChapterTypeListResp)
-	data.List = respList
-	resp.OkData("查询成功", data, c)
-}
-
-// Add
-// @Title 新增报告章节
-// @Description 新增报告章节
+// Sync
+// @Title 同步报告章节
+// @Description 同步报告章节
 // @Param	request  body  crm.ReportChapterTypeAddReq  true  "type json string"
 // @Success 200 string "操作成功"
-// @router /chapter_type/add [post]
-func (this *ReportChapterTypeController) Add(c *gin.Context) {
-	var req crm.ReportChapterTypeAddReq
+// @router /chapter_type/sync [post]
+func (this *ReportChapterTypeController) Sync(c *gin.Context) {
+	var req crm.ReportChapterTypeSyncReq
 	err := c.Bind(&req)
 	if err != nil {
 		errs, ok := err.(validator.ValidationErrors)
@@ -100,300 +30,11 @@ func (this *ReportChapterTypeController) Add(c *gin.Context) {
 		return
 	}
 
-	if req.ReportChapterTypeName == "" {
-		resp.Fail("请输入章节名称", c)
-		return
-	}
-	typeArr := []string{utils.REPORT_TYPE_DAY, utils.REPORT_TYPE_WEEK}
-	if !utils.InArrayByStr(typeArr, req.ResearchType) {
-		resp.Fail("请选择报告类型", c)
-		return
-	}
-	// 重名校验
-	cond := ` report_chapter_type_name = ? AND research_type = ?`
-	pars := make([]interface{}, 0)
-	pars = append(pars, req.ReportChapterTypeName, req.ResearchType)
-	exists, e := crm.GetReportChapterTypeByCondition(cond, pars)
-	if e != nil && e != utils.ErrNoRow {
-		resp.FailData("操作失败", "获取报告章节失败, Err:"+e.Error(), c)
-		return
-	}
-	if exists.ReportChapterTypeId > 0 {
-		resp.Fail("章节名称已存在", c)
-		return
-	}
-
-	nowTime := time.Now().Local()
-	item := new(crm.ReportChapterType)
-	maxSort, e := item.GetMaxSort()
-	if e != nil {
-		resp.FailData("操作失败", "获取章节最大排序失败, Err:"+e.Error(), c)
-		return
-	}
-	item.ReportChapterTypeName = req.ReportChapterTypeName
-	item.Sort = maxSort + 1
-	item.Enabled = 1
-	item.CreatedTime = nowTime
-	item.LastUpdatedTime = nowTime
-	item.ResearchType = req.ResearchType
-	item.ReportChapterTypeName = req.ReportChapterTypeName
-	item.IsSet = 0
-	item.ReportChapterTypeKey = req.ReportChapterTypeName
-	item.TickerTitle = req.ReportChapterTypeName
-	if e = item.Create(); e != nil {
-		resp.FailData("操作失败", "新增报告章节失败, Err:"+e.Error(), c)
-		return
-	}
-
-	// 设置权限
-	cond = ""
-	pars = make([]interface{}, 0)
-	permissionList, e := crmService.GetChartPermissionList(cond, pars)
-	if e != nil {
-		resp.FailData("操作失败", "获取权限列表失败, Err:"+e.Error(), c)
-		return
-	}
-	permissionIdName := make(map[int]string)
-	for i := range permissionList {
-		permissionIdName[permissionList[i].ChartPermissionId] = permissionList[i].PermissionName
-	}
-
-	researchType := item.ResearchType
-	newPermissions := make([]*crm.ReportChapterTypePermission, 0)       // 报告章节权限表(新)
-	newWeekPermissions := make([]*crm.ChartPermissionChapterMapping, 0) // 报告章节权限表(老)
-	for i := range req.ChartPermissionIdList {
-		// todo 检查权限是否被禁用,过滤一级品种,只能绑定二级品种
-		newPermissions = append(newPermissions, &crm.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, &crm.ChartPermissionChapterMapping{
-				ChartPermissionId:   req.ChartPermissionIdList[i],
-				ReportChapterTypeId: item.ReportChapterTypeId,
-				ResearchType:        researchType,
-			})
-		}
-	}
-
-	// 设置权限
-	e = crm.SetReportChapterTypePermission(item.ReportChapterTypeId, researchType, newPermissions, newWeekPermissions)
-	if e != nil {
-		resp.FailData("操作失败", "设置章节类型权限失败, Err:"+e.Error(), c)
-		return
-	}
-	// todo 清除小程序端的章节缓存
-	/*{
-		key := "hongze_yb:report_chapter_type:GetEffectTypeID"
-		_ = utils.Rc.Delete(key)
-	}*/
-
-	resp.Ok("操作成功", c)
-}
-
-// Edit
-// @Title 编辑报告章节
-// @Description 编辑报告章节
-// @Param	request  body  crm.ReportChapterTypeEditReq  true  "type json string"
-// @Success 200 string "操作成功"
-// @router /chapter_type/edit [post]
-func (this *ReportChapterTypeController) Edit(c *gin.Context) {
-	var req crm.ReportChapterTypeEditReq
-	err := c.Bind(&req)
-	if err != nil {
-		errs, ok := err.(validator.ValidationErrors)
-		if !ok {
-			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
-			return
-		}
-		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
-		return
-	}
-
-	if req.ReportChapterTypeId <= 0 {
-		resp.Fail("章节ID有误", c)
-		return
-	}
-	if req.ReportChapterTypeName == "" {
-		resp.Fail("请输入章节名称", c)
-		return
-	}
-	if len(req.ChartPermissionIdList) == 0 {
-		resp.Fail("请选择权限", c)
-		return
-	}
-	typeArr := []string{utils.REPORT_TYPE_DAY, utils.REPORT_TYPE_WEEK}
-	if !utils.InArrayByStr(typeArr, req.ResearchType) {
-		resp.Fail("请选择报告类型", c)
-		return
-	}
-	// 重名校验
-	cond := ` report_chapter_type_name = ? AND research_type = ?`
-	pars := make([]interface{}, 0)
-	pars = append(pars, req.ReportChapterTypeName, req.ResearchType)
-	exists, e := crm.GetReportChapterTypeByCondition(cond, pars)
-	if e != nil && e != utils.ErrNoRow {
-		resp.FailData("操作失败", "获取重名报告章节失败, Err:"+e.Error(), c)
-		return
-	}
-	if exists != nil && exists.ReportChapterTypeId > 0 && exists.ReportChapterTypeId != req.ReportChapterTypeId {
-		resp.Fail("章节名称已存在", c)
-		return
-	}
-	ob := new(crm.ReportChapterType)
-	item, e := ob.GetReportChapterTypeById(req.ReportChapterTypeId)
-	if e != nil {
-		if e == utils.ErrNoRow {
-			resp.Fail("报告章节不存在", c)
-			return
-		}
-		resp.FailData("操作失败", "获取报告章节失败, Err:"+e.Error(), c)
-		return
-	}
-	originReportChapterTypeName := item.ReportChapterTypeName
-	item.ReportChapterTypeName = req.ReportChapterTypeName
-	item.ResearchType = req.ResearchType
-	item.ReportChapterTypeKey = req.ReportChapterTypeName
-	item.TickerTitle = req.ReportChapterTypeName
-	updateCols := []string{"ReportChapterTypeName", "ResearchType", "ReportChapterTypeKey", "TickerTitle"}
-	if e = item.Update(updateCols); e != nil {
-		resp.FailData("操作失败", "更新报告章节失败, Err:"+e.Error(), c)
-		return
-	}
-
-	// 设置权限
-	cond = ""
-	pars = make([]interface{}, 0)
-	permissionList, e := crmService.GetChartPermissionList(cond, pars)
-	if e != nil {
-		resp.FailData("操作失败", "获取权限列表失败, Err:"+e.Error(), c)
-		return
-	}
-	permissionIdName := make(map[int]string)
-	for i := range permissionList {
-		permissionIdName[permissionList[i].ChartPermissionId] = permissionList[i].PermissionName
-	}
-
-	researchType := item.ResearchType
-	nowTime := time.Now().Local()
-	newPermissions := make([]*crm.ReportChapterTypePermission, 0)       // 报告章节权限表(新)
-	newWeekPermissions := make([]*crm.ChartPermissionChapterMapping, 0) // 报告章节权限表(老)
-	for i := range req.ChartPermissionIdList {
-		newPermissions = append(newPermissions, &crm.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, &crm.ChartPermissionChapterMapping{
-				ChartPermissionId:   req.ChartPermissionIdList[i],
-				ReportChapterTypeId: item.ReportChapterTypeId,
-				ResearchType:        researchType,
-			})
-		}
-	}
-
-	// 设置权限
-	e = crm.SetReportChapterTypePermission(item.ReportChapterTypeId, researchType, newPermissions, newWeekPermissions)
-	if e != nil {
-		resp.FailData("操作失败", "设置章节类型权限失败, Err:"+e.Error(), c)
-		return
-	}
-	var ret crm.ReportChapterTypeEditResp
-	ret.OriginReportChapterTypeName = originReportChapterTypeName
-	// todo 清除小程序端的章节缓存
-	/*	{
-		key := "hongze_yb:report_chapter_type:GetEffectTypeID"
-		_ = utils.Rc.Delete(key)
-	}*/
-
-	resp.Ok("操作成功", c)
-}
-
-// SetEnabled
-// @Title 启用/禁用分类接口
-// @Description 启用/禁用分类
-// @Param	request	body models.ClassifyMoveReq true "type json string"
-// @Success 200 新增成功
-// @router /chapter_type/enabled/set [post]
-func (this *ReportChapterTypeController) SetEnabled(c *gin.Context) {
-	var req crm.ReportChapterTypeEnabledReq
-	err := c.Bind(&req)
-	if err != nil {
-		errs, ok := err.(validator.ValidationErrors)
-		if !ok {
-			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
-			return
-		}
-		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
-		return
-	}
-
-	if req.ReportChapterTypeId <= 0 {
-		resp.Fail("请选择章节", c)
-		return
-	}
-	if req.Enabled != 0 && req.Enabled != 1 {
-		resp.Fail("请选择正确的启用禁用状态", c)
-		return
-	}
-	ob := new(crm.ReportChapterType)
-	item, err := ob.GetReportChapterTypeById(req.ReportChapterTypeId)
-	if err != nil {
-		if err == utils.ErrNoRow {
-			resp.FailData("章节不存在", "Err:"+err.Error(), c)
-			return
-		}
-
-		resp.FailData("获取章节信息失败", "Err:"+err.Error(), c)
-		return
-	}
-	if item == nil {
-		resp.Fail("章节不存在", c)
-		return
-	}
-	//设置分类启用、禁用状态
-	err = ob.SetEnabled(req.ReportChapterTypeId, req.Enabled)
-	if err != nil {
-		resp.FailData("操作失败", "Err:"+err.Error(), c)
-		return
-	}
-	resp.Ok("操作成功", c)
-}
-
-// Move
-// @Description  移动报告章节类型
-// @Success 200 {string} string "操作成功"
-// @Router /report_chapter_type/move [post]
-func (this *ReportChapterTypeController) Move(c *gin.Context) {
-	var req crm.ReportChapterTypeMoveReq
-	err := c.Bind(&req)
-	if err != nil {
-		errs, ok := err.(validator.ValidationErrors)
-		if !ok {
-			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
-			return
-		}
-		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
-		return
-	}
-
-	if req.ReportChapterTypeId <= 0 {
-		resp.Fail("请选择报告章节类型", c)
-		return
-	}
-
-	e, msg := crmService.MoveReportChapterType(req)
+	e, msg := crmService.SyncReportChapterType(&req)
 	if e != nil {
 		resp.FailData(msg, e.Error(), c)
 		return
 	}
+
 	resp.Ok("操作成功", c)
 }

+ 1 - 1
models/crm/chart_permission.go

@@ -57,7 +57,7 @@ func (c *ChartPermission) GetItemByCondition(condition string, pars []interface{
 // UpdateOrCreate 更新或者创建权限
 func (c *ChartPermission) UpdateOrCreate() (err error) {
 	// 保存记录,如果不存在则创建,存在则更新
-	global.MYSQL["hz_crm"].Save(&c)
+	err = global.MYSQL["hz_crm"].Save(&c).Error
 	return
 }
 

+ 10 - 5
models/crm/report_chapter_type.go

@@ -133,11 +133,9 @@ type ReportChapterTypeEnabledReq struct {
 	Enabled             int `description:"是否可用,1可用,0禁用"`
 }
 
-type ReportChapterTypeMoveReq struct {
-	ReportChapterTypeId int `description:"报告章节类型id"`
-	//	ParentChartPermissionId int `description:"父级品种id"`
-	PrevReportChapterTypeId int `description:"上一个兄弟节点报告章节类型id"`
-	NextReportChapterTypeId int `description:"下一个兄弟节点报告章节类型id"`
+type ReportChapterTypeSyncReq struct {
+	ReportChapterTypeId int    `description:"报告章节类型id"`
+	ResearchType        string `description:"研报类型"`
 }
 
 // UpdateReportChapterTypeSortByResearchType 根据父类id更新排序
@@ -155,3 +153,10 @@ func (r *ReportChapterType) GetFirstReportChapterTypeByResearchType(researchType
 	err = global.MYSQL["hz_crm"].Where("research_type = ?", researchType).Order("sort asc, report_chapter_type_id asc").First(&item).Error
 	return
 }
+
+// UpdateOrCreate 更新或者编辑章节
+func (r *ReportChapterType) UpdateOrCreate() (err error) {
+	// 保存记录,如果不存在则创建,存在则更新
+	err = global.MYSQL["hz_crm"].Save(&r).Error
+	return
+}

+ 1 - 1
models/eta/chart_permission.go

@@ -56,7 +56,7 @@ func (c *ChartPermission) GetItemByCondition(condition string, pars []interface{
 
 // UpdateOrCreate 更新或者创建权限
 func (c *ChartPermission) UpdateOrCreate() (err error) {
-	global.MYSQL["rddp"].Model(c).Save(&c)
+	err = global.MYSQL["rddp"].Model(c).Save(&c).Error
 	return
 }
 

+ 8 - 0
models/eta/chart_permission_chapter_mapping.go

@@ -1,5 +1,7 @@
 package eta
 
+import "hongze/hz_crm_eta/global"
+
 type ChartPermissionChapterMapping struct {
 	Id                  int    `gorm:"column:id;primary_key;AUTO_INCREMENT;NOT NULL"`
 	ChartPermissionId   int    `gorm:"column:chart_permission_id;default:0"`
@@ -10,3 +12,9 @@ type ChartPermissionChapterMapping struct {
 func (c *ChartPermissionChapterMapping) TableName() string {
 	return "chart_permission_chapter_mapping"
 }
+
+// GetChapterTypeMappingByTypeIdAndResearchType 根据章节类型ID及研报类型获取章节类型权限列表
+func GetChapterTypeMappingByTypeIdAndResearchType(reportChapterTypeId int, researchType string) (items []*ChartPermissionChapterMapping, err error) {
+	err = global.MYSQL["rddp"].Model(ChartPermissionChapterMapping{}).Where("report_chapter_type_id = ? and research_type = ?", reportChapterTypeId, researchType).Order("chart_permission_id ASC").Scan(&items).Error
+	return
+}

+ 164 - 0
models/eta/report_chapter_type.go

@@ -0,0 +1,164 @@
+package eta
+
+import (
+	"fmt"
+	"hongze/hz_crm_eta/global"
+	"time"
+)
+
+type ReportChapterType struct {
+	ReportChapterTypeId    int       `gorm:"column:report_chapter_type_id;primary_key;AUTO_INCREMENT;NOT NULL;comment:'报告章节类型id'"`
+	ReportChapterTypeKey   string    `gorm:"column:report_chapter_type_key;default:NULL;comment:'章节key'"`
+	ReportChapterTypeThumb string    `gorm:"column:report_chapter_type_thumb;default:NULL;comment:'H5展示的图片'"`
+	BannerUrl              string    `gorm:"column:banner_url;default:NULL;comment:'banner显示图片'"`
+	ReportChapterTypeName  string    `gorm:"column:report_chapter_type_name;default:NULL;comment:'报告章节类型名称'"`
+	Sort                   int       `gorm:"column:sort;default:0;NOT NULL;comment:'排序字段'"`
+	Enabled                int       `gorm:"column:enabled;default:NULL;comment:'报告类型状态'"`
+	CreatedTime            time.Time `gorm:"column:created_time;default:CURRENT_TIMESTAMP;comment:'创建时间'"`
+	LastUpdatedTime        time.Time `gorm:"column:last_updated_time;default:CURRENT_TIMESTAMP;NOT NULL"`
+	ResearchType           string    `gorm:"column:research_type;default:week;comment:'研报类型'"`
+	SelectedImage          string    `gorm:"column:selected_image;default:NULL;comment:'选中时的图片'"`
+	UnselectedImage        string    `gorm:"column:unselected_image;default:NULL;comment:'没选中时的图片'"`
+	PcSelectedImage        string    `gorm:"column:pc_selected_image;default:NULL;comment:'选中时的图片'"`
+	PcUnselectedImage      string    `gorm:"column:pc_unselected_image;default:NULL;comment:'没选中时的图片'"`
+	EditImgUrl             string    `gorm:"column:edit_img_url;default:NULL;comment:'管理后台编辑时选用的图'"`
+	TickerTitle            string    `gorm:"column:ticker_title;default:NULL;comment:'指标列的标题'"`
+	IsShow                 int       `gorm:"column:is_show;default:1;comment:'是否显示'"`
+	PauseStartTime         string    `gorm:"column:pause_start_time;default:NULL;comment:'暂停开始日期'"`
+	PauseEndTime           string    `gorm:"column:pause_end_time;default:NULL;comment:'暂停结束日期'"`
+	IsSet                  int       `gorm:"column:is_set;default:0;comment:'是否设置:0为设置,1已设置'"`
+	YbIconUrl              string    `gorm:"column:yb_icon_url;default:NULL;comment:'研报小程序3.0图标地址'"`
+	YbBottomIcon           string    `gorm:"column:yb_bottom_icon;default:NULL;comment:'研报小程序3.0底部菜单图标地址'"`
+	YbHidden               uint      `gorm:"column:yb_hidden;default:0;NOT NULL;comment:'研报小程序隐藏章节: 0-显示; 1-隐藏'"`
+}
+
+func (r *ReportChapterType) TableName() string {
+	return "report_chapter_type"
+}
+
+type ChapterTypeReq struct {
+	ReportType string
+}
+
+// GetReportChapterType 获取章节类型列表
+func GetReportChapterType(condition string, pars []interface{}) (items []*ReportChapterType, err error) {
+	err = global.MYSQL["rddp"].Model(ReportChapterType{}).Where(condition, pars...).Order("sort ASC, created_time DESC").Scan(&items).Error
+	return
+}
+
+// ReportChapterTypeListItem 章节类型列表信息
+type ReportChapterTypeListItem struct {
+	ReportChapterTypeId   int    `description:"报告章节类型id"`
+	ReportChapterTypeName string `description:"报告章节类型名称"`
+	Sort                  int    `description:"排序字段"`
+	CreatedTime           string `description:"创建时间"`
+	ResearchType          string `description:"研报类型"`
+	SelectedImage         string `description:"选中时的图片"`
+	UnselectedImage       string `description:"没选中时的图片"`
+	WordsImage            string `description:"带字的icon"`
+	EditImgUrl            string `description:"管理后台编辑时选用的图"`
+	IsShow                int    `description:"显示隐藏: 1-显示; 0-隐藏"`
+	Enabled               int
+	ChartPermissionIdList []int `description:"权限id数组"`
+}
+
+// ReportChapterTypeAddReq 新增章节类型请求体
+type ReportChapterTypeAddReq struct {
+	ReportChapterTypeName string `description:"报告章节类型名称"`
+	ResearchType          string `description:"研报类型"`
+	ChartPermissionIdList []int  `description:"权限id数组"`
+}
+
+// ReportChapterTypeEditReq 编辑章节类型请求体
+type ReportChapterTypeEditReq struct {
+	ReportChapterTypeId   int    `description:"报告章节类型id"`
+	ReportChapterTypeName string `description:"报告章节类型名称"`
+	ResearchType          string `description:"研报类型"`
+	ChartPermissionIdList []int  `description:"权限id数组"`
+}
+
+// ReportChapterTypeEditResp 编辑章节返回
+type ReportChapterTypeEditResp struct {
+	OriginReportChapterTypeName string `description:"报告章节类型原来的名称"`
+}
+
+// GetReportChapterTypeByCondition 获取章节类型
+func (r *ReportChapterType) GetReportChapterTypeByCondition(condition string, pars []interface{}) (items []*ReportChapterType, err error) {
+	err = global.MYSQL["rddp"].Model(ReportChapterType{}).Where(condition, pars...).Scan(&items).Error
+	return
+}
+
+type ReportChapterTypeListResp struct {
+	List []*ReportChapterTypeListItem
+}
+
+// Create 新增权限
+func (r *ReportChapterType) Create() (err error) {
+	err = global.MYSQL["rddp"].Create(r).Error
+	return
+}
+
+// Update 更新权限
+func (r *ReportChapterType) Update(cols []string) (err error) {
+	err = global.MYSQL["rddp"].Model(r).Select(cols).Updates(r).Error
+	return
+}
+
+// SetEnabled 更新启动禁用
+func (r *ReportChapterType) SetEnabled(id, enabled int) (err error) {
+	err = global.MYSQL["rddp"].Model(r).Where("report_chapter_type_id = ?", id).Update("enabled", enabled).Error
+	return
+}
+
+// GetReportChapterTypeById 获取章节类型
+func (r *ReportChapterType) GetReportChapterTypeById(reportChapterTypeId int) (item *ReportChapterType, err error) {
+	err = global.MYSQL["rddp"].Model(r).Where("report_chapter_type_id = ?", reportChapterTypeId).First(&item).Error
+	return
+}
+
+// GetMaxSort 获取最大的排序值
+func (r *ReportChapterType) GetMaxSort() (maxSort int, err error) {
+	err = global.MYSQL["rddp"].Model(r).Select("max(sort)").Scan(&maxSort).Error
+	return
+}
+
+// GetMaxSortByResearchType 获取最大的排序值
+func (r *ReportChapterType) GetMaxSortByResearchType(researchType string) (maxSort int, err error) {
+	err = global.MYSQL["rddp"].Model(r).Select("max(sort)").Where("research_type = ?", researchType).Scan(&maxSort).Error
+	return
+}
+
+type ReportChapterTypeEnabledReq struct {
+	ReportChapterTypeId int `description:"报告章节类型id"`
+	Enabled             int `description:"是否可用,1可用,0禁用"`
+}
+
+type ReportChapterTypeMoveReq struct {
+	ReportChapterTypeId int `description:"报告章节类型id"`
+	//	ParentChartPermissionId int `description:"父级品种id"`
+	PrevReportChapterTypeId int `description:"上一个兄弟节点报告章节类型id"`
+	NextReportChapterTypeId int `description:"下一个兄弟节点报告章节类型id"`
+}
+
+// UpdateReportChapterTypeSortByResearchType 根据父类id更新排序
+func UpdateReportChapterTypeSortByResearchType(researchType string, reportChapterTypeId, nowSort int, updateSort string) (err error) {
+	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 = global.MYSQL["rddp"].Exec(sql, researchType, nowSort).Error
+	return
+}
+
+// GetFirstReportChapterTypeByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
+func (r *ReportChapterType) GetFirstReportChapterTypeByResearchType(researchType string) (item *ReportChapterType, err error) {
+	err = global.MYSQL["rddp"].Where("research_type = ?", researchType).Order("sort asc, report_chapter_type_id asc").First(&item).Error
+	return
+}
+
+// UpdateOrCreate 更新或者编辑章节
+func (r *ReportChapterType) UpdateOrCreate() (err error) {
+	// 保存记录,如果不存在则创建,存在则更新
+	err = global.MYSQL["rddp"].Save(&r).Error
+	return
+}

+ 26 - 0
models/eta/report_chapter_type_permission.go

@@ -0,0 +1,26 @@
+package eta
+
+import (
+	"hongze/hz_crm_eta/global"
+	"time"
+)
+
+type ReportChapterTypePermission struct {
+	Id                    int       `gorm:"column:id;primary_key;AUTO_INCREMENT;NOT NULL;comment:'自增序号'"`
+	ReportChapterTypeId   int       `gorm:"column:report_chapter_type_id;NOT NULL;comment:'报告章节类型id'"`
+	CreatedTime           time.Time `gorm:"column:created_time;default:CURRENT_TIMESTAMP;NOT NULL;comment:'创建时间'"`
+	ChartPermissionId     int       `gorm:"column:chart_permission_id;NOT NULL;comment:'权限ID,对应chart_permission权限表'"`
+	ResearchType          string    `gorm:"column:research_type;default:week;NOT NULL;comment:'研报类型:day:晨报,week:周报'"`
+	PermissionName        string    `gorm:"column:permission_name;default:NULL;comment:'权限名称'"`
+	ReportChapterTypeName string    `gorm:"column:report_chapter_type_name;default:NULL;comment:'章节名称'"`
+}
+
+func (r *ReportChapterTypePermission) TableName() string {
+	return "report_chapter_type_permission"
+}
+
+// GetChapterTypePermissionByTypeIdAndResearchType 根据章节类型ID及研报类型获取章节类型权限列表
+func GetChapterTypePermissionByTypeIdAndResearchType(reportChapterTypeId int, researchType string) (items []*ReportChapterTypePermission, err error) {
+	err = global.MYSQL["rddp"].Model(ReportChapterTypePermission{}).Where("report_chapter_type_id = ? and research_type = ?", reportChapterTypeId, researchType).Order("chart_permission_id ASC").Scan(&items).Error
+	return
+}

+ 1 - 5
routers/report_chapter_type.go

@@ -8,9 +8,5 @@ import (
 func InitReportChapterType(r *gin.RouterGroup) {
 	control := new(crm.ReportChapterTypeController)
 	group := r.Group("crm/chapter_type")
-	group.POST("list", control.List)
-	group.POST("add", control.Add)
-	group.POST("edit", control.Edit)
-	group.POST("move", control.Move)
-	group.POST("enabled/set", control.SetEnabled)
+	group.POST("sync", control.Sync)
 }

+ 69 - 124
services/crm/report_chapter_type.go

@@ -3,149 +3,94 @@ package crm
 import (
 	"fmt"
 	"hongze/hz_crm_eta/models/crm"
-	"hongze/hz_crm_eta/utils"
-	"time"
+	"hongze/hz_crm_eta/models/eta"
 )
 
-// MoveReportChapterType 移动报告章节
-func MoveReportChapterType(req crm.ReportChapterTypeMoveReq) (err error, errMsg string) {
-	ob := new(crm.ReportChapterType)
-	reportChapterTypeId := req.ReportChapterTypeId
-	prevReportChapterTypeId := req.PrevReportChapterTypeId
-	nextReportChapterTypeId := req.NextReportChapterTypeId
-
-	//如果有传入 上一个兄弟节点分类id
-	var (
-		reportChapterType     *crm.ReportChapterType
-		prevReportChapterType *crm.ReportChapterType
-		nextReportChapterType *crm.ReportChapterType
-
-		prevSort int
-		nextSort int
-	)
-
-	// 移动对象为分类, 判断权限
-	reportChapterType, err = ob.GetReportChapterTypeById(reportChapterTypeId)
-	if err != nil {
-		if err == utils.ErrNoRow {
-			errMsg = "当前报告章节不存在"
-			err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
-			return
-		}
-		errMsg = "移动失败"
-		err = fmt.Errorf("获取章节信息失败,Err:" + err.Error())
-		return
-	} else if reportChapterType.ReportChapterTypeId == 0 {
-		errMsg = "当前报告章节不存在"
-		err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
+// SyncReportChapterType  同步报表章节类型
+func SyncReportChapterType(req *crm.ReportChapterTypeSyncReq) (err error, errMsg string) {
+	//查询是否存在品种
+	etaOb := new(eta.ReportChapterType)
+	list, e := etaOb.GetReportChapterTypeByCondition("", []interface{}{})
+	if e != nil {
+		errMsg = "查询章节信息失败"
+		err = fmt.Errorf("查询章节信息失败, Err: " + e.Error())
 		return
 	}
+	for _, v := range list {
+		ob := &crm.ReportChapterType{
+			ReportChapterTypeId:    v.ReportChapterTypeId,
+			ReportChapterTypeKey:   v.ReportChapterTypeKey,
+			ReportChapterTypeThumb: v.ReportChapterTypeThumb,
+			BannerUrl:              v.BannerUrl,
+			ReportChapterTypeName:  v.ReportChapterTypeName,
+			Sort:                   v.Sort,
+			Enabled:                v.Enabled,
+			CreatedTime:            v.CreatedTime,
+			LastUpdatedTime:        v.LastUpdatedTime,
+			ResearchType:           v.ResearchType,
+			SelectedImage:          v.SelectedImage,
+			UnselectedImage:        v.UnselectedImage,
+			PcSelectedImage:        v.PcSelectedImage,
+			PcUnselectedImage:      v.PcUnselectedImage,
+			EditImgUrl:             v.EditImgUrl,
+			TickerTitle:            v.TickerTitle,
+			IsShow:                 v.IsShow,
+			PauseStartTime:         v.PauseStartTime,
+			PauseEndTime:           v.PauseEndTime,
+			IsSet:                  v.IsSet,
+			YbIconUrl:              v.YbIconUrl,
+			YbBottomIcon:           v.YbBottomIcon,
+			YbHidden:               v.YbHidden,
+		}
 
-	researchType := reportChapterType.ResearchType
-	if prevReportChapterTypeId > 0 {
-		prevReportChapterType, err = ob.GetReportChapterTypeById(prevReportChapterTypeId)
+		err = ob.UpdateOrCreate()
 		if err != nil {
-			errMsg = "移动失败"
-			err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
+			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())
+	if req.ReportChapterTypeId > 0 {
+		//同步权限信息
+		newPermissions := make([]*crm.ReportChapterTypePermission, 0)       // 报告章节权限表(新)
+		newWeekPermissions := make([]*crm.ChartPermissionChapterMapping, 0) // 报告章节权限表(老)
+		newEtaPermissions, er := eta.GetChapterTypePermissionByTypeIdAndResearchType(req.ReportChapterTypeId, req.ResearchType)
+		if er != nil {
+			errMsg = "更新失败"
+			err = fmt.Errorf("查询章节权限失败, Err: " + er.Error())
 			return
 		}
-		nextSort = nextReportChapterType.Sort
-	}
-
-	err, errMsg = moveReportChapterType(reportChapterType, prevReportChapterType, nextReportChapterType, researchType, prevSort, nextSort)
-	return
-}
-
-// moveReportChapterType 移动指标分类
-func moveReportChapterType(reportChapterType, prevReportChapterType, nextReportChapterType *crm.ReportChapterType, researchType string, prevSort, nextSort int) (err error, errMsg string) {
-	ob := new(crm.ReportChapterType)
-	updateCol := make([]string, 0)
-
-	if prevSort > 0 {
-		//如果是移动在两个兄弟节点之间
-		if nextSort > 0 {
-			//下一个兄弟节点
-			//如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
-			if prevSort == nextSort || prevSort == reportChapterType.Sort {
-				//变更兄弟节点的排序
-				updateSortStr := `sort + 2`
-
-				//变更分类
-				if prevReportChapterType != nil {
-					_ = crm.UpdateReportChapterTypeSortByResearchType(researchType, prevReportChapterType.ReportChapterTypeId, prevReportChapterType.Sort, updateSortStr)
-				} else {
-					_ = crm.UpdateReportChapterTypeSortByResearchType(researchType, 0, prevSort, updateSortStr)
-				}
-
-			} else {
-				//如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
-				if nextSort-prevSort == 1 {
-					//变更兄弟节点的排序
-					updateSortStr := `sort + 1`
-
-					//变更分类
-					if prevReportChapterType != nil {
-						_ = crm.UpdateReportChapterTypeSortByResearchType(researchType, prevReportChapterType.ReportChapterTypeId, prevSort, updateSortStr)
-					} else {
-						_ = crm.UpdateReportChapterTypeSortByResearchType(researchType, 0, prevSort, updateSortStr)
-					}
-
-				}
+		for _, v := range newEtaPermissions {
+			tmp := &crm.ReportChapterTypePermission{
+				ReportChapterTypeId:   v.ReportChapterTypeId,
+				CreatedTime:           v.CreatedTime,
+				ChartPermissionId:     v.ChartPermissionId,
+				ResearchType:          v.ResearchType,
+				PermissionName:        v.PermissionName,
+				ReportChapterTypeName: v.ReportChapterTypeName,
 			}
+			newPermissions = append(newPermissions, tmp)
 		}
-
-		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())
+		newWeekEtaPermissions, er := eta.GetChapterTypeMappingByTypeIdAndResearchType(req.ReportChapterTypeId, req.ResearchType)
+		if er != nil {
+			errMsg = "更新失败"
+			err = fmt.Errorf("查询章节权限失败, Err: " + er.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 != utils.ErrNoRow {
-			errMsg = "移动失败"
-			err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
-			return
-		}
-
-		//如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
-		if firstReportChapterType != nil && firstReportChapterType.ReportChapterTypeId != 0 && firstReportChapterType.Sort == 0 {
-			updateSortStr := ` sort + 1 `
-			_ = crm.UpdateReportChapterTypeSortByResearchType(researchType, firstReportChapterType.ReportChapterTypeId-1, 0, updateSortStr)
+		for _, v := range newWeekEtaPermissions {
+			tmp := &crm.ChartPermissionChapterMapping{
+				ChartPermissionId:   v.ChartPermissionId,
+				ReportChapterTypeId: v.ReportChapterTypeId,
+				ResearchType:        v.ResearchType,
+			}
+			newWeekPermissions = append(newWeekPermissions, tmp)
 		}
 
-		reportChapterType.Sort = 0 //那就是排在第一位
-		reportChapterType.LastUpdatedTime = time.Now()
-		updateCol = append(updateCol, "Sort", "LastUpdatedTime")
-	}
-
-	//更新
-	if len(updateCol) > 0 {
-		err = reportChapterType.Update(updateCol)
+		err = crm.SetReportChapterTypePermission(req.ReportChapterTypeId, req.ResearchType, newPermissions, newWeekPermissions)
 		if err != nil {
-			errMsg = "移动失败"
-			err = fmt.Errorf("修改失败,Err:" + err.Error())
+			errMsg = "更新失败"
+			err = fmt.Errorf("设置章节类型权限失败, Err: " + er.Error())
 			return
 		}
 	}