base_from_manual.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. func AddEdbDataFromManual(edbCode string) (err error) {
  32. var condition string
  33. var pars []interface{}
  34. if edbCode != "" {
  35. condition += " AND TRADE_CODE=? "
  36. pars = append(pars, edbCode)
  37. }
  38. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  39. if err != nil {
  40. return
  41. }
  42. dataLen := len(manualDataList)
  43. if dataLen > 0 {
  44. var isAdd bool
  45. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  46. for i := 0; i < dataLen; i++ {
  47. item := manualDataList[i]
  48. eDate := item.Dt
  49. sValue := item.Close
  50. tmpDecimal, err := decimal.NewFromString(sValue)
  51. if err != nil {
  52. return err
  53. }
  54. sValue = tmpDecimal.Round(4).String()
  55. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  56. if err != nil {
  57. return err
  58. }
  59. timestamp := dataTime.UnixNano() / 1e6
  60. timeStr := fmt.Sprintf("%d", timestamp)
  61. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  62. isAdd = true
  63. }
  64. if isAdd {
  65. addSql = strings.TrimRight(addSql, ",")
  66. err = global.DEFAULT_DmSQL.Exec(addSql).Error
  67. if err != nil {
  68. return
  69. }
  70. }
  71. }
  72. return
  73. }
  74. func RefreshEdbDataFromManual(edbInfoId int, edbCode, startDate string) (err error) {
  75. source := utils.DATA_SOURCE_MANUAL
  76. subSource := utils.DATA_SUB_SOURCE_EDB
  77. if err != nil {
  78. return
  79. }
  80. edbInfoIdStr := strconv.Itoa(edbInfoId)
  81. var condition string
  82. var pars []interface{}
  83. if edbCode != "" {
  84. condition += " AND TRADE_CODE=? "
  85. pars = append(pars, edbCode)
  86. }
  87. if startDate != "" {
  88. condition += " AND DT>=? "
  89. pars = append(pars, startDate)
  90. } else {
  91. condition += " AND DT != ? "
  92. pars = append(pars, `0000-00-00`)
  93. }
  94. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  95. if err != nil {
  96. return
  97. }
  98. var realDataMaxDate, edbDataInsertConfigDate time.Time
  99. var edbDataInsertConfig *EdbDataInsertConfig
  100. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  101. edbDataInsertConfigDateStr := ``
  102. {
  103. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  104. if err != nil && err.Error() != utils.ErrNoRow() {
  105. return
  106. }
  107. if edbDataInsertConfig != nil {
  108. edbDataInsertConfigDate = edbDataInsertConfig.Date
  109. edbDataInsertConfigDateStr = edbDataInsertConfig.Date.Format(utils.FormatDate)
  110. }
  111. }
  112. var existCondition string
  113. var existPars []interface{}
  114. existCondition += " AND edb_info_id=? "
  115. existPars = append(existPars, edbInfoId)
  116. if startDate != "" {
  117. existCondition += " AND data_time>=? "
  118. existPars = append(existPars, startDate)
  119. }
  120. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  121. if err != nil {
  122. return err
  123. }
  124. existMap := make(map[string]*EdbInfoSearchData)
  125. for _, v := range existList {
  126. existMap[v.DataTime] = v
  127. }
  128. addSql := ` INSERT INTO edb_data_manual (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  129. var isAdd bool
  130. manualMap := make(map[string]*ManualEdbdata)
  131. for _, v := range manualDataList {
  132. item := v
  133. eDate := item.Dt
  134. sValue := item.Close
  135. tmpDecimal, err := decimal.NewFromString(sValue)
  136. if err != nil {
  137. return err
  138. }
  139. sValue = tmpDecimal.Round(4).String()
  140. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  141. if err != nil {
  142. return err
  143. }
  144. if findItem, ok := existMap[v.Dt]; !ok {
  145. timestamp := dataTime.UnixNano() / 1e6
  146. timeStr := fmt.Sprintf("%d", timestamp)
  147. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  148. isAdd = true
  149. } else {
  150. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Close {
  151. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Close)
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. }
  157. manualMap[v.Dt] = v
  158. {
  159. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  160. realDataMaxDate = dataTime
  161. }
  162. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  163. isFindConfigDateRealData = true
  164. }
  165. }
  166. }
  167. for _, v := range existList {
  168. if _, ok := manualMap[v.DataTime]; !ok {
  169. if edbDataInsertConfigDateStr == `` || edbDataInsertConfigDateStr != v.DataTime {
  170. DeleteEdbDataById(utils.DATA_SOURCE_MANUAL, subSource, v.EdbDataId)
  171. }
  172. }
  173. }
  174. if isAdd {
  175. addSql = strings.TrimRight(addSql, ",")
  176. err = global.DEFAULT_DmSQL.Exec(addSql).Error
  177. if err != nil {
  178. fmt.Println("RefreshAllEdbDataByManual add Err", err.Error())
  179. return
  180. }
  181. }
  182. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  183. return
  184. }
  185. func HandleConfigInsertEdbData(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source, subSource int, existMap map[string]*EdbInfoSearchData, isFindConfigDateRealData bool) {
  186. if edbDataInsertConfig == nil {
  187. return
  188. }
  189. var err error
  190. defer func() {
  191. if err != nil {
  192. fmt.Println("处理手工数据补充的配置失败,err:", err)
  193. }
  194. }()
  195. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  196. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  197. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  198. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  199. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  200. DeleteEdbDataById(source, subSource, item.EdbDataId)
  201. }
  202. } else {
  203. edbDataInsertConfig.RealDate = realDataMaxDate
  204. err = global.DEFAULT_DmSQL.Model(&EdbDataInsertConfig{}).Where("edb_info_id=?", edbInfoId).Update("RealDate", realDataMaxDate).Error
  205. }
  206. return
  207. }
  208. func HandleConfigInsertEdbDataByMongo(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source, subSource int, existMap map[string]*mgo.EdbDataBusiness, isFindConfigDateRealData bool) {
  209. if edbDataInsertConfig == nil {
  210. return
  211. }
  212. var err error
  213. defer func() {
  214. if err != nil {
  215. fmt.Println("处理手工数据补充的配置失败,err:", err)
  216. }
  217. }()
  218. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  219. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  220. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  221. mogDataObj := mgo.EdbDataBusiness{}
  222. coll := mogDataObj.GetCollection()
  223. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  224. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  225. mogDataObj.RemoveManyByColl(coll, bson.M{"_id": item.ID})
  226. }
  227. } else {
  228. edbDataInsertConfig.RealDate = realDataMaxDate
  229. err = global.DEFAULT_DmSQL.Model(&EdbDataInsertConfig{}).Where("edb_info_id=?", edbInfoId).Update("RealDate", realDataMaxDate).Error
  230. }
  231. return
  232. }