edb_data_ys.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * @Author: jgl
  3. * @Date: 2021/9/15 10:15
  4. */
  5. package data_manage
  6. //type EdbDataYs struct {
  7. // EdbDataId int `orm:"column(edb_data_id);pk" gorm:"primaryKey"`
  8. // EdbInfoId int
  9. // EdbCode string
  10. // DataTime string
  11. // Value float64
  12. // Status int
  13. // CreateTime time.Time
  14. // ModifyTime time.Time
  15. // DataTimestamp int64
  16. //}
  17. //func AddEdbDataSmm(items []*EdbDataYs) (err error) {
  18. // o := global.DbMap[utils.DbNameIndex]
  19. // err = o.CreateInBatches(items, utils.MultiAddNum).Error
  20. // return
  21. //}
  22. //func ModifyEdbDataSmm(edbCode, dataTime string, value float64) (err error) {
  23. // o := global.DbMap[utils.DbNameIndex]
  24. // sql := ` UPDATE edb_data_ys SET value=?,status=1,modify_time=NOW() WHERE edb_code=? AND data_time=? `
  25. // err = o.Exec(sql, value, edbCode, dataTime).Error
  26. // return
  27. //}
  28. /*
  29. //有色基础指标同步
  30. func SyncSmmIndexDataBase() {
  31. utils.FileLog.Info("start:" + time.Now().Format(utils.FormatDateTime))
  32. list, err := data_manage.GetBaseFromSmmList()
  33. if err != nil {
  34. fmt.Println("GetBaseFromSmmList Err:" + err.Error())
  35. return
  36. }
  37. token, err := getToken("pqian@hzinsights.com", "hz123456")
  38. if err != nil {
  39. fmt.Println(err)
  40. return
  41. }
  42. endDate := time.Now().Format(utils.FormatDate)
  43. smmIndexAll, err := data_manage.GetBaseFromSmmIndex()
  44. if err != nil {
  45. fmt.Println("GetBaseFromSmmIndex Err:" + err.Error())
  46. return
  47. }
  48. existIndexMap := make(map[string]*data_manage.BaseFromSmmIndex)
  49. for _, item := range smmIndexAll {
  50. existIndexMap[item.IndexCode] = item
  51. }
  52. for k, v := range list {
  53. edbCode := v.Interface
  54. var startDate string
  55. if v.EndDate == "" || v.EndDate == "0000-00-00" {
  56. startDate = v.ApiStartTime
  57. } else {
  58. if v.EndDate != "" {
  59. dateFormate, _ := time.Parse(utils.FormatDate, v.EndDate)
  60. var syncDay int
  61. if v.ApiUpdateType == "day" {
  62. syncDay = utils.DATA_REFRESH
  63. } else if v.ApiUpdateType == "month" {
  64. syncDay = utils.DATA_REFRESH * 30
  65. } else if v.ApiUpdateType == "week" {
  66. syncDay = utils.DATA_REFRESH * 7
  67. } else if v.ApiUpdateType == "year" {
  68. syncDay = utils.DATA_REFRESH * 365
  69. }
  70. startDate = dateFormate.AddDate(0, 0, -syncDay).Format(utils.FormatDate)
  71. }
  72. }
  73. ysItem, err := getApiData(token, edbCode, startDate, endDate)
  74. if err != nil {
  75. fmt.Println(err)
  76. return
  77. }
  78. if ysItem != nil && ysItem.Code == 200 {
  79. frequency := ysItem.Data.Frequency
  80. indexMap := make(map[string]int)
  81. smmIndexIdMap := make(map[int]int)
  82. indexCodeMap := make(map[int]string)
  83. indexKey := 0
  84. var isDateIndex int
  85. for fk, fv := range ysItem.Data.Field {
  86. if fv.IsDate == "1" {
  87. isDateIndex = fk
  88. } else {
  89. if !strings.Contains(fv.Name, "产品名称") &&
  90. !strings.Contains(fv.Name, "单位") &&
  91. !strings.Contains(fv.Name, "时间") &&
  92. !strings.Contains(fv.Name, "备注") {
  93. indexMap[fv.DBColName] = fk
  94. indexKey += 1
  95. indexCode := v.Interface + "#" + strconv.Itoa(indexKey)
  96. if findItem, ok := existIndexMap[indexCode]; !ok {
  97. ssmIndex := new(data_manage.BaseFromSmmIndex)
  98. ssmIndex.Interface = v.Interface
  99. ssmIndex.Name = v.Name
  100. ssmIndex.IndexCode = indexCode
  101. ssmIndex.IndexName = v.Name + "_" + fv.Name
  102. ssmIndex.Type1 = v.Type1
  103. ssmIndex.Type2 = v.Type2
  104. ssmIndex.Type3 = v.Type3
  105. ssmIndex.Frequency = frequency
  106. ssmIndex.Unit = fv.Unit
  107. ssmIndex.ApiStartTime = v.ApiStartTime
  108. ssmIndex.ApiUpdateTime = v.ApiUpdateTime
  109. ssmIndex.StartTime = v.StartTime
  110. ssmIndex.FinishTime = v.FinishTime
  111. ssmIndex.CreateTime = time.Now()
  112. ssmIndex.ModifyTime = time.Now()
  113. lastIndexId, err := data_manage.AddBaseFromSmmIndex(ssmIndex)
  114. if err != nil {
  115. return
  116. }
  117. smmIndexIdMap[fk] = int(lastIndexId)
  118. indexCodeMap[fk] = indexCode
  119. } else {
  120. smmIndexIdMap[fk] = findItem.BaseFromSmmIndexId
  121. indexCodeMap[fk] = findItem.IndexCode
  122. }
  123. }
  124. }
  125. }
  126. existDataMap := make(map[string]*data_manage.BaseFromSmmData)
  127. for _, mv := range indexCodeMap {
  128. indexCode := mv
  129. dataAllList, err := data_manage.GetBaseFromSmmDataAll(indexCode)
  130. if err != nil {
  131. fmt.Println("GetBaseFromSmmData Err:" + err.Error())
  132. return
  133. }
  134. for _, item := range dataAllList {
  135. key := item.IndexCode + item.DataTime
  136. existDataMap[key] = item
  137. }
  138. }
  139. addExistDataMap := make(map[string]string)
  140. for _, dv := range ysItem.Data.Content {
  141. var dataTime string
  142. dataTime = dv[isDateIndex]
  143. if strings.Contains(dataTime, "Q1") {
  144. dataTime = strings.Replace(dataTime, "Q1", "-01", -1)
  145. dataTime += "-31"
  146. }
  147. if strings.Contains(dataTime, "Q2") {
  148. dataTime = strings.Replace(dataTime, "Q2", "-06", -1)
  149. dataTime += "-30"
  150. }
  151. if strings.Contains(dataTime, "Q3") {
  152. dataTime = strings.Replace(dataTime, "Q3", "-09", -1)
  153. dataTime += "-30"
  154. }
  155. if strings.Contains(dataTime, "Q4") {
  156. dataTime = strings.Replace(dataTime, "Q4", "-12", -1)
  157. dataTime += "-31"
  158. }
  159. if strings.Contains(dataTime, "H1") {
  160. dataTime = strings.Replace(dataTime, "H1", "-06", -1)
  161. dataTime += "-30"
  162. }
  163. if strings.Contains(dataTime, "H2") {
  164. dataTime = strings.Replace(dataTime, "H2", "-12", -1)
  165. dataTime += "-31"
  166. }
  167. if frequency == "月" {
  168. monthDate, err := time.Parse("2006-01", dataTime)
  169. if err != nil {
  170. fmt.Println("time.Parse:" + err.Error())
  171. }
  172. lastTime := monthDate.AddDate(0, 1, -1)
  173. lastYear, lastMonth, lastDay := lastTime.Date()
  174. var lastDate string
  175. if int(lastMonth) < 10 {
  176. lastDate = strconv.Itoa(lastYear) + "-" + "0" + strconv.Itoa(int(lastMonth)) + "-" + strconv.Itoa(lastDay)
  177. } else {
  178. lastDate = strconv.Itoa(lastYear) + "-" + strconv.Itoa(int(lastMonth)) + "-" + strconv.Itoa(lastDay)
  179. }
  180. dataTime = lastDate
  181. } else if frequency == "年" {
  182. dataTime = dataTime + "-12-31"
  183. }
  184. saveDataTime, err := time.Parse(utils.FormatDate, dataTime)
  185. if err != nil {
  186. fmt.Println("saveDataTime parse err:" + err.Error() + " dataTime:" + dataTime)
  187. panic(err)
  188. }
  189. timestamp := saveDataTime.UnixNano() / 1e6
  190. //循环指标
  191. for _, v := range indexMap {
  192. indexCode := indexCodeMap[v]
  193. smmIndexId := smmIndexIdMap[v]
  194. dataVal := dv[v]
  195. if indexCode != "" {
  196. key := indexCode + dataTime
  197. val := strings.Replace(dataVal, ",", "", -1)
  198. if findData, dataOk := existDataMap[key]; !dataOk {
  199. if _, addOK := addExistDataMap[key]; !addOK {
  200. if val != "" && val != "-" {
  201. dataItem := new(data_manage.BaseFromSmmData)
  202. dataItem.BaseFromSmmIndexId = int(smmIndexId)
  203. dataItem.IndexCode = indexCode
  204. dataItem.DataTime = dataTime
  205. dataItem.Value = val
  206. dataItem.CreateTime = time.Now()
  207. dataItem.ModifyTime = time.Now()
  208. dataItem.DataTimestamp = timestamp
  209. err = data_manage.AddBaseFromSmmData(dataItem)
  210. if err != nil && !strings.Contains(err.Error(), "idx_index_code_date") {
  211. fmt.Println("AddBaseFromSmmData Err:" + err.Error())
  212. return
  213. }
  214. }
  215. }
  216. } else {
  217. if findData != nil && findData.Value != val { //修改
  218. if _, addOK := addExistDataMap[key]; !addOK {
  219. if val != "" && val != "-" {
  220. err = data_manage.ModifyBaseFromSmmData(findData.SmmDataId, val)
  221. if err != nil {
  222. fmt.Println("ModifyBaseFromSmmData Err:" + err.Error())
  223. return
  224. }
  225. }
  226. }
  227. }
  228. }
  229. addExistDataMap[key] = key
  230. }
  231. }
  232. }
  233. //修改数据开始,结束日期
  234. {
  235. indexList, err := data_manage.GetBaseFromSmmIndexByCode(v.Interface)
  236. if err != nil {
  237. fmt.Println("GetBaseFromSmmIndexByCode Err:" + err.Error())
  238. }
  239. for _, sv := range indexList {
  240. minDate, maxDate, err := data_manage.GetBaseFromSmmMaxOrMinDate(sv.IndexCode)
  241. if err != nil {
  242. fmt.Println("GetEdbDataSmmMaxOrMinDate Err:" + err.Error())
  243. } else {
  244. err = data_manage.ModifyBaseFromSmmMinDateAndMaxDate(sv.BaseFromSmmIndexId, v.BaseFromSmmId, minDate, maxDate)
  245. if err != nil {
  246. fmt.Println("ModifyBaseFromSmmMinDateAndMaxDate Err:" + err.Error())
  247. }
  248. }
  249. }
  250. }
  251. }
  252. fmt.Println(v.Interface, v.Name, k)
  253. time.Sleep(1 * time.Second)
  254. }
  255. utils.FileLog.Info("end:" + time.Now().Format(utils.FormatDateTime))
  256. }
  257. */
  258. //const (
  259. // dataUrl = "https://dataapi.smm.cn/GetData/" //data url (中文)
  260. // //dataUrl = "https://dataapi.smm.cn/GetDataEn/" //data url (english edition)
  261. // authUrl = "https://platform.smm.cn/usercenter/auth" // auth url (for all)
  262. //)
  263. //type TokenResp struct {
  264. // Code int `json:"Code"`
  265. // Msg string `json:"Msg"`
  266. // Data TokenData `json:"Data"`
  267. //}
  268. //type TokenData struct {
  269. // Token string `json:"Token"`
  270. //}
  271. // 获取token
  272. //func getToken(userName string, password string) (string, error) {
  273. // encryptAuth := md5.New()
  274. // encryptAuth.Write([]byte(password)) //encrypt password with md5
  275. // newPassword := hex.EncodeToString(encryptAuth.Sum(nil))
  276. //
  277. // resp, err := http.PostForm(authUrl, url.Values{"user_name": {userName}, "password": {newPassword}})
  278. // if err != nil {
  279. // return "", err
  280. // }
  281. //
  282. // defer resp.Body.Close()
  283. //
  284. // body, err := ioutil.ReadAll(resp.Body)
  285. // if err != nil {
  286. // fmt.Println("reponse error", err)
  287. // return "", err
  288. // }
  289. //
  290. // var bodyJsonContent TokenResp
  291. //
  292. // if err = json.Unmarshal([]byte(body), &bodyJsonContent); err != nil {
  293. // fmt.Println(err, "unmarsal failure")
  294. // return "", err
  295. // }
  296. //
  297. // var token string
  298. // if bodyJsonContent.Code == 0 {
  299. // token = bodyJsonContent.Data.Token
  300. // }
  301. //
  302. // //print(token)
  303. // return token, nil
  304. //}
  305. // request response
  306. //type DataResp struct {
  307. // Code int `json:"Code"`
  308. // Msg string `json:"Msg"`
  309. // Data *ApiData `json:"Data"`
  310. //}
  311. // api data response
  312. //type ApiData struct {
  313. // Status int `json:"Status"` //0 no permission,1 ok
  314. // Field []ApiField `json:"Field"`
  315. // Content [][]string `json:"Content"`
  316. //}
  317. // api title
  318. //type ApiField struct {
  319. // Unit string `json:"Unit"`
  320. // Info string `json:"Info"`
  321. // Name string `json:"Name"`
  322. // ColumnType string `json:"ColumnType"`
  323. // ColIndex uint `json:"ColIndex"`
  324. // IsDate string `json:"IsDate"`
  325. //}
  326. //type YsResult struct {
  327. // Code int64 `json:"Code"`
  328. // Data struct {
  329. // CompanyList []interface{} `json:"CompanyList"`
  330. // Content [][]string `json:"Content"`
  331. // Field []struct {
  332. // ColIndex int64 `json:"ColIndex"`
  333. // ColumnType string `json:"ColumnType"`
  334. // Info string `json:"Info"`
  335. // IsDate string `json:"IsDate"`
  336. // Name string `json:"Name"`
  337. // Unit string `json:"Unit"`
  338. // DBColName string `json:"db_col_name"`
  339. // } `json:"Field"`
  340. // CountPage int64 `json:"count_page"`
  341. // CurrentPage int64 `json:"current_page"`
  342. // Frequency string `json:"frequency"`
  343. // Mindate string `json:"mindate"`
  344. // PageNum int64 `json:"page_num"`
  345. // Status int64 `json:"status"`
  346. // TotalNum int64 `json:"total_num"`
  347. // } `json:"Data"`
  348. // Msg string `json:"Msg"`
  349. //}
  350. /*
  351. * request data
  352. * sdatetime,edatetime ==>format:yyyy-mm-dd,
  353. * apiName ==> data.metal.com(for english)/data.smm.cn (for chinese)
  354. */
  355. //func getApiData(token string, apiName string, sdatetime string, edatetime string) (item *YsResult, err error) {
  356. // reqUrl := dataUrl + apiName
  357. // resp, err := http.PostForm(reqUrl, url.Values{"token": {token}, "sdatetime": {sdatetime}, "edatetime": {edatetime}})
  358. // if err != nil {
  359. // return nil, err
  360. // }
  361. //
  362. // defer resp.Body.Close()
  363. // body, err := ioutil.ReadAll(resp.Body)
  364. // if err != nil {
  365. // fmt.Println("response error")
  366. // return nil, err
  367. // }
  368. // utils.FileLog.Info("ys result:" + string(body))
  369. // dataJsonContent := new(YsResult)
  370. // if err = json.Unmarshal([]byte(body), &dataJsonContent); err != nil {
  371. // fmt.Println(err, "data unmarshal failure")
  372. // return nil, err
  373. // }
  374. //
  375. // if dataJsonContent.Code == 200 && len(dataJsonContent.Data.Content) > 0 {
  376. // return dataJsonContent, nil
  377. // } else {
  378. // err = errors.New("code:" + strconv.Itoa(int(dataJsonContent.Code)) + "msg:" + dataJsonContent.Msg)
  379. // }
  380. // return nil, nil
  381. //}