123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- package excel
- import (
- "errors"
- "eta/eta_mobile/models/data_manage"
- "eta/eta_mobile/models/data_manage/excel"
- "eta/eta_mobile/models/data_manage/excel/request"
- "eta/eta_mobile/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
- }
- 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
-
-
-
-
-
- var parentExcelClassifyInfo *excel.ExcelClassify
- if parentClassifyId > 0 {
- parentExcelClassifyInfo, err = excel.GetExcelClassifyById(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + err.Error())
- return
- }
- }
-
- 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
- }
- 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
-
- if oldParentId != parentClassifyId {
-
-
- }
-
- 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 {
-
-
- 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
- }
-
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, source)
-
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr, source)
- } else {
-
- firstExcel, tErr := excel.GetFirstExcelInfoByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
-
- 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
- }
-
- if oldParentId != parentClassifyId {
-
- }
- }
- } 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 {
-
-
- 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
- }
-
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, firstClassify.ExcelClassifyId-1, 0, updateSortStr, source)
-
- _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr, source)
- } else {
-
- firstExcel, tErr := excel.GetFirstExcelInfoByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
-
- 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
- }
- func GetChildClassifyByClassifyId(targetClassifyId int, source int) (targetList []*excel.ExcelClassifyItems, err error, errMsg string) {
-
- targetClassify, err := excel.GetExcelClassifyById(targetClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- tmpList, err := excel.GetExcelClassifyBySourceOrderByLevel(source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "获取失败"
- err = errors.New("获取数据失败,Err:" + err.Error())
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.ExcelClassifyId == targetClassify.ExcelClassifyId {
- idMap[v.ExcelClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ParentId]; ok {
- idMap[v.ExcelClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ExcelClassifyId]; ok {
- targetItem := new(excel.ExcelClassifyItems)
- targetItem.ExcelClassifyId = v.ExcelClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
|