base_from_manual.go 10 KB

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