edb_data_calculate_time_shift.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. "hongze/hongze_task/utils"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //时间移位
  12. type EdbInfoCalculateMove struct {
  13. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  14. EdbInfoId int `description:"指标id"`
  15. EdbCode string `description:"指标编码"`
  16. FromEdbInfoId int `description:"计算指标id"`
  17. FromEdbCode string `description:"计算指标编码"`
  18. FromEdbName string `description:"计算指标名称"`
  19. FromSource int `description:"计算指标来源"`
  20. FromSourceName string `description:"计算指标来源名称"`
  21. FromTag string `description:"来源指标标签"`
  22. Sort int `description:"计算指标名称排序"`
  23. CreateTime time.Time `description:"创建时间"`
  24. ModifyTime time.Time `description:"修改时间"`
  25. }
  26. //变频
  27. func RefreshCalculateTimeShift(edbInfoId, formulaInt, moveType int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate, moveFrequency string) (err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. tx,err:=o.Begin()
  30. if err != nil {
  31. return
  32. }
  33. defer func() {
  34. if err != nil {
  35. tx.Rollback()
  36. } else {
  37. tx.Commit()
  38. }
  39. }()
  40. if err != nil {
  41. return
  42. }
  43. edbInfoIdStr := strconv.Itoa(edbInfoId)
  44. //计算数据
  45. //计算数据
  46. var condition string
  47. var pars []interface{}
  48. condition += " AND edb_info_id=? "
  49. pars = append(pars, fromEdbInfo.EdbInfoId)
  50. if startDate != "" {
  51. condition += " AND data_time>=? "
  52. pars = append(pars, startDate)
  53. }
  54. if endDate != "" {
  55. condition += " AND data_time<=? "
  56. pars = append(pars, endDate)
  57. }
  58. var shiftDay int
  59. switch moveFrequency {
  60. case "天":
  61. shiftDay = formulaInt
  62. case "周":
  63. shiftDay = formulaInt * 7
  64. case "月":
  65. shiftDay = formulaInt * 30
  66. case "季":
  67. shiftDay = formulaInt * 90
  68. case "年":
  69. shiftDay = formulaInt * 365
  70. default:
  71. shiftDay = formulaInt
  72. }
  73. if moveType == 2 {
  74. shiftDay = -shiftDay
  75. }
  76. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  77. if err != nil {
  78. return err
  79. }
  80. existMap := make(map[string]string)
  81. dataLen := len(dataList)
  82. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  83. var isAdd bool
  84. for i := 0; i < dataLen; i++ {
  85. //当期
  86. currentItem := dataList[i]
  87. existKey := edbCode + currentItem.DataTime
  88. if _, ok := existMap[existKey]; !ok {
  89. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  90. newDate := currentDate.AddDate(0, 0, -shiftDay)
  91. timestamp := newDate.UnixNano() / 1e6
  92. timestampStr := fmt.Sprintf("%d", timestamp)
  93. valStr := decimal.NewFromFloat(currentItem.Value).String()
  94. addSql += GetAddSql(edbInfoIdStr, edbCode, newDate.Format(utils.FormatDate), timestampStr, valStr)
  95. }
  96. existMap[existKey] = currentItem.DataTime
  97. }
  98. if isAdd {
  99. addSql = strings.TrimRight(addSql, ",")
  100. _, err = tx.Raw(addSql).Exec()
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return
  106. }
  107. type EdbInfoCalculateTimeShiftDetail struct {
  108. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  109. EdbInfoId int `description:"指标id"`
  110. EdbCode string `description:"指标编码"`
  111. FromEdbInfoId int `description:"计算指标id"`
  112. FromEdbCode string `description:"计算指标编码"`
  113. FromEdbName string `description:"计算指标名称"`
  114. FromSource int `description:"计算指标来源"`
  115. FromSourceName string `description:"计算指标来源名称"`
  116. FromTag string `description:"来源指标标签"`
  117. Sort int `description:"计算指标名称排序"`
  118. CreateTime time.Time `description:"创建时间"`
  119. ModifyTime time.Time `description:"修改时间"`
  120. StartDate string `description:"开始日期"`
  121. EndDate string `description:"结束日期"`
  122. }
  123. func GetEdbInfoCalculateTimeShiftDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  124. o := orm.NewOrmUsingDB("data")
  125. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  126. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  127. WHERE a.edb_info_id=? `
  128. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  129. return
  130. }
  131. //刷新所有变频数据
  132. func RefreshAllCalculateTimeShift(edbInfoId, source, formulaInt, moveType int, fromEdbInfo *EdbInfo, edbCode, moveFrequency string) (err error) {
  133. o := orm.NewOrmUsingDB("data")
  134. tx,err:=o.Begin()
  135. if err!=nil {
  136. return err
  137. }
  138. defer func() {
  139. if err != nil {
  140. tx.Rollback()
  141. } else {
  142. tx.Commit()
  143. }
  144. }()
  145. if err != nil {
  146. return
  147. }
  148. edbInfoIdStr := strconv.Itoa(edbInfoId)
  149. //计算数据
  150. //计算数据
  151. var condition string
  152. var pars []interface{}
  153. condition += " AND edb_info_id=? "
  154. pars = append(pars, fromEdbInfo.EdbInfoId)
  155. //if startDate != "" {
  156. // condition += " AND data_time>=? "
  157. // pars = append(pars, startDate)
  158. //}
  159. //if endDate != "" {
  160. // condition += " AND data_time<=? "
  161. // pars = append(pars, endDate)
  162. //}
  163. var shiftDay int
  164. switch moveFrequency {
  165. case "天":
  166. shiftDay = formulaInt
  167. case "周":
  168. shiftDay = formulaInt * 7
  169. case "月":
  170. shiftDay = formulaInt * 30
  171. case "季":
  172. shiftDay = formulaInt * 90
  173. case "年":
  174. shiftDay = formulaInt * 365
  175. default:
  176. shiftDay = formulaInt
  177. }
  178. if moveType == 2 {
  179. shiftDay = -shiftDay
  180. }
  181. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  182. if err != nil {
  183. return err
  184. }
  185. var dateArr []string
  186. dataMap := make(map[string]*EdbInfoSearchData)
  187. for _, v := range dataList {
  188. dateArr = append(dateArr, v.DataTime)
  189. dataMap[v.DataTime] = v
  190. }
  191. fmt.Println("source:", source)
  192. //获取指标所有数据
  193. existDataList := make([]*EdbDataBase, 0)
  194. dataTableName := GetEdbDataTableName(source)
  195. fmt.Println("dataTableName:", dataTableName)
  196. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  197. sql = fmt.Sprintf(sql, dataTableName)
  198. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  199. if err != nil {
  200. return err
  201. }
  202. existDataMap := make(map[string]string)
  203. for _, v := range existDataList {
  204. existDataMap[v.DataTime] = v.Value
  205. }
  206. fmt.Println("existDataMap:", existDataMap)
  207. addSql := ` INSERT INTO edb_data_calculate_time_shift (edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  208. var isAdd bool
  209. existMap := make(map[string]string)
  210. dataLen := len(dataList)
  211. for i := 0; i < dataLen; i++ {
  212. //当期
  213. currentItem := dataList[i]
  214. existKey := edbCode + currentItem.DataTime
  215. if _, ok := existMap[existKey]; !ok {
  216. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  217. newDate := currentDate.AddDate(0, 0, shiftDay)
  218. timestamp := newDate.UnixNano() / 1e6
  219. timestampStr := fmt.Sprintf("%d", timestamp)
  220. valStr := decimal.NewFromFloat(currentItem.Value).String()
  221. if existVal, ok := existDataMap[newDate.Format(utils.FormatDate)]; !ok {
  222. isAdd = true
  223. addSql += GetAddSql(edbInfoIdStr, edbCode, newDate.Format(utils.FormatDate), timestampStr, valStr)
  224. } else {
  225. if existVal != valStr {
  226. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  227. sql = fmt.Sprintf(sql, dataTableName)
  228. _, err = tx.Raw(sql, valStr, edbInfoId, newDate.Format(utils.FormatDate)).Exec()
  229. if err != nil {
  230. return err
  231. }
  232. }
  233. }
  234. }
  235. existMap[existKey] = currentItem.DataTime
  236. }
  237. if isAdd {
  238. addSql = strings.TrimRight(addSql, ",")
  239. _, err = tx.Raw(addSql).Exec()
  240. if err != nil {
  241. return err
  242. }
  243. }
  244. return
  245. }