123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- package excel
- import (
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/excel"
- "eta/eta_api/models/data_manage/excel/request"
- "eta/eta_api/utils"
- "time"
- )
- func GetExcelClassifyMaxSort(parentId int, source int) (maxSort int, err error) {
- //获取该层级下最大的排序数
- maxSort, err = excel.GetExcelClassifyMaxSort(parentId, source)
- if err != nil {
- return
- }
- edbMaxSort, err := excel.GetExcelMaxSortByClassifyId(parentId, source)
- if err != nil {
- return
- }
- if maxSort < edbMaxSort {
- maxSort = edbMaxSort
- }
- return
- }
- // MoveExcelClassify 移动指标分类
- func MoveExcelClassify(req request.MoveExcelClassifyReq) (err error, errMsg string) {
- classifyId := req.ClassifyId
- parentClassifyId := req.ParentClassifyId
- prevClassifyId := req.PrevClassifyId
- nextClassifyId := req.NextClassifyId
- excelInfoId := req.ExcelInfoId
- prevExcelInfoId := req.PrevExcelInfoId
- nextExcelInfoId := req.NextExcelInfoId
- source := req.Source
- //首先确定移动的对象是分类还是指标
- //判断上一个节点是分类还是指标
- //判断下一个节点是分类还是指标
- //同时更新分类目录下的分类sort和指标sort
- //更新当前移动的分类或者指标sort
- var parentExcelClassifyInfo *excel.ExcelClassify
- if parentClassifyId > 0 {
- parentExcelClassifyInfo, err = excel.GetExcelClassifyById(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + err.Error())
- return
- }
- }
- //如果有传入 上一个兄弟节点分类id
- var (
- excelClassifyInfo *excel.ExcelClassify
- prevClassify *excel.ExcelClassify
- nextClassify *excel.ExcelClassify
- excelInfo *excel.ExcelInfo
- prevExcelInfo *excel.ExcelInfo
- nextExcelInfo *excel.ExcelInfo
- prevSort int
- nextSort int
- )
- // 移动对象为分类, 判断权限
- if excelInfoId == 0 {
- excelClassifyInfo, err = excel.GetExcelClassifyById(classifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- } else {
- excelInfo, err = excel.GetExcelInfoById(excelInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前表格不存在"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if parentClassifyId == 0 {
- errMsg = "移动失败,表格必须挂在分类下"
- err = errors.New(errMsg)
- return
- }
- }
- if prevClassifyId > 0 {
- prevClassify, err = excel.GetExcelClassifyById(prevClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevClassify.Sort
- } else if prevExcelInfoId > 0 {
- prevExcelInfo, err = excel.GetExcelInfoById(prevExcelInfoId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevExcelInfo.Sort
- }
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = excel.GetExcelClassifyById(nextClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextClassify.Sort
- } else if nextExcelInfoId > 0 {
- //下一个兄弟节点
- nextExcelInfo, err = excel.GetExcelInfoById(nextExcelInfoId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextExcelInfo.Sort
- }
- err, errMsg = moveExcelClassify(parentExcelClassifyInfo, excelClassifyInfo, prevClassify, nextClassify, excelInfo, prevExcelInfo, nextExcelInfo, parentClassifyId, prevSort, nextSort, source)
- return
- }
- // moveExcelClassify 移动表格分类
- func moveExcelClassify(parentExcelClassifyInfo, excelClassifyInfo, prevClassify, nextClassify *excel.ExcelClassify, excelInfo, prevExcelInfo, nextExcelInfo *excel.ExcelInfo, parentClassifyId int, prevSort, nextSort int, source int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- // 移动对象为分类, 判断分类是否存在
- if excelClassifyInfo != nil {
- oldParentId := excelClassifyInfo.ParentId
- /*oldLevel := excelClassifyInfo.Level
- var classifyIds []int*/
- if oldParentId != parentClassifyId {
- //todo 更新子分类对应的level
- /*childList, e, m := GetChildClassifyByClassifyId(excelClassifyInfo.ClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子分类失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.ClassifyId == excelClassifyInfo.ClassifyId {
- continue
- }
- classifyIds = append(classifyIds, v.ClassifyId)
- }
- }*/
- }
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if excelClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- if excelClassifyInfo.Level != parentExcelClassifyInfo.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- excelClassifyInfo.ParentId = parentExcelClassifyInfo.ExcelClassifyId
- excelClassifyInfo.Level = parentExcelClassifyInfo.Level + 1
- excelClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
- } else if excelClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == excelClassifyInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更表格
- if prevExcelInfo != nil {
- //变更兄弟节点的排序
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更表格
- if prevExcelInfo != nil {
- //变更兄弟节点的排序
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
- }
- }
- }
- }
- excelClassifyInfo.Sort = prevSort + 1
- excelClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevExcelInfo == nil && nextExcelInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- maxSort, err = GetExcelClassifyMaxSort(parentClassifyId, source)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- excelClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- excelClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有表格也需要+1
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr, source)
- } else {
- //如果该分类下存在表格,且第一个表格的排序等于0,那么需要调整排序
- firstExcel, tErr := excel.GetFirstExcelInfoByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstExcel != nil && firstExcel.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, firstExcel.ExcelInfoId-1, updateSortStr, source)
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- excelClassifyInfo.Sort = 0 //那就是排在第一位
- excelClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = excelClassifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- //更新对应分类的root_id和层级
- if oldParentId != parentClassifyId {
- /*if len(classifyIds) > 0 {
- levelStep := excelClassifyInfo.Level - oldLevel
- err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, excelClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.Error())
- return
- }
- }*/
- }
- }
- } else {
- if excelInfo == nil {
- errMsg = "当前表格不存在"
- err = errors.New(errMsg)
- return
- }
- //如果改变了分类,那么移动该表格数据
- if excelInfo.ExcelClassifyId != parentClassifyId {
- excelInfo.ExcelClassifyId = parentClassifyId
- excelInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ExcelClassifyId", "ModifyTime")
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == excelInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更表格
- if prevExcelInfo != nil {
- //变更兄弟节点的排序
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更表格
- if prevExcelInfo != nil {
- //变更兄弟节点的排序
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
- } else {
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
- }
- }
- }
- }
- excelInfo.Sort = prevSort + 1
- excelInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevExcelInfo == nil && nextExcelInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- maxSort, err = GetExcelClassifyMaxSort(parentClassifyId, source)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- excelInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- excelInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := excel.GetFirstExcelClassifyByParentId(parentClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, firstClassify.ExcelClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有表格也需要+1
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr, source)
- } else {
- //如果该分类下存在表格,且第一个表格的排序等于0,那么需要调整排序
- firstExcel, tErr := excel.GetFirstExcelInfoByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstExcel != nil && firstExcel.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, firstExcel.ExcelInfoId-1, updateSortStr, source)
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- excelInfo.Sort = 0 //那就是排在第一位
- excelInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = excelInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- }
- return
- }
|