edb_data_lz.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package data_manage
  2. import (
  3. "fmt"
  4. "hongze/hongze_task/utils"
  5. "github.com/rdlucklib/rdluck_tools/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.NewOrm()
  52. o.Using("edb")
  53. if condition != "" {
  54. sql += condition
  55. }
  56. sql += ` ORDER BY a.data_time DESC `
  57. _, err = o.Raw(sql, pars).QueryRows(&item)
  58. return
  59. }
  60. func GetLzSurveyDataExistByTradeCode(surveyProductId int) (items []*LongzhongSurveyData, err error) {
  61. sql := ` SELECT * FROM longzhong_survey_data WHERE 1=1 AND survey_product_id=? `
  62. o := orm.NewOrm()
  63. o.Using("edb")
  64. _, err = o.Raw(sql, surveyProductId).QueryRows(&items)
  65. return
  66. }
  67. func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  68. o := orm.NewOrm()
  69. o.Using("data")
  70. sql := ` SELECT COUNT(1) AS count FROM edb_data_lz WHERE edb_code=? AND data_time=? `
  71. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  72. return
  73. }
  74. func ModifyEdbDataLz(edbInfoId int64, dataTime, value string) (err error) {
  75. o := orm.NewOrm()
  76. o.Using("data")
  77. sql := ` UPDATE edb_data_lz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  78. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  79. return
  80. }
  81. //刷新隆众指标数据
  82. func RefreshEdbDataByLz(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  83. o := orm.NewOrm()
  84. o.Using("data")
  85. o.Begin()
  86. defer func() {
  87. if err != nil {
  88. o.Rollback()
  89. } else {
  90. o.Commit()
  91. }
  92. }()
  93. if err != nil {
  94. return
  95. }
  96. edbInfoIdStr := strconv.Itoa(edbInfoId)
  97. //计算数据
  98. var condition string
  99. var pars []interface{}
  100. if edbCode != "" {
  101. condition += " AND b.lz_code=? "
  102. pars = append(pars, edbCode)
  103. }
  104. if startDate != "" {
  105. condition += " AND a.data_time>=? "
  106. pars = append(pars, startDate)
  107. }
  108. if endDate != "" {
  109. condition += " AND a.data_time<=? "
  110. pars = append(pars, endDate)
  111. }
  112. lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
  113. //获取已存在指标所有数据
  114. existDataList := make([]*EdbDataBase, 0)
  115. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_LZ)
  116. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  117. sql = fmt.Sprintf(sql, dataTableName)
  118. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  119. if err != nil {
  120. return err
  121. }
  122. existDataMap := make(map[string]string)
  123. for _, v := range existDataList {
  124. existDataMap[v.DataTime] = v.Value
  125. }
  126. addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  127. var isAdd bool
  128. addExistMap := make(map[string]string)
  129. for _, v := range lzDataList {
  130. item := v
  131. if _, ok := addExistMap[v.DataTime]; !ok {
  132. if existVal, existOk := existDataMap[v.DataTime]; !existOk {
  133. eDate := item.DataTime
  134. sValue := item.InputValue
  135. if sValue != "" {
  136. dataTime, err := time.Parse(utils.FormatDate, eDate)
  137. if err != nil {
  138. return err
  139. }
  140. timestamp := dataTime.UnixNano() / 1e6
  141. timeStr := fmt.Sprintf("%d", timestamp)
  142. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  143. isAdd = true
  144. }
  145. } else {
  146. if existVal != item.InputValue {
  147. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  148. sql = fmt.Sprintf(sql, dataTableName)
  149. _, err = o.Raw(sql, item.InputValue, edbInfoId, v.DataTime).Exec()
  150. if err != nil {
  151. return err
  152. }
  153. }
  154. }
  155. }
  156. addExistMap[v.DataTime] = v.InputValue
  157. }
  158. if isAdd {
  159. addSql = strings.TrimRight(addSql, ",")
  160. _, err = o.Raw(addSql).Exec()
  161. if err != nil {
  162. return err
  163. }
  164. }
  165. return
  166. }