base_from_dl.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package models
  2. import (
  3. "eta/eta_index_lib/global"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type BaseFromTradeDalianIndex struct {
  12. //BaseFromTradeDalianIndexId int `orm:"column(base_from_trade_dalian_index_id);pk"`
  13. BaseFromTradeDalianIndexId int `gorm:"column:base_from_trade_dalian_index_id;primaryKey"`
  14. Rank int
  15. DealShortName string
  16. DealName string
  17. DealCode string
  18. DealValue string
  19. DealChange int
  20. BuyShortName string
  21. BuyName string
  22. BuyCode string
  23. BuyValue string
  24. BuyChange int
  25. SoldShortName string
  26. SoldName string
  27. SoldCode string
  28. SoldValue string
  29. SoldChange int
  30. Frequency string
  31. ClassifyName string
  32. ClassifyType string
  33. CreateTime time.Time
  34. ModifyTime time.Time
  35. DataTime string
  36. }
  37. func GetBaseFromDalianDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeDalianIndex, err error) {
  38. //o := orm.NewOrm()
  39. sql := `SELECT * FROM base_from_trade_dalian_index WHERE %s_code=? `
  40. sql = fmt.Sprintf(sql, suffix)
  41. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  42. err = global.DEFAULT_DB.Raw(sql, indexCode).Find(&list).Error
  43. return
  44. }
  45. type BaseFromDlDataSimple struct {
  46. Id int `gorm:"column:base_from_trade_dalian_index_id;primaryKey"`
  47. //Id int `orm:"column(base_from_trade_dalian_index_id);pk"`
  48. DealCode string
  49. BuyCode string
  50. SoldCode string
  51. DataTime string
  52. DealValue string
  53. BuyValue string
  54. SoldValue string
  55. }
  56. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  57. func (m *BaseFromDlDataSimple) AfterFind(db *gorm.DB) (err error) {
  58. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  59. return
  60. }
  61. func GetDlDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromDlDataSimple, err error) {
  62. sql := ` SELECT * FROM base_from_trade_dalian_index WHERE 1=1 `
  63. //o := orm.NewOrm()
  64. if condition != "" {
  65. sql += condition
  66. }
  67. sql += ` ORDER BY data_time DESC `
  68. //_, err = o.Raw(sql, pars).QueryRows(&item)
  69. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&item).Error
  70. return
  71. }
  72. // 新增郑商所指标数据
  73. func AddEdbDataFromDl(edbCode string) (err error) {
  74. var suffix string
  75. if strings.Contains(edbCode, "deal") {
  76. suffix = "deal"
  77. } else if strings.Contains(edbCode, "buy") {
  78. suffix = "buy"
  79. } else if strings.Contains(edbCode, "sold") {
  80. suffix = "sold"
  81. }
  82. //o := orm.NewOrm()
  83. dlBaseDataAll, err := GetBaseFromDalianDataAllByIndexCode(edbCode, suffix)
  84. if err != nil && !utils.IsErrNoRow(err) {
  85. return
  86. }
  87. var isAdd bool
  88. addSql := ` INSERT INTO edb_data_dl(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  89. addSql = utils.ReplaceDriverKeywords("", addSql)
  90. existMap := make(map[string]string)
  91. for _, sv := range dlBaseDataAll {
  92. eDate := sv.DataTime
  93. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  94. if err != nil {
  95. fmt.Println("time.Parse Err:" + eDate)
  96. return err
  97. }
  98. timestamp := dataTime.UnixNano() / 1e6
  99. timeStr := fmt.Sprintf("%d", timestamp)
  100. if _, ok := existMap[eDate]; !ok {
  101. if suffix == "deal" {
  102. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.DealValue)
  103. } else if suffix == "buy" {
  104. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.BuyValue)
  105. } else {
  106. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.SoldValue)
  107. }
  108. isAdd = true
  109. }
  110. if suffix == "deal" {
  111. existMap[eDate] = sv.DealValue
  112. } else if suffix == "buy" {
  113. existMap[eDate] = sv.BuyValue
  114. } else {
  115. existMap[eDate] = sv.SoldValue
  116. }
  117. }
  118. if isAdd {
  119. addSql = strings.TrimRight(addSql, ",")
  120. utils.FileLog.Info("addSql:" + addSql)
  121. //_, err = o.Raw(addSql).Exec()
  122. err = global.DEFAULT_DB.Exec(addSql).Error
  123. if err != nil {
  124. return err
  125. }
  126. }
  127. return
  128. }
  129. // 刷新大商所指标数据
  130. func RefreshEdbDataFromDl(edbInfoId int, edbCode, startDate string) (err error) {
  131. source := utils.DATA_SOURCE_DL
  132. subSource := utils.DATA_SUB_SOURCE_EDB
  133. //o := orm.NewOrm()
  134. if err != nil {
  135. return
  136. }
  137. var suffix string
  138. if strings.Contains(edbCode, "deal") {
  139. suffix = "deal"
  140. } else if strings.Contains(edbCode, "buy") {
  141. suffix = "buy"
  142. } else if strings.Contains(edbCode, "sold") {
  143. suffix = "sold"
  144. }
  145. edbInfoIdStr := strconv.Itoa(edbInfoId)
  146. //计算数据
  147. var condition string
  148. var pars []interface{}
  149. if edbCode != "" {
  150. if suffix == "deal" {
  151. condition += " AND deal_code=? "
  152. } else if suffix == "buy" {
  153. condition += " AND buy_code=? "
  154. } else {
  155. condition += " AND sold_code=? "
  156. }
  157. pars = append(pars, edbCode)
  158. }
  159. if startDate != "" {
  160. condition += " AND data_time>=? "
  161. pars = append(pars, startDate)
  162. }
  163. glDataList, err := GetDlDataByTradeCode(condition, pars)
  164. if err != nil {
  165. return
  166. }
  167. // 真实数据的最大日期 , 插入规则配置的日期
  168. var realDataMaxDate, edbDataInsertConfigDate time.Time
  169. var edbDataInsertConfig *EdbDataInsertConfig
  170. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  171. {
  172. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  173. if err != nil && !utils.IsErrNoRow(err) {
  174. return
  175. }
  176. if edbDataInsertConfig != nil {
  177. edbDataInsertConfigDate = edbDataInsertConfig.Date
  178. }
  179. }
  180. //获取指标所有数据
  181. var existCondition string
  182. var existPars []interface{}
  183. existCondition += " AND edb_info_id=? "
  184. existPars = append(existPars, edbInfoId)
  185. if startDate != "" {
  186. existCondition += " AND data_time>=? "
  187. existPars = append(existPars, startDate)
  188. }
  189. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  190. if err != nil {
  191. return err
  192. }
  193. existMap := make(map[string]*EdbInfoSearchData)
  194. for _, v := range existList {
  195. existMap[v.DataTime] = v
  196. }
  197. addSql := ` INSERT INTO edb_data_dl(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  198. addSql = utils.ReplaceDriverKeywords("", addSql)
  199. var isAdd bool
  200. for _, v := range glDataList {
  201. var value string
  202. if suffix == "deal" {
  203. value = v.DealValue
  204. } else if suffix == "buy" {
  205. value = v.BuyValue
  206. } else {
  207. value = v.SoldValue
  208. }
  209. item := v
  210. itemValue := value
  211. eDate := item.DataTime
  212. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  213. if err != nil {
  214. return err
  215. }
  216. if _, ok := existMap[v.DataTime]; !ok {
  217. sValue := itemValue
  218. if sValue != "" {
  219. timestamp := dataTime.UnixNano() / 1e6
  220. timeStr := fmt.Sprintf("%d", timestamp)
  221. saveValue := sValue
  222. if findItem, ok := existMap[eDate]; !ok {
  223. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  224. isAdd = true
  225. } else {
  226. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  227. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  228. if err != nil {
  229. return err
  230. }
  231. }
  232. }
  233. }
  234. }
  235. // 下面代码主要目的是处理掉手动插入的数据判断
  236. {
  237. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  238. realDataMaxDate = dataTime
  239. }
  240. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  241. isFindConfigDateRealData = true
  242. }
  243. }
  244. }
  245. // 处理手工数据补充的配置
  246. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  247. if isAdd {
  248. addSql = strings.TrimRight(addSql, ",")
  249. //_, err = o.Raw(addSql).Exec()
  250. err = global.DEFAULT_DB.Exec(addSql).Error
  251. if err != nil {
  252. return err
  253. }
  254. }
  255. return
  256. }