base_from_lz.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. //隆众
  11. type lzSurveyData struct {
  12. DataTime string `orm:"column(data_time)" description:"日期"`
  13. InputValue string `orm:"column(input_value)" description:"值"`
  14. }
  15. func GetLzSurveyDataFromLz(condition string, pars []interface{}) (item []*lzSurveyData, err error) {
  16. sql := ` SELECT a.* FROM longzhong_survey_data AS a
  17. INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
  18. WHERE 1=1 `
  19. o := orm.NewOrmUsingDB("edb")
  20. if condition != "" {
  21. sql += condition
  22. }
  23. sql += ` GROUP BY data_time ORDER BY a.data_time DESC `
  24. _, err = o.Raw(sql, pars).QueryRows(&item)
  25. return
  26. }
  27. // 新增钢联指标数据
  28. func AddEdbDataFromLz(edbCode string) (err error) {
  29. o := orm.NewOrm()
  30. var condition string
  31. var pars []interface{}
  32. if edbCode != "" {
  33. condition += " AND b.lz_code=? "
  34. pars = append(pars, edbCode)
  35. }
  36. lzDataList, err := GetLzSurveyDataFromLz(condition, pars)
  37. if err != nil {
  38. return
  39. }
  40. dataLen := len(lzDataList)
  41. if dataLen > 0 {
  42. var isAdd bool
  43. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  44. for i := 0; i < dataLen; i++ {
  45. item := lzDataList[i]
  46. eDate := item.DataTime
  47. sValue := item.InputValue
  48. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  49. if err != nil {
  50. return err
  51. }
  52. timestamp := dataTime.UnixNano() / 1e6
  53. timeStr := fmt.Sprintf("%d", timestamp)
  54. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  55. isAdd = true
  56. }
  57. if isAdd {
  58. addSql = strings.TrimRight(addSql, ",")
  59. utils.FileLog.Info("addSql:" + addSql)
  60. _, err = o.Raw(addSql).Exec()
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. }
  66. return
  67. }
  68. // 刷新隆众指标数据
  69. func RefreshEdbDataFromLz(edbInfoId int, edbCode, startDate string) (err error) {
  70. source := utils.DATA_SOURCE_LZ
  71. subSource := utils.DATA_SUB_SOURCE_EDB
  72. o := orm.NewOrm()
  73. // 真实数据的最大日期 , 插入规则配置的日期
  74. var realDataMaxDate, edbDataInsertConfigDate time.Time
  75. var edbDataInsertConfig *EdbDataInsertConfig
  76. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  77. {
  78. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  79. if err != nil && err.Error() != utils.ErrNoRow() {
  80. return
  81. }
  82. if edbDataInsertConfig != nil {
  83. edbDataInsertConfigDate = edbDataInsertConfig.Date
  84. }
  85. }
  86. edbInfoIdStr := strconv.Itoa(edbInfoId)
  87. //计算数据
  88. var condition string
  89. var pars []interface{}
  90. if edbCode != "" {
  91. condition += " AND b.lz_code=? "
  92. pars = append(pars, edbCode)
  93. }
  94. if startDate != "" {
  95. condition += " AND a.data_time>=? "
  96. pars = append(pars, startDate)
  97. }
  98. lzDataList, err := GetLzSurveyDataFromLz(condition, pars)
  99. var existCondition string
  100. var existPars []interface{}
  101. existCondition += " AND edb_info_id=? "
  102. existPars = append(existPars, edbInfoId)
  103. if startDate != "" {
  104. existCondition += " AND data_time>=? "
  105. existPars = append(existPars, startDate)
  106. }
  107. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  108. if err != nil {
  109. return err
  110. }
  111. existMap := make(map[string]*EdbInfoSearchData)
  112. for _, v := range existList {
  113. existMap[v.DataTime] = v
  114. }
  115. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  116. var isAdd bool
  117. for _, v := range lzDataList {
  118. item := v
  119. eDate := item.DataTime
  120. sValue := item.InputValue
  121. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  122. if err != nil {
  123. return err
  124. }
  125. timestamp := dataTime.UnixNano() / 1e6
  126. timeStr := fmt.Sprintf("%d", timestamp)
  127. saveValue := sValue
  128. if findItem, ok := existMap[eDate]; !ok {
  129. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  130. isAdd = true
  131. } else {
  132. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != saveValue {
  133. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, saveValue)
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. }
  139. // 下面代码主要目的是处理掉手动插入的数据判断
  140. {
  141. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  142. realDataMaxDate = dataTime
  143. }
  144. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  145. isFindConfigDateRealData = true
  146. }
  147. }
  148. }
  149. // 处理手工数据补充的配置
  150. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  151. if isAdd {
  152. addSql = strings.TrimRight(addSql, ",")
  153. _, err = o.Raw(addSql).Exec()
  154. if err != nil {
  155. return err
  156. }
  157. }
  158. return
  159. }