edb_info_calculate_ljzzy.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package data_manage
  2. import (
  3. "fmt"
  4. "hongze/hongze_task/utils"
  5. "rdluck_tools/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type EdbInfoCalculateLjzzy struct {
  11. EdbInfoCalculateLjzzyId int `orm:"column(edb_info_calculate_ljzzy_id);pk"`
  12. EdbInfoId int `description:"指标id"`
  13. EdbCode string `description:"指标编码"`
  14. FromEdbInfoId int `description:"计算指标id"`
  15. FromEdbCode string `description:"计算指标编码"`
  16. FromEdbName string `description:"计算指标名称"`
  17. FromSource int `description:"计算指标来源"`
  18. FromSourceName string `description:"计算指标来源名称"`
  19. FromTag string `description:"来源指标标签"`
  20. Sort int `description:"计算指标名称排序"`
  21. CreateTime time.Time `description:"创建时间"`
  22. ModifyTime time.Time `description:"修改时间"`
  23. }
  24. //刷新累计值转月值数据
  25. func RefreshCalculateLjzzy(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  26. o := orm.NewOrm()
  27. o.Using("data")
  28. o.Begin()
  29. defer func() {
  30. if err != nil {
  31. o.Rollback()
  32. } else {
  33. o.Commit()
  34. }
  35. }()
  36. if err != nil {
  37. return
  38. }
  39. edbInfoIdStr := strconv.Itoa(edbInfoId)
  40. //计算数据
  41. var condition string
  42. var pars []interface{}
  43. condition += " AND edb_info_id=? "
  44. pars = append(pars, fromEdbInfo.EdbInfoId)
  45. if startDate != "" {
  46. condition += " AND data_time>=? "
  47. pars = append(pars, startDate)
  48. }
  49. if endDate != "" {
  50. condition += " AND data_time<=? "
  51. pars = append(pars, endDate)
  52. }
  53. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 1)
  54. if err != nil {
  55. return err
  56. }
  57. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  58. dataLen := len(dataList)
  59. for i := 0; i < dataLen; i++ {
  60. item := dataList[i]
  61. //日其中获取年
  62. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  63. if err != nil {
  64. return err
  65. }
  66. year := itemDate.Year()
  67. month := int(itemDate.Month())
  68. if monthMap, yok := yearMap[year]; yok {
  69. monthMap[month] = item
  70. yearMap[year] = monthMap
  71. } else {
  72. monthMap = make(map[int]*EdbInfoSearchData)
  73. monthMap[month] = item
  74. yearMap[year] = monthMap
  75. }
  76. }
  77. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  78. nowStr := time.Now().Format(utils.FormatDateTime)
  79. var isAdd bool
  80. for yk, yv := range yearMap {
  81. if _, mok := yv[2]; !mok {
  82. //fmt.Println(yk, yv, "continue")
  83. continue
  84. }
  85. for i := 1; i <= 12; i++ {
  86. fmt.Println(yk, i, yv[i])
  87. dataCurrentItem := yv[i]
  88. var date string
  89. var val float64
  90. if i == 1 || i == 2 {
  91. dataTwoItem := yv[2]
  92. if dataCurrentItem != nil && dataTwoItem != nil {
  93. date = dataCurrentItem.DataTime
  94. val = dataTwoItem.Value
  95. }
  96. } else {
  97. dataPreItem := yv[i-1]
  98. if dataCurrentItem != nil && dataPreItem != nil {
  99. date = dataCurrentItem.DataTime
  100. val = dataCurrentItem.Value - dataPreItem.Value
  101. }
  102. }
  103. if date != "" {
  104. //判断数据是否存在
  105. count, err := GetEdbDataCalculateLjzzyByCodeAndDate(edbCode, date)
  106. if err != nil && err.Error() != utils.ErrNoRow() {
  107. return err
  108. }
  109. if count <= 0 {
  110. dataTime, _ := time.Parse(utils.FormatDate, date)
  111. timestamp := dataTime.UnixNano() / 1e6
  112. timeStr := fmt.Sprintf("%d", timestamp)
  113. addSql += "("
  114. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + date + "'" + "," + utils.SubFloatToString(val, 4) + "," + "'" + nowStr + "'" +
  115. "," + "'" + nowStr + "'" + "," + "1"
  116. addSql += "," + "'" + timeStr + "'"
  117. addSql += "),"
  118. isAdd = true
  119. } else {
  120. val = utils.FixFloat(val, 4)
  121. err = ModifyEdbDataCalculateLjzzy(int64(edbInfoId), date, val)
  122. if err != nil {
  123. return err
  124. }
  125. }
  126. }
  127. }
  128. }
  129. if isAdd {
  130. addSql = strings.TrimRight(addSql, ",")
  131. _, err = o.Raw(addSql).Exec()
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. return
  137. }
  138. type EdbInfoCalculateLjzzyDetail struct {
  139. EdbInfoCalculateLjzzyId int `orm:"column(edb_info_calculate_ljzzy_id);pk"`
  140. EdbInfoId int `description:"指标id"`
  141. EdbCode string `description:"指标编码"`
  142. FromEdbInfoId int `description:"计算指标id"`
  143. FromEdbCode string `description:"计算指标编码"`
  144. FromEdbName string `description:"计算指标名称"`
  145. FromSource int `description:"计算指标来源"`
  146. FromSourceName string `description:"计算指标来源名称"`
  147. FromTag string `description:"来源指标标签"`
  148. Sort int `description:"计算指标名称排序"`
  149. CreateTime time.Time `description:"创建时间"`
  150. ModifyTime time.Time `description:"修改时间"`
  151. StartDate string `description:"开始日期"`
  152. EndDate string `description:"结束日期"`
  153. }
  154. func GetEdbInfoCalculateLjzzyDetail(edbInfoId int) (item *EdbInfoCalculateLjzzyDetail, err error) {
  155. o := orm.NewOrm()
  156. o.Using("data")
  157. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_ljzzy AS a
  158. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  159. WHERE a.edb_info_id=? `
  160. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  161. return
  162. }
  163. func GetEdbDataCalculateLjzzyByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  164. o := orm.NewOrm()
  165. o.Using("data")
  166. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_ljzzy WHERE edb_code=? AND data_time=? `
  167. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  168. return
  169. }
  170. func ModifyEdbDataCalculateLjzzy(edbInfoId int64, dataTime string, value float64) (err error) {
  171. o := orm.NewOrm()
  172. o.Using("data")
  173. sql := ` UPDATE edb_info_calculate_ljzzy SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  174. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  175. return
  176. }