edb_info_calculate_nszydpjjs.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. "hongze/hongze_task/utils"
  6. "rdluck_tools/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type EdbInfoCalculateNszydpjjs struct {
  12. EdbInfoCalculateNszydpjjsId int `orm:"column(edb_info_calculate_nszydpjjs_id);pk"`
  13. EdbInfoId int `description:"指标id"`
  14. EdbCode string `description:"指标编码"`
  15. FromEdbInfoId int `description:"计算指标id"`
  16. FromEdbCode string `description:"计算指标编码"`
  17. FromEdbName string `description:"计算指标名称"`
  18. FromSource int `description:"计算指标来源"`
  19. FromSourceName string `description:"计算指标来源名称"`
  20. FromTag string `description:"来源指标标签"`
  21. Sort int `description:"计算指标名称排序"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. //刷新 N数值移动平均计算
  26. func RefreshCalculateNszydpjjs(edbInfoId, source, formulaInt int, fromEdbInfo *EdbInfo, edbCode string) (err error) {
  27. o := orm.NewOrm()
  28. o.Using("data")
  29. o.Begin()
  30. defer func() {
  31. if err != nil {
  32. o.Rollback()
  33. } else {
  34. o.Commit()
  35. }
  36. }()
  37. if err != nil {
  38. return
  39. }
  40. edbInfoIdStr := strconv.Itoa(edbInfoId)
  41. //计算数据
  42. var condition string
  43. var pars []interface{}
  44. condition += " AND edb_info_id=? "
  45. pars = append(pars, edbInfoId)
  46. dataList, err := GetEdbDataListAll(condition, pars, source, 0)
  47. if err != nil {
  48. return err
  49. }
  50. existDataMap := make(map[string]*EdbInfoSearchData)
  51. for _, v := range dataList {
  52. utils.FileLog.Info("exist:",v.DataTime,v.Value)
  53. existDataMap[v.DataTime] = v
  54. }
  55. fmt.Println("existDataMap:",existDataMap)
  56. //计算来源数据
  57. //startDate := time.Now().AddDate(-2, 0, 0).Format(utils.FormatDate)
  58. var fromCondition string
  59. var fromPars []interface{}
  60. fromCondition += " AND edb_info_id=? "
  61. fromPars = append(fromPars, fromEdbInfo.EdbInfoId)
  62. //fromCondition += " AND data_time>=? "
  63. //fromPars = append(fromPars, startDate)
  64. fromDataList, err := GetEdbDataListAll(fromCondition, fromPars, fromEdbInfo.Source, 0)
  65. if err != nil {
  66. return err
  67. }
  68. var fromDateArr []string
  69. fromDataMap := make(map[string]*EdbInfoSearchData)
  70. for _, v := range fromDataList {
  71. fromDateArr = append(fromDateArr, v.DataTime)
  72. fromDataMap[v.DataTime] = v
  73. }
  74. addSql := ` INSERT INTO edb_data_calculate_nszydpjjs(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  75. var isAdd bool
  76. arrLen := len(fromDateArr)
  77. for ak, av := range fromDateArr {
  78. //处理第一个值
  79. var valArr []float64
  80. if findItem, ok := fromDataMap[av]; ok {
  81. valArr = append(valArr, findItem.Value)
  82. } else {
  83. continue
  84. }
  85. if ak+1 != arrLen {
  86. //处理除第一个值之外的N-1个值
  87. for i := 1; i < formulaInt; i++ {
  88. arrIndex := ak + i
  89. if arrIndex >= arrLen {
  90. break
  91. }
  92. arrVal := fromDateArr[arrIndex]
  93. if findItem, ok := fromDataMap[arrVal]; ok {
  94. valArr = append(valArr, findItem.Value)
  95. } else {
  96. continue
  97. }
  98. }
  99. }
  100. valArrLen := len(valArr)
  101. totalVal := decimal.NewFromFloat(0.00)
  102. for _, v := range valArr {
  103. //totalVal = totalVal + v
  104. newDecimal := decimal.NewFromFloat(v)
  105. totalVal = totalVal.Add(newDecimal)
  106. }
  107. //af := decimal.NewFromFloat(totalVal)
  108. af := totalVal
  109. bf := decimal.NewFromFloat(float64(valArrLen))
  110. val, _ := af.Div(bf).Float64()
  111. currentDate, err := time.Parse(utils.FormatDate, av)
  112. if err != nil {
  113. return err
  114. }
  115. timestamp := currentDate.UnixNano() / 1e6
  116. timestampStr := fmt.Sprintf("%d", timestamp)
  117. valStr := utils.SubFloatToString(val, 4)
  118. if existItem, existOk := existDataMap[av]; !existOk {
  119. //count, err := GetEdbDataCalculateNszydpjjsByCodeAndDate(edbCode, av)
  120. //if err != nil && err.Error() != utils.ErrNoRow() {
  121. // return err
  122. //}
  123. //if count <= 0 {
  124. // addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, valStr)
  125. // isAdd = true
  126. //} else {
  127. // err = ModifyEdbDataCalculateNszydpjjs(int64(edbInfoId), av, valStr)
  128. // if err != nil {
  129. // return err
  130. // }
  131. //}
  132. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, valStr)
  133. isAdd = true
  134. } else {
  135. if existItem.Value != val {
  136. err = ModifyEdbDataCalculateNszydpjjs(int64(edbInfoId), av, valStr)
  137. if err != nil {
  138. return err
  139. }
  140. }
  141. }
  142. }
  143. if isAdd {
  144. addSql = strings.TrimRight(addSql, ",")
  145. _, err = o.Raw(addSql).Exec()
  146. if err != nil {
  147. return err
  148. }
  149. }
  150. return
  151. }
  152. type EdbInfoCalculateNszydpjjsDetail struct {
  153. EdbInfoCalculateNszydpjjsId int `orm:"column(edb_info_calculate_nszydpjjs_id);pk"`
  154. EdbInfoId int `description:"指标id"`
  155. EdbCode string `description:"指标编码"`
  156. FromEdbInfoId int `description:"计算指标id"`
  157. FromEdbCode string `description:"计算指标编码"`
  158. FromEdbName string `description:"计算指标名称"`
  159. FromSource int `description:"计算指标来源"`
  160. FromSourceName string `description:"计算指标来源名称"`
  161. FromTag string `description:"来源指标标签"`
  162. Sort int `description:"计算指标名称排序"`
  163. CreateTime time.Time `description:"创建时间"`
  164. ModifyTime time.Time `description:"修改时间"`
  165. StartDate string `description:"开始日期"`
  166. EndDate string `description:"结束日期"`
  167. }
  168. func GetEdbInfoCalculateNszydpjjsDetail(edbInfoId int) (item *EdbInfoCalculateNszydpjjsDetail, err error) {
  169. o := orm.NewOrm()
  170. o.Using("data")
  171. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_nszydpjjs AS a
  172. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  173. WHERE a.edb_info_id=? `
  174. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  175. return
  176. }
  177. func GetEdbDataCalculateNszydpjjsByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  178. o := orm.NewOrm()
  179. o.Using("data")
  180. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_nszydpjjs WHERE edb_code=? AND data_time=? `
  181. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  182. return
  183. }
  184. func ModifyEdbDataCalculateNszydpjjs(edbInfoId int64, dataTime, value string) (err error) {
  185. o := orm.NewOrm()
  186. o.Using("data")
  187. sql := ` UPDATE edb_data_calculate_nszydpjjs SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  188. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  189. return
  190. }