base_from_lz.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/utils"
  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. o := orm.NewOrm()
  72. // 真实数据的最大日期 , 插入规则配置的日期
  73. var realDataMaxDate, edbDataInsertConfigDate time.Time
  74. var edbDataInsertConfig *EdbDataInsertConfig
  75. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  76. {
  77. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  78. if err != nil && err.Error() != utils.ErrNoRow() {
  79. return
  80. }
  81. if edbDataInsertConfig != nil {
  82. edbDataInsertConfigDate = edbDataInsertConfig.Date
  83. }
  84. }
  85. edbInfoIdStr := strconv.Itoa(edbInfoId)
  86. //计算数据
  87. var condition string
  88. var pars []interface{}
  89. if edbCode != "" {
  90. condition += " AND b.lz_code=? "
  91. pars = append(pars, edbCode)
  92. }
  93. if startDate != "" {
  94. condition += " AND a.data_time>=? "
  95. pars = append(pars, startDate)
  96. }
  97. lzDataList, err := GetLzSurveyDataFromLz(condition, pars)
  98. var existCondition string
  99. var existPars []interface{}
  100. existCondition += " AND edb_info_id=? "
  101. existPars = append(existPars, edbInfoId)
  102. if startDate != "" {
  103. existCondition += " AND data_time>=? "
  104. existPars = append(existPars, startDate)
  105. }
  106. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  107. if err != nil {
  108. return err
  109. }
  110. existMap := make(map[string]*EdbInfoSearchData)
  111. for _, v := range existList {
  112. existMap[v.DataTime] = v
  113. }
  114. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  115. var isAdd bool
  116. for _, v := range lzDataList {
  117. item := v
  118. eDate := item.DataTime
  119. sValue := item.InputValue
  120. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  121. if err != nil {
  122. return err
  123. }
  124. timestamp := dataTime.UnixNano() / 1e6
  125. timeStr := fmt.Sprintf("%d", timestamp)
  126. saveValue := sValue
  127. if findItem, ok := existMap[eDate]; !ok {
  128. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  129. isAdd = true
  130. } else {
  131. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != saveValue {
  132. err = ModifyEdbDataById(source, findItem.EdbDataId, saveValue)
  133. if err != nil {
  134. return err
  135. }
  136. }
  137. }
  138. // 下面代码主要目的是处理掉手动插入的数据判断
  139. {
  140. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  141. realDataMaxDate = dataTime
  142. }
  143. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  144. isFindConfigDateRealData = true
  145. }
  146. }
  147. }
  148. // 处理手工数据补充的配置
  149. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  150. if isAdd {
  151. addSql = strings.TrimRight(addSql, ",")
  152. _, err = o.Raw(addSql).Exec()
  153. if err != nil {
  154. return err
  155. }
  156. }
  157. return
  158. }