123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- package data
- import (
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/utils"
- "fmt"
- "time"
- )
- func AddSciHqClassify(classifyName, lang string, parentId int) (ok bool, msg string, err error) {
- var count int
- switch lang {
- case utils.EnLangVersion:
- count, err = data_manage.GetBaseFromSciHqClassifyEnCount(classifyName, parentId)
- default:
- count, err = data_manage.GetBaseFromSciHqClassifyCount(classifyName, parentId)
- }
- if err != nil {
- return
- }
- if count > 0 {
- return false, "分类已存在", nil
- }
- sort, err := data_manage.GetChildSciHqClassifyMaxSortById(parentId)
- if err != nil {
- return false, "", err
- }
- if parentId > 0 {
- count, err = data_manage.GetSciHqClassifyCountById(parentId)
- if err != nil {
- return false, "", err
- }
- if count == 0 {
- return false, "", errors.New("父分类不存在")
- }
- sciHqClassify := &data_manage.BaseFromSciHqClassify{
- ClassifyName: classifyName,
- ClassifyNameEn: classifyName,
- ParentId: parentId,
- Level: 2,
- Sort: sort + 1,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- _, err = sciHqClassify.Add()
- if err != nil {
- return
- }
- } else {
- sciHqClassify := &data_manage.BaseFromSciHqClassify{
- ClassifyName: classifyName,
- ClassifyNameEn: classifyName,
- ParentId: 0,
- Level: 1,
- Sort: sort + 1,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- insertId, er := sciHqClassify.Add()
- if er != nil {
- return false, "", er
- }
- sciHqClassifyList := []*data_manage.BaseFromSciHqClassify{
- {
- ClassifyName: "价格",
- ClassifyNameEn: "价格",
- ParentId: int(insertId),
- Level: 2,
- Sort: 1,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- {
- ClassifyName: "供应",
- ClassifyNameEn: "供应",
- ParentId: int(insertId),
- Level: 2,
- Sort: 2,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- {
- ClassifyName: "需求",
- ClassifyNameEn: "需求",
- ParentId: int(insertId),
- Level: 2,
- Sort: 3,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- {
- ClassifyName: "库存",
- ClassifyNameEn: "库存",
- ParentId: int(insertId),
- Level: 2,
- Sort: 4,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- }
- err = data_manage.BatchAddSciHqClassify(sciHqClassifyList)
- if err != nil {
- return
- }
- }
- return true, "", nil
- }
- func DelSciHqClassify(classifyId int) (err error) {
- classify, err := data_manage.GetSciHqClassifyById(classifyId)
- if err != nil {
- return
- }
- var classifyIds []int
- classifyIds = append(classifyIds, classify.ClassifyId)
- if classify.ParentId == 0 {
- tmpClassifyIds, er := data_manage.GetChildSciHqClassifyIdsById(classify.ClassifyId)
- if er != nil {
- err = er
- return
- }
- classifyIds = append(classifyIds, tmpClassifyIds...)
- }
- // 获取分类下的所有指标
- count, err := data_manage.GetSciHqIndexCountByClassifyIds(classifyIds)
- if err != nil {
- err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- err = errors.New("该分类下有指标,不可删除")
- return
- }
- // 删除对应的分类
- err = data_manage.DeleteSciHqClassifyByClassifyId(classifyIds)
- if err != nil {
- return
- }
- return
- }
- // ResetSciHqIndex 指标数据清除分类接口
- func ResetSciHqIndex(indexId int) (err error) {
- sciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
- if err != nil {
- return
- }
- // 更新指标在未分类下的排序,永远排在未分类的最后一个
- //移动排序
- classifyId := 0
- updateCol := make([]string, 0)
- var currentSort, prevSort int
- currentSort = sciHqIndex.Sort
- //未分类的最大的sort值
- prevSort, err = data_manage.GetSciHqIndexMaxSortByClassifyId(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
- }
- sciHqIndex.Sort = prevSort
- }
- //更新
- sciHqIndex.ModifyTime = time.Now()
- sciHqIndex.ClassifyId = classifyId
- updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
- err = sciHqIndex.Update(updateCol)
- if err != nil {
- err = errors.New("移动失败,Err:" + err.Error())
- return
- }
- return
- }
- // 批量删除卓创红期指标
- func BatchDelSciHqData(indexIds []int) (existIndex []*data_manage.BaseFromSciHqIndex, err error) {
- sciHqIndexList, err := data_manage.GetSciHqIndexListByIndexIds(indexIds)
- if err != nil {
- return
- }
- indexCodes := make([]string, 0)
- codeToIndex := make(map[string]*data_manage.BaseFromSciHqIndex)
- for _, v := range sciHqIndexList {
- indexCodes = append(indexCodes, v.IndexCode)
- codeToIndex[v.IndexCode] = v
- }
- edbInfoList, err := data_manage.GetEdbInfoListByEdbCodes(utils.DATA_SOURCE_SCI_HQ, 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.BaseFromSciHqIndexId)
- }
- // 删除对应的指标
- err = data_manage.DeleteSciHqIndexByIds(deleteIndexIds)
- if err != nil {
- return
- }
- return
- }
- // DelSciHqData 删除卓创红期指标
- func DelSciHqData(indexId int) (err error, errMsg string) {
- baseFromSciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- // 获取已经加入到EDB指标库的Sci指标
- edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI_HQ, baseFromSciHqIndex.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.DeleteSciHqIndexById(indexId)
- if err != nil {
- return
- }
- return
- }
- // EditSciHqIndex 编辑卓创红期指标
- func EditSciHqIndex(indexId, classifyId int) (baseFromSciHqIndex *data_manage.BaseFromSciHqIndex, errMsg string, err error) {
- baseFromSciHqIndex, err = data_manage.GetSciHqIndexByIndexId(indexId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- classify, err := data_manage.GetSciHqClassifyById(classifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该分类不存在或已删除`
- err = nil
- }
- return
- }
- if classify.Level == 1 { // 顶级分类
- errMsg = `不合法的编辑`
- err = nil
- return
- }
- maxSort, err := data_manage.GetChildSciHqClassifyMaxSortById(classifyId)
- if err != nil {
- return
- }
- baseFromSciHqIndex.ClassifyId = classifyId
- baseFromSciHqIndex.Sort = maxSort + 1
- baseFromSciHqIndex.ModifyTime = time.Now()
- err = baseFromSciHqIndex.Update([]string{"classify_id", "sort", "modify_time"})
- return
- }
- type SciHqIndexSource2EdbReq struct {
- EdbCode string
- EdbName string
- Frequency string
- Unit string
- ClassifyId int
- AdminId int
- AdminRealName string
- }
- // SciHqIndexSource2Edb 新增卓创红期数据从数据源到指标库
- func SciHqIndexSource2Edb(req SciHqIndexSource2EdbReq, 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("SciHqIndexSource2Edb新增失败, Err: %s", err.Error())
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- }
- }()
- source := utils.DATA_SOURCE_SCI_HQ
- // 是否新增过指标
- 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
- }
- // MoveSciHqClassify 移动卓创红期分类
- func MoveSciHqClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
- //判断分类是否存在
- classifyInfo, err := data_manage.GetSciHqClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- updateCol := make([]string, 0)
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- parentClassifyInfo, tmpErr := data_manage.GetSciHqClassifyById(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.BaseFromSciHqClassify
- var nextClassify *data_manage.BaseFromSciHqClassify
- //如果有传入 上一个兄弟节点分类id
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.GetSciHqClassifyById(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.GetSciHqClassifyById(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.GetSciHqIndexClassifyMinSort(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.MoveDownSciHqIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- classifyInfo.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
- //往上移动
- err = data_manage.MoveUpSciHqIndexClassifyBySort(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
- }
- // MoveSciHqData 移动卓创红期指标
- func MoveSciHqData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
- //分类信息
- sciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- //判断分类是否存在
- if classifyId > 0 {
- _, err = data_manage.GetSciHqClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到该分类"
- err = nil
- }
- return
- }
- }
- //如果改变了分类,那么移动该图表数据
- if sciHqIndex.ClassifyId != classifyId {
- tmpErr := data_manage.MoveSciHqIndex(indexId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("移动失败,Err:" + tmpErr.Error())
- return
- }
- }
- //移动排序
- updateCol := make([]string, 0)
- var currentSort, prevSort, nextSort int
- currentSort = sciHqIndex.Sort
- //如果有传入 上一个兄弟节点分类id
- var prevIndex *data_manage.BaseFromSciHqIndex
- var nextIndex *data_manage.BaseFromSciHqIndex
- if prevIndexId > 0 {
- prevIndex, err = data_manage.GetSciHqIndexByIndexId(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.GetSciHqIndexByIndexId(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.GetSciHqIndexMinSortByClassifyId(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.MoveDownSciHqIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- sciHqIndex.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
- //往上移动
- err = data_manage.MoveUpSciHqIndexBySort(classifyId, nextSort, currentSort)
- if err != nil {
- err = errors.New("向上移动出错:" + err.Error())
- return
- }
- sciHqIndex.Sort = nextSort
- }
- //更新
- sciHqIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- err = sciHqIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
|