gn.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. }
  285. err = data_manage.AddEdbClassify(classifyInfo)
  286. if err != nil {
  287. return
  288. }
  289. classifyInfo.RootID = classifyInfo.ClassifyID
  290. err = classifyInfo.Update([]string{"root_id"})
  291. if err != nil {
  292. return
  293. }
  294. currClassify = CurrClassify{
  295. ClassifyId: classifyInfo.ClassifyID,
  296. ParentId: classifyInfo.ParentID,
  297. ClassifyName: classifyInfo.ClassifyName,
  298. }
  299. currClassifyListMap[classifyName] = currClassify
  300. currParentClassifyMap[parentId] = currClassifyListMap
  301. CurrLevelParentClassifyMap[level] = currParentClassifyMap
  302. }
  303. firstClassifyId = currClassify.ClassifyId
  304. }
  305. // 二级分类
  306. {
  307. parentId := firstClassifyId
  308. classifyName := secondClassifyName
  309. level := twoLevel
  310. if secondClassifyName == `` {
  311. return
  312. }
  313. // 获取层级下的父级分类map
  314. currParentClassifyMap, ok := CurrLevelParentClassifyMap[level]
  315. if !ok {
  316. currParentClassifyMap = make(map[int64]map[string]CurrClassify)
  317. }
  318. // 获取父级id下的分类列表
  319. currClassifyListMap, ok := currParentClassifyMap[parentId]
  320. if !ok {
  321. currClassifyListMap = make(map[string]CurrClassify)
  322. }
  323. // 根据分类名称获取分类
  324. currClassify, ok := currClassifyListMap[classifyName]
  325. if !ok {
  326. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  327. classifyInfo := &data_manage.EdbClassify{
  328. //ClassifyId: 0,
  329. ClassifyType: 0,
  330. ClassifyName: classifyName,
  331. ClassifyNameEn: classifyName,
  332. ParentID: parentId,
  333. RootID: firstClassifyId,
  334. HasData: 0,
  335. CreateTime: time.Now(),
  336. ModifyTime: time.Now(),
  337. SysUserID: 0,
  338. SysUserRealName: "",
  339. Level: level,
  340. UniqueCode: utils.MD5(fmt.Sprint(parentId, "_", utils.DATA_PREFIX+"_"+timestamp)),
  341. Sort: 0,
  342. }
  343. err = data_manage.AddEdbClassify(classifyInfo)
  344. if err != nil {
  345. return
  346. }
  347. currClassify = CurrClassify{
  348. ClassifyId: classifyInfo.ClassifyID,
  349. ParentId: classifyInfo.ParentID,
  350. ClassifyName: classifyInfo.ClassifyName,
  351. }
  352. currClassifyListMap[classifyName] = currClassify
  353. currParentClassifyMap[parentId] = currClassifyListMap
  354. CurrLevelParentClassifyMap[level] = currParentClassifyMap
  355. }
  356. secondClassifyId = currClassify.ClassifyId
  357. }
  358. // 三级分类
  359. {
  360. parentId := secondClassifyId
  361. classifyName := thirdClassifyName
  362. level := threeLevel
  363. if thirdClassifyName == `` {
  364. return
  365. }
  366. // 获取层级下的父级分类map
  367. currParentClassifyMap, ok := CurrLevelParentClassifyMap[level]
  368. if !ok {
  369. currParentClassifyMap = make(map[int64]map[string]CurrClassify)
  370. }
  371. // 获取父级id下的分类列表
  372. currClassifyListMap, ok := currParentClassifyMap[parentId]
  373. if !ok {
  374. currClassifyListMap = make(map[string]CurrClassify)
  375. }
  376. // 根据分类名称获取分类
  377. currClassify, ok := currClassifyListMap[classifyName]
  378. if !ok {
  379. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  380. classifyInfo := &data_manage.EdbClassify{
  381. //ClassifyId: 0,
  382. ClassifyType: 0,
  383. ClassifyName: classifyName,
  384. ClassifyNameEn: classifyName,
  385. ParentID: parentId,
  386. RootID: firstClassifyId,
  387. HasData: 1,
  388. CreateTime: time.Now(),
  389. ModifyTime: time.Now(),
  390. SysUserID: 0,
  391. SysUserRealName: "",
  392. Level: level,
  393. UniqueCode: utils.MD5(fmt.Sprint(parentId, "_", utils.DATA_PREFIX+"_"+timestamp)),
  394. Sort: 0,
  395. }
  396. err = data_manage.AddEdbClassify(classifyInfo)
  397. if err != nil {
  398. return
  399. }
  400. currClassify = CurrClassify{
  401. ClassifyId: classifyInfo.ClassifyID,
  402. ParentId: classifyInfo.ParentID,
  403. ClassifyName: classifyInfo.ClassifyName,
  404. }
  405. currClassifyListMap[classifyName] = currClassify
  406. currParentClassifyMap[parentId] = currClassifyListMap
  407. CurrLevelParentClassifyMap[level] = currParentClassifyMap
  408. }
  409. thirdClassifyId = currClassify.ClassifyId
  410. }
  411. return
  412. }
  413. func getClassifyName(classifyName string) string {
  414. classifyName = strings.TrimSpace(classifyName)
  415. if classifyName == `` {
  416. return classifyName
  417. }
  418. // 如果不是“其它指标”,那么就是处理掉带有序号的、
  419. if classifyName != `其它指标` {
  420. classifyNameList := strings.Split(classifyName, `、`)
  421. if len(classifyNameList) > 0 {
  422. classifyName = classifyNameList[len(classifyNameList)-1]
  423. }
  424. }
  425. return classifyName
  426. }
  427. // handleEdbInfo
  428. // @Description: 处理指标
  429. // @param index
  430. // @param thirdClassifyId
  431. // @return err
  432. func handleEdbInfo(index IndexInfo, thirdClassifyId int64) (err error) {
  433. // 过滤数据节点指标唯一编码为空的数据
  434. if index.DataIndexCode == `` {
  435. return
  436. }
  437. // 过滤基础指标编码为空的数据
  438. if index.SourceEdbCode == `` {
  439. return
  440. }
  441. edbInfo, ok := CurrEdbInfoMap[index.DataIndexCode]
  442. frequency := Frequency(strings.TrimSpace(index.Frequency))
  443. unit := strings.TrimSpace(index.Unit)
  444. sourceName, sourceId, err := GetSource(strings.TrimSpace(index.SourceName), strings.TrimSpace(index.SourceCode))
  445. if err != nil {
  446. return
  447. }
  448. if !ok {
  449. endDate := time.Date(1899, 1, 1, 0, 0, 0, 0, time.Local)
  450. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  451. edbInfo = &data_manage.EdbInfo{
  452. EdbInfoId: 0,
  453. EdbInfoType: utils.EDB_INFO_TYPE,
  454. SourceName: sourceName,
  455. Source: sourceId,
  456. EdbCode: index.SourceEdbCode,
  457. EdbName: index.EdbName,
  458. EdbNameEn: index.EdbName,
  459. EdbNameSource: index.EdbName,
  460. Frequency: frequency,
  461. Unit: unit,
  462. UnitEn: unit,
  463. StartDate: endDate,
  464. EndDate: endDate,
  465. ClassifyId: int(thirdClassifyId),
  466. SysUserId: 0,
  467. SysUserRealName: "",
  468. UniqueCode: utils.MD5(fmt.Sprint(index.SourceEdbCode, "_", utils.DATA_PREFIX+"_"+timestamp)),
  469. CreateTime: time.Now(),
  470. ModifyTime: time.Now(),
  471. BaseModifyTime: time.Now(),
  472. MinValue: 0,
  473. MaxValue: 0,
  474. CalculateFormula: "",
  475. EdbType: utils.EdbTypeBase,
  476. Sort: 0,
  477. LatestDate: "",
  478. LatestValue: 0,
  479. EndValue: 0,
  480. MoveType: 0,
  481. MoveFrequency: "",
  482. NoUpdate: 0,
  483. ServerUrl: "",
  484. ChartImage: "", // 缩略图
  485. Calendar: "",
  486. DataDateType: "",
  487. ManualSave: 0,
  488. EmptyType: 0,
  489. MaxEmptyType: 0,
  490. TerminalCode: "",
  491. DataUpdateTime: "",
  492. ErDataUpdateDate: "",
  493. SourceIndexName: index.EdbName,
  494. SubSource: 0,
  495. SubSourceName: "",
  496. IndicatorCode: "",
  497. StockCode: "",
  498. Extra: "",
  499. IsJoinPermission: 0,
  500. OriginalEdbCode: index.DataIndexCode,
  501. }
  502. err = data_manage.AddEdbInfo(edbInfo)
  503. if err != nil {
  504. return
  505. }
  506. CurrEdbInfoMap[index.DataIndexCode] = edbInfo
  507. // TODO 刷新指标明细数据
  508. fmt.Println(data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.EndDate.Format(utils.FormatDate)))
  509. return
  510. }
  511. updateCols := make([]string, 0)
  512. if edbInfo.EdbNameEn == edbInfo.EdbName && edbInfo.EdbName != index.EdbName {
  513. edbInfo.EdbNameEn = index.EdbName
  514. updateCols = append(updateCols, "edb_name_en")
  515. }
  516. if edbInfo.EdbName != index.EdbName {
  517. edbInfo.EdbName = index.EdbName
  518. updateCols = append(updateCols, "edb_name")
  519. }
  520. if edbInfo.Frequency != index.Frequency {
  521. edbInfo.Frequency = index.Frequency
  522. updateCols = append(updateCols, "frequency")
  523. }
  524. if edbInfo.UnitEn == edbInfo.Unit && edbInfo.Unit != unit {
  525. edbInfo.UnitEn = unit
  526. updateCols = append(updateCols, "unit_en")
  527. }
  528. if edbInfo.Unit != unit {
  529. edbInfo.Unit = unit
  530. updateCols = append(updateCols, "unit")
  531. }
  532. if edbInfo.ClassifyId != int(thirdClassifyId) {
  533. edbInfo.ClassifyId = int(thirdClassifyId)
  534. updateCols = append(updateCols, "classify_id")
  535. }
  536. if len(updateCols) > 0 {
  537. err = edbInfo.Update(updateCols)
  538. }
  539. return
  540. }
  541. // Frequency
  542. // @Description: 获取频度
  543. // @param unit
  544. // @return string
  545. func Frequency(unit string) string {
  546. switch unit {
  547. case "半月度":
  548. unit = `周度`
  549. case "不定期":
  550. unit = `日度`
  551. case `日度`, `周度`, `旬度`, `月度`, `季度`, `半年度`, `年度`:
  552. default:
  553. unit = ``
  554. }
  555. return unit
  556. }
  557. // GetSource
  558. // @Description: 获取来源
  559. // @param sourceName
  560. // @return gnSourceName
  561. // @return sourceCode
  562. // @return source
  563. // @return err
  564. func GetSource(sourceName, sourceCode string) (gnSourceName string, source int, err error) {
  565. gnSourceName = sourceName
  566. var tableNameSuffix, indexNamePrefix string
  567. tableNamePrefix := "edb_data_gn_"
  568. switch sourceName {
  569. case "CCTD":
  570. tableNameSuffix = "cctd"
  571. case "mysteel":
  572. tableNameSuffix = "mysteel"
  573. case "wind":
  574. tableNameSuffix = "wind"
  575. case "卓创":
  576. tableNameSuffix = "sci"
  577. case "CCI":
  578. tableNameSuffix = "cci"
  579. //return
  580. default:
  581. // 来源编码前缀把还是
  582. tableNameSuffix = strings.ToLower(sourceCode)
  583. //if strings.Contains(sourceName, "国能购销辅助决策系统") {
  584. // gnSourceName = `国能购销辅助决策系统`
  585. // tableNameSuffix = "purchase_sales"
  586. //} else if strings.Contains(sourceName, "国能市场分析平台") {
  587. // gnSourceName = `国能市场分析平台`
  588. // tableNameSuffix = "market_analysis"
  589. //} else {
  590. // // TODO 自动生成表名(暂时以时间作为表名后缀,如果客户提前告知了,那么可以直接先建data表,以及edb_source表写入新的source)
  591. // tableNameSuffix = fmt.Sprint(time.Now().Format(utils.FormatDateTimeUnSpace))
  592. //}
  593. }
  594. sourceItem := data_manage.GetEdbSourceBySourceName(gnSourceName)
  595. // 如果找不到,说明是
  596. if sourceItem == nil {
  597. utils.FileLog.Debug("%s表不存在,需要创建相关的表:%s", gnSourceName, tableNamePrefix+tableNameSuffix)
  598. indexNamePrefix = strings.ToUpper(tableNameSuffix)
  599. sourceItem = &data_manage.EdbSource{
  600. EdbSourceId: 0,
  601. SourceName: gnSourceName,
  602. TableName: tableNamePrefix + tableNameSuffix,
  603. EdbAddMethod: "gn_index/add",
  604. EdbRefreshMethod: "gn_index/refresh",
  605. IsBase: 1,
  606. FromBridge: 1,
  607. BridgeFlag: "bridge_gn",
  608. SourceExtend: gnSourceName,
  609. EdbCodeRequired: 1,
  610. IndexTableName: "",
  611. SourceNameEn: gnSourceName,
  612. }
  613. err = data_manage.AddEdbSource(sourceItem, indexNamePrefix)
  614. if err != nil {
  615. return
  616. }
  617. }
  618. source = sourceItem.EdbSourceId
  619. return
  620. }
  621. func syncGnIndexV2(currIndex, pageSize int, baseLastUpdateTimeStr string, indexId string) (err error, errMsgList []string) {
  622. fmt.Println("开始第", currIndex, "页的更新")
  623. errMsgList = make([]string, 0)
  624. lastUpdateTimeStr := baseLastUpdateTimeStr
  625. if lastUpdateTimeStr != `` {
  626. lastUpdateTimeStr = url.QueryEscape(lastUpdateTimeStr)
  627. }
  628. params := BridgeGnIndexParams{
  629. LastModifyTime: lastUpdateTimeStr,
  630. PageIndex: currIndex,
  631. PageSize: pageSize,
  632. IndexId: indexId,
  633. }
  634. bResult, err, _ := HttpEtaBridgePost(utils.SyncIndexPath, params)
  635. if err != nil {
  636. return
  637. }
  638. var result EtaBridgeGnIndexListResp
  639. err = json.Unmarshal(bResult, &result)
  640. if err != nil {
  641. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", err.Error(), string(bResult))
  642. utils.FileLog.Info("桥接服务get请求失败:\n" + string(bResult))
  643. return
  644. }
  645. //totalPage := result.Data.Paging.Pages
  646. // 处理指标信息
  647. for _, v := range result.Data.List {
  648. tmpErr := handleIndex(v)
  649. if tmpErr != nil {
  650. errMsgList = append(errMsgList, tmpErr.Error())
  651. }
  652. }
  653. fmt.Println(currIndex, "是否已结束:", result.Data.Page.IsEnd)
  654. // 如果还有下一页,那么就继续请求下一页
  655. //if !result.Data.Paging.IsEnd {
  656. // _, tmpErrMsgList := syncGnIndex(currIndex+1, utils.SyncCrmIndexNum, baseLastUpdateTimeStr)
  657. // errMsgList = append(errMsgList, tmpErrMsgList...)
  658. //}
  659. return
  660. }
  661. func LoadGnTempIndexIds() (indexIds []int, err error) {
  662. filePath := "./static/gn_index_ids.json"
  663. b, e := ioutil.ReadFile(filePath)
  664. if e != nil {
  665. err = fmt.Errorf("读取配置失败, err: %v", e)
  666. return
  667. }
  668. if e = json.Unmarshal(b, &indexIds); e != nil {
  669. err = fmt.Errorf("解析配置失败, err: %v", e)
  670. return
  671. }
  672. return
  673. }
  674. // 用户同步的锁
  675. var lockSyncUser sync.Mutex
  676. func SyncGnUser(cont context.Context) (err error) {
  677. lockSyncUser.Lock()
  678. defer func() {
  679. if err != nil {
  680. tips := "SyncUser-定时将第三方的用户数据同步到ETA失败, ErrMsg:\n" + err.Error()
  681. utils.FileLog.Info(tips)
  682. go alarm_msg.SendAlarmMsg(tips, 3)
  683. }
  684. lockSyncUser.Unlock()
  685. }()
  686. uri := "/gn/user/sync"
  687. fmt.Println("开始同步OA用户")
  688. _, err, _ = HttpEtaBridgeGet(uri)
  689. if err != nil {
  690. return
  691. }
  692. fmt.Println("结束同步OA用户")
  693. return
  694. }
  695. // InitRefreshEdb
  696. // @Description: 初始化明细数据指标
  697. // @param cont
  698. // @return err
  699. func InitRefreshEdb(cont context.Context) (err error) {
  700. fmt.Println("准备更新指标明细数据")
  701. lockSyncGnIndex.Lock()
  702. errMsgList := make([]string, 0)
  703. fmt.Println("开始更新指标明细数据")
  704. defer func() {
  705. fmt.Println("初始化指标明细数据结束")
  706. if err != nil {
  707. tips := "SyncGnIndex-初始化指标明细数据结束到ETA失败, ErrMsg:\n" + err.Error()
  708. utils.FileLog.Info(tips)
  709. }
  710. if len(errMsgList) > 0 {
  711. tips := "SyncGnIndex-初始化指标明细数据结束到ETA失败, ErrMsg:\n" + strings.Join(errMsgList, "\n")
  712. utils.FileLog.Info(tips)
  713. }
  714. lockSyncGnIndex.Unlock()
  715. }()
  716. initCurrEdbInfoMap()
  717. count := len(CurrEdbInfoMap)
  718. for _, v := range CurrEdbInfoMap {
  719. count--
  720. fmt.Println("剩余:", count, "条数据待初始化")
  721. // 未初始化明细数据的指标
  722. //if !v.EndDate.IsZero() {
  723. // continue
  724. //}
  725. fmt.Println(data.RefreshEdbData(v.EdbInfoId, v.Source, v.SubSource, v.EdbCode, utils.BaseEdbRefreshStartDate))
  726. }
  727. return
  728. }
  729. // PostOAReq
  730. // @Description: 请求oa待办参数
  731. type PostOAReq struct {
  732. AppPersonId string `json:"appPersonId" form:"appPersonId" description:"⽤户⼯号"`
  733. AppPersonName string `json:"appPersonName" form:"appPersonName" description:"⽤户名"`
  734. AppTaskUrl string `json:"appTaskUrl" form:"appTaskUrl" description:"待办地址,跳转地址"`
  735. TaskName string `json:"taskName" form:"taskName" description:"待办标题"`
  736. StatusName string `json:"statusName" form:"statusName" description:"待办节点名称"`
  737. Status int `json:"status" form:"status" description:"待办状态:1-插⼊,2-更新,3-删除"`
  738. AppTaskId string `json:"appTaskId" form:"appTaskId" description:"待办任务ID:插⼊时不需要传参,由接⼝⽣成唯⼀ID并返回,需要业务系统保存此ID,⽤于后续的更新和删除操作"`
  739. }
  740. // PostOAResp 请求OA响应
  741. type PostOAResp struct {
  742. Code int `json:"code" description:"状态码"`
  743. Msg string `json:"msg" description:"提示信息"`
  744. Data string `json:"data" description:"待办任务ID"`
  745. }