edb_info_calculate_zjpj.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_api/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // EditCalculateZjpj 编辑直接拼接数据
  9. func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, nowEdbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (edbInfoId int, err error) {
  10. edbInfoId = req.EdbInfoId
  11. o := orm.NewOrmUsingDB("data")
  12. to, err := o.Begin()
  13. if err != nil {
  14. return
  15. }
  16. defer func() {
  17. if err != nil {
  18. _ = to.Rollback()
  19. } else {
  20. _ = to.Commit()
  21. }
  22. }()
  23. //修改指标信息
  24. sql := ` UPDATE edb_info SET
  25. edb_name =?,
  26. edb_name_source=?,
  27. frequency = ?,
  28. unit = ?,
  29. classify_id = ?,
  30. calculate_formula=?,
  31. modify_time = NOW()
  32. WHERE edb_info_id = ? `
  33. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, req.Formula, edbInfoId).Exec()
  34. if err != nil {
  35. return
  36. }
  37. var existCondition string
  38. var existPars []interface{}
  39. existCondition += " AND edb_info_id=? "
  40. existPars = append(existPars, edbInfoId)
  41. //查询出所有的关联指标
  42. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  43. if err != nil {
  44. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  45. return
  46. }
  47. var existItemA, existItemB *EdbInfoCalculateMapping
  48. for _, existItem := range existList {
  49. if existItem.FromTag == "A" {
  50. existItemA = existItem
  51. } else if existItem.FromTag == "B" {
  52. existItemB = existItem
  53. }
  54. }
  55. addDataList := make([]*EdbDataCalculateZjpj, 0)
  56. firstDataList := make([]*EdbInfoSearchData, 0)
  57. secondDataList := make([]*EdbInfoSearchData, 0)
  58. //如果 之前的拼接日期 与 现在的拼接日期 不一致的话,需要做以下处理
  59. nowFormulaDate, _ := time.ParseInLocation(utils.FormatDate, nowEdbInfo.CalculateFormula, time.Local)
  60. reqFormulaDate, _ := time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  61. //如果前后选择的日期不一致,那么需要删除一部分数据
  62. if nowEdbInfo.CalculateFormula != req.Formula {
  63. var startDate, endDate time.Time
  64. //如果当前选择的日期 小于 之前选择的日期
  65. if reqFormulaDate.Before(nowFormulaDate) {
  66. startDate = reqFormulaDate
  67. endDate = nowFormulaDate
  68. } else { //如果当前选择的日期 大于 之前选择的日期
  69. startDate = nowFormulaDate
  70. endDate = reqFormulaDate
  71. }
  72. //删除 之前日期 与 当前日期 之间的指标数据
  73. sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time >= ? and data_time < ?`
  74. _, err = to.Raw(sql, edbInfoId, startDate, endDate).Exec()
  75. if err != nil {
  76. err = errors.New("删除 之前日期 与 当前日期 之间的指标数据失败,Err:" + err.Error())
  77. return
  78. }
  79. }
  80. //第一个指标数据
  81. {
  82. var condition string
  83. var pars []interface{}
  84. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  85. //删除之前的A指标关联关系
  86. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  87. _, err = to.Raw(sql, edbInfoId, existItemA.FromEdbInfoId).Exec()
  88. if err != nil {
  89. err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  90. return
  91. }
  92. //删除之前所有的A指标数据
  93. sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time < ?`
  94. _, err = to.Raw(sql, edbInfoId, req.Formula).Exec()
  95. if err != nil {
  96. err = errors.New("删除拼接日期之前的数据失败,Err:" + err.Error())
  97. return
  98. }
  99. //获取第一个指标的数据列表
  100. condition += " AND data_time < ? AND edb_info_id=? "
  101. pars = append(pars, req.Formula, firstEdbInfo.EdbInfoId)
  102. tmpFirstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource, 0)
  103. if tmpErr != nil {
  104. return edbInfoId, tmpErr
  105. }
  106. firstDataList = tmpFirstDataList
  107. //添加新的指标关系
  108. {
  109. calculateMappingItem := new(EdbInfoCalculateMapping)
  110. calculateMappingItem.CreateTime = time.Now()
  111. calculateMappingItem.ModifyTime = time.Now()
  112. calculateMappingItem.Sort = 1
  113. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  114. calculateMappingItem.EdbInfoId = edbInfoId
  115. calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
  116. calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
  117. calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
  118. calculateMappingItem.FromSource = firstEdbInfo.Source
  119. calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
  120. calculateMappingItem.FromTag = "A"
  121. calculateMappingItem.Source = nowEdbInfo.Source
  122. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  123. _, err = to.Insert(calculateMappingItem)
  124. if err != nil {
  125. return
  126. }
  127. }
  128. } else {
  129. if req.Formula != nowEdbInfo.CalculateFormula {
  130. //获取第一个指标的数据列表
  131. condition += " AND data_time >= ? AND data_time < ? AND edb_info_id=? "
  132. pars = append(pars, nowFormulaDate, reqFormulaDate, firstEdbInfo.EdbInfoId)
  133. tmpFirstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource, 0)
  134. if tmpErr != nil {
  135. return edbInfoId, tmpErr
  136. }
  137. firstDataList = tmpFirstDataList
  138. }
  139. }
  140. //待插入数据
  141. for _, v := range firstDataList {
  142. //时间戳
  143. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  144. timestamp := currentDate.UnixNano() / 1e6
  145. edbDataZjpj := &EdbDataCalculateZjpj{
  146. EdbInfoId: edbInfoId,
  147. EdbCode: nowEdbInfo.EdbCode,
  148. DataTime: v.DataTime,
  149. Value: v.Value,
  150. Status: 1,
  151. CreateTime: time.Now(),
  152. ModifyTime: time.Now(),
  153. DataTimestamp: timestamp,
  154. }
  155. addDataList = append(addDataList, edbDataZjpj)
  156. }
  157. }
  158. //第二个指标数据
  159. {
  160. var condition string
  161. var pars []interface{}
  162. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  163. //删除之前的B指标关联关系
  164. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  165. _, err = to.Raw(sql, edbInfoId, existItemB.FromEdbInfoId).Exec()
  166. if err != nil {
  167. err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  168. return
  169. }
  170. //删除历史拼接日期之前所有的B指标数据
  171. sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time >= ?`
  172. _, err = to.Raw(sql, edbInfoId, nowEdbInfo.CalculateFormula).Exec()
  173. if err != nil {
  174. err = errors.New("删除历史拼接日期之后的数据失败,Err:" + err.Error())
  175. return
  176. }
  177. //第二个指标的数据列表
  178. condition = " AND data_time >= ? AND edb_info_id=? "
  179. pars = append(pars, reqFormulaDate, secondEdbInfo.EdbInfoId)
  180. tmpSecondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, secondEdbInfo.SubSource, 0)
  181. if tmpErr != nil {
  182. return edbInfoId, tmpErr
  183. }
  184. secondDataList = tmpSecondDataList
  185. //添加新的指标关系
  186. {
  187. calculateMappingItem := new(EdbInfoCalculateMapping)
  188. calculateMappingItem.CreateTime = time.Now()
  189. calculateMappingItem.ModifyTime = time.Now()
  190. calculateMappingItem.Sort = 1
  191. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  192. calculateMappingItem.EdbInfoId = edbInfoId
  193. calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
  194. calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
  195. calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
  196. calculateMappingItem.FromSource = secondEdbInfo.Source
  197. calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
  198. calculateMappingItem.FromTag = "B"
  199. calculateMappingItem.Source = nowEdbInfo.Source
  200. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  201. _, err = to.Insert(calculateMappingItem)
  202. if err != nil {
  203. return
  204. }
  205. }
  206. } else {
  207. if req.Formula != nowEdbInfo.CalculateFormula {
  208. //获取第二个指标的数据列表
  209. condition += " AND data_time >= ? AND data_time < ? AND edb_info_id=? "
  210. pars = append(pars, reqFormulaDate, nowFormulaDate, secondEdbInfo.EdbInfoId)
  211. tmpSecondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, secondEdbInfo.SubSource, 0)
  212. if tmpErr != nil {
  213. return edbInfoId, tmpErr
  214. }
  215. secondDataList = tmpSecondDataList
  216. }
  217. }
  218. //待插入数据
  219. for _, v := range secondDataList {
  220. //时间戳
  221. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  222. timestamp := currentDate.UnixNano() / 1e6
  223. edbDataZjpj := &EdbDataCalculateZjpj{
  224. EdbInfoId: edbInfoId,
  225. EdbCode: nowEdbInfo.EdbCode,
  226. DataTime: v.DataTime,
  227. Value: v.Value,
  228. Status: 1,
  229. CreateTime: time.Now(),
  230. ModifyTime: time.Now(),
  231. DataTimestamp: timestamp,
  232. }
  233. addDataList = append(addDataList, edbDataZjpj)
  234. }
  235. }
  236. //数据入库
  237. tmpAddDataList := make([]*EdbDataCalculateZjpj, 0)
  238. for _, v := range addDataList {
  239. tmpAddDataList = append(tmpAddDataList, v)
  240. if len(tmpAddDataList) >= 200 {
  241. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  242. if tmpErr != nil {
  243. err = tmpErr
  244. return
  245. }
  246. //重新初始化需要加入的数据切片
  247. tmpAddDataList = make([]*EdbDataCalculateZjpj, 0)
  248. }
  249. }
  250. //最后如果还有需要新增的数据,那么就统一入库
  251. if len(tmpAddDataList) > 0 {
  252. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  253. if tmpErr != nil {
  254. err = tmpErr
  255. return
  256. }
  257. }
  258. return
  259. }