edb_data_ly.go 6.2 KB

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