base_from_manual.go 9.4 KB

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