edb_data_rzd.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // @Author gmy 2024/9/14 16:13:00
  2. package models
  3. import (
  4. "errors"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbDataRzd struct {
  13. edbDataId int `orm:"column(edb_data_id);pk"`
  14. CreateTime string `orm:"column(create_time)"`
  15. ModifyTime string `orm:"column(modify_time)"`
  16. EdbInfoId int `orm:"column(edb_info_id)"`
  17. EdbCode string `orm:"column(edb_code)"`
  18. DataTime string `orm:"column(data_time)"`
  19. Value float64 `orm:"column(value)"`
  20. DataTimestamp uint64 `orm:"column(data_timestamp)"`
  21. }
  22. func init() {
  23. orm.RegisterModel(new(EdbDataRzd))
  24. }
  25. // GetRzdEdbDataByIndexCodeAndDataTime 根据指标编码和时间获取指标库数据
  26. func GetRzdEdbDataByIndexCodeAndDataTime(indexCode string, dataTime string) (items []EdbDataRzd, err error) {
  27. o := orm.NewOrm()
  28. sql := `SELECT * FROM edb_data_rzd WHERE edb_code=? AND data_time=?`
  29. _, err = o.Raw(sql, indexCode, dataTime).QueryRows(&items)
  30. if errors.Is(err, orm.ErrNoRows) {
  31. return nil, nil
  32. }
  33. return
  34. }
  35. // UpdateRzdEdbDataById 更新指标库数据 须根据指标编码和日期更新 仅适合月度数据
  36. func UpdateRzdEdbDataById(id int, value float64) (err error) {
  37. o := orm.NewOrm()
  38. sql := `UPDATE edb_data_rzd SET value=? WHERE edb_data_id=?`
  39. _, err = o.Raw(sql, value, id).Exec()
  40. return
  41. }
  42. // AddRzdEdbDataList 新增指标库数据
  43. func AddRzdEdbDataList(items []EdbDataRzd) (err error) {
  44. o := orm.NewOrm()
  45. _, err = o.InsertMulti(len(items), items)
  46. return
  47. }
  48. // AddEdbDataFromRzd 新增指标数据
  49. func AddEdbDataFromRzd(edbCode string) (err error) {
  50. o := orm.NewOrm()
  51. var condition string
  52. var pars []interface{}
  53. if edbCode != "" {
  54. condition += " AND index_code = ? "
  55. pars = append(pars, edbCode)
  56. }
  57. dataAll, err := GetBaseFromRzdDataByCondition(condition, pars)
  58. if err != nil {
  59. return err
  60. }
  61. dataLen := len(dataAll)
  62. existMap := make(map[string]string)
  63. if dataLen > 0 {
  64. var isAdd bool
  65. addSql := ` INSERT INTO edb_data_rzd (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  66. for i := 0; i < dataLen; i++ {
  67. item := dataAll[i]
  68. eDate := item.DataTime
  69. sValue := utils.SubFloatToString(item.Value, 4)
  70. if sValue != "" {
  71. if _, ok := existMap[eDate]; !ok {
  72. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  73. if err != nil {
  74. return err
  75. }
  76. timestamp := dataTime.UnixNano() / 1e6
  77. timeStr := fmt.Sprintf("%d", timestamp)
  78. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  79. isAdd = true
  80. }
  81. }
  82. existMap[eDate] = eDate
  83. }
  84. if isAdd {
  85. addSql = strings.TrimRight(addSql, ",")
  86. utils.FileLog.Info("addSql:" + addSql)
  87. _, err = o.Raw(addSql).Exec()
  88. if err != nil {
  89. return err
  90. }
  91. }
  92. }
  93. return
  94. }
  95. func RefreshEdbDataFromRzd(edbInfoId int, edbCode, startDate string) (err error) {
  96. source := utils.DATA_SOURCE_RZD
  97. subSource := utils.DATA_SUB_SOURCE_EDB
  98. o := orm.NewOrm()
  99. if err != nil {
  100. return
  101. }
  102. edbInfoIdStr := strconv.Itoa(edbInfoId)
  103. //计算数据
  104. var condition string
  105. var pars []interface{}
  106. if edbCode != "" {
  107. condition += " AND index_code=? "
  108. pars = append(pars, edbCode)
  109. }
  110. if startDate != "" {
  111. condition += " AND data_time>=? "
  112. pars = append(pars, startDate)
  113. }
  114. dataList, err := GetBaseFromRzdDataByCondition(condition, pars)
  115. if err != nil {
  116. return
  117. }
  118. // 真实数据的最大日期 , 插入规则配置的日期
  119. var realDataMaxDate, edbDataInsertConfigDate time.Time
  120. var edbDataInsertConfig *EdbDataInsertConfig
  121. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  122. {
  123. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  124. if err != nil && err.Error() != utils.ErrNoRow() {
  125. return
  126. }
  127. if edbDataInsertConfig != nil {
  128. edbDataInsertConfigDate = edbDataInsertConfig.Date
  129. }
  130. }
  131. var existCondition string
  132. var existPars []interface{}
  133. existCondition += " AND edb_info_id=? "
  134. existPars = append(existPars, edbInfoId)
  135. if startDate != "" {
  136. existCondition += " AND data_time>=? "
  137. existPars = append(existPars, startDate)
  138. }
  139. //获取指标所有数据
  140. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  141. if err != nil {
  142. return err
  143. }
  144. existMap := make(map[string]*EdbInfoSearchData)
  145. for _, v := range existList {
  146. existMap[v.DataTime] = v
  147. }
  148. addSql := ` INSERT INTO edb_data_rzd(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  149. var isAdd bool
  150. addMap := make(map[string]string)
  151. for _, v := range dataList {
  152. item := v
  153. eDate := item.DataTime
  154. sValue := utils.SubFloatToString(item.Value, 4)
  155. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  156. if err != nil {
  157. return err
  158. }
  159. if findItem, ok := existMap[v.DataTime]; !ok {
  160. if sValue != "" {
  161. timestamp := dataTime.UnixNano() / 1e6
  162. timeStr := fmt.Sprintf("%d", timestamp)
  163. saveValue := sValue
  164. if _, addOk := addMap[eDate]; !addOk {
  165. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  166. isAdd = true
  167. }
  168. }
  169. } else {
  170. if findItem != nil && utils.SubFloatToString(findItem.Value, 4) != sValue {
  171. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  172. if err != nil {
  173. return err
  174. }
  175. }
  176. }
  177. addMap[v.DataTime] = v.DataTime
  178. // 下面代码主要目的是处理掉手动插入的数据判断
  179. {
  180. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  181. realDataMaxDate = dataTime
  182. }
  183. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  184. isFindConfigDateRealData = true
  185. }
  186. }
  187. }
  188. // 处理手工数据补充的配置
  189. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  190. if isAdd {
  191. addSql = strings.TrimRight(addSql, ",")
  192. _, err = o.Raw(addSql).Exec()
  193. if err != nil {
  194. return err
  195. }
  196. }
  197. return
  198. }