edb_data_calculate_time_shift.go 7.4 KB

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