base_from_zz.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 BaseFromTradeZhengzhouIndex struct {
  11. BaseFromTradeZhengzhouIndexId int `gorm:"column:base_from_trade_zhengzhou_index_id;primaryKey"`
  12. //BaseFromTradeZhengzhouIndexId int `orm:"column(base_from_trade_zhengzhou_index_id);pk"`
  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. type BaseFromZzDataSimple struct {
  37. Id int `gorm:"column:base_from_trade_zhengzhou_index_id;primaryKey"`
  38. //Id int `orm:"column(base_from_trade_zhengzhou_index_id);pk"`
  39. DealCode string
  40. BuyCode string
  41. SoldCode string
  42. DataTime string
  43. DealValue string
  44. BuyValue string
  45. SoldValue string
  46. }
  47. func GetBaseFromZhengzhouDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeZhengzhouIndex, err error) {
  48. //o := orm.NewOrm()
  49. sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE %s_code=? `
  50. sql = fmt.Sprintf(sql, suffix)
  51. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  52. err = global.DEFAULT_DB.Raw(sql, indexCode).Find(&list).Error
  53. return
  54. }
  55. func GetZzDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromZzDataSimple, err error) {
  56. sql := ` SELECT * FROM base_from_trade_zhengzhou_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 AddEdbDataFromZz(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. zzBaseDataAll, err := GetBaseFromZhengzhouDataAllByIndexCode(edbCode, suffix)
  78. if err != nil && !utils.IsErrNoRow(err) {
  79. return
  80. }
  81. var isAdd bool
  82. addSql := ` INSERT INTO edb_data_zz(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 zzBaseDataAll {
  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 RefreshEdbDataFromZz(edbInfoId int, edbCode, startDate string) (err error) {
  124. source := utils.DATA_SOURCE_ZZ
  125. subSource := utils.DATA_SUB_SOURCE_DATE
  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 := GetZzDataByTradeCode(condition, pars)
  157. if err != nil {
  158. return err
  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_zz(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. if _, ok := existMap[v.DataTime]; !ok {
  204. eDate := item.DataTime
  205. sValue := itemValue
  206. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  207. if err != nil {
  208. return err
  209. }
  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. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  229. realDataMaxDate = dataTime
  230. }
  231. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  232. isFindConfigDateRealData = true
  233. }
  234. }
  235. }
  236. }
  237. // 处理手工数据补充的配置
  238. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  239. if isAdd {
  240. addSql = strings.TrimRight(addSql, ",")
  241. err = global.DEFAULT_DB.Exec(addSql).Error
  242. //_, err = o.Raw(addSql).Exec()
  243. if err != nil {
  244. return err
  245. }
  246. }
  247. return
  248. }