edb_data_lz.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package data_manage
  2. import (
  3. "fmt"
  4. "hongze/hongze_task/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type lzSurveyData struct {
  11. DataTime string `orm:"column(data_time)" description:"日期"`
  12. InputValue string `orm:"column(input_value)" description:"值"`
  13. }
  14. type LongzhongSurveyData struct {
  15. SurveyDataId int `orm:"column(survey_data_id);pk"`
  16. SurveyProductId int
  17. ProjectQuotaId int64
  18. BreedId string
  19. BreedName string
  20. QuotaId string
  21. QuotaName string
  22. UnitId string
  23. UnitName string
  24. SampleType int64
  25. SampleId string
  26. SampleName string
  27. DeviceId string
  28. Device string
  29. ProductCraftId string
  30. ProductCraft string
  31. ProductLine string
  32. InputMode int64
  33. Frequency int64
  34. InputValue string
  35. TaskShouldFinishTime int64
  36. CustomId string
  37. CustomType int64
  38. Custom string
  39. QuotaSampleId int64
  40. TaskActualFinishTime int64
  41. AreaName string
  42. ProvinceName string
  43. ResearchStartData int64
  44. ResearchStopData int64
  45. DataTime string
  46. }
  47. func GetLzSurveyDataByTradeCode(condition string, pars []interface{}) (item []*lzSurveyData, err error) {
  48. sql := ` SELECT a.* FROM longzhong_survey_data AS a
  49. INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
  50. WHERE 1=1 `
  51. o := orm.NewOrmUsingDB("data")
  52. if condition != "" {
  53. sql += condition
  54. }
  55. sql += ` ORDER BY a.data_time DESC `
  56. _, err = o.Raw(sql, pars).QueryRows(&item)
  57. return
  58. }
  59. func GetLzSurveyDataExistByTradeCode(surveyProductId int) (items []*LongzhongSurveyData, err error) {
  60. sql := ` SELECT * FROM longzhong_survey_data WHERE 1=1 AND survey_product_id=? `
  61. o := orm.NewOrmUsingDB("data")
  62. _, err = o.Raw(sql, surveyProductId).QueryRows(&items)
  63. return
  64. }
  65. func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  66. o := orm.NewOrmUsingDB("data")
  67. sql := ` SELECT COUNT(1) AS count FROM edb_data_lz WHERE edb_code=? AND data_time=? `
  68. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  69. return
  70. }
  71. func ModifyEdbDataLz(edbInfoId int64, dataTime, value string) (err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. sql := ` UPDATE edb_data_lz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  74. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  75. return
  76. }
  77. //刷新隆众指标数据
  78. func RefreshEdbDataByLz(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  79. o := orm.NewOrmUsingDB("data")
  80. tx,err:=o.Begin()
  81. defer func() {
  82. if err != nil {
  83. tx.Rollback()
  84. } else {
  85. tx.Commit()
  86. }
  87. }()
  88. if err != nil {
  89. return
  90. }
  91. edbInfoIdStr := strconv.Itoa(edbInfoId)
  92. //计算数据
  93. var condition string
  94. var pars []interface{}
  95. if edbCode != "" {
  96. condition += " AND b.lz_code=? "
  97. pars = append(pars, edbCode)
  98. }
  99. if startDate != "" {
  100. condition += " AND a.data_time>=? "
  101. pars = append(pars, startDate)
  102. }
  103. if endDate != "" {
  104. condition += " AND a.data_time<=? "
  105. pars = append(pars, endDate)
  106. }
  107. lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
  108. //获取已存在指标所有数据
  109. existDataList := make([]*EdbDataBase, 0)
  110. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_LZ)
  111. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  112. sql = fmt.Sprintf(sql, dataTableName)
  113. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  114. if err != nil {
  115. return err
  116. }
  117. existDataMap := make(map[string]string)
  118. for _, v := range existDataList {
  119. existDataMap[v.DataTime] = v.Value
  120. }
  121. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  122. var isAdd bool
  123. addExistMap := make(map[string]string)
  124. for _, v := range lzDataList {
  125. item := v
  126. if _, ok := addExistMap[v.DataTime]; !ok {
  127. if existVal, existOk := existDataMap[v.DataTime]; !existOk {
  128. eDate := item.DataTime
  129. sValue := item.InputValue
  130. if sValue != "" {
  131. dataTime, err := time.Parse(utils.FormatDate, eDate)
  132. if err != nil {
  133. return err
  134. }
  135. timestamp := dataTime.UnixNano() / 1e6
  136. timeStr := fmt.Sprintf("%d", timestamp)
  137. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  138. isAdd = true
  139. }
  140. } else {
  141. if existVal != item.InputValue {
  142. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  143. sql = fmt.Sprintf(sql, dataTableName)
  144. _, err = tx.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
  145. if err != nil {
  146. return err
  147. }
  148. }
  149. }
  150. }
  151. addExistMap[v.DataTime] = v.InputValue
  152. }
  153. if isAdd {
  154. addSql = strings.TrimRight(addSql, ",")
  155. _, err = tx.Raw(addSql).Exec()
  156. if err != nil {
  157. return err
  158. }
  159. }
  160. return
  161. }