base_from_manual.go 9.3 KB

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