gn.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. package eta_bridge
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta_gn/eta_task/models/data_manage"
  6. "eta_gn/eta_task/services/alarm_msg"
  7. "eta_gn/eta_task/services/data"
  8. "eta_gn/eta_task/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "io/ioutil"
  12. "net/url"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. // 同步指标信息锁
  19. var lockSyncGnIndex sync.Mutex
  20. const GnEdbListUri = `/index_data/gn/edb/list` // 国能指标列表接口
  21. // CurrLevelParentClassifyMap 当前层级分类map
  22. var CurrLevelParentClassifyMap map[int64]map[int64]map[string]CurrClassify
  23. // CurrEdbInfoMap 当前库里已有的指标map
  24. var CurrEdbInfoMap map[string]*data_manage.EdbInfo
  25. type CurrClassify struct {
  26. ClassifyId int64
  27. ParentId int64
  28. ClassifyName string
  29. }
  30. // SyncGnIndex
  31. // @Description: 定时同步指标信息
  32. // @author: Roc
  33. // @datetime 2024-03-07 17:39:34
  34. // @param cont context.Context
  35. // @return err error
  36. func SyncGnIndex(cont context.Context) (err error) {
  37. fmt.Println("准备同步指标")
  38. lockSyncGnIndex.Lock()
  39. errMsgList := make([]string, 0)
  40. fmt.Println("开始同步指标")
  41. defer func() {
  42. if err != nil {
  43. tips := "SyncGnIndex-定时同步国能的指标信息到ETA失败, ErrMsg:\n" + err.Error()
  44. utils.FileLog.Info(tips)
  45. go alarm_msg.SendAlarmMsg(tips, 3)
  46. }
  47. if len(errMsgList) > 0 {
  48. tips := "SyncGnIndex-定时同步国能的指标信息到ETA失败, ErrMsg:\n" + strings.Join(errMsgList, "\n")
  49. utils.FileLog.Info(tips)
  50. go alarm_msg.SendAlarmMsg(tips, 3)
  51. }
  52. lockSyncGnIndex.Unlock()
  53. }()
  54. initCurrEdbInfoMap()
  55. initCurrLevelParentClassifyMap()
  56. var lastUpdateTimeStr string // 上一次更新的时间
  57. err, errMsgList = syncGnIndex(1, utils.SyncCrmIndexNum, lastUpdateTimeStr)
  58. // 下面的用完整体注释掉就行
  59. //indexIds, e := LoadGnTempIndexIds()
  60. //if e != nil {
  61. // err = fmt.Errorf("读取IDs配置文件失败, %v", e)
  62. // return
  63. //}
  64. //if len(indexIds) == 0 {
  65. // fmt.Println("IndexIds为空")
  66. // return
  67. //}
  68. //for _, v := range indexIds {
  69. // strId := strconv.Itoa(v)
  70. // fmt.Printf("正在同步IndexId: %s\n", strId)
  71. // err, errMsgList = syncGnIndexV2(1, 1, lastUpdateTimeStr, strId)
  72. // if err != nil {
  73. // errMsg := strings.Join(errMsgList, "\n")
  74. // fmt.Printf("IndexId: %s, 同步失败, ErrMsg: %s", strId, errMsg)
  75. // continue
  76. // }
  77. //}
  78. return
  79. }
  80. // initCurrLevelParentClassifyMap
  81. // @Description: 初始化当前层级分类map
  82. func initCurrLevelParentClassifyMap() {
  83. var condition string
  84. var pars []interface{}
  85. // 普通指标分类
  86. condition = " AND classify_type = ? "
  87. pars = append(pars, 0)
  88. classifyList, err := data_manage.GetAllEdbClassifyListByCondition(condition, pars)
  89. if err != nil {
  90. utils.FileLog.Error("获取分类列表数据失败:" + err.Error())
  91. return
  92. }
  93. // 清空缓存
  94. CurrLevelParentClassifyMap = make(map[int64]map[int64]map[string]CurrClassify)
  95. for _, v := range classifyList {
  96. currParentClassifyMap, ok := CurrLevelParentClassifyMap[v.Level]
  97. if !ok {
  98. currParentClassifyMap = make(map[int64]map[string]CurrClassify)
  99. }
  100. currClassifyMap, ok := currParentClassifyMap[v.ParentID]
  101. if !ok {
  102. currClassifyMap = make(map[string]CurrClassify)
  103. }
  104. classifyName := strings.TrimSpace(v.ClassifyName)
  105. currClassifyMap[classifyName] = CurrClassify{
  106. ClassifyId: v.ClassifyID,
  107. ParentId: v.ParentID,
  108. ClassifyName: classifyName,
  109. }
  110. currParentClassifyMap[v.ParentID] = currClassifyMap
  111. CurrLevelParentClassifyMap[v.Level] = currParentClassifyMap
  112. }
  113. }
  114. // initCurrEdbInfoMap
  115. // @Description: 初始化当前指标map
  116. func initCurrEdbInfoMap() {
  117. // 获取指标列表
  118. edbInfoList, err := data_manage.GetAllBaseEdbInfo()
  119. if err != nil {
  120. utils.FileLog.Error("获取指标列表数据失败:" + err.Error())
  121. return
  122. }
  123. // 清空缓存
  124. CurrEdbInfoMap = make(map[string]*data_manage.EdbInfo)
  125. for _, v := range edbInfoList {
  126. CurrEdbInfoMap[v.OriginalEdbCode] = v
  127. }
  128. }
  129. // EtaBridgeGnIndexListResp
  130. // @Description: 指标列表返回数据
  131. type EtaBridgeGnIndexListResp struct {
  132. Code int `json:"code" description:"状态码"`
  133. Msg string `json:"msg" description:"提示信息"`
  134. Data IndexListResp `json:"data" description:"返回数据"`
  135. }
  136. // IndexListResp
  137. // @Description: 指标列表数据
  138. type IndexListResp struct {
  139. Page paging.PagingItem `description:"分页数据"`
  140. List []IndexInfo
  141. }
  142. // IndexInfo
  143. // @Description: 指标信息
  144. type IndexInfo struct {
  145. ClassifyNameOne string `description:"一级目录"`
  146. ClassifyNameTwo string `description:"二级目录"`
  147. ClassifyNameThree string `description:"三级目录"`
  148. DataIndexCode string `description:"数据节点指标编码"`
  149. SourceEdbCode string `description:"数据源指标原始编码"`
  150. EdbName string `description:"指标名称"`
  151. Frequency string `description:"频度"`
  152. Unit string `description:"单位"`
  153. SourceName string `description:"来源"`
  154. SourceCode string `description:"来源编码"`
  155. }
  156. // BridgeGnIndexParams
  157. // @Description: 桥接服务-获取国能指标数据入参
  158. type BridgeGnIndexParams struct {
  159. LastModifyTime string `json:"last_modify_time" description:"最近一次更新时间"`
  160. PageIndex int `json:"page_index" description:"当前页码"`
  161. PageSize int `json:"page_size" description:"每页数量"`
  162. IndexId string `json:"index_id" description:"指标ID,不为空时表示只取该指标"`
  163. }
  164. // syncCrmIndex
  165. // @Description: 开始同步CRM指标信息
  166. // @author: Roc
  167. // @datetime 2024-05-17 15:55:11
  168. // @param assetPkgCd string
  169. // @param currIndex int
  170. // @param pageSize int
  171. // @param lastUpdateTimeStr string
  172. // @return err error
  173. // @return errMsgList []string
  174. func syncGnIndex(currIndex, pageSize int, baseLastUpdateTimeStr string) (err error, errMsgList []string) {
  175. fmt.Println("开始第", currIndex, "页的更新")
  176. errMsgList = make([]string, 0)
  177. lastUpdateTimeStr := baseLastUpdateTimeStr
  178. if lastUpdateTimeStr != `` {
  179. lastUpdateTimeStr = url.QueryEscape(lastUpdateTimeStr)
  180. }
  181. params := BridgeGnIndexParams{
  182. LastModifyTime: lastUpdateTimeStr,
  183. PageIndex: currIndex,
  184. PageSize: pageSize,
  185. }
  186. bResult, err, _ := HttpEtaBridgePost(utils.SyncIndexPath, params)
  187. if err != nil {
  188. return
  189. }
  190. var result EtaBridgeGnIndexListResp
  191. err = json.Unmarshal(bResult, &result)
  192. if err != nil {
  193. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", err.Error(), string(bResult))
  194. utils.FileLog.Info("桥接服务get请求失败:\n" + string(bResult))
  195. return
  196. }
  197. //totalPage := result.Data.Paging.Pages
  198. // 处理指标信息
  199. for _, v := range result.Data.List {
  200. tmpErr := handleIndex(v)
  201. if tmpErr != nil {
  202. errMsgList = append(errMsgList, tmpErr.Error())
  203. }
  204. }
  205. fmt.Println(currIndex, "是否已结束:", result.Data.Page.IsEnd)
  206. // 如果还有下一页,那么就继续请求下一页
  207. if !result.Data.Page.IsEnd {
  208. _, tmpErrMsgList := syncGnIndex(currIndex+1, utils.SyncCrmIndexNum, baseLastUpdateTimeStr)
  209. errMsgList = append(errMsgList, tmpErrMsgList...)
  210. }
  211. return
  212. }
  213. // handleIndex
  214. // @Description: 指标处理
  215. // @param index
  216. // @return err
  217. func handleIndex(index IndexInfo) (err error) {
  218. // 处理分类(如果不存在就创建)
  219. oneClassifyId, twoClassifyId, thirdClassifyId, err := handleClassify(index)
  220. if err != nil {
  221. return
  222. }
  223. classifyId := thirdClassifyId
  224. if classifyId <= 0 {
  225. classifyId = twoClassifyId
  226. }
  227. if classifyId <= 0 {
  228. classifyId = oneClassifyId
  229. }
  230. // 处理指标(如果不存在就创建)
  231. err = handleEdbInfo(index, classifyId)
  232. return
  233. }
  234. // handleClassify
  235. // @Description: 分类处理
  236. // @param index
  237. // @return firstClassifyId
  238. // @return secondClassifyId
  239. // @return thirdClassifyId
  240. // @return err
  241. func handleClassify(index IndexInfo) (firstClassifyId, secondClassifyId, thirdClassifyId int64, err error) {
  242. firstClassifyName := getClassifyName(index.ClassifyNameOne)
  243. secondClassifyName := getClassifyName(index.ClassifyNameTwo)
  244. thirdClassifyName := getClassifyName(index.ClassifyNameThree)
  245. var oneLevel, twoLevel, threeLevel int64
  246. oneLevel = 1
  247. twoLevel = 2
  248. threeLevel = 3
  249. // 一级分类
  250. {
  251. var parentId int64
  252. parentId = 0
  253. classifyName := firstClassifyName
  254. level := oneLevel
  255. // 获取层级下的父级分类map
  256. currParentClassifyMap, ok := CurrLevelParentClassifyMap[level]
  257. if !ok {
  258. currParentClassifyMap = make(map[int64]map[string]CurrClassify)
  259. }
  260. // 获取父级id下的分类列表
  261. currClassifyListMap, ok := currParentClassifyMap[parentId]
  262. if !ok {
  263. currClassifyListMap = make(map[string]CurrClassify)
  264. }
  265. // 根据分类名称获取分类
  266. currClassify, ok := currClassifyListMap[classifyName]
  267. if !ok {
  268. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  269. classifyInfo := &data_manage.EdbClassify{
  270. //ClassifyId: 0,
  271. ClassifyType: 0,
  272. ClassifyName: classifyName,
  273. ClassifyNameEn: classifyName,
  274. ParentID: parentId,
  275. RootID: 0,
  276. HasData: 0,
  277. CreateTime: time.Now(),
  278. ModifyTime: time.Now(),
  279. SysUserID: 0,
  280. SysUserRealName: "",
  281. Level: level,
  282. UniqueCode: utils.MD5(fmt.Sprint(parentId, "_", utils.DATA_PREFIX+"_"+timestamp)),
  283. Sort: 0,
  284. ClassifyNamePath: classifyName,
  285. }
  286. err = data_manage.AddEdbClassify(classifyInfo)
  287. if err != nil {
  288. return
  289. }
  290. classifyInfo.RootID = classifyInfo.ClassifyID
  291. classifyInfo.ClassifyIdPath = fmt.Sprint(classifyInfo.ClassifyID)
  292. err = classifyInfo.Update([]string{"root_id", "classify_id_path"})
  293. if err != nil {
  294. return
  295. }
  296. currClassify = CurrClassify{
  297. ClassifyId: classifyInfo.ClassifyID,
  298. ParentId: classifyInfo.ParentID,
  299. ClassifyName: classifyInfo.ClassifyName,
  300. }
  301. currClassifyListMap[classifyName] = currClassify
  302. currParentClassifyMap[parentId] = currClassifyListMap
  303. CurrLevelParentClassifyMap[level] = currParentClassifyMap
  304. }
  305. firstClassifyId = currClassify.ClassifyId
  306. }
  307. // 二级分类
  308. {
  309. parentId := firstClassifyId
  310. classifyName := secondClassifyName
  311. level := twoLevel
  312. if secondClassifyName == `` {
  313. return
  314. }
  315. // 获取层级下的父级分类map
  316. currParentClassifyMap, ok := CurrLevelParentClassifyMap[level]
  317. if !ok {
  318. currParentClassifyMap = make(map[int64]map[string]CurrClassify)
  319. }
  320. // 获取父级id下的分类列表
  321. currClassifyListMap, ok := currParentClassifyMap[parentId]
  322. if !ok {
  323. currClassifyListMap = make(map[string]CurrClassify)
  324. }
  325. // 根据分类名称获取分类
  326. currClassify, ok := currClassifyListMap[classifyName]
  327. if !ok {
  328. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  329. classifyInfo := &data_manage.EdbClassify{
  330. //ClassifyId: 0,
  331. ClassifyType: 0,
  332. ClassifyName: classifyName,
  333. ClassifyNameEn: classifyName,
  334. ParentID: parentId,
  335. RootID: firstClassifyId,
  336. HasData: 0,
  337. CreateTime: time.Now(),
  338. ModifyTime: time.Now(),
  339. SysUserID: 0,
  340. SysUserRealName: "",
  341. Level: level,
  342. UniqueCode: utils.MD5(fmt.Sprint(parentId, "_", utils.DATA_PREFIX+"_"+timestamp)),
  343. Sort: 0,
  344. ClassifyNamePath: fmt.Sprint(firstClassifyName, "|", classifyName),
  345. }
  346. err = data_manage.AddEdbClassify(classifyInfo)
  347. if err != nil {
  348. return
  349. }
  350. classifyInfo.ClassifyIdPath = fmt.Sprint(firstClassifyId, ",", classifyInfo.ClassifyID)
  351. err = classifyInfo.Update([]string{"classify_id_path"})
  352. if err != nil {
  353. return
  354. }
  355. currClassify = CurrClassify{
  356. ClassifyId: classifyInfo.ClassifyID,
  357. ParentId: classifyInfo.ParentID,
  358. ClassifyName: classifyInfo.ClassifyName,
  359. }
  360. currClassifyListMap[classifyName] = currClassify
  361. currParentClassifyMap[parentId] = currClassifyListMap
  362. CurrLevelParentClassifyMap[level] = currParentClassifyMap
  363. }
  364. secondClassifyId = currClassify.ClassifyId
  365. }
  366. // 三级分类
  367. {
  368. parentId := secondClassifyId
  369. classifyName := thirdClassifyName
  370. level := threeLevel
  371. if thirdClassifyName == `` {
  372. return
  373. }
  374. // 获取层级下的父级分类map
  375. currParentClassifyMap, ok := CurrLevelParentClassifyMap[level]
  376. if !ok {
  377. currParentClassifyMap = make(map[int64]map[string]CurrClassify)
  378. }
  379. // 获取父级id下的分类列表
  380. currClassifyListMap, ok := currParentClassifyMap[parentId]
  381. if !ok {
  382. currClassifyListMap = make(map[string]CurrClassify)
  383. }
  384. // 根据分类名称获取分类
  385. currClassify, ok := currClassifyListMap[classifyName]
  386. if !ok {
  387. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  388. classifyInfo := &data_manage.EdbClassify{
  389. //ClassifyId: 0,
  390. ClassifyType: 0,
  391. ClassifyName: classifyName,
  392. ClassifyNameEn: classifyName,
  393. ParentID: parentId,
  394. RootID: firstClassifyId,
  395. HasData: 1,
  396. CreateTime: time.Now(),
  397. ModifyTime: time.Now(),
  398. SysUserID: 0,
  399. SysUserRealName: "",
  400. Level: level,
  401. UniqueCode: utils.MD5(fmt.Sprint(parentId, "_", utils.DATA_PREFIX+"_"+timestamp)),
  402. Sort: 0,
  403. ClassifyNamePath: fmt.Sprint(firstClassifyName, "|", secondClassifyName, "|", classifyName),
  404. }
  405. err = data_manage.AddEdbClassify(classifyInfo)
  406. if err != nil {
  407. return
  408. }
  409. classifyInfo.ClassifyIdPath = fmt.Sprint(firstClassifyId, ",", secondClassifyId, ",", classifyInfo.ClassifyID)
  410. err = classifyInfo.Update([]string{"classify_id_path"})
  411. if err != nil {
  412. return
  413. }
  414. currClassify = CurrClassify{
  415. ClassifyId: classifyInfo.ClassifyID,
  416. ParentId: classifyInfo.ParentID,
  417. ClassifyName: classifyInfo.ClassifyName,
  418. }
  419. currClassifyListMap[classifyName] = currClassify
  420. currParentClassifyMap[parentId] = currClassifyListMap
  421. CurrLevelParentClassifyMap[level] = currParentClassifyMap
  422. }
  423. thirdClassifyId = currClassify.ClassifyId
  424. }
  425. return
  426. }
  427. func getClassifyName(classifyName string) string {
  428. classifyName = strings.TrimSpace(classifyName)
  429. if classifyName == `` {
  430. return classifyName
  431. }
  432. // 如果不是“其它指标”,那么就是处理掉带有序号的、
  433. if classifyName != `其它指标` {
  434. classifyNameList := strings.Split(classifyName, `、`)
  435. if len(classifyNameList) > 0 {
  436. classifyName = classifyNameList[len(classifyNameList)-1]
  437. }
  438. }
  439. return classifyName
  440. }
  441. // handleEdbInfo
  442. // @Description: 处理指标
  443. // @param index
  444. // @param thirdClassifyId
  445. // @return err
  446. func handleEdbInfo(index IndexInfo, thirdClassifyId int64) (err error) {
  447. // 过滤数据节点指标唯一编码为空的数据
  448. if index.DataIndexCode == `` {
  449. return
  450. }
  451. // 过滤基础指标编码为空的数据
  452. if index.SourceEdbCode == `` {
  453. return
  454. }
  455. edbInfo, ok := CurrEdbInfoMap[index.DataIndexCode]
  456. frequency := Frequency(strings.TrimSpace(index.Frequency))
  457. unit := strings.TrimSpace(index.Unit)
  458. sourceName, sourceId, err := GetSource(strings.TrimSpace(index.SourceName), strings.TrimSpace(index.SourceCode))
  459. if err != nil {
  460. return
  461. }
  462. if !ok {
  463. endDate := time.Date(1899, 1, 1, 0, 0, 0, 0, time.Local)
  464. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  465. edbInfo = &data_manage.EdbInfo{
  466. EdbInfoId: 0,
  467. EdbInfoType: utils.EDB_INFO_TYPE,
  468. SourceName: sourceName,
  469. Source: sourceId,
  470. EdbCode: index.SourceEdbCode,
  471. EdbName: index.EdbName,
  472. EdbNameEn: index.EdbName,
  473. EdbNameSource: index.EdbName,
  474. Frequency: frequency,
  475. Unit: unit,
  476. UnitEn: unit,
  477. StartDate: endDate,
  478. EndDate: endDate,
  479. ClassifyId: int(thirdClassifyId),
  480. SysUserId: 0,
  481. SysUserRealName: "",
  482. UniqueCode: utils.MD5(fmt.Sprint(index.SourceEdbCode, "_", utils.DATA_PREFIX+"_"+timestamp)),
  483. CreateTime: time.Now(),
  484. ModifyTime: time.Now(),
  485. BaseModifyTime: time.Now(),
  486. MinValue: 0,
  487. MaxValue: 0,
  488. CalculateFormula: "",
  489. EdbType: utils.EdbTypeBase,
  490. Sort: 0,
  491. LatestDate: "",
  492. LatestValue: 0,
  493. EndValue: 0,
  494. MoveType: 0,
  495. MoveFrequency: "",
  496. NoUpdate: 0,
  497. ServerUrl: "",
  498. ChartImage: "", // 缩略图
  499. Calendar: "",
  500. DataDateType: "",
  501. ManualSave: 0,
  502. EmptyType: 0,
  503. MaxEmptyType: 0,
  504. TerminalCode: "",
  505. DataUpdateTime: "",
  506. ErDataUpdateDate: "",
  507. SourceIndexName: index.EdbName,
  508. SubSource: 0,
  509. SubSourceName: "",
  510. IndicatorCode: "",
  511. StockCode: "",
  512. Extra: "",
  513. IsJoinPermission: 0,
  514. OriginalEdbCode: index.DataIndexCode,
  515. }
  516. err = data_manage.AddEdbInfo(edbInfo)
  517. if err != nil {
  518. return
  519. }
  520. CurrEdbInfoMap[index.DataIndexCode] = edbInfo
  521. // TODO 刷新指标明细数据
  522. fmt.Println(data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.EndDate.Format(utils.FormatDate)))
  523. return
  524. }
  525. updateCols := make([]string, 0)
  526. if edbInfo.EdbNameEn == edbInfo.EdbName && edbInfo.EdbName != index.EdbName {
  527. edbInfo.EdbNameEn = index.EdbName
  528. updateCols = append(updateCols, "edb_name_en")
  529. }
  530. if edbInfo.EdbName != index.EdbName {
  531. edbInfo.EdbName = index.EdbName
  532. updateCols = append(updateCols, "edb_name")
  533. }
  534. if edbInfo.Frequency != index.Frequency {
  535. edbInfo.Frequency = index.Frequency
  536. updateCols = append(updateCols, "frequency")
  537. }
  538. if edbInfo.UnitEn == edbInfo.Unit && edbInfo.Unit != unit {
  539. edbInfo.UnitEn = unit
  540. updateCols = append(updateCols, "unit_en")
  541. }
  542. if edbInfo.Unit != unit {
  543. edbInfo.Unit = unit
  544. updateCols = append(updateCols, "unit")
  545. }
  546. if edbInfo.ClassifyId != int(thirdClassifyId) {
  547. edbInfo.ClassifyId = int(thirdClassifyId)
  548. updateCols = append(updateCols, "classify_id")
  549. }
  550. if len(updateCols) > 0 {
  551. err = edbInfo.Update(updateCols)
  552. }
  553. return
  554. }
  555. // Frequency
  556. // @Description: 获取频度
  557. // @param unit
  558. // @return string
  559. func Frequency(unit string) string {
  560. switch unit {
  561. case "半月度":
  562. unit = `周度`
  563. case "不定期":
  564. unit = `日度`
  565. case `日度`, `周度`, `旬度`, `月度`, `季度`, `半年度`, `年度`:
  566. default:
  567. unit = ``
  568. }
  569. return unit
  570. }
  571. // GetSource
  572. // @Description: 获取来源
  573. // @param sourceName
  574. // @return gnSourceName
  575. // @return sourceCode
  576. // @return source
  577. // @return err
  578. func GetSource(sourceName, sourceCode string) (gnSourceName string, source int, err error) {
  579. gnSourceName = sourceName
  580. var tableNameSuffix, indexNamePrefix string
  581. tableNamePrefix := "edb_data_gn_"
  582. switch sourceName {
  583. case "CCTD":
  584. tableNameSuffix = "cctd"
  585. case "mysteel":
  586. tableNameSuffix = "mysteel"
  587. case "wind":
  588. tableNameSuffix = "wind"
  589. case "卓创":
  590. tableNameSuffix = "sci"
  591. case "CCI":
  592. tableNameSuffix = "cci"
  593. //return
  594. default:
  595. // 来源编码前缀把还是
  596. tableNameSuffix = strings.ToLower(sourceCode)
  597. //if strings.Contains(sourceName, "国能购销辅助决策系统") {
  598. // gnSourceName = `国能购销辅助决策系统`
  599. // tableNameSuffix = "purchase_sales"
  600. //} else if strings.Contains(sourceName, "国能市场分析平台") {
  601. // gnSourceName = `国能市场分析平台`
  602. // tableNameSuffix = "market_analysis"
  603. //} else {
  604. // // TODO 自动生成表名(暂时以时间作为表名后缀,如果客户提前告知了,那么可以直接先建data表,以及edb_source表写入新的source)
  605. // tableNameSuffix = fmt.Sprint(time.Now().Format(utils.FormatDateTimeUnSpace))
  606. //}
  607. }
  608. sourceItem := data_manage.GetEdbSourceBySourceName(gnSourceName)
  609. // 如果找不到,说明是
  610. if sourceItem == nil {
  611. utils.FileLog.Debug("%s表不存在,需要创建相关的表:%s", gnSourceName, tableNamePrefix+tableNameSuffix)
  612. indexNamePrefix = strings.ToUpper(tableNameSuffix)
  613. sourceItem = &data_manage.EdbSource{
  614. EdbSourceId: 0,
  615. SourceName: gnSourceName,
  616. TableName: tableNamePrefix + tableNameSuffix,
  617. EdbAddMethod: "gn_index/add",
  618. EdbRefreshMethod: "gn_index/refresh",
  619. IsBase: 1,
  620. FromBridge: 1,
  621. BridgeFlag: "bridge_gn",
  622. SourceExtend: gnSourceName,
  623. EdbCodeRequired: 1,
  624. IndexTableName: "",
  625. SourceNameEn: gnSourceName,
  626. }
  627. err = data_manage.AddEdbSource(sourceItem, indexNamePrefix)
  628. if err != nil {
  629. return
  630. }
  631. }
  632. source = sourceItem.EdbSourceId
  633. return
  634. }
  635. func syncGnIndexV2(currIndex, pageSize int, baseLastUpdateTimeStr string, indexId string) (err error, errMsgList []string) {
  636. fmt.Println("开始第", currIndex, "页的更新")
  637. errMsgList = make([]string, 0)
  638. lastUpdateTimeStr := baseLastUpdateTimeStr
  639. if lastUpdateTimeStr != `` {
  640. lastUpdateTimeStr = url.QueryEscape(lastUpdateTimeStr)
  641. }
  642. params := BridgeGnIndexParams{
  643. LastModifyTime: lastUpdateTimeStr,
  644. PageIndex: currIndex,
  645. PageSize: pageSize,
  646. IndexId: indexId,
  647. }
  648. bResult, err, _ := HttpEtaBridgePost(utils.SyncIndexPath, params)
  649. if err != nil {
  650. return
  651. }
  652. var result EtaBridgeGnIndexListResp
  653. err = json.Unmarshal(bResult, &result)
  654. if err != nil {
  655. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", err.Error(), string(bResult))
  656. utils.FileLog.Info("桥接服务get请求失败:\n" + string(bResult))
  657. return
  658. }
  659. //totalPage := result.Data.Paging.Pages
  660. // 处理指标信息
  661. for _, v := range result.Data.List {
  662. tmpErr := handleIndex(v)
  663. if tmpErr != nil {
  664. errMsgList = append(errMsgList, tmpErr.Error())
  665. }
  666. }
  667. fmt.Println(currIndex, "是否已结束:", result.Data.Page.IsEnd)
  668. // 如果还有下一页,那么就继续请求下一页
  669. //if !result.Data.Paging.IsEnd {
  670. // _, tmpErrMsgList := syncGnIndex(currIndex+1, utils.SyncCrmIndexNum, baseLastUpdateTimeStr)
  671. // errMsgList = append(errMsgList, tmpErrMsgList...)
  672. //}
  673. return
  674. }
  675. func LoadGnTempIndexIds() (indexIds []int, err error) {
  676. filePath := "./static/gn_index_ids.json"
  677. b, e := ioutil.ReadFile(filePath)
  678. if e != nil {
  679. err = fmt.Errorf("读取配置失败, err: %v", e)
  680. return
  681. }
  682. if e = json.Unmarshal(b, &indexIds); e != nil {
  683. err = fmt.Errorf("解析配置失败, err: %v", e)
  684. return
  685. }
  686. return
  687. }
  688. // 用户同步的锁
  689. var lockSyncUser sync.Mutex
  690. func SyncGnUser(cont context.Context) (err error) {
  691. lockSyncUser.Lock()
  692. defer func() {
  693. if err != nil {
  694. tips := "SyncUser-定时将第三方的用户数据同步到ETA失败, ErrMsg:\n" + err.Error()
  695. utils.FileLog.Info(tips)
  696. go alarm_msg.SendAlarmMsg(tips, 3)
  697. }
  698. lockSyncUser.Unlock()
  699. }()
  700. uri := "/gn/user/sync"
  701. fmt.Println("开始同步OA用户")
  702. _, err, _ = HttpEtaBridgeGet(uri)
  703. if err != nil {
  704. return
  705. }
  706. fmt.Println("结束同步OA用户")
  707. return
  708. }
  709. // InitRefreshEdb
  710. // @Description: 初始化明细数据指标
  711. // @param cont
  712. // @return err
  713. func InitRefreshEdb(cont context.Context) (err error) {
  714. fmt.Println("准备更新指标明细数据")
  715. lockSyncGnIndex.Lock()
  716. errMsgList := make([]string, 0)
  717. fmt.Println("开始更新指标明细数据")
  718. defer func() {
  719. fmt.Println("初始化指标明细数据结束")
  720. if err != nil {
  721. tips := "SyncGnIndex-初始化指标明细数据结束到ETA失败, ErrMsg:\n" + err.Error()
  722. utils.FileLog.Info(tips)
  723. }
  724. if len(errMsgList) > 0 {
  725. tips := "SyncGnIndex-初始化指标明细数据结束到ETA失败, ErrMsg:\n" + strings.Join(errMsgList, "\n")
  726. utils.FileLog.Info(tips)
  727. }
  728. lockSyncGnIndex.Unlock()
  729. }()
  730. initCurrEdbInfoMap()
  731. count := len(CurrEdbInfoMap)
  732. for _, v := range CurrEdbInfoMap {
  733. count--
  734. fmt.Println("剩余:", count, "条数据待初始化")
  735. // 未初始化明细数据的指标
  736. //if !v.EndDate.IsZero() {
  737. // continue
  738. //}
  739. fmt.Println(data.RefreshEdbData(v.EdbInfoId, v.Source, v.SubSource, v.EdbCode, utils.BaseEdbRefreshStartDate))
  740. }
  741. return
  742. }
  743. // PostOAReq
  744. // @Description: 请求oa待办参数
  745. type PostOAReq struct {
  746. AppPersonId string `json:"appPersonId" form:"appPersonId" description:"⽤户⼯号"`
  747. AppPersonName string `json:"appPersonName" form:"appPersonName" description:"⽤户名"`
  748. AppTaskUrl string `json:"appTaskUrl" form:"appTaskUrl" description:"待办地址,跳转地址"`
  749. TaskName string `json:"taskName" form:"taskName" description:"待办标题"`
  750. StatusName string `json:"statusName" form:"statusName" description:"待办节点名称"`
  751. Status int `json:"status" form:"status" description:"待办状态:1-插⼊,2-更新,3-删除"`
  752. AppTaskId string `json:"appTaskId" form:"appTaskId" description:"待办任务ID:插⼊时不需要传参,由接⼝⽣成唯⼀ID并返回,需要业务系统保存此ID,⽤于后续的更新和删除操作"`
  753. }
  754. // PostOAResp 请求OA响应
  755. type PostOAResp struct {
  756. Code int `json:"code" description:"状态码"`
  757. Msg string `json:"msg" description:"提示信息"`
  758. Data string `json:"data" description:"待办任务ID"`
  759. }