package services

import (
	"eta/eta_api/models"
	"eta/eta_api/utils"
	"fmt"
	"time"
)

// MoveReportChapterType 移动报告章节
func MoveReportChapterType(req *models.ReportChapterTypeMoveReq) (err error, errMsg string) {
	ob := new(models.ReportChapterType)
	reportChapterTypeId := req.ReportChapterTypeId
	prevReportChapterTypeId := req.PrevReportChapterTypeId
	nextReportChapterTypeId := req.NextReportChapterTypeId

	//如果有传入 上一个兄弟节点分类id
	var (
		reportChapterType     *models.ReportChapterType
		prevReportChapterType *models.ReportChapterType
		nextReportChapterType *models.ReportChapterType

		prevSort int
		nextSort int
	)

	// 移动对象为分类， 判断权限
	reportChapterType, err = ob.GetReportChapterTypeById(reportChapterTypeId)
	if err != nil {
		if err.Error() == 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())
		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
}

// 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)

	if prevSort > 0 {
		//如果是移动在两个兄弟节点之间
		if nextSort > 0 {
			//下一个兄弟节点
			//如果上一个兄弟与下一个兄弟的排序权重是一致的，那么需要将下一个兄弟（以及下个兄弟的同样排序权重）的排序权重+2，自己变成上一个兄弟的排序权重+1
			if prevSort == nextSort || prevSort == reportChapterType.Sort {
				//变更兄弟节点的排序
				updateSortStr := `sort + 2`

				//变更分类
				if prevReportChapterType != nil {
					_ = models.UpdateReportChapterTypeSortByResearchType(researchType, prevReportChapterType.ReportChapterTypeId, prevReportChapterType.Sort, updateSortStr)
				} else {
					_ = models.UpdateReportChapterTypeSortByResearchType(researchType, 0, prevSort, updateSortStr)
				}

			} else {
				//如果下一个兄弟的排序权重正好是上个兄弟节点的下一层，那么需要再加一层了
				if nextSort-prevSort == 1 {
					//变更兄弟节点的排序
					updateSortStr := `sort + 1`

					//变更分类
					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")
	}

	//更新
	if len(updateCol) > 0 {
		err = reportChapterType.Update(updateCol)
		if err != nil {
			errMsg = "移动失败"
			err = fmt.Errorf("修改失败,Err:" + err.Error())
			return
		}
	}
	return
}
