base_from_manual.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 `orm:"column:CLOSE" description:"值"`
  20. ModifyTime time.Time `orm:"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. } else {
  105. condition += " AND DT != ? "
  106. pars = append(pars, `0000-00-00`)
  107. }
  108. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  109. if err != nil {
  110. return
  111. }
  112. // 真实数据的最大日期 , 插入规则配置的日期
  113. var realDataMaxDate, edbDataInsertConfigDate time.Time
  114. var edbDataInsertConfig *EdbDataInsertConfig
  115. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  116. edbDataInsertConfigDateStr := ``
  117. {
  118. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  119. if err != nil && !utils.IsErrNoRow(err) {
  120. return
  121. }
  122. if edbDataInsertConfig != nil {
  123. edbDataInsertConfigDate = edbDataInsertConfig.Date
  124. edbDataInsertConfigDateStr = edbDataInsertConfig.Date.Format(utils.FormatDate)
  125. }
  126. }
  127. var existCondition string
  128. var existPars []interface{}
  129. existCondition += " AND edb_info_id=? "
  130. existPars = append(existPars, edbInfoId)
  131. if startDate != "" {
  132. existCondition += " AND data_time>=? "
  133. existPars = append(existPars, startDate)
  134. }
  135. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  136. if err != nil {
  137. return err
  138. }
  139. existMap := make(map[string]*EdbInfoSearchData)
  140. for _, v := range existList {
  141. existMap[v.DataTime] = v
  142. }
  143. addSql := ` INSERT INTO edb_data_manual (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  144. addSql = utils.ReplaceDriverKeywords("", addSql)
  145. var isAdd bool
  146. manualMap := make(map[string]*ManualEdbdata)
  147. //fmt.Println("manualDataList:", len(manualDataList))
  148. for _, v := range manualDataList {
  149. item := v
  150. eDate := item.Dt
  151. sValue := item.Close
  152. tmpDecimal, err := decimal.NewFromString(sValue)
  153. if err != nil {
  154. return err
  155. }
  156. sValue = tmpDecimal.Round(4).String()
  157. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  158. if err != nil {
  159. return err
  160. }
  161. //fmt.Println("Item:", item.Dt, item.Close, item.TradeCode, item.ModifyTime)
  162. if findItem, ok := existMap[v.Dt]; !ok {
  163. timestamp := dataTime.UnixNano() / 1e6
  164. timeStr := fmt.Sprintf("%d", timestamp)
  165. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  166. isAdd = true
  167. } else {
  168. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Close {
  169. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Close)
  170. if err != nil {
  171. return err
  172. }
  173. }
  174. }
  175. manualMap[v.Dt] = v
  176. // 下面代码主要目的是处理掉手动插入的数据判断
  177. {
  178. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  179. realDataMaxDate = dataTime
  180. }
  181. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  182. isFindConfigDateRealData = true
  183. }
  184. }
  185. }
  186. for _, v := range existList {
  187. if _, ok := manualMap[v.DataTime]; !ok {
  188. // 正常来讲,如果来源数据移除了该日期的数据,也需要删除ETA指标的值
  189. // 但是由于引入手动插入最新值,那么需要判断该日期值,是否等于,如果等于,则不删除该日期值
  190. if edbDataInsertConfigDateStr == `` || edbDataInsertConfigDateStr != v.DataTime {
  191. DeleteEdbDataById(utils.DATA_SOURCE_MANUAL, subSource, v.EdbDataId)
  192. }
  193. }
  194. }
  195. if isAdd {
  196. addSql = strings.TrimRight(addSql, ",")
  197. //_, err = o.Raw(addSql).Exec()
  198. err = global.DEFAULT_DB.Exec(addSql).Error
  199. if err != nil {
  200. fmt.Println("RefreshAllEdbDataByManual add Err", err.Error())
  201. return
  202. }
  203. }
  204. // 处理手工数据补充的配置
  205. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  206. return
  207. }
  208. // HandleConfigInsertEdbData
  209. // @Description: 处理人工数据补充的配置(mysql)
  210. // @author: Roc
  211. // @datetime 2024-05-09 13:07:32
  212. // @param realDataMaxDate time.Time
  213. // @param edbDataInsertConfig *EdbDataInsertConfig
  214. // @param edbInfoId int
  215. // @param source int
  216. // @param subSource int
  217. // @param existMap map[string]*EdbInfoSearchData
  218. // @param isFindConfigDateRealData bool
  219. func HandleConfigInsertEdbData(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source, subSource int, existMap map[string]*EdbInfoSearchData, isFindConfigDateRealData bool) {
  220. if edbDataInsertConfig == nil {
  221. return
  222. }
  223. var err error
  224. defer func() {
  225. if err != nil {
  226. fmt.Println("处理手工数据补充的配置失败,err:", err)
  227. }
  228. }()
  229. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  230. // 如果存在真实数据的最大日期 && 存在配置插入数据的最大日期 && 真实数据的最大日期 晚于/等于 配置插入数据的最大日期
  231. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  232. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  233. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  234. // 如果没有找到找到配置日期的实际数据,那么就直接删除
  235. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  236. DeleteEdbDataById(source, subSource, item.EdbDataId)
  237. }
  238. } else {
  239. edbDataInsertConfig.RealDate = realDataMaxDate
  240. err = global.DEFAULT_DB.Model(edbDataInsertConfig).Select([]string{"RealDate"}).Updates(edbDataInsertConfig).Error
  241. }
  242. return
  243. }
  244. // HandleConfigInsertEdbDataByMongo
  245. // @Description: 处理人工数据补充的配置(mongo)
  246. // @author: Roc
  247. // @datetime 2024-05-09 13:07:12
  248. // @param realDataMaxDate time.Time
  249. // @param edbDataInsertConfig *EdbDataInsertConfig
  250. // @param edbInfoId int
  251. // @param source int
  252. // @param subSource int
  253. // @param existMap map[string]*mgo.EdbDataBusiness
  254. // @param isFindConfigDateRealData bool
  255. func HandleConfigInsertEdbDataByMongo(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source, subSource int, existMap map[string]*mgo.EdbDataBusiness, isFindConfigDateRealData bool) {
  256. if edbDataInsertConfig == nil {
  257. return
  258. }
  259. var err error
  260. defer func() {
  261. if err != nil {
  262. fmt.Println("处理手工数据补充的配置失败,err:", err)
  263. }
  264. }()
  265. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  266. // 如果存在真实数据的最大日期 && 存在配置插入数据的最大日期 && 真实数据的最大日期 晚于/等于 配置插入数据的最大日期
  267. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  268. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  269. mogDataObj := mgo.EdbDataBusiness{}
  270. coll := mogDataObj.GetCollection()
  271. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  272. // 如果没有找到找到配置日期的实际数据,那么就直接删除
  273. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  274. mogDataObj.RemoveManyByColl(coll, bson.M{"_id": item.ID})
  275. }
  276. } else {
  277. //o := orm.NewOrm()
  278. edbDataInsertConfig.RealDate = realDataMaxDate
  279. //_, err = o.Update(edbDataInsertConfig, "RealDate")
  280. err = global.DEFAULT_DB.Model(&edbDataInsertConfig).Select("RealDate").Updates(&edbDataInsertConfig).Error
  281. }
  282. return
  283. }