123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823 |
- package data
- import (
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "time"
- )
- func AddClarksonsClassify(classifyName string, parentId int) (ok bool, msg string, err error) {
- var count int
- count, err = data_manage.GetBaseFromClarksonsClassifyCount(classifyName, parentId)
- if err != nil {
- return
- }
- if count > 0 {
- return false, "分类已存在", nil
- }
- sort, err := data_manage.GetChildClarksonsClassifyMaxSortById(parentId)
- if err != nil {
- return false, "", err
- }
- if parentId > 0 {
- count, err = data_manage.GetClarksonsClassifyCountById(parentId)
- if err != nil {
- return false, "", err
- }
- if count == 0 {
- return false, "", errors.New("父分类不存在")
- }
- clarksonsClassify := &data_manage.BaseFromClarksonsClassify{
- ClassifyName: classifyName,
- ParentId: parentId,
- Level: 2,
- Sort: sort + 1,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- _, err = clarksonsClassify.Add()
- if err != nil {
- return
- }
- } else {
- clarksonsClassify := &data_manage.BaseFromClarksonsClassify{
- ClassifyName: classifyName,
- ParentId: 0,
- Level: 1,
- Sort: sort + 1,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- _, er := clarksonsClassify.Add()
- if er != nil {
- return false, "", er
- }
- }
- return true, "", nil
- }
- func DelClarksonsClassify(classifyId int) (err error) {
- classify, err := data_manage.GetClarksonsClassifyById(classifyId)
- if err != nil {
- return
- }
- var classifyIds []int
- classifyIds = append(classifyIds, classify.BaseFromClassifyId)
- if classify.ParentId == 0 {
- tmpClassifyIds, er := data_manage.GetChildClarksonsClassifyIdsById(classify.BaseFromClassifyId)
- if er != nil {
- err = er
- return
- }
- classifyIds = append(classifyIds, tmpClassifyIds...)
- }
- // 获取分类下的所有指标
- count, err := data_manage.GetClarksonsIndexCountByClassifyIds(classifyIds)
- if err != nil {
- err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- err = errors.New("该分类下有指标,不可删除")
- return
- }
- // 删除对应的分类
- err = data_manage.DeleteClarksonsClassifyByClassifyId(classifyIds)
- if err != nil {
- return
- }
- return
- }
- // ResetClarksonsIndex 指标数据清除分类接口
- func ResetClarksonsIndex(indexId int) (err error) {
- clarksonsIndex, err := data_manage.GetClarksonsIndexByIndexId(indexId)
- if err != nil {
- return
- }
- // 更新指标在未分类下的排序,永远排在未分类的最后一个
- //移动排序
- classifyId := 0
- updateCol := make([]string, 0)
- var currentSort, prevSort int
- currentSort = clarksonsIndex.Sort
- //未分类的最大的sort值
- prevSort, err = data_manage.GetClarksonsIndexMaxSortByClassifyId(classifyId)
- if err != nil {
- err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
- return
- }
- //如果前面一个节点的sort值比当前大,则当前节点往下移动
- if prevSort >= currentSort {
- //往下移动
- err = data_manage.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- clarksonsIndex.Sort = prevSort
- }
- //更新
- clarksonsIndex.ModifyTime = time.Now()
- clarksonsIndex.ClassifyId = classifyId
- updateCol = append(updateCol, "Sort", "ModifyTime", "BaseFromClassifyId")
- err = clarksonsIndex.Update(updateCol)
- if err != nil {
- err = errors.New("移动失败,Err:" + err.Error())
- return
- }
- return
- }
- // 批量删除克拉克森指标
- func BatchDelClarksonsData(indexIds []int) (existIndex []*data_manage.BaseFromClarksonsIndex, err error) {
- clarksonsIndexList, err := data_manage.GetClarksonsIndexListByIndexIds(indexIds)
- if err != nil {
- return
- }
- indexCodes := make([]string, 0)
- codeToIndex := make(map[string]*data_manage.BaseFromClarksonsIndex)
- for _, v := range clarksonsIndexList {
- indexCodes = append(indexCodes, v.IndexCode)
- codeToIndex[v.IndexCode] = v
- }
- edbInfoList, err := data_manage.GetEdbInfoListByEdbCodes(utils.DATA_SOURCE_CLARKSONS, indexCodes)
- if err != nil {
- return
- }
- deleteIndexIds := make([]int, 0)
- for _, v := range edbInfoList {
- if index, ok := codeToIndex[v.EdbCode]; ok {
- existIndex = append(existIndex, index)
- delete(codeToIndex, v.EdbCode)
- }
- }
- for _, v := range codeToIndex {
- deleteIndexIds = append(deleteIndexIds, v.BaseFromClarksonsIndexId)
- }
- // 删除对应的指标
- err = data_manage.DeleteClarksonsIndexByIds(deleteIndexIds)
- if err != nil {
- return
- }
- return
- }
- // DelClarksonsData 删除克拉克森指标
- func DelClarksonsData(indexId int) (err error, errMsg string) {
- baseFromClarksonsIndex, err := data_manage.GetClarksonsIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- // 获取已经加入到EDB指标库的clarksons指标
- edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_CLARKSONS, baseFromClarksonsIndex.IndexCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
- return
- }
- if edbInfo != nil {
- errMsg = "当前指标已被引用,不可删除"
- err = nil
- return
- }
- // 删除对应的指标
- err = data_manage.DeleteClarksonsIndexById(indexId)
- if err != nil {
- return
- }
- return
- }
- // EditClarksonsIndex 编辑克拉克森指标
- func EditClarksonsIndex(indexId, classifyId int) (baseFromClarksonsIndex *data_manage.BaseFromClarksonsIndex, errMsg string, err error) {
- baseFromClarksonsIndex, err = data_manage.GetClarksonsIndexByIndexId(indexId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- if classifyId > 0 {
- classify, e := data_manage.GetClarksonsClassifyById(classifyId)
- if e != nil {
- err = e
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该分类不存在或已删除`
- err = nil
- }
- return
- }
- if classify.Level == 1 { // 顶级分类
- errMsg = `不合法的编辑`
- err = nil
- return
- }
- }
- maxSort, err := data_manage.GetChildClarksonsClassifyMaxSortById(classifyId)
- if err != nil {
- return
- }
- baseFromClarksonsIndex.ClassifyId = classifyId
- baseFromClarksonsIndex.Sort = maxSort + 1
- baseFromClarksonsIndex.ModifyTime = time.Now()
- err = baseFromClarksonsIndex.Update([]string{"classify_id", "sort", "modify_time"})
- return
- }
- type ClarksonsIndexSource2EdbReq struct {
- EdbCode string
- EdbName string
- Frequency string
- Unit string
- ClassifyId int
- AdminId int
- AdminRealName string
- }
- // ClarksonsIndexSource2Edb 新增克拉克森数据从数据源到指标库
- func ClarksonsIndexSource2Edb(req ClarksonsIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, errMsg string, skip bool, err error) {
- if req.EdbCode == "" {
- err = fmt.Errorf("指标ID为空")
- return
- }
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("ClarksonsIndexSource2Edb新增失败, Err: %s", err.Error())
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- }
- }()
- source := utils.DATA_SOURCE_CLARKSONS
- // 是否新增过指标
- exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
- return
- }
- if exist != nil {
- skip = true
- return
- }
- // 开始结束时间
- var startDate, endDate string
- // 新增指标库
- edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
- if e != nil {
- errMsg = msg
- err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
- return
- }
- edb = edbInfo
- return
- }
- // MoveClarksonsClassify 移动克拉克森分类
- func MoveClarksonsClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
- //判断分类是否存在
- classifyInfo, err := data_manage.GetClarksonsClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- updateCol := make([]string, 0)
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- // 校验移动的父级目录下是否有重名分类
- count, e := data_manage.GetBaseFromClarksonsClassifyCount(classifyInfo.ClassifyName, parentClassifyId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取父级目录下的同名分类失败, Err: " + e.Error())
- return
- }
- if count > 0 {
- errMsg = "移动失败,分类名称已存在"
- err = errors.New("该父级目录下已存在同名分类,请重新命名")
- return
- }
- parentClassifyInfo, tmpErr := data_manage.GetClarksonsClassifyById(parentClassifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
- return
- }
- classifyInfo.ParentId = parentClassifyInfo.BaseFromClassifyId
- classifyInfo.Level = parentClassifyInfo.Level + 1
- updateCol = append(updateCol, "ParentId", "Level")
- }
- var currentSort, prevSort, nextSort int
- currentSort = classifyInfo.Sort
- var prevClassify *data_manage.BaseFromClarksonsClassify
- var nextClassify *data_manage.BaseFromClarksonsClassify
- //如果有传入 上一个兄弟节点分类id
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.GetClarksonsClassifyById(prevClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevClassify.Sort
- }
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = data_manage.GetClarksonsClassifyById(nextClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextClassify.Sort
- }
- // 修改历史数据中的排序为0的情况
- if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
- //更新为0排序的数据为当前最小排序
- if nextClassify != nil {
- minSort, e := data_manage.GetClarksonsIndexClassifyMinSort(parentClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
- return
- }
- if minSort > 1 {
- minSort -= 1
- }
- nextClassify.Sort = minSort
- err = nextClassify.Update([]string{"sort"})
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = minSort
- }
- }
- //移到两个排序值中间操作
- if prevSort >= currentSort {
- //往下移动
- err = data_manage.MoveDownClarksonsIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- classifyInfo.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
- //往上移动
- err = data_manage.MoveUpClarksonsIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
- if err != nil {
- err = errors.New("向上移动出错:" + err.Error())
- return
- }
- classifyInfo.Sort = nextSort
- }
- classifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- err = classifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
- // MoveClarksonsData 移动克拉克森指标
- func MoveClarksonsData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
- //分类信息
- clarksonsIndex, err := data_manage.GetClarksonsIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- //判断分类是否存在
- if classifyId > 0 {
- _, err = data_manage.GetClarksonsClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到该分类"
- err = nil
- }
- return
- }
- }
- //如果改变了分类,那么移动该图表数据
- if clarksonsIndex.ClassifyId != classifyId {
- tmpErr := data_manage.MoveClarksonsIndex(indexId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("移动失败,Err:" + tmpErr.Error())
- return
- }
- }
- //移动排序
- updateCol := make([]string, 0)
- var currentSort, prevSort, nextSort int
- currentSort = clarksonsIndex.Sort
- //如果有传入 上一个兄弟节点分类id
- var prevIndex *data_manage.BaseFromClarksonsIndex
- var nextIndex *data_manage.BaseFromClarksonsIndex
- if prevIndexId > 0 {
- prevIndex, err = data_manage.GetClarksonsIndexByIndexId(prevIndexId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevIndex.Sort
- }
- if nextIndexId > 0 {
- //下一个兄弟节点
- nextIndex, err = data_manage.GetClarksonsIndexByIndexId(nextIndexId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextIndex.Sort
- }
- // 修改历史数据中的排序为0的情况
- if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
- //更新为0排序的数据为当前最小排序
- if nextIndex != nil {
- minSort, e := data_manage.GetClarksonsIndexMinSortByClassifyId(classifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
- return
- }
- if minSort > 1 {
- minSort -= 1
- }
- nextIndex.Sort = minSort
- err = nextIndex.Update([]string{"Sort"})
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = minSort
- }
- }
- //移到两个排序值中间操作
- if prevSort >= currentSort {
- //往下移动
- err = data_manage.MoveDownClarksonsIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- clarksonsIndex.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
- //往上移动
- err = data_manage.MoveUpClarksonsIndexBySort(classifyId, nextSort, currentSort)
- if err != nil {
- err = errors.New("向上移动出错:" + err.Error())
- return
- }
- clarksonsIndex.Sort = nextSort
- }
- //更新
- clarksonsIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- err = clarksonsIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
- // GetClarksonsIndexInfo 获取指标信息-分页
- func GetClarksonsIndexInfo(keyWord string, classifyIdList []string, frequencyList []string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexPage, err error) {
- // 获取指标
- var condition string
- var pars []interface{}
- if keyWord != "" {
- condition += ` AND (index_name like ? or index_code like ?)`
- pars = append(pars, "%"+keyWord+"%", "%"+keyWord+"%")
- }
- if len(classifyIdList) > 0 {
- condition += ` AND classify_id IN (`
- for _, v := range classifyIdList {
- condition += `?,`
- pars = append(pars, v)
- }
- condition = condition[:len(condition)-1] + `)`
- }
- if len(frequencyList) > 0 {
- condition += ` AND frequency IN (`
- for _, v := range frequencyList {
- condition += `?,`
- pars = append(pars, v)
- }
- condition = condition[:len(condition)-1] + `)`
- }
- count, err := data_manage.GetClarksonsIndexInfoCount(condition, pars)
- if err != nil {
- return nil, err
- }
- indexPage := data_manage.BaseFromRzdIndexPage{}
- page := paging.GetPaging(currentIndex, pageSize, count)
- if count <= 0 {
- indexPage.Paging = page
- return &indexPage, nil
- }
- condition += ` ORDER BY base_from_clarksons_index_id asc`
- // 分页
- condition += ` LIMIT ?, ?`
- pars = append(pars, startSize, pageSize)
- indexInfoPage, err := data_manage.GetClarksonsIndexInfoPage(condition, pars)
- if err != nil {
- return nil, err
- }
- var indexCodes []string
- for _, indexInfo := range indexInfoPage {
- indexCodes = append(indexCodes, indexInfo.IndexCode)
- }
- IndexDataList, err := data_manage.GetClarksonsLastUpdateTimeLastByIndexCode(indexCodes)
- if err != nil {
- return nil, err
- }
- var indexDataMap = make(map[string]*data_manage.BaseFromClarksonsData, 0)
- for _, data := range IndexDataList {
- indexDataMap[data.IndexCode] = data
- }
- for _, indexInfo := range indexInfoPage {
- if indexDataMap[indexInfo.IndexCode] == nil {
- continue
- }
- indexInfo.ModifyTimeMax = indexDataMap[indexInfo.IndexCode].DataTime
- indexInfo.Value = indexDataMap[indexInfo.IndexCode].Value
- }
- indexPage.List = indexInfoPage
- indexPage.Paging = page
- return &indexPage, nil
- }
- // GetClarkssonsIndexList 获取指标列表
- func GetClarkssonsIndexList(searchParams string) (rzdIndexInfoList []*data_manage.BaseFromRzdIndexList, err error) {
- // 获取指标
- var condition string
- var pars []interface{}
- if searchParams != "" {
- condition += ` and index_code like ? or index_name like ?`
- pars = append(pars, "%"+searchParams+"%", "%"+searchParams+"%")
- }
- rzdIndexList, err := data_manage.GetRzdIndex(condition, pars)
- if err != nil {
- return nil, err
- }
- return rzdIndexList, nil
- }
- // MoveClarksonsClassify 移动克拉克森分类
- func MoveClarksonsClassifyV2(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
- //判断分类是否存在
- classifyInfo, err := data_manage.GetClarksonsClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- updateCol := make([]string, 0)
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- // 校验移动的父级目录下是否有重名分类
- count, e := data_manage.GetBaseFromClarksonsClassifyCount(classifyInfo.ClassifyName, parentClassifyId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取父级目录下的同名分类失败, Err: " + e.Error())
- return
- }
- if count > 0 {
- errMsg = "移动失败,分类名称已存在"
- err = errors.New("该父级目录下已存在同名分类,请重新命名")
- return
- }
- parentClassifyInfo, tmpErr := data_manage.GetClarksonsClassifyById(parentClassifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
- return
- }
- classifyInfo.ParentId = parentClassifyInfo.BaseFromClassifyId
- classifyInfo.Level = parentClassifyInfo.Level + 1
- updateCol = append(updateCol, "ParentId", "Level")
- }
- //var currentSort, prevSort, nextSort int
- //currentSort = classifyInfo.Sort
- var prevClassify *data_manage.BaseFromClarksonsClassify
- var nextClassify *data_manage.BaseFromClarksonsClassify
- //如果有传入 上一个兄弟节点分类id
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.GetClarksonsClassifyById(prevClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- //如果是移动在两个兄弟节点之间
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = data_manage.GetClarksonsClassifyById(nextClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == classifyInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- _ = data_manage.UpdateClarksonsClassifySortByParentId(prevClassify.ParentId, prevClassify.BaseFromClassifyId, prevClassify.Sort, updateSortStr)
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextClassify.Sort-prevClassify.Sort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- _ = data_manage.UpdateClarksonsClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
- }
- }
- }
- classifyInfo.Sort = prevClassify.Sort + 1
- classifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstClassify, e := data_manage.GetFirstClarksonsClassifyByParentId(classifyInfo.ParentId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = e
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateClarksonsClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
- }
- classifyInfo.Sort = 0 //那就是排在第一位
- classifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //if nextClassifyId > 0 {
- // //下一个兄弟节点
- // nextClassify, err = data_manage.GetClarksonsClassifyById(nextClassifyId)
- // if err != nil {
- // if err.Error() == utils.ErrNoRow() {
- // errMsg = "移动失败"
- // err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
- // return
- // }
- // errMsg = "移动失败"
- // err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- // return
- // }
- // nextSort = nextClassify.Sort
- //}
- //
- //// 修改历史数据中的排序为0的情况
- //if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
- // //更新为0排序的数据为当前最小排序
- // if nextClassify != nil {
- // minSort, e := data_manage.GetClarksonsIndexClassifyMinSort(parentClassifyId)
- // if e != nil {
- // errMsg = "移动失败"
- // err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
- // return
- // }
- // if minSort > 1 {
- // minSort -= 1
- // }
- // nextClassify.Sort = minSort
- // err = nextClassify.Update([]string{"sort"})
- // if err != nil {
- // errMsg = "移动失败"
- // err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
- // return
- // }
- // nextSort = minSort
- // }
- //}
- ////移到两个排序值中间操作
- //if prevSort >= currentSort {
- // //往下移动
- // err = data_manage.MoveDownClarksonsIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
- // if err != nil {
- // err = errors.New("向下移动出错:" + err.Error())
- // return
- // }
- // classifyInfo.Sort = prevSort
- //} else if nextSort <= currentSort && nextSort != 0 {
- // //往上移动
- // err = data_manage.MoveUpClarksonsIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
- // if err != nil {
- // err = errors.New("向上移动出错:" + err.Error())
- // return
- // }
- // classifyInfo.Sort = nextSort
- //}
- //
- //classifyInfo.ModifyTime = time.Now()
- //updateCol = append(updateCol, "Sort", "ModifyTime")
- //err = classifyInfo.Update(updateCol)
- //if err != nil {
- // errMsg = "移动失败"
- // err = errors.New("修改失败,Err:" + err.Error())
- // return
- //}
- //更新
- if len(updateCol) > 0 {
- err = classifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- return
- }
|