base_from_smm.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. "eta/eta_api/models/data_manage"
  12. "eta/eta_api/models/system"
  13. "eta/eta_api/utils"
  14. "fmt"
  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, lang string) (classifyInfo *data_manage.BaseFromSmmClassify, err error, errMsg string) {
  347. // 校验分类名称相同的数量
  348. {
  349. var count int
  350. switch lang {
  351. case utils.EnLangVersion:
  352. count, err = data_manage.GetBaseFromSmmClassifyEnCount(classifyName, parentId)
  353. default:
  354. count, err = data_manage.GetBaseFromSmmClassifyCount(classifyName, parentId)
  355. }
  356. if err != nil {
  357. errMsg = `判断名称是否已存在失败`
  358. return
  359. }
  360. if count > 0 {
  361. errMsg = `分类名称已存在,请重新输入`
  362. err = errors.New(errMsg)
  363. return
  364. }
  365. }
  366. level := 1
  367. if parentId > 0 {
  368. parentClassify, e := data_manage.GetBaseFromSmmClassifyById(parentId)
  369. if e != nil {
  370. if e.Error() == utils.ErrNoRow() {
  371. err = e
  372. errMsg = `一级分类不存在`
  373. return
  374. }
  375. err = e
  376. errMsg = `查询一级分类失败`
  377. return
  378. }
  379. level = parentClassify.Level + 1
  380. }
  381. classifyInfo = &data_manage.BaseFromSmmClassify{
  382. ClassifyName: classifyName,
  383. ClassifyNameEn: classifyName,
  384. ParentId: parentId,
  385. SysUserId: sysUserId,
  386. SysUserRealName: sysUserName,
  387. Sort: 0,
  388. Level: level,
  389. ModifyTime: time.Now(),
  390. CreateTime: time.Now(),
  391. }
  392. classifyId, err := data_manage.AddBaseFromSmmClassify(classifyInfo)
  393. if err != nil {
  394. errMsg = "保存分类失败"
  395. return
  396. }
  397. classifyInfo.ClassifyId = int(classifyId)
  398. //更新sort为classifyId
  399. err = data_manage.UpdateBaseFromSmmClassifySort(int(classifyId))
  400. return
  401. }
  402. // EditSmmClassify 编辑Smm分类
  403. func EditSmmClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
  404. item, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
  405. if err != nil {
  406. errMsg = `保存失败`
  407. return
  408. }
  409. switch lang {
  410. case utils.EnLangVersion:
  411. // 名字相同,那么就直接返回
  412. if item.ClassifyNameEn == classifyName {
  413. return
  414. }
  415. // 判断名称是否已存在
  416. count, tmpErr := data_manage.GetBaseFromSmmClassifyEnCount(classifyName, item.ParentId)
  417. if tmpErr != nil {
  418. err = tmpErr
  419. errMsg = "判断名称是否已存在失败"
  420. return
  421. }
  422. if count > 0 {
  423. errMsg = "分类名称已存在,请重新输入"
  424. err = errors.New(errMsg)
  425. return
  426. }
  427. // 修改数据
  428. err = data_manage.EditBaseFromSmmClassifyEn(classifyId, classifyName)
  429. if err != nil {
  430. errMsg = "保存失败"
  431. }
  432. default:
  433. // 名字相同,那么就直接返回
  434. if item.ClassifyName == classifyName {
  435. return
  436. }
  437. // 判断名称是否已存在
  438. count, tmpErr := data_manage.GetBaseFromSmmClassifyCount(classifyName, item.ParentId)
  439. if tmpErr != nil {
  440. err = tmpErr
  441. errMsg = "判断名称是否已存在失败"
  442. return
  443. }
  444. if count > 0 {
  445. errMsg = "分类名称已存在,请重新输入"
  446. err = errors.New(errMsg)
  447. return
  448. }
  449. // 修改数据
  450. err = data_manage.EditBaseFromSmmClassify(classifyId, classifyName)
  451. if err != nil {
  452. errMsg = "保存失败"
  453. }
  454. }
  455. return
  456. }
  457. // MoveSmmClassify 移动Smm分类
  458. func MoveSmmClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  459. //判断分类是否存在
  460. classifyInfo, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
  461. if err != nil {
  462. errMsg = "移动失败"
  463. err = errors.New("获取分类信息失败,Err:" + err.Error())
  464. return
  465. }
  466. updateCol := make([]string, 0)
  467. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  468. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  469. parentClassifyInfo, tmpErr := data_manage.GetBaseFromSmmClassifyById(parentClassifyId)
  470. if tmpErr != nil {
  471. errMsg = "移动失败"
  472. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  473. return
  474. }
  475. classifyInfo.ParentId = parentClassifyInfo.ClassifyId
  476. classifyInfo.Level = parentClassifyInfo.Level + 1
  477. updateCol = append(updateCol, "ParentId", "Level")
  478. }
  479. var currentSort, prevSort, nextSort int
  480. currentSort = classifyInfo.Sort
  481. var prevClassify *data_manage.BaseFromSmmClassify
  482. var nextClassify *data_manage.BaseFromSmmClassify
  483. //如果有传入 上一个兄弟节点分类id
  484. if prevClassifyId > 0 {
  485. prevClassify, err = data_manage.GetBaseFromSmmClassifyById(prevClassifyId)
  486. if err != nil {
  487. if err.Error() == utils.ErrNoRow() {
  488. errMsg = "移动失败"
  489. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  490. return
  491. }
  492. errMsg = "移动失败"
  493. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  494. return
  495. }
  496. prevSort = prevClassify.Sort
  497. }
  498. if nextClassifyId > 0 {
  499. //下一个兄弟节点
  500. nextClassify, err = data_manage.GetBaseFromSmmClassifyById(nextClassifyId)
  501. if err != nil {
  502. if err.Error() == utils.ErrNoRow() {
  503. errMsg = "移动失败"
  504. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  505. return
  506. }
  507. errMsg = "移动失败"
  508. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  509. return
  510. }
  511. nextSort = nextClassify.Sort
  512. }
  513. // 修改历史数据中的排序为0的情况
  514. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  515. //更新为0排序的数据为当前最小排序
  516. if nextClassify != nil {
  517. minSort, e := data_manage.GetSmmIndexClassifyMinSort(parentClassifyId)
  518. if e != nil {
  519. errMsg = "移动失败"
  520. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  521. return
  522. }
  523. if minSort > 1 {
  524. minSort -= 1
  525. }
  526. nextClassify.Sort = minSort
  527. err = nextClassify.Update([]string{"Sort"})
  528. if err != nil {
  529. errMsg = "移动失败"
  530. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  531. return
  532. }
  533. nextSort = minSort
  534. }
  535. }
  536. //移到两个排序值中间操作
  537. if prevSort >= currentSort {
  538. //往下移动
  539. err = data_manage.MoveDownSmmIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  540. if err != nil {
  541. err = errors.New("向下移动出错:" + err.Error())
  542. return
  543. }
  544. classifyInfo.Sort = prevSort
  545. } else if nextSort <= currentSort && nextSort != 0 {
  546. //往上移动
  547. err = data_manage.MoveUpSmmIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  548. if err != nil {
  549. err = errors.New("向上移动出错:" + err.Error())
  550. return
  551. }
  552. classifyInfo.Sort = nextSort
  553. }
  554. classifyInfo.ModifyTime = time.Now()
  555. updateCol = append(updateCol, "Sort", "ModifyTime")
  556. err = classifyInfo.Update(updateCol)
  557. if err != nil {
  558. errMsg = "移动失败"
  559. err = errors.New("修改失败,Err:" + err.Error())
  560. return
  561. }
  562. return
  563. }
  564. // DelSmmClassify 删除SMM分类
  565. func DelSmmClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
  566. //判断分类是否存在
  567. classifyInfo, err := data_manage.GetBaseFromSmmClassifyById(classifyId)
  568. if err != nil {
  569. errMsg = "删除失败"
  570. err = errors.New("获取分类信息失败,Err:" + err.Error())
  571. return
  572. }
  573. // 需要删除的分类id集合
  574. classifyIdList := make([]int, 0)
  575. classifyIdList = append(classifyIdList, classifyId)
  576. // 查找所有的子集id
  577. if classifyInfo.ParentId <= 0 {
  578. childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromSmmClassifyByParentId(classifyId)
  579. if tmpErr != nil {
  580. errMsg = "删除失败"
  581. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  582. return
  583. }
  584. for _, v := range childrenClassifyInfoList {
  585. classifyIdList = append(classifyIdList, v.ClassifyId)
  586. }
  587. }
  588. // 获取分类下的所有指标
  589. count, err := data_manage.GetBaseFromSmmIndexCountByClassifyIds(classifyIdList)
  590. if err != nil {
  591. errMsg = "删除失败"
  592. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  593. return
  594. }
  595. if count > 0 {
  596. errMsg = "该分类下有指标,不可删除"
  597. err = errors.New("该分类下有指标,不可删除")
  598. return
  599. }
  600. // 删除对应的分类
  601. err = data_manage.DeleteSmmClassifyByClassifyId(classifyIdList)
  602. if err != nil {
  603. return
  604. }
  605. return
  606. }
  607. // MoveSmmData 移动SMM指标
  608. func MoveSmmData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  609. //分类信息
  610. smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  611. if err != nil {
  612. errMsg = `获取数据失败`
  613. if err.Error() == utils.ErrNoRow() {
  614. errMsg = `该指标未入库`
  615. err = nil
  616. }
  617. return
  618. }
  619. //判断分类是否存在
  620. if classifyId > 0 {
  621. _, err = data_manage.GetBaseFromSmmClassifyById(classifyId)
  622. if err != nil {
  623. errMsg = "移动失败"
  624. err = errors.New("获取分类信息失败,Err:" + err.Error())
  625. if err.Error() == utils.ErrNoRow() {
  626. errMsg = "找不到该分类"
  627. err = nil
  628. }
  629. return
  630. }
  631. }
  632. //如果改变了分类,那么移动该图表数据
  633. if smmIndex.ClassifyId != classifyId {
  634. tmpErr := data_manage.MoveBaseFromSmmIndex(indexId, classifyId)
  635. if tmpErr != nil {
  636. errMsg = "移动失败"
  637. err = errors.New("移动失败,Err:" + tmpErr.Error())
  638. return
  639. }
  640. }
  641. //移动排序
  642. updateCol := make([]string, 0)
  643. //如果有传入 上一个兄弟节点分类id
  644. if prevIndexId > 0 {
  645. prevIndex, tmpErr := data_manage.GetBaseFromSmmIndexByIndexId(prevIndexId)
  646. if tmpErr != nil {
  647. errMsg = "移动失败"
  648. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  649. return
  650. }
  651. //如果是移动在两个兄弟节点之间
  652. if nextIndexId > 0 {
  653. //下一个兄弟节点
  654. nextIndex, tmpErr := data_manage.GetBaseFromSmmIndexByIndexId(nextIndexId)
  655. if tmpErr != nil {
  656. errMsg = "移动失败"
  657. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  658. return
  659. }
  660. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  661. if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == smmIndex.Sort {
  662. //变更兄弟节点的排序
  663. updateSortStr := `sort + 2`
  664. err = data_manage.UpdateBaseFromSmmIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSmmIndexId, updateSortStr)
  665. if err != nil {
  666. errMsg = "移动失败"
  667. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  668. return
  669. }
  670. } else {
  671. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  672. if nextIndex.Sort-prevIndex.Sort == 1 {
  673. //变更兄弟节点的排序
  674. updateSortStr := `sort + 1`
  675. err = data_manage.UpdateBaseFromSmmIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSmmIndexId, updateSortStr)
  676. if err != nil {
  677. errMsg = "移动失败"
  678. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  679. return
  680. }
  681. }
  682. }
  683. }
  684. smmIndex.Sort = prevIndex.Sort + 1
  685. smmIndex.ModifyTime = time.Now()
  686. updateCol = append(updateCol, "Sort", "ModifyTime")
  687. } else {
  688. firstClassify, tmpErr := data_manage.GetFirstBaseFromSmmIndexByClassifyId(classifyId)
  689. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  690. errMsg = "移动失败"
  691. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  692. return
  693. }
  694. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  695. if firstClassify != nil && firstClassify.Sort == 0 {
  696. updateSortStr := ` sort + 1 `
  697. _ = data_manage.UpdateBaseFromSmmIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromSmmIndexId-1, updateSortStr)
  698. }
  699. smmIndex.Sort = 0 //那就是排在第一位
  700. smmIndex.ModifyTime = time.Now()
  701. updateCol = append(updateCol, "Sort", "ModifyTime")
  702. }
  703. //更新
  704. if len(updateCol) > 0 {
  705. err = smmIndex.Update(updateCol)
  706. if err != nil {
  707. errMsg = "移动失败"
  708. err = errors.New("修改失败,Err:" + err.Error())
  709. return
  710. }
  711. }
  712. return
  713. }
  714. // MoveSmmData2 移动SMM指标
  715. func MoveSmmData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  716. //分类信息
  717. smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  718. if err != nil {
  719. errMsg = `获取数据失败`
  720. if err.Error() == utils.ErrNoRow() {
  721. errMsg = `该指标未入库`
  722. err = nil
  723. }
  724. return
  725. }
  726. //判断分类是否存在
  727. if classifyId > 0 {
  728. _, err = data_manage.GetBaseFromSmmClassifyById(classifyId)
  729. if err != nil {
  730. errMsg = "移动失败"
  731. err = errors.New("获取分类信息失败,Err:" + err.Error())
  732. if err.Error() == utils.ErrNoRow() {
  733. errMsg = "找不到该分类"
  734. err = nil
  735. }
  736. return
  737. }
  738. }
  739. //如果改变了分类,那么移动该图表数据
  740. if smmIndex.ClassifyId != classifyId {
  741. tmpErr := data_manage.MoveBaseFromSmmIndex(indexId, classifyId)
  742. if tmpErr != nil {
  743. errMsg = "移动失败"
  744. err = errors.New("移动失败,Err:" + tmpErr.Error())
  745. return
  746. }
  747. }
  748. //移动排序
  749. updateCol := make([]string, 0)
  750. var currentSort, prevSort, nextSort int
  751. currentSort = smmIndex.Sort
  752. //如果有传入 上一个兄弟节点分类id
  753. var prevIndex *data_manage.BaseFromSmmIndex
  754. var nextIndex *data_manage.BaseFromSmmIndex
  755. if prevIndexId > 0 {
  756. prevIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(prevIndexId)
  757. if err != nil {
  758. if err.Error() == utils.ErrNoRow() {
  759. errMsg = "移动失败"
  760. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  761. return
  762. }
  763. errMsg = "移动失败"
  764. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  765. return
  766. }
  767. prevSort = prevIndex.Sort
  768. }
  769. if nextIndexId > 0 {
  770. //下一个兄弟节点
  771. nextIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(nextIndexId)
  772. if err != nil {
  773. if err.Error() == utils.ErrNoRow() {
  774. errMsg = "移动失败"
  775. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  776. return
  777. }
  778. errMsg = "移动失败"
  779. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  780. return
  781. }
  782. nextSort = nextIndex.Sort
  783. }
  784. // 修改历史数据中的排序为0的情况
  785. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  786. //更新为0排序的数据为当前最小排序
  787. if nextIndex != nil {
  788. minSort, e := data_manage.GetBaseFromSmmIndexMinSortByClassifyId(classifyId)
  789. if e != nil {
  790. errMsg = "移动失败"
  791. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  792. return
  793. }
  794. if minSort > 1 {
  795. minSort -= 1
  796. }
  797. nextIndex.Sort = minSort
  798. err = nextIndex.Update([]string{"Sort"})
  799. if err != nil {
  800. errMsg = "移动失败"
  801. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  802. return
  803. }
  804. nextSort = minSort
  805. }
  806. }
  807. //移到两个排序值中间操作
  808. if prevSort >= currentSort {
  809. //往下移动
  810. err = data_manage.MoveDownSmmIndexBySort(classifyId, prevSort, currentSort)
  811. if err != nil {
  812. err = errors.New("向下移动出错:" + err.Error())
  813. return
  814. }
  815. smmIndex.Sort = prevSort
  816. } else if nextSort <= currentSort && nextSort != 0 {
  817. //往上移动
  818. err = data_manage.MoveUpSmmIndexBySort(classifyId, nextSort, currentSort)
  819. if err != nil {
  820. err = errors.New("向上移动出错:" + err.Error())
  821. return
  822. }
  823. smmIndex.Sort = nextSort
  824. }
  825. //更新
  826. smmIndex.ModifyTime = time.Now()
  827. updateCol = append(updateCol, "Sort", "ModifyTime")
  828. err = smmIndex.Update(updateCol)
  829. if err != nil {
  830. errMsg = "移动失败"
  831. err = errors.New("修改失败,Err:" + err.Error())
  832. return
  833. }
  834. return
  835. }
  836. // EditSmmIndex 编辑SMM指标
  837. func EditSmmIndex(indexId, classifyId int, sysUser *system.Admin) (baseFromSmmIndex *data_manage.BaseFromSmmIndex, err error, errMsg string) {
  838. baseFromSmmIndex, err = data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  839. if err != nil {
  840. errMsg = `获取数据失败`
  841. if err.Error() == utils.ErrNoRow() {
  842. errMsg = `该指标未入库`
  843. err = nil
  844. }
  845. return
  846. }
  847. baseFromSmmIndex.ClassifyId = classifyId
  848. baseFromSmmIndex.ModifyTime = time.Now()
  849. err = baseFromSmmIndex.Update([]string{"ClassifyId", "ModifyTime"})
  850. if err != nil {
  851. errMsg = "保存分类失败"
  852. }
  853. return
  854. }
  855. func InitBaseFromSmmClassify() (err error) {
  856. //查询历史数据所有分类
  857. typeList, err := data_manage.GetBaseFromSmmIndexType()
  858. if err != nil {
  859. return
  860. }
  861. classifyList := make([]*data_manage.BaseFromSmmClassify, 0)
  862. classifyMap := make(map[string][]string, 0)
  863. parentClassifyMap := make(map[string]struct{}, 0)
  864. now := time.Now()
  865. for _, v := range typeList {
  866. if _, ok := parentClassifyMap[v.Type2]; !ok {
  867. parentClassifyMap[v.Type2] = struct{}{}
  868. tmp := new(data_manage.BaseFromSmmClassify)
  869. tmp.ClassifyName = v.Type2
  870. tmp.Level = 1
  871. tmp.ModifyTime = now
  872. tmp.CreateTime = now
  873. classifyList = append(classifyList, tmp)
  874. }
  875. classifyMap[v.Type2] = append(classifyMap[v.Type2], v.Type3)
  876. }
  877. //添加一级分类
  878. err = data_manage.AddSmmClassifyMulti(classifyList)
  879. if err != nil {
  880. err = errors.New("新增分类出错:" + err.Error())
  881. return
  882. }
  883. //查询分类表的所有父级
  884. rootList, err := data_manage.GetBaseFromSmmClassifyByParentId(0)
  885. if err != nil {
  886. return
  887. }
  888. classifyIdMap := make(map[string]int)
  889. for _, v := range rootList {
  890. classifyIdMap[v.ClassifyName] = v.ClassifyId
  891. }
  892. classifyList2 := make([]*data_manage.BaseFromSmmClassify, 0)
  893. for _, v := range typeList {
  894. if parentId, ok := classifyIdMap[v.Type2]; ok {
  895. tmp := new(data_manage.BaseFromSmmClassify)
  896. tmp.ParentId = parentId
  897. tmp.ClassifyName = v.Type3
  898. tmp.Level = 2
  899. tmp.ModifyTime = now
  900. tmp.CreateTime = now
  901. classifyList2 = append(classifyList2, tmp)
  902. }
  903. }
  904. //添加二级分类
  905. err = data_manage.AddSmmClassifyMulti(classifyList2)
  906. if err != nil {
  907. err = errors.New("新增二级分类出错:" + err.Error())
  908. return
  909. }
  910. //初始化sort字段
  911. err = data_manage.InitSmmClassifySort()
  912. if err != nil {
  913. return
  914. }
  915. //更新index表里的分类id
  916. err = data_manage.InitSmmIndexClassifyId()
  917. return
  918. }
  919. // DelSmmData 删除SMM指标
  920. func DelSmmData(indexId int) (err error, errMsg string) {
  921. baseFromSmmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  922. if err != nil {
  923. errMsg = `获取数据失败`
  924. if err.Error() == utils.ErrNoRow() {
  925. errMsg = `该指标未入库`
  926. err = nil
  927. }
  928. return
  929. }
  930. // 获取已经加入到EDB指标库的Smm指标
  931. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, baseFromSmmIndex.IndexCode)
  932. if err != nil && err.Error() != utils.ErrNoRow() {
  933. errMsg = "删除失败"
  934. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  935. return
  936. }
  937. if edbInfo != nil {
  938. errMsg = "当前指标已被引用,不可删除"
  939. err = nil
  940. return
  941. }
  942. // 删除对应的分类和指标
  943. err = data_manage.DeleteBaseFromSmmIndexById(indexId)
  944. if err != nil {
  945. return
  946. }
  947. return
  948. }
  949. // ResetSmmData 指标数据清除分类接口
  950. func ResetSmmData(indexId int) (err error, errMsg string) {
  951. smmIndex, err := data_manage.GetBaseFromSmmIndexByIndexId(indexId)
  952. if err != nil {
  953. errMsg = `获取数据失败`
  954. if err.Error() == utils.ErrNoRow() {
  955. errMsg = `该指标未入库`
  956. err = nil
  957. }
  958. return
  959. }
  960. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  961. //移动排序
  962. classifyId := 0
  963. updateCol := make([]string, 0)
  964. var currentSort, prevSort int
  965. currentSort = smmIndex.Sort
  966. //未分类的最大的sort值
  967. prevSort, err = data_manage.GetBaseFromSmmIndexMaxSortByClassifyId(classifyId)
  968. if err != nil {
  969. errMsg = "移动失败"
  970. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  971. return
  972. }
  973. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  974. if prevSort >= currentSort {
  975. //往下移动
  976. err = data_manage.MoveDownSmmIndexBySort(classifyId, prevSort, currentSort)
  977. if err != nil {
  978. err = errors.New("向下移动出错:" + err.Error())
  979. return
  980. }
  981. smmIndex.Sort = prevSort
  982. }
  983. //更新
  984. smmIndex.ModifyTime = time.Now()
  985. smmIndex.ClassifyId = classifyId
  986. updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
  987. err = smmIndex.Update(updateCol)
  988. if err != nil {
  989. errMsg = "移动失败"
  990. err = errors.New("修改失败,Err:" + err.Error())
  991. return
  992. }
  993. return
  994. }