base_from_dl.go 7.3 KB

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