123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- package data
- import (
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/utils"
- "time"
- )
- func AddSciClassify(classifyName string, parentId, sysUserId int, sysUserName, lang string) (classifyInfo *data_manage.BaseFromSciClassify, err error, errMsg string) {
-
- {
- var count int
- switch lang {
- case utils.EnLangVersion:
- count, err = data_manage.GetBaseFromSciClassifyEnCount(classifyName, parentId)
- default:
- count, err = data_manage.GetBaseFromSciClassifyCount(classifyName, parentId)
- }
- if err != nil {
- errMsg = `判断名称是否已存在失败`
- return
- }
- if count > 0 {
- errMsg = `分类名称已存在,请重新输入`
- err = errors.New(errMsg)
- return
- }
- }
- level := 1
- if parentId > 0 {
- parentClassify, e := data_manage.GetBaseFromSciClassifyById(parentId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- err = e
- errMsg = `一级分类不存在`
- return
- }
- err = e
- errMsg = `查询一级分类失败`
- return
- }
- level = parentClassify.Level + 1
- }
- classifyInfo = &data_manage.BaseFromSciClassify{
- ClassifyName: classifyName,
- ClassifyNameEn: classifyName,
- ParentId: parentId,
- SysUserId: sysUserId,
- SysUserRealName: sysUserName,
- Sort: 0,
- Level: level,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- classifyId, err := data_manage.AddBaseFromSciClassify(classifyInfo)
- if err != nil {
- errMsg = "保存分类失败"
- return
- }
- classifyInfo.ClassifyId = int(classifyId)
-
- err = data_manage.UpdateBaseFromSciClassifySort(int(classifyId))
- return
- }
- func EditSciClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
- item, err := data_manage.GetBaseFromSciClassifyById(classifyId)
- if err != nil {
- errMsg = `保存失败`
- return
- }
- switch lang {
- case utils.EnLangVersion:
-
- if item.ClassifyNameEn == classifyName {
- return
- }
-
- count, tmpErr := data_manage.GetBaseFromSciClassifyEnCount(classifyName, item.ParentId)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
-
- err = data_manage.EditBaseFromSciClassifyEn(classifyId, classifyName)
- if err != nil {
- errMsg = "保存失败"
- }
- default:
-
- if item.ClassifyName == classifyName {
- return
- }
-
- count, tmpErr := data_manage.GetBaseFromSciClassifyCount(classifyName, item.ParentId)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
-
- err = data_manage.EditBaseFromSciClassify(classifyId, classifyName)
- if err != nil {
- errMsg = "保存失败"
- }
- }
- return
- }
- func MoveSciClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
-
- classifyInfo, err := data_manage.GetBaseFromSciClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- updateCol := make([]string, 0)
-
- if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- parentClassifyInfo, tmpErr := data_manage.GetBaseFromSciClassifyById(parentClassifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
- return
- }
- classifyInfo.ParentId = parentClassifyInfo.ClassifyId
- classifyInfo.Level = parentClassifyInfo.Level + 1
- updateCol = append(updateCol, "ParentId", "Level")
- }
- var currentSort, prevSort, nextSort int
- currentSort = classifyInfo.Sort
- var prevClassify *data_manage.BaseFromSciClassify
- var nextClassify *data_manage.BaseFromSciClassify
-
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.GetBaseFromSciClassifyById(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.GetBaseFromSciClassifyById(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
- }
-
- if prevSort == 0 && nextSort == 0 {
-
- if nextClassify != nil {
- minSort, e := data_manage.GetSciIndexClassifyMinSort(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.MoveDownSciIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- classifyInfo.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
-
- err = data_manage.MoveUpSciIndexClassifyBySort(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
- }
- func DelSciClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
-
- classifyInfo, err := data_manage.GetBaseFromSciClassifyById(classifyId)
- if err != nil {
- errMsg = "删除失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
-
- classifyIdList := make([]int, 0)
- classifyIdList = append(classifyIdList, classifyId)
-
- if classifyInfo.ParentId <= 0 {
- childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromSciClassifyByParentId(classifyId)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
- return
- }
- for _, v := range childrenClassifyInfoList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- }
-
- count, err := data_manage.GetBaseFromSciIndexCountByClassifyIds(classifyIdList)
- if err != nil {
- errMsg = "删除失败"
- err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "该分类下有指标,不可删除"
- err = errors.New("该分类下有指标,不可删除")
- return
- }
-
- err = data_manage.DeleteSciClassifyByClassifyId(classifyIdList)
- if err != nil {
- return
- }
- return
- }
- func MoveSciData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
-
- sciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
- if classifyId > 0 {
- _, err = data_manage.GetBaseFromSciClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到该分类"
- err = nil
- }
- return
- }
- }
-
- if sciIndex.ClassifyId != classifyId {
- tmpErr := data_manage.MoveBaseFromSciIndex(indexId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("移动失败,Err:" + tmpErr.Error())
- return
- }
- }
-
- updateCol := make([]string, 0)
-
- if prevIndexId > 0 {
- prevIndex, tmpErr := data_manage.GetBaseFromSciIndexByIndexId(prevIndexId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if nextIndexId > 0 {
-
- nextIndex, tmpErr := data_manage.GetBaseFromSciIndexByIndexId(nextIndexId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == sciIndex.Sort {
-
- updateSortStr := `sort + 2`
- err = data_manage.UpdateBaseFromSciIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSciIndexId, updateSortStr)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
- return
- }
- } else {
-
- if nextIndex.Sort-prevIndex.Sort == 1 {
-
- updateSortStr := `sort + 1`
- err = data_manage.UpdateBaseFromSciIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSciIndexId, updateSortStr)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- sciIndex.Sort = prevIndex.Sort + 1
- sciIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstClassify, tmpErr := data_manage.GetFirstBaseFromSciIndexByClassifyId(classifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateBaseFromSciIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromSciIndexId-1, updateSortStr)
- }
- sciIndex.Sort = 0
- sciIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
-
- if len(updateCol) > 0 {
- err = sciIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- return
- }
- func MoveSciData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
-
- sciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
- if classifyId > 0 {
- _, err = data_manage.GetBaseFromSciClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到该分类"
- err = nil
- }
- return
- }
- }
-
- if sciIndex.ClassifyId != classifyId {
- tmpErr := data_manage.MoveBaseFromSciIndex(indexId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("移动失败,Err:" + tmpErr.Error())
- return
- }
- }
-
- updateCol := make([]string, 0)
- var currentSort, prevSort, nextSort int
- currentSort = sciIndex.Sort
-
- var prevIndex *data_manage.BaseFromSciIndex
- var nextIndex *data_manage.BaseFromSciIndex
- if prevIndexId > 0 {
- prevIndex, err = data_manage.GetBaseFromSciIndexByIndexId(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.GetBaseFromSciIndexByIndexId(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
- }
-
- if prevSort == 0 && nextSort == 0 {
-
- if nextIndex != nil {
- minSort, e := data_manage.GetBaseFromSciIndexMinSortByClassifyId(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.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- sciIndex.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
-
- err = data_manage.MoveUpSciIndexBySort(classifyId, nextSort, currentSort)
- if err != nil {
- err = errors.New("向上移动出错:" + err.Error())
- return
- }
- sciIndex.Sort = nextSort
- }
-
- sciIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- err = sciIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
- func EditSciIndex(indexId, classifyId int, sysUser *system.Admin) (baseFromSciIndex *data_manage.BaseFromSciIndex, err error, errMsg string) {
- baseFromSciIndex, err = data_manage.GetBaseFromSciIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- baseFromSciIndex.ClassifyId = classifyId
- baseFromSciIndex.ModifyTime = time.Now()
- err = baseFromSciIndex.Update([]string{"ClassifyId", "ModifyTime"})
- if err != nil {
- errMsg = "保存分类失败"
- }
- return
- }
- func InitBaseFromSciClassify() (err error) {
-
- typeList, err := data_manage.GetBaseFromSciIndexType()
- if err != nil {
- return
- }
- classifyList := make([]*data_manage.BaseFromSciClassify, 0)
- classifyMap := make(map[string][]string, 0)
- parentClassifyMap := make(map[string]struct{}, 0)
- now := time.Now()
- for _, v := range typeList {
- if _, ok := parentClassifyMap[v.Type2]; !ok {
- parentClassifyMap[v.Type2] = struct{}{}
- tmp := new(data_manage.BaseFromSciClassify)
- tmp.ClassifyName = v.Type2
- tmp.Level = 1
- tmp.ModifyTime = now
- tmp.CreateTime = now
- classifyList = append(classifyList, tmp)
- }
- classifyMap[v.Type2] = append(classifyMap[v.Type2], v.Type3)
- }
-
- err = data_manage.AddSciClassifyMulti(classifyList)
- if err != nil {
- err = errors.New("新增分类出错:" + err.Error())
- return
- }
-
- rootList, err := data_manage.GetBaseFromSciClassifyByParentId(0)
- if err != nil {
- return
- }
- classifyIdMap := make(map[string]int)
- for _, v := range rootList {
- classifyIdMap[v.ClassifyName] = v.ClassifyId
- }
- classifyList2 := make([]*data_manage.BaseFromSciClassify, 0)
- for _, v := range typeList {
- if parentId, ok := classifyIdMap[v.Type2]; ok {
- tmp := new(data_manage.BaseFromSciClassify)
- tmp.ParentId = parentId
- tmp.ClassifyName = v.Type3
- tmp.Level = 2
- tmp.ModifyTime = now
- tmp.CreateTime = now
- classifyList2 = append(classifyList2, tmp)
- }
- }
-
- err = data_manage.AddSciClassifyMulti(classifyList2)
- if err != nil {
- err = errors.New("新增二级分类出错:" + err.Error())
- return
- }
-
- err = data_manage.InitSciClassifySort()
- if err != nil {
- return
- }
-
- err = data_manage.InitSciIndexClassifyId()
- return
- }
- func DelSciData(indexId int) (err error, errMsg string) {
- baseFromSciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
- edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI, baseFromSciIndex.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.DeleteBaseFromSciIndexById(indexId)
- if err != nil {
- return
- }
- return
- }
- func ResetSciData(indexId int) (err error, errMsg string) {
- sciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
-
- classifyId := 0
- updateCol := make([]string, 0)
- var currentSort, prevSort int
- currentSort = sciIndex.Sort
-
- prevSort, err = data_manage.GetBaseFromSciIndexMaxSortByClassifyId(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
- return
- }
-
- if prevSort >= currentSort {
-
- err = data_manage.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- sciIndex.Sort = prevSort
- }
-
- sciIndex.ModifyTime = time.Now()
- sciIndex.ClassifyId = classifyId
- updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
- err = sciIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
|