base_from_manual.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //弘则手工数据
  12. type ManualEdbdata struct {
  13. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  14. Dt string `orm:"column(DT)" description:"日期"`
  15. Close string `orm:"column(CLOSE)" description:"值"`
  16. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  17. }
  18. func GetEdbdataManualByCondition(condition string, pars []interface{}) (item []*ManualEdbdata, err error) {
  19. sql := ` SELECT * FROM edbdata WHERE 1=1 `
  20. if condition != "" {
  21. sql += condition
  22. }
  23. sql += ` ORDER BY DT DESC `
  24. o := orm.NewOrmUsingDB("edb")
  25. _, err = o.Raw(sql, pars).QueryRows(&item)
  26. return
  27. }
  28. // AddEdbDataFromManual 新增弘则手工指标数据
  29. func AddEdbDataFromManual(edbCode string) (err error) {
  30. o := orm.NewOrm()
  31. var condition string
  32. var pars []interface{}
  33. if edbCode != "" {
  34. condition += " AND TRADE_CODE=? "
  35. pars = append(pars, edbCode)
  36. }
  37. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  38. if err != nil {
  39. return
  40. }
  41. dataLen := len(manualDataList)
  42. if dataLen > 0 {
  43. var isAdd bool
  44. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  45. for i := 0; i < dataLen; i++ {
  46. item := manualDataList[i]
  47. eDate := item.Dt
  48. sValue := item.Close
  49. tmpDecimal, err := decimal.NewFromString(sValue)
  50. if err != nil {
  51. return err
  52. }
  53. sValue = tmpDecimal.Round(4).String()
  54. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  55. if err != nil {
  56. return err
  57. }
  58. timestamp := dataTime.UnixNano() / 1e6
  59. timeStr := fmt.Sprintf("%d", timestamp)
  60. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  61. isAdd = true
  62. }
  63. if isAdd {
  64. addSql = strings.TrimRight(addSql, ",")
  65. _, err = o.Raw(addSql).Exec()
  66. if err != nil {
  67. return
  68. }
  69. }
  70. }
  71. return
  72. }
  73. // RefreshEdbDataFromManual 刷新手工指标数据
  74. func RefreshEdbDataFromManual(edbInfoId int, edbCode, startDate string) (err error) {
  75. source := utils.DATA_SOURCE_MANUAL
  76. o := orm.NewOrm()
  77. if err != nil {
  78. return
  79. }
  80. edbInfoIdStr := strconv.Itoa(edbInfoId)
  81. //计算数据
  82. var condition string
  83. var pars []interface{}
  84. if edbCode != "" {
  85. condition += " AND TRADE_CODE=? "
  86. pars = append(pars, edbCode)
  87. }
  88. if startDate != "" {
  89. condition += " AND DT>=? "
  90. pars = append(pars, startDate)
  91. } else {
  92. condition += " AND DT != ? "
  93. pars = append(pars, `0000-00-00`)
  94. }
  95. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  96. if err != nil {
  97. return
  98. }
  99. // 真实数据的最大日期 , 插入规则配置的日期
  100. var realDataMaxDate, edbDataInsertConfigDate time.Time
  101. var edbDataInsertConfig *EdbDataInsertConfig
  102. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  103. edbDataInsertConfigDateStr := ``
  104. {
  105. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  106. if err != nil && err.Error() != utils.ErrNoRow() {
  107. return
  108. }
  109. if edbDataInsertConfig != nil {
  110. edbDataInsertConfigDate = edbDataInsertConfig.Date
  111. edbDataInsertConfigDateStr = edbDataInsertConfig.Date.Format(utils.FormatDate)
  112. }
  113. }
  114. var existCondition string
  115. var existPars []interface{}
  116. existCondition += " AND edb_info_id=? "
  117. existPars = append(existPars, edbInfoId)
  118. if startDate != "" {
  119. existCondition += " AND data_time>=? "
  120. existPars = append(existPars, startDate)
  121. }
  122. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  123. if err != nil {
  124. return err
  125. }
  126. existMap := make(map[string]*EdbInfoSearchData)
  127. for _, v := range existList {
  128. existMap[v.DataTime] = v
  129. }
  130. addSql := ` INSERT INTO edb_data_manual (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  131. var isAdd bool
  132. manualMap := make(map[string]*ManualEdbdata)
  133. //fmt.Println("manualDataList:", len(manualDataList))
  134. for _, v := range manualDataList {
  135. item := v
  136. eDate := item.Dt
  137. sValue := item.Close
  138. tmpDecimal, err := decimal.NewFromString(sValue)
  139. if err != nil {
  140. return err
  141. }
  142. sValue = tmpDecimal.Round(4).String()
  143. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  144. if err != nil {
  145. return err
  146. }
  147. //fmt.Println("Item:", item.Dt, item.Close, item.TradeCode, item.ModifyTime)
  148. if findItem, ok := existMap[v.Dt]; !ok {
  149. timestamp := dataTime.UnixNano() / 1e6
  150. timeStr := fmt.Sprintf("%d", timestamp)
  151. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  152. isAdd = true
  153. } else {
  154. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Close {
  155. err = ModifyEdbDataById(source, findItem.EdbDataId, item.Close)
  156. if err != nil {
  157. return err
  158. }
  159. }
  160. }
  161. manualMap[v.Dt] = v
  162. // 下面代码主要目的是处理掉手动插入的数据判断
  163. {
  164. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  165. realDataMaxDate = dataTime
  166. }
  167. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  168. isFindConfigDateRealData = true
  169. }
  170. }
  171. }
  172. for _, v := range existList {
  173. if _, ok := manualMap[v.DataTime]; !ok {
  174. // 正常来讲,如果来源数据移除了该日期的数据,也需要删除ETA指标的值
  175. // 但是由于引入手动插入最新值,那么需要判断该日期值,是否等于,如果等于,则不删除该日期值
  176. if edbDataInsertConfigDateStr == `` || edbDataInsertConfigDateStr != v.DataTime {
  177. DeleteEdbDataById(utils.DATA_SOURCE_MANUAL, v.EdbDataId)
  178. }
  179. }
  180. }
  181. if isAdd {
  182. addSql = strings.TrimRight(addSql, ",")
  183. _, err = o.Raw(addSql).Exec()
  184. if err != nil {
  185. fmt.Println("RefreshAllEdbDataByManual add Err", err.Error())
  186. return
  187. }
  188. }
  189. // 处理手工数据补充的配置
  190. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  191. return
  192. }
  193. // HandleConfigInsertEdbData 处理手工数据补充的配置
  194. func HandleConfigInsertEdbData(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source int, existMap map[string]*EdbInfoSearchData, isFindConfigDateRealData bool) {
  195. if edbDataInsertConfig == nil {
  196. return
  197. }
  198. var err error
  199. defer func() {
  200. if err != nil {
  201. fmt.Println("处理手工数据补充的配置失败,err:", err)
  202. }
  203. }()
  204. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  205. // 如果存在真实数据的最大日期 && 存在配置插入数据的最大日期 && 真实数据的最大日期 晚于/等于 配置插入数据的最大日期
  206. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  207. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  208. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  209. // 如果没有找到找到配置日期的实际数据,那么就直接删除
  210. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  211. DeleteEdbDataById(source, item.EdbDataId)
  212. }
  213. } else {
  214. o := orm.NewOrm()
  215. edbDataInsertConfig.RealDate = realDataMaxDate
  216. _, err = o.Update(edbDataInsertConfig, "RealDate")
  217. }
  218. return
  219. }