edb_info_calculate_zjpj.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "eta/eta_api/utils"
  7. "strings"
  8. "time"
  9. )
  10. // AddCalculateZjpj 新增直接拼接数据
  11. func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  12. fmt.Println("AddCalculateZjpj")
  13. o := orm.NewOrmUsingDB("data")
  14. to, err := o.Begin()
  15. if err != nil {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("AddCalculateZjpj,Err:" + err.Error())
  21. _ = to.Rollback()
  22. } else {
  23. _ = to.Commit()
  24. }
  25. }()
  26. var edbInfo *EdbInfo
  27. if req.EdbInfoId <= 0 {
  28. edbInfo = &EdbInfo{
  29. SourceName: "直接拼接",
  30. Source: utils.DATA_SOURCE_CALCULATE_ZJPJ,
  31. EdbCode: edbCode,
  32. EdbName: req.EdbName,
  33. EdbNameSource: req.EdbName,
  34. Frequency: req.Frequency,
  35. Unit: req.Unit,
  36. StartDate: firstEdbInfo.StartDate,
  37. EndDate: firstEdbInfo.EndDate,
  38. ClassifyId: req.ClassifyId,
  39. SysUserId: sysUserId,
  40. SysUserRealName: sysUserRealName,
  41. UniqueCode: uniqueCode,
  42. CreateTime: time.Now(),
  43. ModifyTime: time.Now(),
  44. CalculateFormula: req.Formula,
  45. EdbType: 2,
  46. }
  47. newEdbInfoId, err := to.Insert(edbInfo)
  48. if err != nil {
  49. return edbInfoId, err
  50. }
  51. edbInfoId = int(newEdbInfoId)
  52. } else {
  53. edbInfoId = req.EdbInfoId
  54. //查询
  55. tmpEdbInfo, tmpErr := GetEdbInfoById(edbInfoId)
  56. if tmpErr != nil {
  57. err = tmpErr
  58. return
  59. }
  60. tmpEdbInfo.EdbName = req.EdbName
  61. tmpEdbInfo.ClassifyId = req.ClassifyId
  62. tmpEdbInfo.Frequency = req.Frequency
  63. tmpEdbInfo.Unit = req.Unit
  64. tmpEdbInfo.CalculateFormula = req.Formula
  65. edbInfo = tmpEdbInfo
  66. //删除指标数据
  67. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_ZJPJ)
  68. fmt.Println("dataTableName:" + dataTableName)
  69. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  70. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  71. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  72. if err != nil {
  73. return 0, err
  74. }
  75. //删除指标关系
  76. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  77. _, err = to.Raw(sql, edbInfoId).Exec()
  78. if err != nil {
  79. return
  80. }
  81. }
  82. //关联关系
  83. //第一个指标
  84. {
  85. calculateMappingItem := new(EdbInfoCalculateMapping)
  86. calculateMappingItem.CreateTime = time.Now()
  87. calculateMappingItem.ModifyTime = time.Now()
  88. calculateMappingItem.Sort = 1
  89. calculateMappingItem.EdbCode = edbCode
  90. calculateMappingItem.EdbInfoId = edbInfoId
  91. calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
  92. calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
  93. calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
  94. calculateMappingItem.FromSource = firstEdbInfo.Source
  95. calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
  96. calculateMappingItem.FromTag = "A"
  97. calculateMappingItem.Source = edbInfo.Source
  98. calculateMappingItem.SourceName = edbInfo.SourceName
  99. _, err = to.Insert(calculateMappingItem)
  100. if err != nil {
  101. return
  102. }
  103. }
  104. //第二个指标
  105. {
  106. calculateMappingItem := new(EdbInfoCalculateMapping)
  107. calculateMappingItem.CreateTime = time.Now()
  108. calculateMappingItem.ModifyTime = time.Now()
  109. calculateMappingItem.Sort = 1
  110. calculateMappingItem.EdbCode = edbCode
  111. calculateMappingItem.EdbInfoId = edbInfoId
  112. calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
  113. calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
  114. calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
  115. calculateMappingItem.FromSource = secondEdbInfo.Source
  116. calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
  117. calculateMappingItem.FromTag = "B"
  118. calculateMappingItem.Source = edbInfo.Source
  119. calculateMappingItem.SourceName = edbInfo.SourceName
  120. _, err = to.Insert(calculateMappingItem)
  121. if err != nil {
  122. return
  123. }
  124. }
  125. addDataList := make([]*EdbDataCalculateZjpj, 0)
  126. //拼接数据
  127. //第一个指标
  128. {
  129. var condition string
  130. var pars []interface{}
  131. condition += " AND data_time < ? AND edb_info_id=? "
  132. pars = append(pars, req.Formula, firstEdbInfo.EdbInfoId)
  133. //第一个指标的数据列表
  134. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, 0)
  135. if tmpErr != nil {
  136. return edbInfoId, tmpErr
  137. }
  138. for _, v := range firstDataList {
  139. //时间戳
  140. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  141. timestamp := currentDate.UnixNano() / 1e6
  142. edbDataZjpj := &EdbDataCalculateZjpj{
  143. EdbInfoId: edbInfoId,
  144. EdbCode: edbInfo.EdbCode,
  145. DataTime: v.DataTime,
  146. Value: v.Value,
  147. Status: 1,
  148. CreateTime: time.Now(),
  149. ModifyTime: time.Now(),
  150. DataTimestamp: timestamp,
  151. }
  152. addDataList = append(addDataList, edbDataZjpj)
  153. if len(addDataList) >= 200 {
  154. _, tmpErr := to.InsertMulti(len(addDataList), addDataList)
  155. if tmpErr != nil {
  156. err = tmpErr
  157. return
  158. }
  159. //重新初始化需要加入的数据切片
  160. addDataList = make([]*EdbDataCalculateZjpj, 0)
  161. }
  162. }
  163. }
  164. //第二个指标
  165. {
  166. var condition string
  167. var pars []interface{}
  168. condition += " AND data_time >= ? AND edb_info_id = ? "
  169. pars = append(pars, req.Formula, secondEdbInfo.EdbInfoId)
  170. //第二个指标的数据列表
  171. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, 0)
  172. if tmpErr != nil {
  173. return edbInfoId, tmpErr
  174. }
  175. for _, v := range secondDataList {
  176. //时间戳
  177. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  178. timestamp := currentDate.UnixNano() / 1e6
  179. edbDataZjpj := &EdbDataCalculateZjpj{
  180. EdbInfoId: edbInfoId,
  181. EdbCode: edbInfo.EdbCode,
  182. DataTime: v.DataTime,
  183. Value: v.Value,
  184. Status: 1,
  185. CreateTime: time.Now(),
  186. ModifyTime: time.Now(),
  187. DataTimestamp: timestamp,
  188. }
  189. addDataList = append(addDataList, edbDataZjpj)
  190. if len(addDataList) >= 200 {
  191. _, tmpErr := to.InsertMulti(len(addDataList), addDataList)
  192. if tmpErr != nil {
  193. err = tmpErr
  194. return
  195. }
  196. //重新初始化需要加入的数据切片
  197. addDataList = make([]*EdbDataCalculateZjpj, 0)
  198. }
  199. }
  200. }
  201. //将剩余的数据入库
  202. if len(addDataList) > 0 {
  203. _, tmpErr := to.InsertMulti(len(addDataList), addDataList)
  204. if tmpErr != nil {
  205. err = tmpErr
  206. return
  207. }
  208. }
  209. return
  210. }
  211. // EditCalculateZjpj 编辑直接拼接数据
  212. func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, nowEdbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (edbInfoId int, err error) {
  213. edbInfoId = req.EdbInfoId
  214. o := orm.NewOrmUsingDB("data")
  215. to, err := o.Begin()
  216. if err != nil {
  217. return
  218. }
  219. defer func() {
  220. if err != nil {
  221. _ = to.Rollback()
  222. } else {
  223. _ = to.Commit()
  224. }
  225. }()
  226. //修改指标信息
  227. sql := ` UPDATE edb_info SET
  228. edb_name =?,
  229. edb_name_source=?,
  230. frequency = ?,
  231. unit = ?,
  232. classify_id = ?,
  233. calculate_formula=?,
  234. modify_time = NOW()
  235. WHERE edb_info_id = ? `
  236. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, req.Formula, edbInfoId).Exec()
  237. if err != nil {
  238. return
  239. }
  240. var existCondition string
  241. var existPars []interface{}
  242. existCondition += " AND edb_info_id=? "
  243. existPars = append(existPars, edbInfoId)
  244. //查询出所有的关联指标
  245. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  246. if err != nil {
  247. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  248. return
  249. }
  250. var existItemA, existItemB *EdbInfoCalculateMapping
  251. for _, existItem := range existList {
  252. if existItem.FromTag == "A" {
  253. existItemA = existItem
  254. } else if existItem.FromTag == "B" {
  255. existItemB = existItem
  256. }
  257. }
  258. addDataList := make([]*EdbDataCalculateZjpj, 0)
  259. firstDataList := make([]*EdbInfoSearchData, 0)
  260. secondDataList := make([]*EdbInfoSearchData, 0)
  261. //如果 之前的拼接日期 与 现在的拼接日期 不一致的话,需要做以下处理
  262. nowFormulaDate, _ := time.ParseInLocation(utils.FormatDate, nowEdbInfo.CalculateFormula, time.Local)
  263. reqFormulaDate, _ := time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  264. //如果前后选择的日期不一致,那么需要删除一部分数据
  265. if nowEdbInfo.CalculateFormula != req.Formula {
  266. var startDate, endDate time.Time
  267. //如果当前选择的日期 小于 之前选择的日期
  268. if reqFormulaDate.Before(nowFormulaDate) {
  269. startDate = reqFormulaDate
  270. endDate = nowFormulaDate
  271. } else { //如果当前选择的日期 大于 之前选择的日期
  272. startDate = nowFormulaDate
  273. endDate = reqFormulaDate
  274. }
  275. //删除 之前日期 与 当前日期 之间的指标数据
  276. sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time >= ? and data_time < ?`
  277. _, err = to.Raw(sql, edbInfoId, startDate, endDate).Exec()
  278. if err != nil {
  279. err = errors.New("删除 之前日期 与 当前日期 之间的指标数据失败,Err:" + err.Error())
  280. return
  281. }
  282. }
  283. //第一个指标数据
  284. {
  285. var condition string
  286. var pars []interface{}
  287. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  288. //删除之前的A指标关联关系
  289. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  290. _, err = to.Raw(sql, edbInfoId, existItemA.FromEdbInfoId).Exec()
  291. if err != nil {
  292. err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  293. return
  294. }
  295. //删除之前所有的A指标数据
  296. sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time < ?`
  297. _, err = to.Raw(sql, edbInfoId, req.Formula).Exec()
  298. if err != nil {
  299. err = errors.New("删除拼接日期之前的数据失败,Err:" + err.Error())
  300. return
  301. }
  302. //获取第一个指标的数据列表
  303. condition += " AND data_time < ? AND edb_info_id=? "
  304. pars = append(pars, req.Formula, firstEdbInfo.EdbInfoId)
  305. tmpFirstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, 0)
  306. if tmpErr != nil {
  307. return edbInfoId, tmpErr
  308. }
  309. firstDataList = tmpFirstDataList
  310. //添加新的指标关系
  311. {
  312. calculateMappingItem := new(EdbInfoCalculateMapping)
  313. calculateMappingItem.CreateTime = time.Now()
  314. calculateMappingItem.ModifyTime = time.Now()
  315. calculateMappingItem.Sort = 1
  316. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  317. calculateMappingItem.EdbInfoId = edbInfoId
  318. calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
  319. calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
  320. calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
  321. calculateMappingItem.FromSource = firstEdbInfo.Source
  322. calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
  323. calculateMappingItem.FromTag = "A"
  324. calculateMappingItem.Source = nowEdbInfo.Source
  325. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  326. _, err = to.Insert(calculateMappingItem)
  327. if err != nil {
  328. return
  329. }
  330. }
  331. } else {
  332. if req.Formula != nowEdbInfo.CalculateFormula {
  333. //获取第一个指标的数据列表
  334. condition += " AND data_time >= ? AND data_time < ? AND edb_info_id=? "
  335. pars = append(pars, nowFormulaDate, reqFormulaDate, firstEdbInfo.EdbInfoId)
  336. tmpFirstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, 0)
  337. if tmpErr != nil {
  338. return edbInfoId, tmpErr
  339. }
  340. firstDataList = tmpFirstDataList
  341. }
  342. }
  343. //待插入数据
  344. for _, v := range firstDataList {
  345. //时间戳
  346. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  347. timestamp := currentDate.UnixNano() / 1e6
  348. edbDataZjpj := &EdbDataCalculateZjpj{
  349. EdbInfoId: edbInfoId,
  350. EdbCode: nowEdbInfo.EdbCode,
  351. DataTime: v.DataTime,
  352. Value: v.Value,
  353. Status: 1,
  354. CreateTime: time.Now(),
  355. ModifyTime: time.Now(),
  356. DataTimestamp: timestamp,
  357. }
  358. addDataList = append(addDataList, edbDataZjpj)
  359. }
  360. }
  361. //第二个指标数据
  362. {
  363. var condition string
  364. var pars []interface{}
  365. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  366. //删除之前的B指标关联关系
  367. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  368. _, err = to.Raw(sql, edbInfoId, existItemB.FromEdbInfoId).Exec()
  369. if err != nil {
  370. err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  371. return
  372. }
  373. //删除历史拼接日期之前所有的B指标数据
  374. sql = ` DELETE FROM edb_data_calculate_zjpj WHERE edb_info_id = ? and data_time >= ?`
  375. _, err = to.Raw(sql, edbInfoId, nowEdbInfo.CalculateFormula).Exec()
  376. if err != nil {
  377. err = errors.New("删除历史拼接日期之后的数据失败,Err:" + err.Error())
  378. return
  379. }
  380. //第二个指标的数据列表
  381. condition = " AND data_time >= ? AND edb_info_id=? "
  382. pars = append(pars, reqFormulaDate, secondEdbInfo.EdbInfoId)
  383. tmpSecondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, 0)
  384. if tmpErr != nil {
  385. return edbInfoId, tmpErr
  386. }
  387. secondDataList = tmpSecondDataList
  388. //添加新的指标关系
  389. {
  390. calculateMappingItem := new(EdbInfoCalculateMapping)
  391. calculateMappingItem.CreateTime = time.Now()
  392. calculateMappingItem.ModifyTime = time.Now()
  393. calculateMappingItem.Sort = 1
  394. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  395. calculateMappingItem.EdbInfoId = edbInfoId
  396. calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
  397. calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
  398. calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
  399. calculateMappingItem.FromSource = secondEdbInfo.Source
  400. calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
  401. calculateMappingItem.FromTag = "B"
  402. calculateMappingItem.Source = nowEdbInfo.Source
  403. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  404. _, err = to.Insert(calculateMappingItem)
  405. if err != nil {
  406. return
  407. }
  408. }
  409. } else {
  410. if req.Formula != nowEdbInfo.CalculateFormula {
  411. //获取第二个指标的数据列表
  412. condition += " AND data_time >= ? AND data_time < ? AND edb_info_id=? "
  413. pars = append(pars, reqFormulaDate, nowFormulaDate, secondEdbInfo.EdbInfoId)
  414. tmpSecondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, 0)
  415. if tmpErr != nil {
  416. return edbInfoId, tmpErr
  417. }
  418. secondDataList = tmpSecondDataList
  419. }
  420. }
  421. //待插入数据
  422. for _, v := range secondDataList {
  423. //时间戳
  424. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  425. timestamp := currentDate.UnixNano() / 1e6
  426. edbDataZjpj := &EdbDataCalculateZjpj{
  427. EdbInfoId: edbInfoId,
  428. EdbCode: nowEdbInfo.EdbCode,
  429. DataTime: v.DataTime,
  430. Value: v.Value,
  431. Status: 1,
  432. CreateTime: time.Now(),
  433. ModifyTime: time.Now(),
  434. DataTimestamp: timestamp,
  435. }
  436. addDataList = append(addDataList, edbDataZjpj)
  437. }
  438. }
  439. //数据入库
  440. tmpAddDataList := make([]*EdbDataCalculateZjpj, 0)
  441. for _, v := range addDataList {
  442. tmpAddDataList = append(tmpAddDataList, v)
  443. if len(tmpAddDataList) >= 200 {
  444. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  445. if tmpErr != nil {
  446. err = tmpErr
  447. return
  448. }
  449. //重新初始化需要加入的数据切片
  450. tmpAddDataList = make([]*EdbDataCalculateZjpj, 0)
  451. }
  452. }
  453. //最后如果还有需要新增的数据,那么就统一入库
  454. if len(tmpAddDataList) > 0 {
  455. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  456. if tmpErr != nil {
  457. err = tmpErr
  458. return
  459. }
  460. }
  461. return
  462. }
  463. // RefreshAllCalculateZjpj 刷新所有 直接拼接 数据
  464. func RefreshAllCalculateZjpj(edbInfo *EdbInfo) (err error) {
  465. o := orm.NewOrmUsingDB("data")
  466. to, err := o.Begin()
  467. if err != nil {
  468. return
  469. }
  470. defer func() {
  471. if err != nil {
  472. _ = to.Rollback()
  473. } else {
  474. _ = to.Commit()
  475. }
  476. }()
  477. //查询当前指标现有的数据
  478. var condition string
  479. var pars []interface{}
  480. condition += " AND edb_info_id=? "
  481. pars = append(pars, edbInfo.EdbInfoId)
  482. dataList, err := GetAllEdbDataCalculateZjpjByEdbInfoId(edbInfo.EdbInfoId)
  483. if err != nil {
  484. return err
  485. }
  486. var dateArr []string
  487. dataMap := make(map[string]*EdbDataCalculateZjpj)
  488. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  489. for _, v := range dataList {
  490. dateArr = append(dateArr, v.DataTime)
  491. dataMap[v.DataTime] = v
  492. removeDataTimeMap[v.DataTime] = 1
  493. }
  494. //查询关联指标信息
  495. var existCondition string
  496. var existPars []interface{}
  497. existCondition += " AND edb_info_id=? "
  498. existPars = append(existPars, edbInfo.EdbInfoId)
  499. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  500. if err != nil {
  501. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  502. return
  503. }
  504. var existItemA, existItemB *EdbInfoCalculateMapping
  505. for _, existItem := range existList {
  506. if existItem.FromTag == "A" {
  507. existItemA = existItem
  508. } else if existItem.FromTag == "B" {
  509. existItemB = existItem
  510. }
  511. }
  512. addDataList := make([]*EdbDataCalculateZjpj, 0)
  513. //第一个指标
  514. {
  515. var condition string
  516. var pars []interface{}
  517. condition += " AND data_time < ? AND edb_info_id=? "
  518. pars = append(pars, edbInfo.CalculateFormula, existItemA.FromEdbInfoId)
  519. //第一个指标的数据列表
  520. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemA.FromSource, 0)
  521. if tmpErr != nil {
  522. return tmpErr
  523. }
  524. for _, v := range firstDataList {
  525. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  526. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  527. delete(removeDataTimeMap, v.DataTime)
  528. }
  529. //时间戳
  530. if edbData, ok := dataMap[v.DataTime]; ok {
  531. if edbData.Value != v.Value {
  532. //更新指标数据
  533. edbData.Value = v.Value
  534. _, err = to.Update(edbData, "Value")
  535. if err != nil {
  536. return
  537. }
  538. }
  539. } else {
  540. //时间戳
  541. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  542. timestamp := currentDate.UnixNano() / 1e6
  543. edbDataZjpj := &EdbDataCalculateZjpj{
  544. EdbInfoId: edbInfo.EdbInfoId,
  545. EdbCode: edbInfo.EdbCode,
  546. DataTime: v.DataTime,
  547. Value: v.Value,
  548. Status: 1,
  549. CreateTime: time.Now(),
  550. ModifyTime: time.Now(),
  551. DataTimestamp: timestamp,
  552. }
  553. addDataList = append(addDataList, edbDataZjpj)
  554. }
  555. }
  556. }
  557. //第二个指标
  558. {
  559. condition = ``
  560. pars = make([]interface{}, 0)
  561. condition += " AND data_time >= ? AND edb_info_id = ? "
  562. pars = append(pars, edbInfo.CalculateFormula, existItemB.FromEdbInfoId)
  563. //第二个指标的数据列表
  564. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemB.FromSource, 0)
  565. if tmpErr != nil {
  566. return tmpErr
  567. }
  568. for _, v := range secondDataList {
  569. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  570. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  571. delete(removeDataTimeMap, v.DataTime)
  572. }
  573. if edbData, ok := dataMap[v.DataTime]; ok {
  574. if edbData.Value != v.Value {
  575. //更新指标数据
  576. edbData.Value = v.Value
  577. edbData.ModifyTime = time.Now()
  578. _, tmpErr := to.Update(edbData, "Value", "ModifyTime")
  579. if tmpErr != nil {
  580. fmt.Println("tmpErr:", tmpErr)
  581. }
  582. }
  583. } else {
  584. //时间戳
  585. currentDate, _ := time.Parse(utils.FormatDate, v.DataTime)
  586. timestamp := currentDate.UnixNano() / 1e6
  587. edbDataZjpj := &EdbDataCalculateZjpj{
  588. EdbInfoId: edbInfo.EdbInfoId,
  589. EdbCode: edbInfo.EdbCode,
  590. DataTime: v.DataTime,
  591. Value: v.Value,
  592. Status: 1,
  593. CreateTime: time.Now(),
  594. ModifyTime: time.Now(),
  595. DataTimestamp: timestamp,
  596. }
  597. addDataList = append(addDataList, edbDataZjpj)
  598. }
  599. }
  600. }
  601. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  602. {
  603. removeDateList := make([]string, 0)
  604. for dateTime := range removeDataTimeMap {
  605. removeDateList = append(removeDateList, dateTime)
  606. }
  607. if len(removeDateList) > 0 {
  608. removeDateStr := strings.Join(removeDateList, `","`)
  609. removeDateStr = `"` + removeDateStr + `"`
  610. //如果拼接指标变更了,那么需要删除所有的指标数据
  611. tableName := GetEdbDataTableName(edbInfo.Source)
  612. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  613. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  614. if err != nil {
  615. err = errors.New("删除不存在的直接拼接指标数据失败,Err:" + err.Error())
  616. return
  617. }
  618. }
  619. }
  620. //数据入库
  621. tmpAddDataList := make([]*EdbDataCalculateZjpj, 0)
  622. for _, v := range addDataList {
  623. tmpAddDataList = append(tmpAddDataList, v)
  624. if len(tmpAddDataList) >= 200 {
  625. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  626. if tmpErr != nil {
  627. err = tmpErr
  628. return
  629. }
  630. //重新初始化需要加入的数据切片
  631. tmpAddDataList = make([]*EdbDataCalculateZjpj, 0)
  632. }
  633. }
  634. //最后如果还有需要新增的数据,那么就统一入库
  635. if len(tmpAddDataList) > 0 {
  636. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  637. if tmpErr != nil {
  638. err = tmpErr
  639. return
  640. }
  641. }
  642. return
  643. }