edb_data_calculate_zjpj.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // EdbDataCalculateZjpj 直接拼接数据结构体
  10. type EdbDataCalculateZjpj struct {
  11. EdbDataId int `orm:"column(edb_data_id);pk"`
  12. EdbInfoId int
  13. EdbCode string
  14. DataTime string
  15. Value float64
  16. Status int
  17. CreateTime time.Time
  18. ModifyTime time.Time
  19. DataTimestamp int64
  20. }
  21. // AddCalculateZjpj 新增直接拼接数据
  22. func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
  23. o := orm.NewOrm()
  24. to, err := o.Begin()
  25. if err != nil {
  26. return
  27. }
  28. defer func() {
  29. if err != nil {
  30. fmt.Println("AddCalculateZjpj,Err:" + err.Error())
  31. _ = to.Rollback()
  32. } else {
  33. _ = to.Commit()
  34. }
  35. }()
  36. if req.EdbInfoId <= 0 {
  37. edbInfo = &EdbInfo{
  38. SourceName: "直接拼接",
  39. Source: utils.DATA_SOURCE_CALCULATE_ZJPJ,
  40. EdbCode: edbCode,
  41. EdbName: req.EdbName,
  42. EdbNameSource: req.EdbName,
  43. Frequency: req.Frequency,
  44. Unit: req.Unit,
  45. StartDate: firstEdbInfo.StartDate,
  46. EndDate: firstEdbInfo.EndDate,
  47. ClassifyId: req.ClassifyId,
  48. SysUserId: sysUserId,
  49. SysUserRealName: sysUserRealName,
  50. UniqueCode: uniqueCode,
  51. CreateTime: time.Now(),
  52. ModifyTime: time.Now(),
  53. CalculateFormula: req.Formula,
  54. EdbNameEn: req.EdbName,
  55. UnitEn: req.Unit,
  56. EdbType: 2,
  57. Sort: GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.EDB_INFO_TYPE),
  58. }
  59. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  60. if tmpErr != nil {
  61. err = tmpErr
  62. return
  63. }
  64. edbInfo.EdbInfoId = int(newEdbInfoId)
  65. } else {
  66. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  67. if err != nil {
  68. return
  69. }
  70. //查询
  71. tmpEdbInfo, tmpErr := GetEdbInfoById(edbInfo.EdbInfoId)
  72. if tmpErr != nil {
  73. err = tmpErr
  74. return
  75. }
  76. tmpEdbInfo.EdbName = req.EdbName
  77. tmpEdbInfo.ClassifyId = req.ClassifyId
  78. tmpEdbInfo.Frequency = req.Frequency
  79. tmpEdbInfo.Unit = req.Unit
  80. tmpEdbInfo.CalculateFormula = req.Formula
  81. edbInfo = tmpEdbInfo
  82. //删除指标数据
  83. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SUB_SOURCE_EDB)
  84. fmt.Println("dataTableName:" + dataTableName)
  85. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  86. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  87. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  88. if err != nil {
  89. return
  90. }
  91. //删除指标关系
  92. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  93. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  94. }
  95. //关联关系
  96. var existItemA, existItemB *EdbInfoCalculateMapping
  97. //第一个指标
  98. {
  99. existItemA = &EdbInfoCalculateMapping{
  100. EdbInfoCalculateMappingId: 0,
  101. EdbInfoId: edbInfo.EdbInfoId,
  102. Source: edbInfo.Source,
  103. SourceName: edbInfo.SourceName,
  104. EdbCode: edbInfo.EdbCode,
  105. FromEdbInfoId: firstEdbInfo.EdbInfoId,
  106. FromEdbCode: firstEdbInfo.EdbCode,
  107. FromEdbName: firstEdbInfo.EdbName,
  108. FromSource: firstEdbInfo.Source,
  109. FromSourceName: firstEdbInfo.SourceName,
  110. FromTag: "A",
  111. Sort: 1,
  112. CreateTime: time.Now(),
  113. ModifyTime: time.Now(),
  114. FromSubSource: firstEdbInfo.SubSource,
  115. }
  116. insertId, tmpErr := to.Insert(existItemA)
  117. if tmpErr != nil {
  118. err = tmpErr
  119. return
  120. }
  121. existItemA.EdbInfoCalculateMappingId = int(insertId)
  122. }
  123. //第二个指标
  124. {
  125. existItemB = &EdbInfoCalculateMapping{
  126. EdbInfoCalculateMappingId: 0,
  127. EdbInfoId: edbInfo.EdbInfoId,
  128. Source: edbInfo.Source,
  129. SourceName: edbInfo.SourceName,
  130. EdbCode: edbInfo.EdbCode,
  131. FromEdbInfoId: secondEdbInfo.EdbInfoId,
  132. FromEdbCode: secondEdbInfo.EdbCode,
  133. FromEdbName: secondEdbInfo.EdbName,
  134. FromSource: secondEdbInfo.Source,
  135. FromSourceName: secondEdbInfo.SourceName,
  136. FromTag: "B",
  137. Sort: 1,
  138. CreateTime: time.Now(),
  139. ModifyTime: time.Now(),
  140. FromSubSource: secondEdbInfo.SubSource,
  141. }
  142. insertId, tmpErr := to.Insert(existItemB)
  143. if tmpErr != nil {
  144. err = tmpErr
  145. return
  146. }
  147. existItemB.EdbInfoCalculateMappingId = int(insertId)
  148. }
  149. //拼接数据
  150. err = refreshAllCalculateZjpj(to, edbInfo, existItemA, existItemB)
  151. return
  152. }
  153. // EditCalculateZjpj 编辑直接拼接数据
  154. func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (err error) {
  155. o := orm.NewOrm()
  156. to, err := o.Begin()
  157. if err != nil {
  158. return
  159. }
  160. defer func() {
  161. if err != nil {
  162. fmt.Println("EditCalculateZjpj,Err:" + err.Error())
  163. _ = to.Rollback()
  164. } else {
  165. _ = to.Commit()
  166. }
  167. }()
  168. nowEdbInfo := *edbInfo // 现在的指标信息
  169. //修改指标信息
  170. edbInfo.EdbName = req.EdbName
  171. edbInfo.EdbNameSource = req.EdbName
  172. edbInfo.Frequency = req.Frequency
  173. edbInfo.Unit = req.Unit
  174. edbInfo.ClassifyId = req.ClassifyId
  175. edbInfo.CalculateFormula = req.Formula
  176. edbInfo.EdbNameEn = req.EdbNameEn
  177. edbInfo.UnitEn = req.UnitEn
  178. edbInfo.ModifyTime = time.Now()
  179. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn")
  180. if err != nil {
  181. return
  182. }
  183. var existCondition string
  184. var existPars []interface{}
  185. existCondition += " AND edb_info_id=? "
  186. existPars = append(existPars, edbInfo.EdbInfoId)
  187. //查询出所有的关联指标
  188. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  189. if err != nil {
  190. err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error())
  191. return
  192. }
  193. var existItemA, existItemB *EdbInfoCalculateMapping
  194. for _, existItem := range existList {
  195. if existItem.FromTag == "A" {
  196. existItemA = existItem
  197. } else if existItem.FromTag == "B" {
  198. existItemB = existItem
  199. }
  200. }
  201. // 是否需要删除数据重新计算
  202. isNeedCalculateData := false
  203. // 如果截止日期变更,那么需要重新计算
  204. if req.Formula != nowEdbInfo.CalculateFormula {
  205. isNeedCalculateData = true
  206. }
  207. //第一个指标数据
  208. {
  209. // 如果指标变了,那么需要删除关系,并重新计算
  210. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  211. //删除之前的A指标关联关系
  212. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  213. _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemA.FromEdbInfoId).Exec()
  214. if err != nil {
  215. err = fmt.Errorf("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  216. return
  217. }
  218. //添加新的指标关系
  219. {
  220. existItemA = &EdbInfoCalculateMapping{
  221. EdbInfoCalculateMappingId: 0,
  222. EdbInfoId: edbInfo.EdbInfoId,
  223. Source: edbInfo.Source,
  224. SourceName: edbInfo.SourceName,
  225. EdbCode: edbInfo.EdbCode,
  226. FromEdbInfoId: firstEdbInfo.EdbInfoId,
  227. FromEdbCode: firstEdbInfo.EdbCode,
  228. FromEdbName: firstEdbInfo.EdbName,
  229. FromSource: firstEdbInfo.Source,
  230. FromSourceName: firstEdbInfo.SourceName,
  231. FromTag: "A",
  232. Sort: 1,
  233. CreateTime: time.Now(),
  234. ModifyTime: time.Now(),
  235. FromSubSource: firstEdbInfo.SubSource,
  236. }
  237. insertId, tmpErr := to.Insert(existItemA)
  238. if tmpErr != nil {
  239. err = tmpErr
  240. return
  241. }
  242. existItemA.EdbInfoCalculateMappingId = int(insertId)
  243. isNeedCalculateData = true
  244. }
  245. }
  246. }
  247. //第二个指标数据
  248. {
  249. // 如果指标变了,那么需要删除关系,并重新计算
  250. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  251. //删除之前的B指标关联关系
  252. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  253. _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemB.FromEdbInfoId).Exec()
  254. if err != nil {
  255. err = fmt.Errorf("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  256. return
  257. }
  258. // 添加新的指标关联关系
  259. existItemB = &EdbInfoCalculateMapping{
  260. EdbInfoCalculateMappingId: 0,
  261. EdbInfoId: edbInfo.EdbInfoId,
  262. Source: edbInfo.Source,
  263. SourceName: edbInfo.SourceName,
  264. EdbCode: edbInfo.EdbCode,
  265. FromEdbInfoId: secondEdbInfo.EdbInfoId,
  266. FromEdbCode: secondEdbInfo.EdbCode,
  267. FromEdbName: secondEdbInfo.EdbName,
  268. FromSource: secondEdbInfo.Source,
  269. FromSourceName: secondEdbInfo.SourceName,
  270. FromTag: "B",
  271. Sort: 2,
  272. CreateTime: time.Now(),
  273. ModifyTime: time.Now(),
  274. FromSubSource: secondEdbInfo.SubSource,
  275. }
  276. insertId, tmpErr := to.Insert(existItemB)
  277. if tmpErr != nil {
  278. err = tmpErr
  279. return
  280. }
  281. existItemB.EdbInfoCalculateMappingId = int(insertId)
  282. isNeedCalculateData = true
  283. }
  284. }
  285. // 如果需要重新计算,那么先删除所有的指标数据,然后再重新计算
  286. if isNeedCalculateData {
  287. // 删除之前所有的指标数据
  288. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  289. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
  290. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  291. if err != nil {
  292. err = fmt.Errorf("删除历史数据失败,Err:" + err.Error())
  293. return
  294. }
  295. err = refreshAllCalculateZjpj(to, edbInfo, existItemA, existItemB)
  296. }
  297. return
  298. }
  299. // GetAllEdbDataCalculateZjpjByEdbInfoId 根据指标id获取全部的数据
  300. func GetAllEdbDataCalculateZjpjByEdbInfoId(edbInfoId int) (items []*EdbDataCalculateZjpj, err error) {
  301. o := orm.NewOrm()
  302. sql := ` SELECT * FROM edb_data_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC `
  303. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  304. return
  305. }
  306. // RefreshAllCalculateZjpj 刷新所有 直接拼接 数据
  307. func RefreshAllCalculateZjpj(edbInfo *EdbInfo) (err error) {
  308. o := orm.NewOrm()
  309. to, err := o.Begin()
  310. defer func() {
  311. if err != nil {
  312. fmt.Println("RefreshAllCalculateZjpj,Err:" + err.Error())
  313. _ = to.Rollback()
  314. } else {
  315. _ = to.Commit()
  316. }
  317. }()
  318. //查询关联指标信息
  319. var existCondition string
  320. var existPars []interface{}
  321. existCondition += " AND edb_info_id=? "
  322. existPars = append(existPars, edbInfo.EdbInfoId)
  323. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  324. if err != nil {
  325. err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error())
  326. return
  327. }
  328. var existItemA, existItemB *EdbInfoCalculateMapping
  329. for _, existItem := range existList {
  330. if existItem.FromTag == "A" {
  331. existItemA = existItem
  332. } else if existItem.FromTag == "B" {
  333. existItemB = existItem
  334. }
  335. }
  336. // 刷新数据
  337. err = refreshAllCalculateZjpj(to, edbInfo, existItemA, existItemB)
  338. return
  339. }
  340. // refreshAllCalculateZjpj 刷新所有 直接拼接 数据
  341. func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, existItemB *EdbInfoCalculateMapping) (err error) {
  342. //查询当前指标现有的数据
  343. var condition string
  344. var pars []interface{}
  345. condition += " AND edb_info_id=? "
  346. pars = append(pars, edbInfo.EdbInfoId)
  347. var dataList []*EdbDataCalculateZjpj
  348. sql := ` SELECT * FROM edb_data_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC `
  349. _, err = to.Raw(sql, edbInfo.EdbInfoId).QueryRows(&dataList)
  350. if err != nil {
  351. return err
  352. }
  353. var dateArr []string
  354. dataMap := make(map[string]*EdbDataCalculateZjpj)
  355. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  356. for _, v := range dataList {
  357. dateArr = append(dateArr, v.DataTime)
  358. dataMap[v.DataTime] = v
  359. removeDataTimeMap[v.DataTime] = 1
  360. }
  361. addDataList := make([]*EdbDataCalculateZjpj, 0)
  362. //第一个指标
  363. {
  364. //第一个指标的数据列表
  365. firstDataList, tmpErr := GetEdbDataListAllByTo(to, existItemA.FromSource, existItemA.FromSubSource, FindEdbDataListAllCond{
  366. EdbInfoId: existItemA.FromEdbInfoId,
  367. EndDataTime: edbInfo.CalculateFormula,
  368. EndDataTimeCond: "<",
  369. }, 0)
  370. if tmpErr != nil {
  371. return tmpErr
  372. }
  373. for _, v := range firstDataList {
  374. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  375. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  376. delete(removeDataTimeMap, v.DataTime)
  377. }
  378. //时间戳
  379. if edbData, ok := dataMap[v.DataTime]; ok {
  380. if edbData.Value != v.Value {
  381. //更新指标数据
  382. edbData.Value = v.Value
  383. _, _ = to.Update(edbData, "Value")
  384. }
  385. } else {
  386. //时间戳
  387. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  388. timestamp := currentDate.UnixNano() / 1e6
  389. edbDataZjpj := &EdbDataCalculateZjpj{
  390. EdbInfoId: edbInfo.EdbInfoId,
  391. EdbCode: edbInfo.EdbCode,
  392. DataTime: v.DataTime,
  393. Value: v.Value,
  394. Status: 1,
  395. CreateTime: time.Now(),
  396. ModifyTime: time.Now(),
  397. DataTimestamp: timestamp,
  398. }
  399. addDataList = append(addDataList, edbDataZjpj)
  400. }
  401. }
  402. }
  403. //第二个指标
  404. {
  405. //第二个指标的数据列表
  406. secondDataList, tmpErr := GetEdbDataListAllByTo(to, existItemB.FromSource, existItemB.FromSubSource, FindEdbDataListAllCond{
  407. EdbInfoId: existItemB.FromEdbInfoId,
  408. StartDataTime: edbInfo.CalculateFormula,
  409. StartDataTimeCond: ">=",
  410. }, 0)
  411. if tmpErr != nil {
  412. return tmpErr
  413. }
  414. for _, v := range secondDataList {
  415. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  416. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  417. delete(removeDataTimeMap, v.DataTime)
  418. }
  419. if edbData, ok := dataMap[v.DataTime]; ok {
  420. if edbData.Value != v.Value {
  421. //更新指标数据
  422. edbData.Value = v.Value
  423. edbData.ModifyTime = time.Now()
  424. _, tmpErr := to.Update(edbData, "Value", "ModifyTime")
  425. if tmpErr != nil {
  426. fmt.Println("tmpErr:", tmpErr)
  427. }
  428. }
  429. } else {
  430. //时间戳
  431. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  432. timestamp := currentDate.UnixNano() / 1e6
  433. edbDataZjpj := &EdbDataCalculateZjpj{
  434. EdbInfoId: edbInfo.EdbInfoId,
  435. EdbCode: edbInfo.EdbCode,
  436. DataTime: v.DataTime,
  437. Value: v.Value,
  438. Status: 1,
  439. CreateTime: time.Now(),
  440. ModifyTime: time.Now(),
  441. DataTimestamp: timestamp,
  442. }
  443. addDataList = append(addDataList, edbDataZjpj)
  444. }
  445. }
  446. }
  447. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  448. {
  449. removeDateList := make([]string, 0)
  450. for dateTime := range removeDataTimeMap {
  451. removeDateList = append(removeDateList, dateTime)
  452. }
  453. if len(removeDateList) > 0 {
  454. removeDateStr := strings.Join(removeDateList, `","`)
  455. removeDateStr = `"` + removeDateStr + `"`
  456. //如果拼接指标变更了,那么需要删除所有的指标数据
  457. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  458. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  459. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  460. if err != nil {
  461. err = fmt.Errorf("删除不存在的直接拼接指标数据失败,Err:" + err.Error())
  462. return
  463. }
  464. }
  465. }
  466. //数据入库
  467. if len(addDataList) > 0 {
  468. tmpAddDataList := make([]*EdbDataCalculateZjpj, 0)
  469. i := 0
  470. for _, v := range addDataList {
  471. tmpAddDataList = append(tmpAddDataList, v)
  472. i++
  473. if i >= 500 {
  474. _, err = to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  475. if err != nil {
  476. return
  477. }
  478. i = 0
  479. tmpAddDataList = make([]*EdbDataCalculateZjpj, 0)
  480. }
  481. }
  482. if len(tmpAddDataList) > 0 {
  483. _, err = to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  484. if err != nil {
  485. return
  486. }
  487. }
  488. }
  489. return
  490. }