base_from_smm.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /**
  2. * @Author: jgl
  3. * @Date: 2021/9/14 11:15
  4. */
  5. package data
  6. import (
  7. "crypto/md5"
  8. "encoding/hex"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "eta/eta_api/models/data_manage"
  13. "eta/eta_api/models/system"
  14. "eta/eta_api/utils"
  15. "io/ioutil"
  16. "net/http"
  17. "net/url"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. type YsIndex struct {
  23. Code int64 `json:"code"`
  24. Data struct {
  25. Apis []struct {
  26. Aid int64 `json:"aid"`
  27. APILabel string `json:"api_label"`
  28. APIStartTime string `json:"api_start_time"`
  29. APIType int64 `json:"api_type"`
  30. APITypeAll string `json:"api_type_all"`
  31. APIUpdate int64 `json:"api_update"`
  32. APIUpdateTime string `json:"api_update_time"`
  33. APIUpdateType string `json:"api_update_type"`
  34. AuthDur int64 `json:"auth_dur"`
  35. AuthDurType string `json:"auth_dur_type"`
  36. AuthLang string `json:"auth_lang"`
  37. AuthModule string `json:"auth_module"`
  38. EditPerson string `json:"edit_person"`
  39. EditTime string `json:"edit_time"`
  40. Enable string `json:"enable"`
  41. FinishTime string `json:"finish_time"`
  42. Interface string `json:"interface"`
  43. Name string `json:"name"`
  44. StartTime string `json:"start_time"`
  45. Type1 string `json:"type_1"`
  46. Type2 string `json:"type_2"`
  47. Type3 string `json:"type_3"`
  48. } `json:"apis"`
  49. Types []struct {
  50. Name string `json:"name"`
  51. Tid int64 `json:"tid"`
  52. } `json:"types"`
  53. } `json:"data"`
  54. Msg string `json:"msg"`
  55. }
  56. // 已作废,暂时保留
  57. func GetSmmIndexData() {
  58. list, err := data_manage.GetBaseFromSmmList()
  59. if err != nil {
  60. fmt.Println("GetBaseFromSmmList Err:" + err.Error())
  61. return
  62. }
  63. token, err := getToken("pqian@hzinsights.com", "hz123456")
  64. if err != nil {
  65. fmt.Println(err)
  66. return
  67. }
  68. endDate := time.Now().Format(utils.FormatDate)
  69. smmIndexAll, err := data_manage.GetBaseFromSmmIndex()
  70. if err != nil {
  71. fmt.Println("GetBaseFromSmmIndex Err:" + err.Error())
  72. return
  73. }
  74. existIndexMap := make(map[string]*data_manage.BaseFromSmmIndex)
  75. for _, item := range smmIndexAll {
  76. existIndexMap[item.IndexCode] = item
  77. }
  78. dataAllList, err := data_manage.GetBaseFromSmmDataAll()
  79. if err != nil {
  80. fmt.Println("GetBaseFromSmmData Err:" + err.Error())
  81. return
  82. }
  83. existDataMap := make(map[string]*data_manage.BaseFromSmmData)
  84. for _, item := range dataAllList {
  85. key := item.IndexCode + item.DataTime
  86. existDataMap[key] = item
  87. }
  88. for _, v := range list {
  89. edbCode := v.Interface
  90. startDate := v.ApiStartTime
  91. ysItem, err := getApiData(token, edbCode, startDate, endDate)
  92. if err != nil {
  93. fmt.Println(err)
  94. return
  95. }
  96. if ysItem != nil && ysItem.Code == 200 {
  97. frequency := ysItem.Data.Frequency
  98. var smmIndexIdArr []int64
  99. var indexCodeArr []string
  100. indexKey := 0
  101. containsCount := 0
  102. for fk, fv := range ysItem.Data.Field {
  103. if fk == 0 {
  104. continue
  105. }
  106. if strings.Contains(fv.Name, "产品名称") ||
  107. strings.Contains(fv.Name, "单位") ||
  108. strings.Contains(fv.Name, "时间") {
  109. containsCount++
  110. }
  111. if !strings.Contains(fv.Name, "产品名称") &&
  112. !strings.Contains(fv.Name, "单位") &&
  113. !strings.Contains(fv.Name, "时间") {
  114. indexKey += 1
  115. indexCode := v.Interface + "_" + strconv.Itoa(indexKey)
  116. if findItem, ok := existIndexMap[indexCode]; !ok {
  117. ssmIndex := new(data_manage.BaseFromSmmIndex)
  118. ssmIndex.Interface = v.Interface
  119. ssmIndex.Name = v.Name
  120. ssmIndex.IndexCode = indexCode
  121. ssmIndex.IndexName = v.Name + "_" + fv.Name
  122. ssmIndex.Type1 = v.Type1
  123. ssmIndex.Type2 = v.Type2
  124. ssmIndex.Type3 = v.Type3
  125. ssmIndex.Frequency = frequency
  126. ssmIndex.Unit = fv.Unit
  127. ssmIndex.ApiStartTime = v.ApiStartTime
  128. ssmIndex.ApiUpdateTime = v.ApiUpdateTime
  129. ssmIndex.StartTime = v.StartTime
  130. ssmIndex.FinishTime = v.FinishTime
  131. ssmIndex.CreateTime = time.Now()
  132. ssmIndex.ModifyTime = time.Now()
  133. lastIndexId, err := data_manage.AddBaseFromSmmIndex(ssmIndex)
  134. if err != nil {
  135. return
  136. }
  137. smmIndexIdArr = append(smmIndexIdArr, lastIndexId)
  138. indexCodeArr = append(indexCodeArr, indexCode)
  139. } else {
  140. smmIndexIdArr = append(smmIndexIdArr, int64(findItem.BaseFromSmmIndexId))
  141. indexCodeArr = append(indexCodeArr, findItem.IndexCode)
  142. }
  143. }
  144. }
  145. for _, dv := range ysItem.Data.Content {
  146. var dataTime string
  147. for dataK, dataV := range dv {
  148. fmt.Println(dataK, dataV)
  149. if dataK == 0 {
  150. dataTime = dataV
  151. if frequency == "月" {
  152. monthDate, err := time.Parse("2006-01", dataTime)
  153. if err != nil {
  154. fmt.Println("time.Parse:" + err.Error())
  155. }
  156. lastTime := monthDate.AddDate(0, 1, -1)
  157. lastYear, lastMonth, lastDay := lastTime.Date()
  158. var lastDate string
  159. if int(lastMonth) < 10 {
  160. lastDate = strconv.Itoa(lastYear) + "-" + "0" + strconv.Itoa(int(lastMonth)) + "-" + strconv.Itoa(lastDay)
  161. } else {
  162. lastDate = strconv.Itoa(lastYear) + "-" + strconv.Itoa(int(lastMonth)) + "-" + strconv.Itoa(lastDay)
  163. }
  164. dataTime = lastDate
  165. }
  166. } else {
  167. if !strings.Contains(ysItem.Data.Field[dataK].Name, "产品名称") &&
  168. !strings.Contains(ysItem.Data.Field[dataK].Name, "单位") &&
  169. !strings.Contains(ysItem.Data.Field[dataK].Name, "时间") {
  170. saveDataTime, err := time.Parse(utils.FormatDate, dataTime)
  171. if err != nil {
  172. }
  173. timestamp := saveDataTime.UnixNano() / 1e6
  174. var smmIndexId int64
  175. var indexCode string
  176. smmIndexId = smmIndexIdArr[dataK-(containsCount+1)]
  177. indexCode = indexCodeArr[dataK-(containsCount+1)]
  178. key := indexCode + dataTime
  179. val := strings.Replace(dataV, ",", "", -1)
  180. if findData, dataOk := existDataMap[key]; !dataOk {
  181. dataItem := new(data_manage.BaseFromSmmData)
  182. dataItem.BaseFromSmmIndexId = int(smmIndexId)
  183. dataItem.IndexCode = indexCode
  184. dataItem.DataTime = dataTime
  185. dataItem.Value = val
  186. dataItem.CreateTime = time.Now()
  187. dataItem.ModifyTime = time.Now()
  188. dataItem.DataTimestamp = timestamp
  189. _, err = data_manage.AddBaseFromSmmData(dataItem)
  190. if err != nil && !strings.Contains(err.Error(), "idx_index_code_date") {
  191. fmt.Println("AddBaseFromSmmData Err:" + err.Error())
  192. return
  193. }
  194. } else {
  195. if findData != nil && findData.Value != val { //修改
  196. err = data_manage.ModifyBaseFromSmmData(findData.SmmDataId, val)
  197. if err != nil {
  198. fmt.Println("ModifyBaseFromSmmData Err:" + err.Error())
  199. return
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. //修改数据开始,结束日期
  209. {
  210. indexList, err := data_manage.GetBaseFromSmmIndexBySmmCode(v.Interface)
  211. if err != nil {
  212. fmt.Println("GetBaseFromSmmIndexByCode Err:" + err.Error())
  213. }
  214. for _, sv := range indexList {
  215. minDate, maxDate, err := data_manage.GetBaseFromSmmMaxOrMinDate(sv.IndexCode)
  216. if err != nil {
  217. fmt.Println("GetEdbDataSmmMaxOrMinDate Err:" + err.Error())
  218. } else {
  219. err = data_manage.ModifyBaseFromSmmMinDateAndMaxDate(sv.BaseFromSmmIndexId, minDate, maxDate)
  220. if err != nil {
  221. fmt.Println("GetEdbDataSmmMaxOrMinDate Err:" + err.Error())
  222. }
  223. }
  224. }
  225. }
  226. fmt.Println(v.Interface, v.Name)
  227. time.Sleep(3 * time.Second)
  228. }
  229. }
  230. const (
  231. dataUrl = "https://dataapi.smm.cn/GetData/" //data url (中文)
  232. //dataUrl = "https://dataapi.smm.cn/GetDataEn/" //data url (english edition)
  233. authUrl = "https://platform.smm.cn/usercenter/auth" // auth url (for all)
  234. )
  235. type TokenResp struct {
  236. Code int `json:"Code"`
  237. Msg string `json:"Msg"`
  238. Data TokenData `json:"Data"`
  239. }
  240. type TokenData struct {
  241. Token string `json:"Token"`
  242. }
  243. // 获取token
  244. func getToken(userName string, password string) (string, error) {
  245. encryptAuth := md5.New()
  246. encryptAuth.Write([]byte(password)) //encrypt password with md5
  247. newPassword := hex.EncodeToString(encryptAuth.Sum(nil))
  248. resp, err := http.PostForm(authUrl, url.Values{"user_name": {userName}, "password": {newPassword}})
  249. if err != nil {
  250. return "", err
  251. }
  252. defer resp.Body.Close()
  253. body, err := ioutil.ReadAll(resp.Body)
  254. if err != nil {
  255. fmt.Println("reponse error", err)
  256. return "", err
  257. }
  258. var bodyJsonContent TokenResp
  259. if err = json.Unmarshal([]byte(body), &bodyJsonContent); err != nil {
  260. fmt.Println(err, "unmarsal failure")
  261. return "", err
  262. }
  263. var token string
  264. if bodyJsonContent.Code == 0 {
  265. token = bodyJsonContent.Data.Token
  266. }
  267. //print(token)
  268. return token, nil
  269. }
  270. // request response
  271. type DataResp struct {
  272. Code int `json:"Code"`
  273. Msg string `json:"Msg"`
  274. Data *ApiData `json:"Data"`
  275. }
  276. // api data response
  277. type ApiData struct {
  278. Status int `json:"Status"` //0 no permission,1 ok
  279. Field []ApiField `json:"Field"`
  280. Content [][]string `json:"Content"`
  281. }
  282. // api title
  283. type ApiField struct {
  284. Unit string `json:"Unit"`
  285. Info string `json:"Info"`
  286. Name string `json:"Name"`
  287. ColumnType string `json:"ColumnType"`
  288. ColIndex uint `json:"ColIndex"`
  289. IsDate string `json:"IsDate"`
  290. }
  291. type YsResult struct {
  292. Code int64 `json:"Code"`
  293. Data struct {
  294. CompanyList []interface{} `json:"CompanyList"`
  295. Content [][]string `json:"Content"`
  296. Field []struct {
  297. ColIndex int64 `json:"ColIndex"`
  298. ColumnType string `json:"ColumnType"`
  299. Info string `json:"Info"`
  300. IsDate string `json:"IsDate"`
  301. Name string `json:"Name"`
  302. Unit string `json:"Unit"`
  303. DBColName string `json:"db_col_name"`
  304. } `json:"Field"`
  305. CountPage int64 `json:"count_page"`
  306. CurrentPage int64 `json:"current_page"`
  307. Frequency string `json:"frequency"`
  308. Mindate string `json:"mindate"`
  309. PageNum int64 `json:"page_num"`
  310. Status int64 `json:"status"`
  311. TotalNum int64 `json:"total_num"`
  312. } `json:"Data"`
  313. Msg string `json:"Msg"`
  314. }
  315. /*
  316. * request data
  317. * sdatetime,edatetime ==>format:yyyy-mm-dd,
  318. * apiName ==> data.metal.com(for english)/data.smm.cn (for chinese)
  319. */
  320. func getApiData(token string, apiName string, sdatetime string, edatetime string) (item *YsResult, err error) {
  321. reqUrl := dataUrl + apiName
  322. resp, err := http.PostForm(reqUrl, url.Values{"token": {token}, "sdatetime": {sdatetime}, "edatetime": {edatetime}})
  323. if err != nil {
  324. return nil, err
  325. }
  326. defer resp.Body.Close()
  327. body, err := ioutil.ReadAll(resp.Body)
  328. if err != nil {
  329. fmt.Println("response error")
  330. return nil, err
  331. }
  332. utils.FileLog.Info("ys result:" + string(body))
  333. dataJsonContent := new(YsResult)
  334. if err = json.Unmarshal([]byte(body), &dataJsonContent); err != nil {
  335. fmt.Println(err, "data unmarshal failure")
  336. return nil, err
  337. }
  338. if dataJsonContent.Code == 200 && len(dataJsonContent.Data.Content) > 0 {
  339. return dataJsonContent, nil
  340. } else {
  341. err = errors.New("code:" + strconv.Itoa(int(dataJsonContent.Code)) + "msg:" + dataJsonContent.Msg)
  342. }
  343. return nil, nil
  344. }
  345. // AddSmmClassify 添加SMM分类
  346. func AddSmmClassify(classifyName string, parentId, sysUserId int, sysUserName string) (classifyInfo *data_manage.BaseFromSmmClassify, err error, errMsg string) {
  347. count, err := data_manage.GetBaseFromSmmClassifyCount(classifyName, parentId)
  348. if err != nil {
  349. errMsg = `判断名称是否已存在失败`
  350. return
  351. }
  352. if count > 0 {
  353. errMsg = `分类名称已存在,请重新输入`
  354. return
  355. }
  356. level := 1
  357. if parentId > 0 {
  358. parentClassify, e := data_manage.GetBaseFromSmmClassifyById(parentId)
  359. if e != nil {
  360. if e.Error() == utils.ErrNoRow() {
  361. err = e
  362. errMsg = `一级分类不存在`
  363. return
  364. }
  365. err = e
  366. errMsg = `查询一级分类失败`
  367. return
  368. }
  369. level = parentClassify.Level + 1
  370. }
  371. classifyInfo = &data_manage.BaseFromSmmClassify{
  372. ClassifyName: classifyName,
  373. ParentId: parentId,
  374. SysUserId: sysUserId,
  375. SysUserRealName: sysUserName,
  376. Sort: 0,
  377. Level: level,
  378. ModifyTime: time.Now(),
  379. CreateTime: time.Now(),
  380. }
  381. classifyId, err := data_manage.AddBaseFromSmmClassify(classifyInfo)
  382. if err != nil {
  383. errMsg = "保存分类失败"
  384. return
  385. }
  386. classifyInfo.ClassifyId = int(classifyId)
  387. //更新sort为classifyId
  388. err = data_manage.UpdateBaseFromSmmClassifySort(int(classifyId))
  389. return
  390. }
  391. // EditSmmClassify 编辑Smm分类
  392. func EditSmmClassify(classifyId int, classifyName string, sysUser *system.Admin) (err error, errMsg string) {
  393. item, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
  394. if err != nil {
  395. errMsg = `保存失败`
  396. return
  397. }
  398. // 名字相同,那么就直接返回
  399. if item.ClassifyName == classifyName {
  400. return
  401. }
  402. // 判断名称是否已存在
  403. count, err := data_manage.GetBaseFromSmmClassifyCount(classifyName, item.ParentId)
  404. if err != nil {
  405. errMsg = "判断名称是否已存在失败"
  406. return
  407. }
  408. if count > 0 {
  409. errMsg = "分类名称已存在,请重新输入"
  410. err = errors.New(errMsg)
  411. return
  412. }
  413. // 修改数据
  414. err = data_manage.EditBaseFromSmmClassify(classifyId, classifyName)
  415. if err != nil {
  416. errMsg = "保存失败"
  417. }
  418. return
  419. }
  420. // MoveSmmClassify 移动Smm分类
  421. func MoveSmmClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  422. //判断分类是否存在
  423. classifyInfo, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
  424. if err != nil {
  425. errMsg = "移动失败"
  426. err = errors.New("获取分类信息失败,Err:" + err.Error())
  427. return
  428. }
  429. updateCol := make([]string, 0)
  430. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  431. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  432. parentClassifyInfo, tmpErr := data_manage.GetBaseFromSmmClassifyById(parentClassifyId)
  433. if tmpErr != nil {
  434. errMsg = "移动失败"
  435. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  436. return
  437. }
  438. classifyInfo.ParentId = parentClassifyInfo.ClassifyId
  439. classifyInfo.Level = parentClassifyInfo.Level + 1
  440. updateCol = append(updateCol, "ParentId", "Level")
  441. }
  442. var currentSort, prevSort, nextSort int
  443. currentSort = classifyInfo.Sort
  444. var prevClassify *data_manage.BaseFromSmmClassify
  445. var nextClassify *data_manage.BaseFromSmmClassify
  446. //如果有传入 上一个兄弟节点分类id
  447. if prevClassifyId > 0 {
  448. prevClassify, err = data_manage.GetBaseFromSmmClassifyById(prevClassifyId)
  449. if err != nil {
  450. if err.Error() == utils.ErrNoRow() {
  451. errMsg = "移动失败"
  452. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  453. return
  454. }
  455. errMsg = "移动失败"
  456. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  457. return
  458. }
  459. prevSort = prevClassify.Sort
  460. }
  461. if nextClassifyId > 0 {
  462. //下一个兄弟节点
  463. nextClassify, err = data_manage.GetBaseFromSmmClassifyById(nextClassifyId)
  464. if err != nil {
  465. if err.Error() == utils.ErrNoRow() {
  466. errMsg = "移动失败"
  467. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  468. return
  469. }
  470. errMsg = "移动失败"
  471. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  472. return
  473. }
  474. nextSort = nextClassify.Sort
  475. }
  476. // 修改历史数据中的排序为0的情况
  477. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  478. //更新为0排序的数据为当前最小排序
  479. if nextClassify != nil {
  480. minSort, e := data_manage.GetSmmIndexClassifyMinSort(parentClassifyId)
  481. if e != nil {
  482. errMsg = "移动失败"
  483. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  484. return
  485. }
  486. if minSort > 1 {
  487. minSort -= 1
  488. }
  489. nextClassify.Sort = minSort
  490. err = nextClassify.Update([]string{"Sort"})
  491. if err != nil {
  492. errMsg = "移动失败"
  493. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  494. return
  495. }
  496. nextSort = minSort
  497. }
  498. }
  499. //移到两个排序值中间操作
  500. if prevSort >= currentSort {
  501. //往下移动
  502. err = data_manage.MoveDownSmmIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  503. if err != nil {
  504. err = errors.New("向下移动出错:" + err.Error())
  505. return
  506. }
  507. classifyInfo.Sort = prevSort
  508. } else if nextSort <= currentSort && nextSort != 0 {
  509. //往上移动
  510. err = data_manage.MoveUpSmmIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  511. if err != nil {
  512. err = errors.New("向上移动出错:" + err.Error())
  513. return
  514. }
  515. classifyInfo.Sort = nextSort
  516. }
  517. classifyInfo.ModifyTime = time.Now()
  518. updateCol = append(updateCol, "Sort", "ModifyTime")
  519. err = classifyInfo.Update(updateCol)
  520. if err != nil {
  521. errMsg = "移动失败"
  522. err = errors.New("修改失败,Err:" + err.Error())
  523. return
  524. }
  525. return
  526. }
  527. // DelSmmClassify 删除SMM分类
  528. func DelSmmClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
  529. //判断分类是否存在
  530. classifyInfo, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
  531. if err != nil {
  532. errMsg = "删除失败"
  533. err = errors.New("获取分类信息失败,Err:" + err.Error())
  534. return
  535. }
  536. // 需要删除的分类id集合
  537. classifyIdList := make([]int, 0)
  538. classifyIdList = append(classifyIdList, classifyId)
  539. // 查找所有的子集id
  540. if classifyInfo.ParentId <= 0 {
  541. childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromSmmClassifyByParentId(classifyId)
  542. if tmpErr != nil {
  543. errMsg = "删除失败"
  544. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  545. return
  546. }
  547. for _, v := range childrenClassifyInfoList {
  548. classifyIdList = append(classifyIdList, v.ClassifyId)
  549. }
  550. }
  551. // 获取分类下的所有指标
  552. count, err := data_manage.GetBaseFromSmmIndexCountByClassifyIds(classifyIdList)
  553. if err != nil {
  554. errMsg = "删除失败"
  555. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  556. return
  557. }
  558. if count > 0 {
  559. errMsg = "该分类下有指标,不可删除"
  560. err = errors.New("该分类下有指标,不可删除")
  561. return
  562. }
  563. // 删除对应的分类
  564. err = data_manage.DeleteSmmClassifyByClassifyId(classifyIdList)
  565. if err != nil {
  566. return
  567. }
  568. return
  569. }
  570. // MoveSmmData 移动SMM指标
  571. func MoveSmmData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  572. //分类信息
  573. smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  574. if err != nil {
  575. errMsg = `获取数据失败`
  576. if err.Error() == utils.ErrNoRow() {
  577. errMsg = `该指标未入库`
  578. err = nil
  579. }
  580. return
  581. }
  582. //判断分类是否存在
  583. if classifyId > 0 {
  584. _, err = data_manage.GetBaseFromSmmClassifyById(classifyId)
  585. if err != nil {
  586. errMsg = "移动失败"
  587. err = errors.New("获取分类信息失败,Err:" + err.Error())
  588. if err.Error() == utils.ErrNoRow() {
  589. errMsg = "找不到该分类"
  590. err = nil
  591. }
  592. return
  593. }
  594. }
  595. //如果改变了分类,那么移动该图表数据
  596. if smmIndex.ClassifyId != classifyId {
  597. tmpErr := data_manage.MoveBaseFromSmmIndex(indexId, classifyId)
  598. if tmpErr != nil {
  599. errMsg = "移动失败"
  600. err = errors.New("移动失败,Err:" + tmpErr.Error())
  601. return
  602. }
  603. }
  604. //移动排序
  605. updateCol := make([]string, 0)
  606. //如果有传入 上一个兄弟节点分类id
  607. if prevIndexId > 0 {
  608. prevIndex, tmpErr := data_manage.GetBaseFromSmmIndexByIndexId(prevIndexId)
  609. if tmpErr != nil {
  610. errMsg = "移动失败"
  611. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  612. return
  613. }
  614. //如果是移动在两个兄弟节点之间
  615. if nextIndexId > 0 {
  616. //下一个兄弟节点
  617. nextIndex, tmpErr := data_manage.GetBaseFromSmmIndexByIndexId(nextIndexId)
  618. if tmpErr != nil {
  619. errMsg = "移动失败"
  620. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  621. return
  622. }
  623. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  624. if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == smmIndex.Sort {
  625. //变更兄弟节点的排序
  626. updateSortStr := `sort + 2`
  627. err = data_manage.UpdateBaseFromSmmIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSmmIndexId, updateSortStr)
  628. if err != nil {
  629. errMsg = "移动失败"
  630. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  631. return
  632. }
  633. } else {
  634. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  635. if nextIndex.Sort-prevIndex.Sort == 1 {
  636. //变更兄弟节点的排序
  637. updateSortStr := `sort + 1`
  638. err = data_manage.UpdateBaseFromSmmIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSmmIndexId, updateSortStr)
  639. if err != nil {
  640. errMsg = "移动失败"
  641. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  642. return
  643. }
  644. }
  645. }
  646. }
  647. smmIndex.Sort = prevIndex.Sort + 1
  648. smmIndex.ModifyTime = time.Now()
  649. updateCol = append(updateCol, "Sort", "ModifyTime")
  650. } else {
  651. firstClassify, tmpErr := data_manage.GetFirstBaseFromSmmIndexByClassifyId(classifyId)
  652. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  653. errMsg = "移动失败"
  654. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  655. return
  656. }
  657. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  658. if firstClassify != nil && firstClassify.Sort == 0 {
  659. updateSortStr := ` sort + 1 `
  660. _ = data_manage.UpdateBaseFromSmmIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromSmmIndexId-1, updateSortStr)
  661. }
  662. smmIndex.Sort = 0 //那就是排在第一位
  663. smmIndex.ModifyTime = time.Now()
  664. updateCol = append(updateCol, "Sort", "ModifyTime")
  665. }
  666. //更新
  667. if len(updateCol) > 0 {
  668. err = smmIndex.Update(updateCol)
  669. if err != nil {
  670. errMsg = "移动失败"
  671. err = errors.New("修改失败,Err:" + err.Error())
  672. return
  673. }
  674. }
  675. return
  676. }
  677. // MoveSmmData2 移动SMM指标
  678. func MoveSmmData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  679. //分类信息
  680. smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  681. if err != nil {
  682. errMsg = `获取数据失败`
  683. if err.Error() == utils.ErrNoRow() {
  684. errMsg = `该指标未入库`
  685. err = nil
  686. }
  687. return
  688. }
  689. //判断分类是否存在
  690. if classifyId > 0 {
  691. _, err = data_manage.GetBaseFromSmmClassifyById(classifyId)
  692. if err != nil {
  693. errMsg = "移动失败"
  694. err = errors.New("获取分类信息失败,Err:" + err.Error())
  695. if err.Error() == utils.ErrNoRow() {
  696. errMsg = "找不到该分类"
  697. err = nil
  698. }
  699. return
  700. }
  701. }
  702. //如果改变了分类,那么移动该图表数据
  703. if smmIndex.ClassifyId != classifyId {
  704. tmpErr := data_manage.MoveBaseFromSmmIndex(indexId, classifyId)
  705. if tmpErr != nil {
  706. errMsg = "移动失败"
  707. err = errors.New("移动失败,Err:" + tmpErr.Error())
  708. return
  709. }
  710. }
  711. //移动排序
  712. updateCol := make([]string, 0)
  713. var currentSort, prevSort, nextSort int
  714. currentSort = smmIndex.Sort
  715. //如果有传入 上一个兄弟节点分类id
  716. var prevIndex *data_manage.BaseFromSmmIndex
  717. var nextIndex *data_manage.BaseFromSmmIndex
  718. if prevIndexId > 0 {
  719. prevIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(prevIndexId)
  720. if err != nil {
  721. if err.Error() == utils.ErrNoRow() {
  722. errMsg = "移动失败"
  723. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  724. return
  725. }
  726. errMsg = "移动失败"
  727. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  728. return
  729. }
  730. prevSort = prevIndex.Sort
  731. }
  732. if nextIndexId > 0 {
  733. //下一个兄弟节点
  734. nextIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(nextIndexId)
  735. if err != nil {
  736. if err.Error() == utils.ErrNoRow() {
  737. errMsg = "移动失败"
  738. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  739. return
  740. }
  741. errMsg = "移动失败"
  742. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  743. return
  744. }
  745. nextSort = nextIndex.Sort
  746. }
  747. // 修改历史数据中的排序为0的情况
  748. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  749. //更新为0排序的数据为当前最小排序
  750. if nextIndex != nil {
  751. minSort, e := data_manage.GetBaseFromSmmIndexMinSortByClassifyId(classifyId)
  752. if e != nil {
  753. errMsg = "移动失败"
  754. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  755. return
  756. }
  757. if minSort > 1 {
  758. minSort -= 1
  759. }
  760. nextIndex.Sort = minSort
  761. err = nextIndex.Update([]string{"Sort"})
  762. if err != nil {
  763. errMsg = "移动失败"
  764. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  765. return
  766. }
  767. nextSort = minSort
  768. }
  769. }
  770. //移到两个排序值中间操作
  771. if prevSort >= currentSort {
  772. //往下移动
  773. err = data_manage.MoveDownSmmIndexBySort(classifyId, prevSort, currentSort)
  774. if err != nil {
  775. err = errors.New("向下移动出错:" + err.Error())
  776. return
  777. }
  778. smmIndex.Sort = prevSort
  779. } else if nextSort <= currentSort && nextSort != 0 {
  780. //往上移动
  781. err = data_manage.MoveUpSmmIndexBySort(classifyId, nextSort, currentSort)
  782. if err != nil {
  783. err = errors.New("向上移动出错:" + err.Error())
  784. return
  785. }
  786. smmIndex.Sort = nextSort
  787. }
  788. //更新
  789. smmIndex.ModifyTime = time.Now()
  790. updateCol = append(updateCol, "Sort", "ModifyTime")
  791. err = smmIndex.Update(updateCol)
  792. if err != nil {
  793. errMsg = "移动失败"
  794. err = errors.New("修改失败,Err:" + err.Error())
  795. return
  796. }
  797. return
  798. }
  799. // EditSmmIndex 编辑SMM指标
  800. func EditSmmIndex(indexId, classifyId int, sysUser *system.Admin) (baseFromSmmIndex *data_manage.BaseFromSmmIndex, err error, errMsg string) {
  801. baseFromSmmIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  802. if err != nil {
  803. errMsg = `获取数据失败`
  804. if err.Error() == utils.ErrNoRow() {
  805. errMsg = `该指标未入库`
  806. err = nil
  807. }
  808. return
  809. }
  810. baseFromSmmIndex.ClassifyId = classifyId
  811. baseFromSmmIndex.ModifyTime = time.Now()
  812. err = baseFromSmmIndex.Update([]string{"ClassifyId", "ModifyTime"})
  813. if err != nil {
  814. errMsg = "保存分类失败"
  815. }
  816. return
  817. }
  818. func InitBaseFromSmmClassify() (err error) {
  819. //查询历史数据所有分类
  820. typeList, err := data_manage.GetBaseFromSmmIndexType()
  821. if err != nil {
  822. return
  823. }
  824. classifyList := make([]*data_manage.BaseFromSmmClassify, 0)
  825. classifyMap := make(map[string][]string, 0)
  826. parentClassifyMap := make(map[string]struct{}, 0)
  827. now := time.Now()
  828. for _, v := range typeList {
  829. if _, ok := parentClassifyMap[v.Type2]; !ok {
  830. parentClassifyMap[v.Type2] = struct{}{}
  831. tmp := new(data_manage.BaseFromSmmClassify)
  832. tmp.ClassifyName = v.Type2
  833. tmp.Level = 1
  834. tmp.ModifyTime = now
  835. tmp.CreateTime = now
  836. classifyList = append(classifyList, tmp)
  837. }
  838. classifyMap[v.Type2] = append(classifyMap[v.Type2], v.Type3)
  839. }
  840. //添加一级分类
  841. err = data_manage.AddSmmClassifyMulti(classifyList)
  842. if err != nil {
  843. err = errors.New("新增分类出错:" + err.Error())
  844. return
  845. }
  846. //查询分类表的所有父级
  847. rootList, err := data_manage.GetBaseFromSmmClassifyByParentId(0)
  848. if err != nil {
  849. return
  850. }
  851. classifyIdMap := make(map[string]int)
  852. for _, v := range rootList {
  853. classifyIdMap[v.ClassifyName] = v.ClassifyId
  854. }
  855. classifyList2 := make([]*data_manage.BaseFromSmmClassify, 0)
  856. for _, v := range typeList {
  857. if parentId, ok := classifyIdMap[v.Type2]; ok {
  858. tmp := new(data_manage.BaseFromSmmClassify)
  859. tmp.ParentId = parentId
  860. tmp.ClassifyName = v.Type3
  861. tmp.Level = 2
  862. tmp.ModifyTime = now
  863. tmp.CreateTime = now
  864. classifyList2 = append(classifyList2, tmp)
  865. }
  866. }
  867. //添加二级分类
  868. err = data_manage.AddSmmClassifyMulti(classifyList2)
  869. if err != nil {
  870. err = errors.New("新增二级分类出错:" + err.Error())
  871. return
  872. }
  873. //初始化sort字段
  874. err = data_manage.InitSmmClassifySort()
  875. if err != nil {
  876. return
  877. }
  878. //更新index表里的分类id
  879. err = data_manage.InitSmmIndexClassifyId()
  880. return
  881. }
  882. // DelSmmData 删除SMM指标
  883. func DelSmmData(indexId int) (err error, errMsg string) {
  884. baseFromSmmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  885. if err != nil {
  886. errMsg = `获取数据失败`
  887. if err.Error() == utils.ErrNoRow() {
  888. errMsg = `该指标未入库`
  889. err = nil
  890. }
  891. return
  892. }
  893. // 获取已经加入到EDB指标库的Smm指标
  894. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, baseFromSmmIndex.IndexCode)
  895. if err != nil && err.Error() != utils.ErrNoRow() {
  896. errMsg = "删除失败"
  897. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  898. return
  899. }
  900. if edbInfo != nil {
  901. errMsg = "当前指标已被引用,不可删除"
  902. err = nil
  903. return
  904. }
  905. // 删除对应的分类和指标
  906. err = data_manage.DeleteBaseFromSmmIndexById(indexId)
  907. if err != nil {
  908. return
  909. }
  910. return
  911. }
  912. // ResetSmmData 指标数据清除分类接口
  913. func ResetSmmData(indexId int) (err error, errMsg string) {
  914. smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  915. if err != nil {
  916. errMsg = `获取数据失败`
  917. if err.Error() == utils.ErrNoRow() {
  918. errMsg = `该指标未入库`
  919. err = nil
  920. }
  921. return
  922. }
  923. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  924. //移动排序
  925. classifyId := 0
  926. updateCol := make([]string, 0)
  927. var currentSort, prevSort int
  928. currentSort = smmIndex.Sort
  929. //未分类的最大的sort值
  930. prevSort, err = data_manage.GetBaseFromSmmIndexMaxSortByClassifyId(classifyId)
  931. if err != nil {
  932. errMsg = "移动失败"
  933. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  934. return
  935. }
  936. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  937. if prevSort >= currentSort {
  938. //往下移动
  939. err = data_manage.MoveDownSmmIndexBySort(classifyId, prevSort, currentSort)
  940. if err != nil {
  941. err = errors.New("向下移动出错:" + err.Error())
  942. return
  943. }
  944. smmIndex.Sort = prevSort
  945. }
  946. //更新
  947. smmIndex.ModifyTime = time.Now()
  948. smmIndex.ClassifyId = classifyId
  949. updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
  950. err = smmIndex.Update(updateCol)
  951. if err != nil {
  952. errMsg = "移动失败"
  953. err = errors.New("修改失败,Err:" + err.Error())
  954. return
  955. }
  956. return
  957. }