edb_data_rzd.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // UpdateRzdEdbDataByIndexCodeAndDataTime 根据指标编码和时间更新数据
  43. func UpdateRzdEdbDataByIndexCodeAndDataTime(indexCode int, dataTime string, value float64) (err error) {
  44. o := orm.NewOrm()
  45. sql := `UPDATE edb_data_rzd SET value=? WHERE edb_info_id=? AND data_time=?`
  46. _, err = o.Raw(sql, value, indexCode, dataTime).Exec()
  47. return
  48. }
  49. // AddRzdEdbDataList 新增指标库数据
  50. func AddRzdEdbDataList(items []EdbDataRzd) (err error) {
  51. o := orm.NewOrm()
  52. _, err = o.InsertMulti(len(items), items)
  53. return
  54. }
  55. // AddEdbDataFromRzd 新增指标数据
  56. func AddEdbDataFromRzd(edbCode string) (err error) {
  57. o := orm.NewOrm()
  58. var condition string
  59. var pars []interface{}
  60. if edbCode != "" {
  61. condition += " AND index_code = ? "
  62. pars = append(pars, edbCode)
  63. }
  64. dataAll, err := GetBaseFromRzdDataByCondition(condition, pars)
  65. if err != nil {
  66. return err
  67. }
  68. dataLen := len(dataAll)
  69. existMap := make(map[string]string)
  70. if dataLen > 0 {
  71. var isAdd bool
  72. addSql := ` INSERT INTO edb_data_rzd (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  73. for i := 0; i < dataLen; i++ {
  74. item := dataAll[i]
  75. eDate := item.DataTime
  76. sValue := utils.SubFloatToString(item.Value, 4)
  77. if sValue != "" {
  78. if _, ok := existMap[eDate]; !ok {
  79. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  80. if err != nil {
  81. return err
  82. }
  83. timestamp := dataTime.UnixNano() / 1e6
  84. timeStr := fmt.Sprintf("%d", timestamp)
  85. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  86. isAdd = true
  87. }
  88. }
  89. existMap[eDate] = eDate
  90. }
  91. if isAdd {
  92. addSql = strings.TrimRight(addSql, ",")
  93. utils.FileLog.Info("addSql:" + addSql)
  94. _, err = o.Raw(addSql).Exec()
  95. if err != nil {
  96. return err
  97. }
  98. }
  99. }
  100. return
  101. }
  102. func RefreshEdbDataFromRzd(edbInfoId int, edbCode, startDate string) (err error) {
  103. source := utils.DATA_SOURCE_RZD
  104. subSource := utils.DATA_SUB_SOURCE_EDB
  105. o := orm.NewOrm()
  106. if err != nil {
  107. return
  108. }
  109. edbInfoIdStr := strconv.Itoa(edbInfoId)
  110. //计算数据
  111. var condition string
  112. var pars []interface{}
  113. if edbCode != "" {
  114. condition += " AND index_code=? "
  115. pars = append(pars, edbCode)
  116. }
  117. if startDate != "" {
  118. condition += " AND data_time>=? "
  119. pars = append(pars, startDate)
  120. }
  121. dataList, err := GetBaseFromRzdDataByCondition(condition, pars)
  122. if err != nil {
  123. return
  124. }
  125. // 真实数据的最大日期 , 插入规则配置的日期
  126. var realDataMaxDate, edbDataInsertConfigDate time.Time
  127. var edbDataInsertConfig *EdbDataInsertConfig
  128. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  129. {
  130. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  131. if err != nil && err.Error() != utils.ErrNoRow() {
  132. return
  133. }
  134. if edbDataInsertConfig != nil {
  135. edbDataInsertConfigDate = edbDataInsertConfig.Date
  136. }
  137. }
  138. var existCondition string
  139. var existPars []interface{}
  140. existCondition += " AND edb_info_id=? "
  141. existPars = append(existPars, edbInfoId)
  142. if startDate != "" {
  143. existCondition += " AND data_time>=? "
  144. existPars = append(existPars, startDate)
  145. }
  146. //获取指标所有数据
  147. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  148. if err != nil {
  149. return err
  150. }
  151. existMap := make(map[string]*EdbInfoSearchData)
  152. for _, v := range existList {
  153. existMap[v.DataTime] = v
  154. }
  155. addSql := ` INSERT INTO edb_data_rzd(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  156. var isAdd bool
  157. addMap := make(map[string]string)
  158. for _, v := range dataList {
  159. item := v
  160. eDate := item.DataTime
  161. sValue := utils.SubFloatToString(item.Value, 4)
  162. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  163. if err != nil {
  164. return err
  165. }
  166. if findItem, ok := existMap[v.DataTime]; !ok {
  167. if sValue != "" {
  168. timestamp := dataTime.UnixNano() / 1e6
  169. timeStr := fmt.Sprintf("%d", timestamp)
  170. saveValue := sValue
  171. if _, addOk := addMap[eDate]; !addOk {
  172. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  173. isAdd = true
  174. }
  175. }
  176. } else {
  177. if findItem != nil && utils.SubFloatToString(findItem.Value, 4) != sValue {
  178. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  179. if err != nil {
  180. return err
  181. }
  182. }
  183. }
  184. addMap[v.DataTime] = v.DataTime
  185. // 下面代码主要目的是处理掉手动插入的数据判断
  186. {
  187. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  188. realDataMaxDate = dataTime
  189. }
  190. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  191. isFindConfigDateRealData = true
  192. }
  193. }
  194. }
  195. // 处理手工数据补充的配置
  196. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  197. if isAdd {
  198. addSql = strings.TrimRight(addSql, ",")
  199. _, err = o.Raw(addSql).Exec()
  200. if err != nil {
  201. return err
  202. }
  203. }
  204. return
  205. }