base_from_manual.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/models/mgo"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  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. type ManualEdbdata struct {
  21. TradeCode string `gorm:"column:TRADE_CODE;primaryKey" description:"指标编码"`
  22. Dt string `gorm:"column:DT" description:"日期"`
  23. Close string `gorm:"column:CLOSE" description:"值"`
  24. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  25. }
  26. func (m *ManualEdbdata) TableName() string {
  27. return "edb_data_manual"
  28. }
  29. func GetEdbdataManualByCondition(condition string, pars []interface{}) (item []*ManualEdbdata, err error) {
  30. sql := ` SELECT * FROM edbdata WHERE 1=1 `
  31. if condition != "" {
  32. sql += condition
  33. }
  34. sql += ` ORDER BY DT DESC `
  35. //o := orm.NewOrmUsingDB("edb")
  36. //_, err = o.Raw(sql, pars).QueryRows(&item)
  37. err = global.DmSQL["edb"].Raw(sql, pars...).Find(&item).Error
  38. return
  39. }
  40. // AddEdbDataFromManual 新增弘则手工指标数据
  41. func AddEdbDataFromManual(edbCode string) (err error) {
  42. //o := orm.NewOrm()
  43. var condition string
  44. var pars []interface{}
  45. if edbCode != "" {
  46. condition += " AND TRADE_CODE=? "
  47. pars = append(pars, edbCode)
  48. }
  49. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  50. if err != nil {
  51. return
  52. }
  53. dataLen := len(manualDataList)
  54. if dataLen > 0 {
  55. var isAdd bool
  56. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  57. for i := 0; i < dataLen; i++ {
  58. item := manualDataList[i]
  59. eDate := item.Dt
  60. sValue := item.Close
  61. tmpDecimal, err := decimal.NewFromString(sValue)
  62. if err != nil {
  63. return err
  64. }
  65. sValue = tmpDecimal.Round(4).String()
  66. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  67. if err != nil {
  68. return err
  69. }
  70. timestamp := dataTime.UnixNano() / 1e6
  71. timeStr := fmt.Sprintf("%d", timestamp)
  72. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  73. isAdd = true
  74. }
  75. if isAdd {
  76. addSql = strings.TrimRight(addSql, ",")
  77. //_, err = o.Raw(addSql).Exec()
  78. err = global.DEFAULT_DmSQL.Exec(addSql).Error
  79. if err != nil {
  80. return
  81. }
  82. }
  83. }
  84. return
  85. }
  86. // RefreshEdbDataFromManual 刷新手工指标数据
  87. func RefreshEdbDataFromManual(edbInfoId int, edbCode, startDate string) (err error) {
  88. source := utils.DATA_SOURCE_MANUAL
  89. subSource := utils.DATA_SUB_SOURCE_EDB
  90. //o := orm.NewOrm()
  91. if err != nil {
  92. return
  93. }
  94. edbInfoIdStr := strconv.Itoa(edbInfoId)
  95. //计算数据
  96. var condition string
  97. var pars []interface{}
  98. if edbCode != "" {
  99. condition += " AND TRADE_CODE=? "
  100. pars = append(pars, edbCode)
  101. }
  102. if startDate != "" {
  103. condition += " AND DT>=? "
  104. pars = append(pars, startDate)
  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 && err.Error() != utils.ErrNoRow() {
  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. 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_DmSQL.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. //o := orm.NewOrm()
  240. edbDataInsertConfig.RealDate = realDataMaxDate
  241. //_, err = o.Update(edbDataInsertConfig, "RealDate")
  242. err = global.DEFAULT_DmSQL.Model(&EdbDataInsertConfig{}).Where("edb_info_id=?", edbInfoId).Update("RealDate", realDataMaxDate).Error
  243. }
  244. return
  245. }
  246. // HandleConfigInsertEdbDataByMongo
  247. // @Description: 处理人工数据补充的配置(mongo)
  248. // @author: Roc
  249. // @datetime 2024-05-09 13:07:12
  250. // @param realDataMaxDate time.Time
  251. // @param edbDataInsertConfig *EdbDataInsertConfig
  252. // @param edbInfoId int
  253. // @param source int
  254. // @param subSource int
  255. // @param existMap map[string]*mgo.EdbDataBusiness
  256. // @param isFindConfigDateRealData bool
  257. func HandleConfigInsertEdbDataByMongo(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source, subSource int, existMap map[string]*mgo.EdbDataBusiness, isFindConfigDateRealData bool) {
  258. if edbDataInsertConfig == nil {
  259. return
  260. }
  261. var err error
  262. defer func() {
  263. if err != nil {
  264. fmt.Println("处理手工数据补充的配置失败,err:", err)
  265. }
  266. }()
  267. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  268. // 如果存在真实数据的最大日期 && 存在配置插入数据的最大日期 && 真实数据的最大日期 晚于/等于 配置插入数据的最大日期
  269. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  270. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  271. mogDataObj := mgo.EdbDataBusiness{}
  272. coll := mogDataObj.GetCollection()
  273. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  274. // 如果没有找到找到配置日期的实际数据,那么就直接删除
  275. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  276. mogDataObj.RemoveManyByColl(coll, bson.M{"_id": item.ID})
  277. }
  278. } else {
  279. //o := orm.NewOrm()
  280. edbDataInsertConfig.RealDate = realDataMaxDate
  281. //_, err = o.Update(edbDataInsertConfig, "RealDate")
  282. err = global.DEFAULT_DmSQL.Model(&EdbDataInsertConfig{}).Where("edb_info_id=?", edbInfoId).Update("RealDate", realDataMaxDate).Error
  283. }
  284. return
  285. }