base_from_mysteel.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. //钢联
  11. type GlData struct {
  12. InputValue float64 `orm:"column(DATA_VALUE)" description:"日期"`
  13. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  14. }
  15. func GetGlDataByCondition(condition string, pars []interface{}) (item []*GlData, err error) {
  16. condition += " AND IS_DELETE=0 "
  17. sql1 := ` SELECT * FROM mb_index_main_data WHERE 1=1 AND DATA_VALUE is not null `
  18. o := orm.NewOrmUsingDB("gl")
  19. if condition != "" {
  20. sql1 += condition
  21. }
  22. sql := `select * from (` + sql1 + ` having 1 order by PUBLISH_TIME DESC ) tmp group by DATA_DATE ORDER BY PUBLISH_TIME DESC `
  23. //select * from (
  24. // select * from mb_index_main_data where
  25. // 1 = 1
  26. // AND DATA_VALUE IS NOT NULL
  27. // AND INDEX_CODE ="ID00182580"
  28. // AND DATA_DATE >="2016-09-01"
  29. // AND IS_DELETE = 0 having 1 order by PUBLISH_TIME DESC
  30. //
  31. //
  32. // ) tmp group by DATA_DATE
  33. //
  34. // order by PUBLISH_TIME DESC
  35. _, err = o.Raw(sql, pars).QueryRows(&item)
  36. return
  37. }
  38. // 新增钢联指标数据
  39. func AddEdbDataFromMysteel(edbCode string) (err error) {
  40. o := orm.NewOrm()
  41. var condition string
  42. var pars []interface{}
  43. if edbCode != "" {
  44. condition += " AND INDEX_CODE=? "
  45. pars = append(pars, edbCode)
  46. }
  47. glDataList, err := GetGlDataByCondition(condition, pars)
  48. if err != nil {
  49. return
  50. }
  51. dataLen := len(glDataList)
  52. existMap := make(map[string]string)
  53. if dataLen > 0 {
  54. var isAdd bool
  55. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  56. for i := 0; i < dataLen; i++ {
  57. item := glDataList[i]
  58. eDate := item.DataTime
  59. sValue := utils.SubFloatToString(item.InputValue, 30)
  60. if sValue != "" {
  61. if _, ok := existMap[eDate]; !ok {
  62. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  63. if err != nil {
  64. return err
  65. }
  66. timestamp := dataTime.UnixNano() / 1e6
  67. timeStr := fmt.Sprintf("%d", timestamp)
  68. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  69. isAdd = true
  70. }
  71. }
  72. existMap[eDate] = eDate
  73. }
  74. if isAdd {
  75. addSql = strings.TrimRight(addSql, ",")
  76. utils.FileLog.Info("addSql:" + addSql)
  77. _, err = o.Raw(addSql).Exec()
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. return
  84. }
  85. // 刷新钢联指标数据
  86. func RefreshEdbDataFromMysteel(edbInfoId int, edbCode, startDate string) (err error) {
  87. source := utils.DATA_SOURCE_GL
  88. subSource := utils.DATA_SUB_SOURCE_EDB
  89. o := orm.NewOrm()
  90. if err != nil {
  91. return
  92. }
  93. edbInfoIdStr := strconv.Itoa(edbInfoId)
  94. //计算数据
  95. var condition string
  96. var pars []interface{}
  97. if edbCode != "" {
  98. condition += " AND INDEX_CODE=? "
  99. pars = append(pars, edbCode)
  100. }
  101. if startDate != "" {
  102. condition += " AND DATA_DATE>=? "
  103. pars = append(pars, startDate)
  104. }
  105. glDataList, err := GetGlDataByCondition(condition, pars)
  106. if err != nil {
  107. return
  108. }
  109. // 真实数据的最大日期 , 插入规则配置的日期
  110. var realDataMaxDate, edbDataInsertConfigDate time.Time
  111. var edbDataInsertConfig *EdbDataInsertConfig
  112. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  113. {
  114. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  115. if err != nil && err.Error() != utils.ErrNoRow() {
  116. return
  117. }
  118. if edbDataInsertConfig != nil {
  119. edbDataInsertConfigDate = edbDataInsertConfig.Date
  120. }
  121. }
  122. var existCondition string
  123. var existPars []interface{}
  124. existCondition += " AND edb_info_id=? "
  125. existPars = append(existPars, edbInfoId)
  126. if startDate != "" {
  127. existCondition += " AND data_time>=? "
  128. existPars = append(existPars, startDate)
  129. }
  130. //获取指标所有数据
  131. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  132. if err != nil {
  133. return err
  134. }
  135. existMap := make(map[string]*EdbInfoSearchData)
  136. for _, v := range existList {
  137. existMap[v.DataTime] = v
  138. }
  139. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  140. var isAdd bool
  141. addMap := make(map[string]string)
  142. for _, v := range glDataList {
  143. item := v
  144. eDate := item.DataTime
  145. sValue := utils.SubFloatToString(item.InputValue, 30)
  146. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  147. if err != nil {
  148. return err
  149. }
  150. if findItem, ok := existMap[v.DataTime]; !ok {
  151. if sValue != "" {
  152. timestamp := dataTime.UnixNano() / 1e6
  153. timeStr := fmt.Sprintf("%d", timestamp)
  154. saveValue := sValue
  155. if _, addOk := addMap[eDate]; !addOk {
  156. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  157. isAdd = true
  158. }
  159. }
  160. } else {
  161. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  162. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  163. if err != nil {
  164. return err
  165. }
  166. }
  167. }
  168. addMap[v.DataTime] = v.DataTime
  169. // 下面代码主要目的是处理掉手动插入的数据判断
  170. {
  171. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  172. realDataMaxDate = dataTime
  173. }
  174. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  175. isFindConfigDateRealData = true
  176. }
  177. }
  178. }
  179. // 处理手工数据补充的配置
  180. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  181. if isAdd {
  182. addSql = strings.TrimRight(addSql, ",")
  183. _, err = o.Raw(addSql).Exec()
  184. if err != nil {
  185. return err
  186. }
  187. }
  188. return
  189. }