123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077 |
- package data
- import (
- "crypto/md5"
- "encoding/hex"
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
- type YsIndex struct {
- Code int64 `json:"code"`
- Data struct {
- Apis []struct {
- Aid int64 `json:"aid"`
- APILabel string `json:"api_label"`
- APIStartTime string `json:"api_start_time"`
- APIType int64 `json:"api_type"`
- APITypeAll string `json:"api_type_all"`
- APIUpdate int64 `json:"api_update"`
- APIUpdateTime string `json:"api_update_time"`
- APIUpdateType string `json:"api_update_type"`
- AuthDur int64 `json:"auth_dur"`
- AuthDurType string `json:"auth_dur_type"`
- AuthLang string `json:"auth_lang"`
- AuthModule string `json:"auth_module"`
- EditPerson string `json:"edit_person"`
- EditTime string `json:"edit_time"`
- Enable string `json:"enable"`
- FinishTime string `json:"finish_time"`
- Interface string `json:"interface"`
- Name string `json:"name"`
- StartTime string `json:"start_time"`
- Type1 string `json:"type_1"`
- Type2 string `json:"type_2"`
- Type3 string `json:"type_3"`
- } `json:"apis"`
- Types []struct {
- Name string `json:"name"`
- Tid int64 `json:"tid"`
- } `json:"types"`
- } `json:"data"`
- Msg string `json:"msg"`
- }
- func GetSmmIndexData() {
- list, err := data_manage.GetBaseFromSmmList()
- if err != nil {
- fmt.Println("GetBaseFromSmmList Err:" + err.Error())
- return
- }
- token, err := getToken("pqian@hzinsights.com", "hz123456")
- if err != nil {
- fmt.Println(err)
- return
- }
- endDate := time.Now().Format(utils.FormatDate)
- smmIndexAll, err := data_manage.GetBaseFromSmmIndex()
- if err != nil {
- fmt.Println("GetBaseFromSmmIndex Err:" + err.Error())
- return
- }
- existIndexMap := make(map[string]*data_manage.BaseFromSmmIndex)
- for _, item := range smmIndexAll {
- existIndexMap[item.IndexCode] = item
- }
- dataAllList, err := data_manage.GetBaseFromSmmDataAll()
- if err != nil {
- fmt.Println("GetBaseFromSmmData Err:" + err.Error())
- return
- }
- existDataMap := make(map[string]*data_manage.BaseFromSmmData)
- for _, item := range dataAllList {
- key := item.IndexCode + item.DataTime
- existDataMap[key] = item
- }
- for _, v := range list {
- edbCode := v.Interface
- startDate := v.ApiStartTime
- ysItem, err := getApiData(token, edbCode, startDate, endDate)
- if err != nil {
- fmt.Println(err)
- return
- }
- if ysItem != nil && ysItem.Code == 200 {
- frequency := ysItem.Data.Frequency
- var smmIndexIdArr []int64
- var indexCodeArr []string
- indexKey := 0
- containsCount := 0
- for fk, fv := range ysItem.Data.Field {
- if fk == 0 {
- continue
- }
- if strings.Contains(fv.Name, "产品名称") ||
- strings.Contains(fv.Name, "单位") ||
- strings.Contains(fv.Name, "时间") {
- containsCount++
- }
- if !strings.Contains(fv.Name, "产品名称") &&
- !strings.Contains(fv.Name, "单位") &&
- !strings.Contains(fv.Name, "时间") {
- indexKey += 1
- indexCode := v.Interface + "_" + strconv.Itoa(indexKey)
- if findItem, ok := existIndexMap[indexCode]; !ok {
- ssmIndex := new(data_manage.BaseFromSmmIndex)
- ssmIndex.Interface = v.Interface
- ssmIndex.Name = v.Name
- ssmIndex.IndexCode = indexCode
- ssmIndex.IndexName = v.Name + "_" + fv.Name
- ssmIndex.Type1 = v.Type1
- ssmIndex.Type2 = v.Type2
- ssmIndex.Type3 = v.Type3
- ssmIndex.Frequency = frequency
- ssmIndex.Unit = fv.Unit
- ssmIndex.ApiStartTime = v.ApiStartTime
- ssmIndex.ApiUpdateTime = v.ApiUpdateTime
- ssmIndex.StartTime = v.StartTime
- ssmIndex.FinishTime = v.FinishTime
- ssmIndex.CreateTime = time.Now()
- ssmIndex.ModifyTime = time.Now()
- lastIndexId, err := data_manage.AddBaseFromSmmIndex(ssmIndex)
- if err != nil {
- return
- }
- smmIndexIdArr = append(smmIndexIdArr, lastIndexId)
- indexCodeArr = append(indexCodeArr, indexCode)
- } else {
- smmIndexIdArr = append(smmIndexIdArr, int64(findItem.BaseFromSmmIndexId))
- indexCodeArr = append(indexCodeArr, findItem.IndexCode)
- }
- }
- }
- for _, dv := range ysItem.Data.Content {
- var dataTime string
- for dataK, dataV := range dv {
- fmt.Println(dataK, dataV)
- if dataK == 0 {
- dataTime = dataV
- if frequency == "月" {
- monthDate, err := time.Parse("2006-01", dataTime)
- if err != nil {
- fmt.Println("time.Parse:" + err.Error())
- }
- lastTime := monthDate.AddDate(0, 1, -1)
- lastYear, lastMonth, lastDay := lastTime.Date()
- var lastDate string
- if int(lastMonth) < 10 {
- lastDate = strconv.Itoa(lastYear) + "-" + "0" + strconv.Itoa(int(lastMonth)) + "-" + strconv.Itoa(lastDay)
- } else {
- lastDate = strconv.Itoa(lastYear) + "-" + strconv.Itoa(int(lastMonth)) + "-" + strconv.Itoa(lastDay)
- }
- dataTime = lastDate
- }
- } else {
- if !strings.Contains(ysItem.Data.Field[dataK].Name, "产品名称") &&
- !strings.Contains(ysItem.Data.Field[dataK].Name, "单位") &&
- !strings.Contains(ysItem.Data.Field[dataK].Name, "时间") {
- saveDataTime, err := time.Parse(utils.FormatDate, dataTime)
- if err != nil {
- }
- timestamp := saveDataTime.UnixNano() / 1e6
- var smmIndexId int64
- var indexCode string
- smmIndexId = smmIndexIdArr[dataK-(containsCount+1)]
- indexCode = indexCodeArr[dataK-(containsCount+1)]
- key := indexCode + dataTime
- val := strings.Replace(dataV, ",", "", -1)
- if findData, dataOk := existDataMap[key]; !dataOk {
- dataItem := new(data_manage.BaseFromSmmData)
- dataItem.BaseFromSmmIndexId = int(smmIndexId)
- dataItem.IndexCode = indexCode
- dataItem.DataTime = dataTime
- dataItem.Value = val
- dataItem.CreateTime = time.Now()
- dataItem.ModifyTime = time.Now()
- dataItem.DataTimestamp = timestamp
- _, err = data_manage.AddBaseFromSmmData(dataItem)
- if err != nil && !strings.Contains(err.Error(), "idx_index_code_date") {
- fmt.Println("AddBaseFromSmmData Err:" + err.Error())
- return
- }
- } else {
- if findData != nil && findData.Value != val {
- err = data_manage.ModifyBaseFromSmmData(findData.SmmDataId, val)
- if err != nil {
- fmt.Println("ModifyBaseFromSmmData Err:" + err.Error())
- return
- }
- }
- }
- }
- }
- }
- }
- }
-
- {
- indexList, err := data_manage.GetBaseFromSmmIndexBySmmCode(v.Interface)
- if err != nil {
- fmt.Println("GetBaseFromSmmIndexByCode Err:" + err.Error())
- }
- for _, sv := range indexList {
- minDate, maxDate, err := data_manage.GetBaseFromSmmMaxOrMinDate(sv.IndexCode)
- if err != nil {
- fmt.Println("GetEdbDataSmmMaxOrMinDate Err:" + err.Error())
- } else {
- err = data_manage.ModifyBaseFromSmmMinDateAndMaxDate(sv.BaseFromSmmIndexId, minDate, maxDate)
- if err != nil {
- fmt.Println("GetEdbDataSmmMaxOrMinDate Err:" + err.Error())
- }
- }
- }
- }
- fmt.Println(v.Interface, v.Name)
- time.Sleep(3 * time.Second)
- }
- }
- const (
- dataUrl = "https://dataapi.smm.cn/GetData/"
-
- authUrl = "https://platform.smm.cn/usercenter/auth"
- )
- type TokenResp struct {
- Code int `json:"Code"`
- Msg string `json:"Msg"`
- Data TokenData `json:"Data"`
- }
- type TokenData struct {
- Token string `json:"Token"`
- }
- func getToken(userName string, password string) (string, error) {
- encryptAuth := md5.New()
- encryptAuth.Write([]byte(password))
- newPassword := hex.EncodeToString(encryptAuth.Sum(nil))
- resp, err := http.PostForm(authUrl, url.Values{"user_name": {userName}, "password": {newPassword}})
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("reponse error", err)
- return "", err
- }
- var bodyJsonContent TokenResp
- if err = json.Unmarshal([]byte(body), &bodyJsonContent); err != nil {
- fmt.Println(err, "unmarsal failure")
- return "", err
- }
- var token string
- if bodyJsonContent.Code == 0 {
- token = bodyJsonContent.Data.Token
- }
-
- return token, nil
- }
- type DataResp struct {
- Code int `json:"Code"`
- Msg string `json:"Msg"`
- Data *ApiData `json:"Data"`
- }
- type ApiData struct {
- Status int `json:"Status"`
- Field []ApiField `json:"Field"`
- Content [][]string `json:"Content"`
- }
- type ApiField struct {
- Unit string `json:"Unit"`
- Info string `json:"Info"`
- Name string `json:"Name"`
- ColumnType string `json:"ColumnType"`
- ColIndex uint `json:"ColIndex"`
- IsDate string `json:"IsDate"`
- }
- type YsResult struct {
- Code int64 `json:"Code"`
- Data struct {
- CompanyList []interface{} `json:"CompanyList"`
- Content [][]string `json:"Content"`
- Field []struct {
- ColIndex int64 `json:"ColIndex"`
- ColumnType string `json:"ColumnType"`
- Info string `json:"Info"`
- IsDate string `json:"IsDate"`
- Name string `json:"Name"`
- Unit string `json:"Unit"`
- DBColName string `json:"db_col_name"`
- } `json:"Field"`
- CountPage int64 `json:"count_page"`
- CurrentPage int64 `json:"current_page"`
- Frequency string `json:"frequency"`
- Mindate string `json:"mindate"`
- PageNum int64 `json:"page_num"`
- Status int64 `json:"status"`
- TotalNum int64 `json:"total_num"`
- } `json:"Data"`
- Msg string `json:"Msg"`
- }
- func getApiData(token string, apiName string, sdatetime string, edatetime string) (item *YsResult, err error) {
- reqUrl := dataUrl + apiName
- resp, err := http.PostForm(reqUrl, url.Values{"token": {token}, "sdatetime": {sdatetime}, "edatetime": {edatetime}})
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("response error")
- return nil, err
- }
- utils.FileLog.Info("ys result:" + string(body))
- dataJsonContent := new(YsResult)
- if err = json.Unmarshal([]byte(body), &dataJsonContent); err != nil {
- fmt.Println(err, "data unmarshal failure")
- return nil, err
- }
- if dataJsonContent.Code == 200 && len(dataJsonContent.Data.Content) > 0 {
- return dataJsonContent, nil
- } else {
- err = errors.New("code:" + strconv.Itoa(int(dataJsonContent.Code)) + "msg:" + dataJsonContent.Msg)
- }
- return nil, nil
- }
- func AddSmmClassify(classifyName string, parentId, sysUserId int, sysUserName, lang string) (classifyInfo *data_manage.BaseFromSmmClassify, err error, errMsg string) {
-
- {
- var count int
- switch lang {
- case utils.EnLangVersion:
- count, err = data_manage.GetBaseFromSmmClassifyEnCount(classifyName, parentId)
- default:
- count, err = data_manage.GetBaseFromSmmClassifyCount(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.GetBaseFromSmmClassifyById(parentId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- err = e
- errMsg = `一级分类不存在`
- return
- }
- err = e
- errMsg = `查询一级分类失败`
- return
- }
- level = parentClassify.Level + 1
- }
- classifyInfo = &data_manage.BaseFromSmmClassify{
- ClassifyName: classifyName,
- ClassifyNameEn: classifyName,
- ParentId: parentId,
- SysUserId: sysUserId,
- SysUserRealName: sysUserName,
- Sort: 0,
- Level: level,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- classifyId, err := data_manage.AddBaseFromSmmClassify(classifyInfo)
- if err != nil {
- errMsg = "保存分类失败"
- return
- }
- classifyInfo.ClassifyId = int(classifyId)
-
- err = data_manage.UpdateBaseFromSmmClassifySort(int(classifyId))
- return
- }
- func EditSmmClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
- item, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
- if err != nil {
- errMsg = `保存失败`
- return
- }
- switch lang {
- case utils.EnLangVersion:
-
- if item.ClassifyNameEn == classifyName {
- return
- }
-
- count, tmpErr := data_manage.GetBaseFromSmmClassifyEnCount(classifyName, item.ParentId)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
-
- err = data_manage.EditBaseFromSmmClassifyEn(classifyId, classifyName)
- if err != nil {
- errMsg = "保存失败"
- }
- default:
-
- if item.ClassifyName == classifyName {
- return
- }
-
- count, tmpErr := data_manage.GetBaseFromSmmClassifyCount(classifyName, item.ParentId)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
-
- err = data_manage.EditBaseFromSmmClassify(classifyId, classifyName)
- if err != nil {
- errMsg = "保存失败"
- }
- }
- return
- }
- func MoveSmmClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
-
- classifyInfo, err := data_manage.GetBaseFromSmmClassifyById(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.GetBaseFromSmmClassifyById(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.BaseFromSmmClassify
- var nextClassify *data_manage.BaseFromSmmClassify
-
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.GetBaseFromSmmClassifyById(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.GetBaseFromSmmClassifyById(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.GetSmmIndexClassifyMinSort(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.MoveDownSmmIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- classifyInfo.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
-
- err = data_manage.MoveUpSmmIndexClassifyBySort(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 DelSmmClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
-
- classifyInfo, err := data_manage.GetBaseFromSmmClassifyById(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.GetBaseFromSmmClassifyByParentId(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.GetBaseFromSmmIndexCountByClassifyIds(classifyIdList)
- if err != nil {
- errMsg = "删除失败"
- err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "该分类下有指标,不可删除"
- err = errors.New("该分类下有指标,不可删除")
- return
- }
-
- err = data_manage.DeleteSmmClassifyByClassifyId(classifyIdList)
- if err != nil {
- return
- }
- return
- }
- func MoveSmmData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
-
- smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
- if classifyId > 0 {
- _, err = data_manage.GetBaseFromSmmClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到该分类"
- err = nil
- }
- return
- }
- }
-
- if smmIndex.ClassifyId != classifyId {
- tmpErr := data_manage.MoveBaseFromSmmIndex(indexId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("移动失败,Err:" + tmpErr.Error())
- return
- }
- }
-
- updateCol := make([]string, 0)
-
- if prevIndexId > 0 {
- prevIndex, tmpErr := data_manage.GetBaseFromSmmIndexByIndexId(prevIndexId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if nextIndexId > 0 {
-
- nextIndex, tmpErr := data_manage.GetBaseFromSmmIndexByIndexId(nextIndexId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == smmIndex.Sort {
-
- updateSortStr := `sort + 2`
- err = data_manage.UpdateBaseFromSmmIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSmmIndexId, 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.UpdateBaseFromSmmIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSmmIndexId, updateSortStr)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- smmIndex.Sort = prevIndex.Sort + 1
- smmIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstClassify, tmpErr := data_manage.GetFirstBaseFromSmmIndexByClassifyId(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.UpdateBaseFromSmmIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromSmmIndexId-1, updateSortStr)
- }
- smmIndex.Sort = 0
- smmIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
-
- if len(updateCol) > 0 {
- err = smmIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- return
- }
- func MoveSmmData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
-
- smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
- if classifyId > 0 {
- _, err = data_manage.GetBaseFromSmmClassifyById(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到该分类"
- err = nil
- }
- return
- }
- }
-
- if smmIndex.ClassifyId != classifyId {
- tmpErr := data_manage.MoveBaseFromSmmIndex(indexId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("移动失败,Err:" + tmpErr.Error())
- return
- }
- }
-
- updateCol := make([]string, 0)
- var currentSort, prevSort, nextSort int
- currentSort = smmIndex.Sort
-
- var prevIndex *data_manage.BaseFromSmmIndex
- var nextIndex *data_manage.BaseFromSmmIndex
- if prevIndexId > 0 {
- prevIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(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.GetBaseFromSmmIndexByIndexId(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.GetBaseFromSmmIndexMinSortByClassifyId(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.MoveDownSmmIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- smmIndex.Sort = prevSort
- } else if nextSort <= currentSort && nextSort != 0 {
-
- err = data_manage.MoveUpSmmIndexBySort(classifyId, nextSort, currentSort)
- if err != nil {
- err = errors.New("向上移动出错:" + err.Error())
- return
- }
- smmIndex.Sort = nextSort
- }
-
- smmIndex.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- err = smmIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
- func EditSmmIndex(indexId, classifyId int, sysUser *system.Admin) (baseFromSmmIndex *data_manage.BaseFromSmmIndex, err error, errMsg string) {
- baseFromSmmIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
- baseFromSmmIndex.ClassifyId = classifyId
- baseFromSmmIndex.ModifyTime = time.Now()
- err = baseFromSmmIndex.Update([]string{"ClassifyId", "ModifyTime"})
- if err != nil {
- errMsg = "保存分类失败"
- }
- return
- }
- func InitBaseFromSmmClassify() (err error) {
-
- typeList, err := data_manage.GetBaseFromSmmIndexType()
- if err != nil {
- return
- }
- classifyList := make([]*data_manage.BaseFromSmmClassify, 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.BaseFromSmmClassify)
- 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.AddSmmClassifyMulti(classifyList)
- if err != nil {
- err = errors.New("新增分类出错:" + err.Error())
- return
- }
-
- rootList, err := data_manage.GetBaseFromSmmClassifyByParentId(0)
- if err != nil {
- return
- }
- classifyIdMap := make(map[string]int)
- for _, v := range rootList {
- classifyIdMap[v.ClassifyName] = v.ClassifyId
- }
- classifyList2 := make([]*data_manage.BaseFromSmmClassify, 0)
- for _, v := range typeList {
- if parentId, ok := classifyIdMap[v.Type2]; ok {
- tmp := new(data_manage.BaseFromSmmClassify)
- tmp.ParentId = parentId
- tmp.ClassifyName = v.Type3
- tmp.Level = 2
- tmp.ModifyTime = now
- tmp.CreateTime = now
- classifyList2 = append(classifyList2, tmp)
- }
- }
-
- err = data_manage.AddSmmClassifyMulti(classifyList2)
- if err != nil {
- err = errors.New("新增二级分类出错:" + err.Error())
- return
- }
-
- err = data_manage.InitSmmClassifySort()
- if err != nil {
- return
- }
-
- err = data_manage.InitSmmIndexClassifyId()
- return
- }
- func DelSmmData(indexId int) (err error, errMsg string) {
- baseFromSmmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
- if err != nil {
- errMsg = `获取数据失败`
- if err.Error() == utils.ErrNoRow() {
- errMsg = `该指标未入库`
- err = nil
- }
- return
- }
-
- edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, baseFromSmmIndex.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.DeleteBaseFromSmmIndexById(indexId)
- if err != nil {
- return
- }
- return
- }
- func ResetSmmData(indexId int) (err error, errMsg string) {
- smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(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 = smmIndex.Sort
-
- prevSort, err = data_manage.GetBaseFromSmmIndexMaxSortByClassifyId(classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
- return
- }
-
- if prevSort >= currentSort {
-
- err = data_manage.MoveDownSmmIndexBySort(classifyId, prevSort, currentSort)
- if err != nil {
- err = errors.New("向下移动出错:" + err.Error())
- return
- }
- smmIndex.Sort = prevSort
- }
-
- smmIndex.ModifyTime = time.Now()
- smmIndex.ClassifyId = classifyId
- updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
- err = smmIndex.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- return
- }
|