base_from_dl.go 7.9 KB

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