base_from_gz.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type BaseFromTradeGuangzhouIndex struct {
  11. BaseFromTradeGuangzhouIndexId int `orm:"column(base_from_trade_guangzhou_index_id);pk"`
  12. BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
  13. IndexCode string `description:"指标编码"`
  14. IndexName string `description:"指标名称"`
  15. Frequency string `description:"频率"`
  16. Unit string `description:"单位"`
  17. StartDate string `description:"开始日期"`
  18. EndDate string `description:"结束日期"`
  19. CreateTime time.Time `description:"创建日期"`
  20. ModifyTime time.Time `description:"修改日期"`
  21. }
  22. type BaseFromTradeGuangzhouData struct {
  23. BaseFromTradeGuangzhouDataId int `orm:"column(base_from_trade_guangzhou_data_id);pk"`
  24. BaseFromTradeGuangzhouIndexId int `description:"指标id"`
  25. IndexCode string `description:"指标编码"`
  26. DataTime string `description:"数据日期"`
  27. Value string `description:"数据值"`
  28. QtySub float64 `description:"增减"`
  29. CreateTime time.Time `description:"创建日期"`
  30. ModifyTime time.Time `description:"修改日期"`
  31. }
  32. func GetBaseFromGuangzhouDataByIndexCode(indexCode string) (items []*BaseFromTradeGuangzhouData, err error) {
  33. o := orm.NewOrm()
  34. sql := `SELECT * FROM base_from_trade_guangzhou_data WHERE index_code=? `
  35. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  36. return
  37. }
  38. func GetBaseFromGuangzhouDataDataByCondition(condition string, pars []interface{}) (item []*BaseFromTradeGuangzhouData, err error) {
  39. sql := ` SELECT * FROM base_from_trade_guangzhou_data WHERE 1=1 `
  40. o := orm.NewOrm()
  41. if condition != "" {
  42. sql += condition
  43. }
  44. sql += ` ORDER BY data_time DESC `
  45. _, err = o.Raw(sql, pars).QueryRows(&item)
  46. return
  47. }
  48. // 新增广期所指标数据
  49. func AddEdbDataFromGz(edbCode string) (err error) {
  50. o := orm.NewOrm()
  51. dataAll, err := GetBaseFromGuangzhouDataByIndexCode(edbCode)
  52. if err != nil && err.Error() != utils.ErrNoRow() {
  53. return
  54. }
  55. var isAdd bool
  56. addSql := ` INSERT INTO edb_data_gz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  57. existMap := make(map[string]string)
  58. for _, sv := range dataAll {
  59. eDate := sv.DataTime
  60. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  61. if err != nil {
  62. fmt.Println("time.Parse Err:" + eDate)
  63. return err
  64. }
  65. timestamp := dataTime.UnixNano() / 1e6
  66. timeStr := fmt.Sprintf("%d", timestamp)
  67. if _, ok := existMap[eDate]; !ok {
  68. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  69. isAdd = true
  70. }
  71. }
  72. if isAdd {
  73. addSql = strings.TrimRight(addSql, ",")
  74. utils.FileLog.Info("addSql:" + addSql)
  75. _, err = o.Raw(addSql).Exec()
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return
  81. }
  82. // 刷新广期所指标数据
  83. func RefreshEdbDataFromGz(edbInfoId int, edbCode, startDate string) (err error) {
  84. source := utils.DATA_SOURCE_GFEX
  85. subSource := utils.DATA_SUB_SOURCE_EDB
  86. o := orm.NewOrm()
  87. if err != nil {
  88. return
  89. }
  90. edbInfoIdStr := strconv.Itoa(edbInfoId)
  91. //计算数据
  92. var condition string
  93. var pars []interface{}
  94. if edbCode != "" {
  95. condition += " AND index_code=? "
  96. pars = append(pars, edbCode)
  97. }
  98. if startDate != "" {
  99. condition += " AND data_time>=? "
  100. pars = append(pars, startDate)
  101. }
  102. dataList, err := GetBaseFromGuangzhouDataDataByCondition(condition, pars)
  103. if err != nil {
  104. return
  105. }
  106. // 真实数据的最大日期 , 插入规则配置的日期
  107. var realDataMaxDate, edbDataInsertConfigDate time.Time
  108. var edbDataInsertConfig *EdbDataInsertConfig
  109. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  110. {
  111. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  112. if err != nil && err.Error() != utils.ErrNoRow() {
  113. return
  114. }
  115. if edbDataInsertConfig != nil {
  116. edbDataInsertConfigDate = edbDataInsertConfig.Date
  117. }
  118. }
  119. //获取指标所有数据
  120. var existCondition string
  121. var existPars []interface{}
  122. existCondition += " AND edb_info_id=? "
  123. existPars = append(existPars, edbInfoId)
  124. if startDate != "" {
  125. existCondition += " AND data_time>=? "
  126. existPars = append(existPars, startDate)
  127. }
  128. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  129. if err != nil {
  130. return err
  131. }
  132. existMap := make(map[string]*EdbInfoSearchData)
  133. for _, v := range existList {
  134. existMap[v.DataTime] = v
  135. }
  136. addSql := ` INSERT INTO edb_data_gz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  137. var isAdd bool
  138. for _, v := range dataList {
  139. item := v
  140. itemValue := v.Value
  141. eDate := item.DataTime
  142. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  143. if err != nil {
  144. return err
  145. }
  146. if _, ok := existMap[v.DataTime]; !ok {
  147. sValue := itemValue
  148. if sValue != "" {
  149. timestamp := dataTime.UnixNano() / 1e6
  150. timeStr := fmt.Sprintf("%d", timestamp)
  151. saveValue := sValue
  152. if findItem, ok := existMap[eDate]; !ok {
  153. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  154. isAdd = true
  155. } else {
  156. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  157. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. }
  163. }
  164. }
  165. // 下面代码主要目的是处理掉手动插入的数据判断
  166. {
  167. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  168. realDataMaxDate = dataTime
  169. }
  170. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  171. isFindConfigDateRealData = true
  172. }
  173. }
  174. }
  175. // 处理手工数据补充的配置
  176. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  177. if isAdd {
  178. addSql = strings.TrimRight(addSql, ",")
  179. _, err = o.Raw(addSql).Exec()
  180. if err != nil {
  181. return err
  182. }
  183. }
  184. return
  185. }
  186. //
  187. //type RefreshSHExchangeReq struct {
  188. // Url string `description:"交易所链接"`
  189. // Exchange string `description:"交易所"`
  190. // Date string `description:"日期"`
  191. // Data SHMessage
  192. //}
  193. //
  194. //type Position []struct {
  195. // ContractCode string `json:"INSTRUMENTID"`
  196. // ProductSortNo int `json:"PRODUCTSORTNO"`
  197. // Rank int `json:"RANK"`
  198. // ParticipantID1 string `json:"PARTICIPANTID1"`
  199. // ParticipantName1 string `json:"PARTICIPANTABBR1"`
  200. // Deal interface{} `json:"CJ1"`
  201. // Change1 interface{} `json:"CJ1_CHG"`
  202. // ParticipantID2 string `json:"PARTICIPANTID2"`
  203. // ParticipantName2 string `json:"PARTICIPANTABBR2"`
  204. // BuyIn interface{} `json:"CJ2"`
  205. // Change2 interface{} `json:"CJ2_CHG"`
  206. // ParticipantID3 string `json:"PARTICIPANTID3"`
  207. // ParticipantName3 string `json:"PARTICIPANTABBR3"`
  208. // SoldOut interface{} `json:"CJ3"`
  209. // Change3 interface{} `json:"CJ3_CHG"`
  210. // ProductName string `json:"PRODUCTNAME"`
  211. //}
  212. //
  213. //type SHMessage struct {
  214. // Position Position `json:"o_cursor"`
  215. // Length string `json:"showlength"`
  216. // Code int `json:"o_code"`
  217. // Msg string `json:"o_msg"`
  218. // ReportDate string `json:"report_date"`
  219. // UpdateDate string `json:"update_date"`
  220. // PrintDate string `json:"print_date"`
  221. //}
  222. //
  223. //type BaseFromTradeShanghaiIndex struct {
  224. // BaseFromTradeShangHaiIndexId int `orm:"column(base_from_trade_shanghai_index_id);pk"`
  225. // Rank int
  226. // DealShortName string
  227. // DealName string
  228. // DealCode string
  229. // DealValue int
  230. // DealChange int
  231. // BuyShortName string
  232. // BuyName string
  233. // BuyCode string
  234. // BuyValue int
  235. // BuyChange int
  236. // SoldShortName string
  237. // SoldName string
  238. // SoldCode string
  239. // SoldValue int
  240. // SoldChange int
  241. // Frequency string
  242. // ClassifyName string
  243. // ClassifyType string
  244. // CreateTime time.Time
  245. // ModifyTime time.Time
  246. // DataTime string
  247. //}
  248. //
  249. //func AddBaseFromTradeShangHaiIndex(item *BaseFromTradeShanghaiIndex) (lastId int64, err error) {
  250. // o := orm.NewOrm()
  251. // lastId, err = o.Insert(item)
  252. // return
  253. //}
  254. //
  255. //func GetBaseFromTradeShangHaiIndexAll(dateStr string) (list []*BaseFromTradeShanghaiIndex, err error) {
  256. // o := orm.NewOrm()
  257. // sql := `SELECT * FROM base_from_trade_shanghai_index WHERE data_time=?`
  258. // _, err = o.Raw(sql, dateStr).QueryRows(&list)
  259. // return
  260. //}
  261. //
  262. //func ModifyBaseFromTradeShangHaiIndex(dealValue, buyValue, soldValue int, dataId int) (err error) {
  263. // o := orm.NewOrm()
  264. // sql := `UPDATE base_from_trade_shanghai_index SET deal_value=?,buy_value=?,sold_value=?,modify_time=NOW() WHERE base_from_trade_shanghai_index_id=? `
  265. // _, err = o.Raw(sql, dealValue, buyValue, soldValue, dataId).Exec()
  266. // return
  267. //}