base_from_ly.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // @Author gmy 2024/8/13 16:10:00
  2. package models
  3. import (
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type BaseFromLyData struct {
  12. BaseFromLyDataId int `orm:"column(base_from_ly_data_id);pk"` // 数据ID
  13. CreateTime string `orm:"column(create_time)"` // 创建时间
  14. ModifyTime string `orm:"column(modify_time)"` // 修改时间
  15. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id)"` // 指标id
  16. IndexCode string `orm:"column(index_code)"` // 指标编码
  17. DataTime string `orm:"column(data_time)"` // 数据日期
  18. Value float64 `orm:"column(value)"` // 数据值
  19. }
  20. func init() {
  21. orm.RegisterModel(new(BaseFromLyData))
  22. }
  23. // GetBaseFromLyDataByIndexCode 根据指标编码查询
  24. func GetBaseFromLyDataByIndexCode(condition string, pars []interface{}) (items []BaseFromLyData, err error) {
  25. sql := `SELECT * FROM base_from_ly_data WHERE 1=1 `
  26. o := orm.NewOrmUsingDB("data")
  27. if condition != "" {
  28. sql += condition
  29. }
  30. _, err = o.Raw(sql, pars...).QueryRows(&items)
  31. return
  32. }
  33. // AddEdbDataFromLy 新增指标数据
  34. func AddEdbDataFromLy(edbCode string) (err error) {
  35. o := orm.NewOrm()
  36. var condition string
  37. var pars []interface{}
  38. if edbCode != "" {
  39. condition += " AND index_code = ? "
  40. pars = append(pars, edbCode)
  41. }
  42. dataAll, err := GetBaseFromLyDataByIndexCode(condition, pars)
  43. if err != nil {
  44. return err
  45. }
  46. dataLen := len(dataAll)
  47. existMap := make(map[string]string)
  48. if dataLen > 0 {
  49. var isAdd bool
  50. addSql := ` INSERT INTO ebd_data_ly (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  51. for i := 0; i < dataLen; i++ {
  52. item := dataAll[i]
  53. eDate := item.DataTime
  54. sValue := utils.SubFloatToString(item.Value, 4)
  55. if sValue != "" {
  56. if _, ok := existMap[eDate]; !ok {
  57. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  58. if err != nil {
  59. return err
  60. }
  61. timestamp := dataTime.UnixNano() / 1e6
  62. timeStr := fmt.Sprintf("%d", timestamp)
  63. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  64. isAdd = true
  65. }
  66. }
  67. existMap[eDate] = eDate
  68. }
  69. if isAdd {
  70. addSql = strings.TrimRight(addSql, ",")
  71. utils.FileLog.Info("addSql:" + addSql)
  72. _, err = o.Raw(addSql).Exec()
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. }
  78. return
  79. }
  80. func RefreshEdbDataFromLy(edbInfoId int, edbCode, startDate string) (err error) {
  81. source := utils.DATA_SOURCE_LY
  82. subSource := utils.DATA_SUB_SOURCE_EDB
  83. o := orm.NewOrm()
  84. if err != nil {
  85. return
  86. }
  87. edbInfoIdStr := strconv.Itoa(edbInfoId)
  88. //计算数据
  89. var condition string
  90. var pars []interface{}
  91. if edbCode != "" {
  92. condition += " AND index_code=? "
  93. pars = append(pars, edbCode)
  94. }
  95. if startDate != "" {
  96. condition += " AND data_time>=? "
  97. pars = append(pars, startDate)
  98. }
  99. dataList, err := GetBaseFromLyDataByIndexCode(condition, pars)
  100. if err != nil {
  101. return
  102. }
  103. // 真实数据的最大日期 , 插入规则配置的日期
  104. var realDataMaxDate, edbDataInsertConfigDate time.Time
  105. var edbDataInsertConfig *EdbDataInsertConfig
  106. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  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. }
  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. //获取指标所有数据
  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 ebd_data_ly(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  134. var isAdd bool
  135. addMap := make(map[string]string)
  136. for _, v := range dataList {
  137. item := v
  138. eDate := item.DataTime
  139. sValue := utils.SubFloatToString(item.Value, 4)
  140. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  141. if err != nil {
  142. return err
  143. }
  144. if findItem, ok := existMap[v.DataTime]; !ok {
  145. if sValue != "" {
  146. timestamp := dataTime.UnixNano() / 1e6
  147. timeStr := fmt.Sprintf("%d", timestamp)
  148. saveValue := sValue
  149. if _, addOk := addMap[eDate]; !addOk {
  150. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  151. isAdd = true
  152. }
  153. }
  154. } else {
  155. if findItem != nil && utils.SubFloatToString(findItem.Value, 4) != sValue {
  156. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  157. if err != nil {
  158. return err
  159. }
  160. }
  161. }
  162. addMap[v.DataTime] = v.DataTime
  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. // 处理手工数据补充的配置
  174. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  175. if isAdd {
  176. addSql = strings.TrimRight(addSql, ",")
  177. _, err = o.Raw(addSql).Exec()
  178. if err != nil {
  179. return err
  180. }
  181. }
  182. return
  183. }