base_from_manual.go 9.7 KB

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