base_from_mysteel.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package models
  2. import (
  3. "fmt"
  4. "hongze/hongze_edb_lib/utils"
  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. o := orm.NewOrm()
  89. if err != nil {
  90. return
  91. }
  92. edbInfoIdStr := strconv.Itoa(edbInfoId)
  93. //计算数据
  94. var condition string
  95. var pars []interface{}
  96. if edbCode != "" {
  97. condition += " AND INDEX_CODE=? "
  98. pars = append(pars, edbCode)
  99. }
  100. if startDate != "" {
  101. condition += " AND DATA_DATE>=? "
  102. pars = append(pars, startDate)
  103. }
  104. glDataList, err := GetGlDataByCondition(condition, pars)
  105. if err != nil {
  106. return
  107. }
  108. // 真实数据的最大日期 , 插入规则配置的日期
  109. var realDataMaxDate, edbDataInsertConfigDate time.Time
  110. var edbDataInsertConfig *EdbDataInsertConfig
  111. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  112. {
  113. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  114. if err != nil && err.Error() != utils.ErrNoRow() {
  115. return
  116. }
  117. if edbDataInsertConfig != nil {
  118. edbDataInsertConfigDate = edbDataInsertConfig.Date
  119. }
  120. }
  121. var existCondition string
  122. var existPars []interface{}
  123. existCondition += " AND edb_info_id=? "
  124. existPars = append(existPars, edbInfoId)
  125. if startDate != "" {
  126. existCondition += " AND data_time>=? "
  127. existPars = append(existPars, startDate)
  128. }
  129. //获取指标所有数据
  130. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  131. if err != nil {
  132. return err
  133. }
  134. existMap := make(map[string]*EdbInfoSearchData)
  135. for _, v := range existList {
  136. existMap[v.DataTime] = v
  137. }
  138. addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  139. var isAdd bool
  140. addMap := make(map[string]string)
  141. for _, v := range glDataList {
  142. item := v
  143. eDate := item.DataTime
  144. sValue := utils.SubFloatToString(item.InputValue, 30)
  145. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  146. if err != nil {
  147. return err
  148. }
  149. if findItem, ok := existMap[v.DataTime]; !ok {
  150. if sValue != "" {
  151. timestamp := dataTime.UnixNano() / 1e6
  152. timeStr := fmt.Sprintf("%d", timestamp)
  153. saveValue := sValue
  154. if _, addOk := addMap[eDate]; !addOk {
  155. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  156. isAdd = true
  157. }
  158. }
  159. } else {
  160. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  161. err = ModifyEdbDataById(source, findItem.EdbDataId, sValue)
  162. if err != nil {
  163. return err
  164. }
  165. }
  166. }
  167. addMap[v.DataTime] = v.DataTime
  168. // 下面代码主要目的是处理掉手动插入的数据判断
  169. {
  170. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  171. realDataMaxDate = dataTime
  172. }
  173. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  174. isFindConfigDateRealData = true
  175. }
  176. }
  177. }
  178. // 处理手工数据补充的配置
  179. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  180. if isAdd {
  181. addSql = strings.TrimRight(addSql, ",")
  182. _, err = o.Raw(addSql).Exec()
  183. if err != nil {
  184. return err
  185. }
  186. }
  187. return
  188. }