base_from_zz.go 7.8 KB

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