shanghai_smm.go 13 KB

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