edb_info_calculate_zjpj.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_task/utils"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strings"
  8. "time"
  9. )
  10. type EdbInfoCalculateZjpjDetail struct {
  11. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_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. StartDate string `description:"开始日期"`
  24. EndDate string `description:"结束日期"`
  25. }
  26. // RefreshAllCalculateZjpj 刷新所有 直接拼接 数据
  27. func RefreshAllCalculateZjpj(edbInfo *EdbInfo) (err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. tx,err:=o.Begin()
  30. if err!=nil {
  31. return err
  32. }
  33. defer func() {
  34. if err != nil {
  35. tx.Rollback()
  36. } else {
  37. tx.Commit()
  38. }
  39. }()
  40. //查询当前指标现有的数据
  41. var condition string
  42. var pars []interface{}
  43. condition += " AND edb_info_id=? "
  44. pars = append(pars, edbInfo.EdbInfoId)
  45. dataList, err := GetAllEdbDataCalculateZjpjByEdbInfoId(edbInfo.EdbInfoId)
  46. if err != nil {
  47. return err
  48. }
  49. var dateArr []string
  50. dataMap := make(map[string]*EdbDataCalculateZjpj)
  51. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  52. for _, v := range dataList {
  53. dateArr = append(dateArr, v.DataTime)
  54. dataMap[v.DataTime] = v
  55. removeDataTimeMap[v.DataTime] = 1
  56. }
  57. //查询关联指标信息
  58. var existCondition string
  59. var existPars []interface{}
  60. existCondition += " AND edb_info_id=? "
  61. existPars = append(existPars, edbInfo.EdbInfoId)
  62. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  63. if err != nil {
  64. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  65. return
  66. }
  67. var existItemA, existItemB *EdbInfoCalculateMapping
  68. for _, existItem := range existList {
  69. if existItem.FromTag == "A" {
  70. existItemA = existItem
  71. } else if existItem.FromTag == "B" {
  72. existItemB = existItem
  73. }
  74. }
  75. addDataList := make([]*EdbDataCalculateZjpj, 0)
  76. //第一个指标
  77. {
  78. var condition string
  79. var pars []interface{}
  80. condition += " AND data_time < ? AND edb_info_id=? "
  81. pars = append(pars, edbInfo.CalculateFormula, existItemA.FromEdbInfoId)
  82. //第一个指标的数据列表
  83. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemA.FromSource, 0)
  84. if tmpErr != nil {
  85. return tmpErr
  86. }
  87. for _, v := range firstDataList {
  88. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  89. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  90. delete(removeDataTimeMap, v.DataTime)
  91. }
  92. //时间戳
  93. if edbData, ok := dataMap[v.DataTime]; ok {
  94. if edbData.Value != v.Value {
  95. //更新指标数据
  96. edbData.Value = v.Value
  97. o.Update(edbData, "Value")
  98. }
  99. } else {
  100. //时间戳
  101. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  102. timestamp := currentDate.UnixNano() / 1e6
  103. edbDataZjpj := &EdbDataCalculateZjpj{
  104. EdbInfoId: edbInfo.EdbInfoId,
  105. EdbCode: edbInfo.EdbCode,
  106. DataTime: v.DataTime,
  107. Value: v.Value,
  108. Status: 1,
  109. CreateTime: time.Now(),
  110. ModifyTime: time.Now(),
  111. DataTimestamp: timestamp,
  112. }
  113. addDataList = append(addDataList, edbDataZjpj)
  114. }
  115. }
  116. }
  117. //第二个指标
  118. {
  119. condition = ``
  120. pars = make([]interface{}, 0)
  121. condition += " AND data_time >= ? AND edb_info_id = ? "
  122. pars = append(pars, edbInfo.CalculateFormula, existItemB.FromEdbInfoId)
  123. //第二个指标的数据列表
  124. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemB.FromSource, 0)
  125. if tmpErr != nil {
  126. return tmpErr
  127. }
  128. for _, v := range secondDataList {
  129. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  130. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  131. delete(removeDataTimeMap, v.DataTime)
  132. }
  133. if edbData, ok := dataMap[v.DataTime]; ok {
  134. if edbData.Value != v.Value {
  135. //更新指标数据
  136. edbData.Value = v.Value
  137. edbData.ModifyTime = time.Now()
  138. _, tmpErr := o.Update(edbData, "Value", "ModifyTime")
  139. if tmpErr != nil {
  140. fmt.Println("tmpErr:", tmpErr)
  141. }
  142. }
  143. } else {
  144. //时间戳
  145. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  146. timestamp := currentDate.UnixNano() / 1e6
  147. edbDataZjpj := &EdbDataCalculateZjpj{
  148. EdbInfoId: edbInfo.EdbInfoId,
  149. EdbCode: edbInfo.EdbCode,
  150. DataTime: v.DataTime,
  151. Value: v.Value,
  152. Status: 1,
  153. CreateTime: time.Now(),
  154. ModifyTime: time.Now(),
  155. DataTimestamp: timestamp,
  156. }
  157. addDataList = append(addDataList, edbDataZjpj)
  158. }
  159. }
  160. }
  161. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  162. {
  163. removeDateList := make([]string, 0)
  164. for dateTime := range removeDataTimeMap {
  165. removeDateList = append(removeDateList, dateTime)
  166. }
  167. if len(removeDateList) > 0 {
  168. removeDateStr := strings.Join(removeDateList, `","`)
  169. removeDateStr = `"` + removeDateStr + `"`
  170. //如果拼接指标变更了,那么需要删除所有的指标数据
  171. tableName := GetEdbDataTableName(edbInfo.Source)
  172. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  173. _, err = o.Raw(sql, edbInfo.EdbInfoId).Exec()
  174. if err != nil {
  175. err = errors.New("删除不存在的直接拼接指标数据失败,Err:" + err.Error())
  176. return
  177. }
  178. }
  179. }
  180. //数据入库
  181. tmpAddDataList := make([]*EdbDataCalculateZjpj, 0)
  182. for _, v := range addDataList {
  183. tmpAddDataList = append(tmpAddDataList, v)
  184. if len(tmpAddDataList) >= 200 {
  185. _, tmpErr := o.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  186. if tmpErr != nil {
  187. err = tmpErr
  188. return
  189. }
  190. //重新初始化需要加入的数据切片
  191. tmpAddDataList = make([]*EdbDataCalculateZjpj, 0)
  192. }
  193. }
  194. //最后如果还有需要新增的数据,那么就统一入库
  195. if len(tmpAddDataList) > 0 {
  196. _, tmpErr := tx.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  197. if tmpErr != nil {
  198. err = tmpErr
  199. return
  200. }
  201. }
  202. return
  203. }