edb_data_calculate_zjpj.go 16 KB

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