shanghai_smm.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. shanghaismm "eta/eta_index_lib/services/shanghai_smm"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/mozillazg/go-pinyin"
  13. )
  14. type ShanghaiSmmController struct {
  15. BaseAuthController
  16. }
  17. var ShanghaiSmmNameToIndexMap = make(map[string]*models.BaseFromSmmIndex)
  18. var ShanghaiSmmCodeToNameMap = make(map[string]string)
  19. var ShanghaiSmmNameToCodeMap = make(map[string]string)
  20. // @Title 刷新数据
  21. // @Description 刷新数据接口
  22. // @Param request body models.AddEdbClassifyReq true "type json string"
  23. // @Success 200 {object} models.EdbClassify
  24. // @router /refresh/list [post]
  25. func (this *ShanghaiSmmController) RefreshData() {
  26. br := new(models.BaseResponse).Init()
  27. var err error
  28. defer func() {
  29. if err != nil {
  30. utils.FileLog.Error("SMM-RefreshData err:", err)
  31. }
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. var req []*shanghaismm.EdbInfoData
  36. if err = json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  37. br.Msg = "参数解析异常!"
  38. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  39. return
  40. }
  41. allIndex, err := models.GetBaseFromSmmIndex()
  42. if err != nil {
  43. fmt.Println("select Code err:", err)
  44. utils.FileLog.Info("GetBaseFromSci99Index err:", err)
  45. return
  46. }
  47. for _, item := range allIndex {
  48. ShanghaiSmmNameToIndexMap[item.IndexName] = item
  49. ShanghaiSmmNameToCodeMap[item.IndexName] = item.IndexCode
  50. }
  51. // 检查指标是否存在,如果不存在添加指标
  52. addIndexList := make([]*models.BaseFromSmmIndex, 0)
  53. updateIndexList := make([]*models.BaseFromSmmIndex, 0)
  54. addDateList := make([]*models.BaseFromSmmData, 0)
  55. readyDateList := make([]*models.BaseFromSmmData, 0)
  56. updateIndexCode := make([]string, 0)
  57. for _, v := range req {
  58. indexInfo := ShanghaiSmmNameToIndexMap[v.IndexName]
  59. if indexInfo == nil {
  60. // 指标不存在,需要添加指标索引
  61. indexCodeStr := "smm" + v.IndexName
  62. indexCodeStr, needAdd := ShanghaiSmmIndexCodeGenerator(v.IndexName, indexCodeStr)
  63. // 添加指标
  64. if needAdd {
  65. fmt.Printf("Name:%s, indexCode:%s \n", v.IndexName, indexCodeStr)
  66. // 添加指标
  67. item := new(models.BaseFromSmmIndex)
  68. item.IndexCode = indexCodeStr
  69. item.IndexName = v.IndexName
  70. item.Frequency = v.Frequency
  71. item.StartDate = v.RenewDate
  72. item.EndDate = v.RenewDate
  73. item.CreateTime = time.Now()
  74. item.Unit = v.Unit
  75. item.ModifyTime = time.Now()
  76. addIndexList = append(addIndexList, item)
  77. // 添加指标数据
  78. itemDate := new(models.BaseFromSmmData)
  79. itemDate.IndexCode = indexCodeStr
  80. itemDate.DataTime = v.RenewDate
  81. if v.Value == "highs" {
  82. itemDate.Value = strconv.FormatFloat(v.Highs, 'f', -1, 64)
  83. } else if v.Value == "low" {
  84. itemDate.Value = strconv.FormatFloat(v.Low, 'f', -1, 64)
  85. } else {
  86. itemDate.Value = strconv.FormatFloat(v.Average, 'f', -1, 64)
  87. }
  88. itemDate.CreateTime = time.Now()
  89. itemDate.ModifyTime = time.Now()
  90. itemDate.DataTimestamp = time.Now().UnixMilli()
  91. readyDateList = append(readyDateList, itemDate)
  92. } else {
  93. fmt.Printf("有重复的指标或指标名称 Name:%s, indexCode:%s \n", v.IndexName, indexCodeStr)
  94. return
  95. }
  96. } else {
  97. //存在指标,检查是否需要更新指标索引,及指标数据
  98. if indexInfo.EndDate != v.RenewDate {
  99. // 指标日期更新
  100. t := new(models.BaseFromSmmIndex)
  101. t.BaseFromSmmIndexId = indexInfo.BaseFromSmmIndexId
  102. t.EndDate = v.RenewDate
  103. updateIndexList = append(updateIndexList, t)
  104. // 指标数据更新
  105. item := new(models.BaseFromSmmData)
  106. item.BaseFromSmmIndexId = int(indexInfo.BaseFromSmmIndexId)
  107. item.IndexCode = indexInfo.IndexCode
  108. item.DataTime = v.RenewDate
  109. if v.Value == "highs" {
  110. item.Value = strconv.FormatFloat(v.Highs, 'f', -1, 64)
  111. } else if v.Value == "low" {
  112. item.Value = strconv.FormatFloat(v.Low, 'f', -1, 64)
  113. } else {
  114. item.Value = strconv.FormatFloat(v.Average, 'f', -1, 64)
  115. }
  116. item.CreateTime = time.Now()
  117. item.ModifyTime = time.Now()
  118. item.DataTimestamp = time.Now().UnixMilli()
  119. addDateList = append(addDateList, item)
  120. updateIndexCode = append(updateIndexCode, indexInfo.IndexCode)
  121. } else {
  122. smmData, err := models.GetBaseFromSmmDataByCodeAndDate(indexInfo.IndexCode, v.RenewDate)
  123. if err != nil {
  124. fmt.Println("刷新数据失败,Err:", err.Error())
  125. utils.FileLog.Info("刷新数据失败,Err:", err.Error())
  126. continue
  127. }
  128. var smmDataValue string
  129. if v.Value == "highs" {
  130. smmDataValue = strconv.FormatFloat(v.Highs, 'f', -1, 64)
  131. } else if v.Value == "low" {
  132. smmDataValue = strconv.FormatFloat(v.Low, 'f', -1, 64)
  133. } else {
  134. smmDataValue = strconv.FormatFloat(v.Average, 'f', -1, 64)
  135. }
  136. if smmDataValue != "" && smmData.Value != smmDataValue {
  137. smmData.Value = smmDataValue
  138. smmData.ModifyTime = time.Now()
  139. err = smmData.Update([]string{"Value", "ModifyTime"})
  140. if err != nil {
  141. utils.FileLog.Info("indexCode: %s,更新指标数据失败,Err:%s", v.IndexCode, err.Error())
  142. }
  143. updateIndexCode = append(updateIndexCode, indexInfo.IndexCode)
  144. }
  145. }
  146. }
  147. }
  148. if len(addIndexList) > 0 {
  149. _, err = models.AddBaseFromSmmIndex(addIndexList)
  150. if err != nil {
  151. utils.FileLog.Info("Error 插入指标库失败:", err)
  152. br.Msg = "插入指标库失败"
  153. br.ErrMsg = "插入指标库失败,Err:" + err.Error()
  154. return
  155. }
  156. }
  157. if len(updateIndexList) > 0 {
  158. for _, v := range updateIndexList {
  159. err = models.ModifyBaseFromSmmIndexDate(v)
  160. if err != nil {
  161. br.Msg = "更新指标数据失败"
  162. br.ErrMsg = "更新指标失败,Err:" + err.Error()
  163. return
  164. }
  165. }
  166. }
  167. if len(addDateList) > 0 {
  168. err = models.AddBaseFromSmmData(addDateList)
  169. if err != nil {
  170. utils.FileLog.Info("Error 插入指标数据失败:", err)
  171. br.Msg = "插入指标数据失败"
  172. br.ErrMsg = "插入指标数据失败,Err:" + err.Error()
  173. return
  174. }
  175. }
  176. allIndex, err = models.GetBaseFromSmmIndex()
  177. if err != nil {
  178. fmt.Println("select Code err:", err)
  179. utils.FileLog.Info("GetBaseFromSci99Index err:", err)
  180. return
  181. }
  182. // 获得更新后的数据
  183. for _, item := range allIndex {
  184. ShanghaiSmmNameToIndexMap[item.IndexName] = item
  185. ShanghaiSmmNameToCodeMap[item.IndexName] = item.IndexCode
  186. }
  187. for _, v := range readyDateList {
  188. indexName := ShanghaiSmmCodeToNameMap[v.IndexCode]
  189. baseSmmIndex := ShanghaiSmmNameToIndexMap[indexName]
  190. v.BaseFromSmmIndexId = int(baseSmmIndex.BaseFromSmmIndexId)
  191. }
  192. if len(readyDateList) > 0 {
  193. err = models.AddBaseFromSmmData(readyDateList)
  194. if err != nil {
  195. utils.FileLog.Info("Error 插入指标数据失败:", err)
  196. br.Msg = "插入指标数据失败"
  197. br.ErrMsg = "插入指标数据失败,Err:" + err.Error()
  198. return
  199. }
  200. }
  201. var messages []string
  202. defer func() {
  203. if len(messages) > 0 {
  204. errMsg := strings.Join(messages, ",")
  205. utils.FileLog.Info("SMM RefreshData errMsg: %s", errMsg)
  206. }
  207. }()
  208. for _, v := range updateIndexCode {
  209. itemInfo, err := models.GetSmmIndexInfoMaxAndMinInfo(v)
  210. if err == nil && itemInfo != nil {
  211. e := models.ModifySmmIndexMaxAndMinInfo(v, itemInfo)
  212. if e != nil {
  213. messages = append(messages, fmt.Sprintf("更新指标最大最小值失败, indexCode: %s, err: %s", v, e.Error()))
  214. return
  215. }
  216. }
  217. // 同步刷新ETA图库有色的指标
  218. {
  219. // 获取指标详情
  220. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, v)
  221. if e != nil && e.Error() != utils.ErrNoRow() {
  222. messages = append(messages, fmt.Sprintf("刷新ETA指标异常, indexCode: %s, err: %s", v, e.Error()))
  223. }
  224. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  225. if edbInfo != nil {
  226. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  227. }
  228. }
  229. }
  230. br.Msg = "数据刷新成功"
  231. br.Success = true
  232. br.Ret = 200
  233. }
  234. // @Title 刷新数据
  235. // @Description 刷新数据接口
  236. // @Param request body models.AddEdbClassifyReq true "type json string"
  237. // @Success 200 {object} models.EdbClassify
  238. // @router /refresh/excel [post]
  239. func (this *ShanghaiSmmController) RefreshExcel() {
  240. br := new(models.BaseResponse).Init()
  241. defer func() {
  242. this.Data["json"] = br
  243. this.ServeJSON()
  244. }()
  245. var req []*shanghaismm.EdbInfoData
  246. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  247. br.Msg = "参数解析异常!"
  248. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  249. return
  250. }
  251. allIndex, err := models.GetBaseFromSmmIndex()
  252. if err != nil {
  253. fmt.Println("select Code err:", err)
  254. utils.FileLog.Info("GetBaseFromSci99Index err:", err)
  255. return
  256. }
  257. ShanghaiSmmCodeToIndexMap := make(map[string]*models.BaseFromSmmIndex)
  258. ShanghaiSmmCodeToIndexCount := make(map[string]int)
  259. for _, item := range allIndex {
  260. ShanghaiSmmCodeToIndexMap[item.IndexCode] = item
  261. ShanghaiSmmCodeToIndexCount[item.IndexCode] = 0
  262. ShanghaiSmmNameToCodeMap[item.IndexCode] = item.IndexName
  263. }
  264. updateIndexList := make([]*models.BaseFromSmmIndex, 0)
  265. addDataList := make([]*models.BaseFromSmmData, 0)
  266. updateDataList := make([]*models.BaseFromSmmData, 0)
  267. queryIndexIds := make([]int, 0)
  268. for _, v := range req {
  269. indexCode := strings.Replace(v.IndexCode, " ", "", -1)
  270. indexInfo := ShanghaiSmmCodeToIndexMap[indexCode]
  271. if indexInfo == nil {
  272. fmt.Printf("指标名称或指标id有误v.IndexName:%s, v.IndexCode:%s", v.IndexName, v.IndexCode)
  273. return
  274. }
  275. ShanghaiSmmCodeToIndexCount[v.IndexCode] += 1
  276. if ShanghaiSmmCodeToIndexCount[v.IndexCode] > 1 {
  277. fmt.Printf("指标名称或指标id有误v.IndexName:%s, v.IndexCode:%s", v.IndexName, v.IndexCode)
  278. return
  279. }
  280. queryIndexIds = append(queryIndexIds, int(indexInfo.BaseFromSmmIndexId))
  281. }
  282. list, err := models.GetBaseFromSmmDataByIds(queryIndexIds)
  283. if err != nil {
  284. br.Msg = "查询指标数据失败"
  285. br.ErrMsg = "查询指标数据失败,Err:" + err.Error()
  286. return
  287. }
  288. dateDataMap := make(map[int]map[string]string)
  289. for _, v := range list {
  290. dataMap, ok := dateDataMap[v.BaseFromSmmIndexId]
  291. if ok {
  292. dataMap[v.DataTime] = v.Value
  293. } else {
  294. dateDataMap[v.BaseFromSmmIndexId] = map[string]string{
  295. v.DataTime: v.Value,
  296. }
  297. }
  298. }
  299. for _, v := range req {
  300. indexCode := strings.Replace(v.IndexCode, " ", "", -1)
  301. indexInfo := ShanghaiSmmCodeToIndexMap[indexCode]
  302. isAdd := false
  303. indexItem := new(models.BaseFromSmmIndex)
  304. indexItem.BaseFromSmmIndexId = indexInfo.BaseFromSmmIndexId
  305. startDate, _ := time.Parse(utils.FormatDate, indexInfo.StartDate)
  306. endDate, _ := time.Parse(utils.FormatDate, indexInfo.EndDate)
  307. if v.LastDate.After(endDate) {
  308. isAdd = true
  309. indexItem.EndDate = v.LastDate.Format(utils.FormatDate)
  310. } else {
  311. indexItem.EndDate = indexInfo.EndDate
  312. }
  313. if v.OldDate.Before(startDate) {
  314. isAdd = true
  315. indexItem.StartDate = v.OldDate.Format(utils.FormatDate)
  316. } else {
  317. indexItem.StartDate = indexInfo.StartDate
  318. }
  319. if isAdd {
  320. indexItem.IndexName = v.IndexName
  321. indexItem.ModifyTime = time.Now()
  322. updateIndexList = append(updateIndexList, indexItem)
  323. }
  324. for k, dv := range v.Data {
  325. valueStr := strconv.FormatFloat(dv, 'f', -1, 64)
  326. valueRaw := dateDataMap[int(indexInfo.BaseFromSmmIndexId)][k]
  327. if valueRaw == "" {
  328. dataItem := new(models.BaseFromSmmData)
  329. dataItem.BaseFromSmmIndexId = int(indexInfo.BaseFromSmmIndexId)
  330. dataItem.IndexCode = indexInfo.IndexCode
  331. dataItem.DataTime = k
  332. dataItem.Value = valueStr
  333. dataItem.CreateTime = time.Now()
  334. dataItem.ModifyTime = time.Now()
  335. dataItem.DataTimestamp = time.Now().UnixMilli()
  336. addDataList = append(addDataList, dataItem)
  337. } else if valueStr != "" && valueRaw != valueStr {
  338. dataItem := new(models.BaseFromSmmData)
  339. dataItem.BaseFromSmmIndexId = int(indexInfo.BaseFromSmmIndexId)
  340. dataItem.IndexCode = indexInfo.IndexCode
  341. dataItem.DataTime = k
  342. dataItem.Value = valueStr
  343. dataItem.ModifyTime = time.Now()
  344. dataItem.DataTimestamp = time.Now().UnixMilli()
  345. updateDataList = append(updateDataList, dataItem)
  346. }
  347. }
  348. }
  349. if len(addDataList) > 0 {
  350. err = models.AddBaseFromSmmData(addDataList)
  351. if err != nil {
  352. br.Msg = "插入指标数据失败"
  353. br.ErrMsg = "插入指标数据失败,Err:" + err.Error()
  354. return
  355. }
  356. }
  357. if len(updateIndexList) > 0 {
  358. for _, v := range updateIndexList {
  359. err = v.UpdateCols([]string{"end_date", "start_date", "modify_time"})
  360. if err != nil {
  361. br.Msg = "更新指标索引失败"
  362. br.ErrMsg = "更新指标索引失败,Err:" + err.Error()
  363. return
  364. }
  365. }
  366. }
  367. // 不修改
  368. fmt.Println(len(updateDataList))
  369. var messages []string
  370. defer func() {
  371. if len(messages) > 0 {
  372. errMsg := strings.Join(messages, ",")
  373. utils.FileLog.Info("SMM RefreshExcel errMsg: %s", errMsg)
  374. }
  375. }()
  376. for _, v := range updateDataList {
  377. itemInfo, err := models.GetSmmIndexInfoMaxAndMinInfo(v.IndexCode)
  378. if err == nil && itemInfo != nil {
  379. e := models.ModifySmmIndexMaxAndMinInfo(v.IndexCode, itemInfo)
  380. if e != nil {
  381. messages = append(messages, fmt.Sprintf("更新指标最大最小值失败, indexCode: %s, err: %s", v.IndexCode, e.Error()))
  382. return
  383. }
  384. }
  385. // 同步刷新ETA图库有色的指标
  386. {
  387. // 获取指标详情
  388. edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, v.IndexCode)
  389. if e != nil && e.Error() != utils.ErrNoRow() {
  390. messages = append(messages, fmt.Sprintf("刷新ETA指标异常, indexCode: %s, err: %s", v.IndexCode, e.Error()))
  391. }
  392. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  393. if edbInfo != nil {
  394. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  395. }
  396. }
  397. }
  398. // if len(updateDataList) > 0 {
  399. // err = models.UpdateBaseFromSmmDataListByIndexCode(updateDataList)
  400. // if err != nil {
  401. // br.Msg = "更新指标数据失败"
  402. // br.ErrMsg = "更新指标失败,Err:" + err.Error()
  403. // return
  404. // }
  405. // }
  406. br.Ret = 200
  407. br.Success = true
  408. br.Msg = "数据刷新成功"
  409. }
  410. func ShanghaiSmmIndexCodeGenerator(indexName, indexCodeStr string) (indexCode string, needAdd bool) {
  411. strResult := ""
  412. //首字母
  413. a := pinyin.NewArgs()
  414. a.Fallback = func(r rune, a pinyin.Args) []string {
  415. return []string{string(r)}
  416. }
  417. rows := pinyin.Pinyin(indexCodeStr, a)
  418. for i := 0; i < len(rows); i++ {
  419. //strResult += rows[i][0]
  420. if len(rows[i]) != 0 {
  421. str := rows[i][0]
  422. pi := str[0:1]
  423. strResult += pi
  424. }
  425. }
  426. // 去除特殊符号
  427. strResult = strings.Replace(strResult, " ", "", -1)
  428. strResult = strings.Replace(strResult, "-", "", -1)
  429. strResult = strings.Replace(strResult, "/", "", -1)
  430. strResult = strings.Replace(strResult, "#", "", -1)
  431. strResult = strings.Replace(strResult, ":", "", -1)
  432. strResult = strings.Replace(strResult, "(", "", -1)
  433. strResult = strings.Replace(strResult, ")", "", -1)
  434. strResult = strings.Replace(strResult, "%", "", -1)
  435. strResult = strings.Replace(strResult, "<", "1", -1)
  436. strResult = strings.Replace(strResult, "\xe2", "2", -1) // ≥
  437. strResult = strings.Replace(strResult, ".", "", -1)
  438. strResult = strings.Replace(strResult, ",", "", -1)
  439. strResult = strings.Replace(strResult, ":", "", -1)
  440. needAdd = true
  441. indexCode = strings.Replace(strResult, " ", "", -1)
  442. indexCode = strings.ToLower(indexCode)
  443. if _, ok := ShanghaiSmmCodeToNameMap[indexCode]; !ok {
  444. ShanghaiSmmCodeToNameMap[indexCode] = indexName
  445. ShanghaiSmmNameToCodeMap[indexName] = indexCode
  446. } else {
  447. needAdd = false
  448. }
  449. return
  450. }