edb_info_calculate_ljztbpj.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. // AddCalculateLjztbpj 新增累计值同比拼接数据
  13. func AddCalculateLjztbpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  14. fmt.Println("AddCalculateLjztbpj")
  15. o := orm.NewOrmUsingDB("data")
  16. to, err := o.Begin()
  17. if err != nil {
  18. return
  19. }
  20. defer func() {
  21. if err != nil {
  22. fmt.Println("AddCalculateLjztbpj,Err:" + err.Error())
  23. _ = to.Rollback()
  24. } else {
  25. _ = to.Commit()
  26. }
  27. }()
  28. //待拼接指标map
  29. pjDataMap := make(map[string]float64)
  30. //拼接指标的日期切片数据
  31. pjEdbDataTimeList := make([]string, 0)
  32. //最近开始的时间
  33. var lastDateTime time.Time
  34. //获取待拼接指标
  35. {
  36. var condition string
  37. var pars []interface{}
  38. //获取待拼接指标最近的个12月31日有值的年份
  39. condition += " AND data_time like ? AND edb_info_id=? "
  40. pars = append(pars, "%12-31", firstEdbInfo.EdbInfoId)
  41. lastEdbData, tmpErr := GetLastEdbData(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource)
  42. if tmpErr != nil {
  43. return edbInfoId, tmpErr
  44. }
  45. lastDateTime, _ = time.ParseInLocation(utils.FormatDate, lastEdbData.DataTime, time.Local)
  46. //获取待拼接指标的数据列表
  47. condition = ``
  48. pars = make([]interface{}, 0)
  49. condition += " AND data_time <= ? AND edb_info_id=? "
  50. pars = append(pars, lastEdbData.DataTime, firstEdbInfo.EdbInfoId)
  51. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource, 0)
  52. if tmpErr != nil {
  53. return edbInfoId, tmpErr
  54. }
  55. for _, v := range firstDataList {
  56. pjDataMap[v.DataTime] = v.Value
  57. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  58. }
  59. }
  60. var edbInfo *EdbInfo
  61. if req.EdbInfoId <= 0 {
  62. edbInfo = &EdbInfo{
  63. SourceName: "累计值同比拼接",
  64. Source: utils.DATA_SOURCE_CALCULATE_LJZTBPJ,
  65. EdbCode: edbCode,
  66. EdbName: req.EdbName,
  67. EdbNameSource: req.EdbName,
  68. Frequency: req.Frequency,
  69. Unit: req.Unit,
  70. StartDate: firstEdbInfo.StartDate,
  71. EndDate: firstEdbInfo.EndDate,
  72. ClassifyId: req.ClassifyId,
  73. SysUserId: sysUserId,
  74. SysUserRealName: sysUserRealName,
  75. UniqueCode: uniqueCode,
  76. CreateTime: time.Now(),
  77. ModifyTime: time.Now(),
  78. CalculateFormula: lastDateTime.Format(utils.FormatDate),
  79. EdbType: 2,
  80. }
  81. newEdbInfoId, err := to.Insert(edbInfo)
  82. if err != nil {
  83. return edbInfoId, err
  84. }
  85. edbInfoId = int(newEdbInfoId)
  86. } else {
  87. edbInfoId = req.EdbInfoId
  88. //查询
  89. tmpEdbInfo, tmpErr := GetEdbInfoById(edbInfoId)
  90. if tmpErr != nil {
  91. err = tmpErr
  92. return
  93. }
  94. tmpEdbInfo.EdbName = req.EdbName
  95. tmpEdbInfo.ClassifyId = req.ClassifyId
  96. tmpEdbInfo.Frequency = req.Frequency
  97. tmpEdbInfo.Unit = req.Unit
  98. tmpEdbInfo.CalculateFormula = req.Formula
  99. edbInfo = tmpEdbInfo
  100. //删除指标数据
  101. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SUB_SOURCE_EDB)
  102. fmt.Println("dataTableName:" + dataTableName)
  103. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  104. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  105. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  106. if err != nil {
  107. return 0, err
  108. }
  109. //删除指标关系
  110. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  111. _, err = to.Raw(sql, edbInfoId).Exec()
  112. }
  113. //关联关系
  114. //拼接指标
  115. {
  116. calculateMappingItem := new(EdbInfoCalculateMapping)
  117. calculateMappingItem.CreateTime = time.Now()
  118. calculateMappingItem.ModifyTime = time.Now()
  119. calculateMappingItem.Sort = 1
  120. calculateMappingItem.EdbCode = edbCode
  121. calculateMappingItem.EdbInfoId = edbInfoId
  122. calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
  123. calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
  124. calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
  125. calculateMappingItem.FromSource = firstEdbInfo.Source
  126. calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
  127. calculateMappingItem.FromTag = "A"
  128. calculateMappingItem.Source = edbInfo.Source
  129. calculateMappingItem.SourceName = edbInfo.SourceName
  130. _, err = to.Insert(calculateMappingItem)
  131. if err != nil {
  132. return
  133. }
  134. }
  135. //同比值指标
  136. {
  137. calculateMappingItem := new(EdbInfoCalculateMapping)
  138. calculateMappingItem.CreateTime = time.Now()
  139. calculateMappingItem.ModifyTime = time.Now()
  140. calculateMappingItem.Sort = 1
  141. calculateMappingItem.EdbCode = edbCode
  142. calculateMappingItem.EdbInfoId = edbInfoId
  143. calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
  144. calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
  145. calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
  146. calculateMappingItem.FromSource = secondEdbInfo.Source
  147. calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
  148. calculateMappingItem.FromTag = "B"
  149. calculateMappingItem.Source = edbInfo.Source
  150. calculateMappingItem.SourceName = edbInfo.SourceName
  151. _, err = to.Insert(calculateMappingItem)
  152. if err != nil {
  153. return
  154. }
  155. }
  156. //同比值指标map
  157. tbzEdbDataMap := make(map[string]float64)
  158. //同比值日期切片列表
  159. tbzEdbDataTimeList := make([]string, 0)
  160. //同比值指标
  161. {
  162. var condition string
  163. var pars []interface{}
  164. condition += " AND data_time > ? AND edb_info_id = ? "
  165. pars = append(pars, lastDateTime, secondEdbInfo.EdbInfoId)
  166. //同比值指标的数据列表
  167. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, secondEdbInfo.SubSource, 0)
  168. if tmpErr != nil {
  169. return edbInfoId, tmpErr
  170. }
  171. for _, v := range secondDataList {
  172. tbzEdbDataMap[v.DataTime] = v.Value
  173. tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
  174. }
  175. }
  176. sort.Strings(tbzEdbDataTimeList)
  177. for _, v := range tbzEdbDataTimeList {
  178. tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  179. //获取拼接指标上一年同一天的数据
  180. var pjDataTime time.Time
  181. if tbzDataTime.Month() == 2 {
  182. pjDataTime = tbzDataTime.AddDate(0, -11, 0)
  183. pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
  184. } else {
  185. pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
  186. }
  187. //如果存在数据,那么就去计算当前的数据
  188. if pjDataValue, ok := pjDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  189. tbzDataValue := tbzEdbDataMap[v] //同比值
  190. currValue := pjDataValue * (1 + tbzDataValue/100)
  191. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
  192. //将计算后的数据存入待拼接指标map里面,以便后续计算
  193. pjDataMap[v] = currValue
  194. pjEdbDataTimeList = append(pjEdbDataTimeList, v)
  195. }
  196. }
  197. addDataList := make([]*EdbDataCalculateLjztbpj, 0)
  198. //日期排序下
  199. sort.Strings(pjEdbDataTimeList)
  200. //这么做的目的是为了让数据插入的时候,可以正序插入(业务上没啥卵用,就是为了让我看数据的时候舒服点,手动狗头-_-|)
  201. for _, dataTime := range pjEdbDataTimeList {
  202. if dataValue, ok := pjDataMap[dataTime]; ok {
  203. //时间戳
  204. currentDate, _ := time.Parse(utils.FormatDate, dataTime)
  205. timestamp := currentDate.UnixNano() / 1e6
  206. edbDataLjztbpj := &EdbDataCalculateLjztbpj{
  207. EdbInfoId: edbInfoId,
  208. EdbCode: edbInfo.EdbCode,
  209. DataTime: dataTime,
  210. Value: dataValue,
  211. Status: 1,
  212. CreateTime: time.Now(),
  213. ModifyTime: time.Now(),
  214. DataTimestamp: timestamp,
  215. }
  216. addDataList = append(addDataList, edbDataLjztbpj)
  217. }
  218. }
  219. //数据入库
  220. tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0)
  221. for _, v := range addDataList {
  222. tmpAddDataList = append(tmpAddDataList, v)
  223. if len(tmpAddDataList) >= 200 {
  224. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  225. if tmpErr != nil {
  226. err = tmpErr
  227. return
  228. }
  229. //重新初始化需要加入的数据切片
  230. tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0)
  231. }
  232. }
  233. //最后如果还有需要新增的数据,那么就统一入库
  234. if len(tmpAddDataList) > 0 {
  235. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  236. if tmpErr != nil {
  237. err = tmpErr
  238. return
  239. }
  240. }
  241. return
  242. }
  243. // EditCalculateLjztbpj 编辑累计值同比拼接数据
  244. func EditCalculateLjztbpj(req *EdbInfoCalculateBatchEditReq, nowEdbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (edbInfoId int, err error) {
  245. edbInfoId = req.EdbInfoId
  246. o := orm.NewOrmUsingDB("data")
  247. to, err := o.Begin()
  248. if err != nil {
  249. return
  250. }
  251. defer func() {
  252. if err != nil {
  253. _ = to.Rollback()
  254. } else {
  255. _ = to.Commit()
  256. }
  257. }()
  258. sql := ``
  259. var existCondition string
  260. var existPars []interface{}
  261. existCondition += " AND edb_info_id=? "
  262. existPars = append(existPars, edbInfoId)
  263. //查询出所有的关联指标
  264. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  265. if err != nil {
  266. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  267. return
  268. }
  269. var existItemA, existItemB *EdbInfoCalculateMapping
  270. for _, existItem := range existList {
  271. if existItem.FromTag == "A" {
  272. existItemA = existItem
  273. } else if existItem.FromTag == "B" {
  274. existItemB = existItem
  275. }
  276. }
  277. // 原数据开始计算日期
  278. startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, nowEdbInfo.CalculateFormula, time.Local)
  279. //待拼接指标map
  280. pjDataMap := make(map[string]float64) //需要入库的数据
  281. nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新)
  282. //拼接指标的日期切片数据
  283. pjEdbDataTimeList := make([]string, 0)
  284. //最近开始的时间
  285. lastDateTime := startCalculationDate
  286. //待拼接指标数据
  287. {
  288. //如果拼接指标变更了,那么需要删除所有的指标进行重新拼接
  289. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  290. //删除旧的指标数据
  291. {
  292. //删除之前的A指标关联关系
  293. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  294. _, err = to.Raw(sql, edbInfoId, existItemA.FromEdbInfoId).Exec()
  295. if err != nil {
  296. err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  297. return
  298. }
  299. //如果拼接指标变更了,那么需要删除所有的指标数据
  300. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  301. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
  302. _, err = to.Raw(sql, edbInfoId).Exec()
  303. if err != nil {
  304. err = errors.New("删除所有的累计值同比拼接指标数据失败,Err:" + err.Error())
  305. return
  306. }
  307. }
  308. //添加新的指标关系
  309. {
  310. calculateMappingItem := new(EdbInfoCalculateMapping)
  311. calculateMappingItem.CreateTime = time.Now()
  312. calculateMappingItem.ModifyTime = time.Now()
  313. calculateMappingItem.Sort = 1
  314. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  315. calculateMappingItem.EdbInfoId = edbInfoId
  316. calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
  317. calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
  318. calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
  319. calculateMappingItem.FromSource = firstEdbInfo.Source
  320. calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
  321. calculateMappingItem.FromTag = "A"
  322. calculateMappingItem.Source = nowEdbInfo.Source
  323. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  324. _, err = to.Insert(calculateMappingItem)
  325. if err != nil {
  326. return
  327. }
  328. }
  329. var condition string
  330. var pars []interface{}
  331. //获取待拼接指标最近的个12月31日有值的年份
  332. condition += " AND data_time like ? AND edb_info_id=? "
  333. pars = append(pars, "%12-31", firstEdbInfo.EdbInfoId)
  334. //获取最新的待拼接指标
  335. lastEdbData, tmpErr := GetLastEdbData(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource)
  336. if tmpErr != nil {
  337. return edbInfoId, tmpErr
  338. }
  339. lastDateTime, _ = time.ParseInLocation(utils.FormatDate, lastEdbData.DataTime, time.Local)
  340. //获取待拼接指标的数据列表
  341. condition = ``
  342. pars = make([]interface{}, 0)
  343. condition += " AND data_time <= ? AND edb_info_id=? "
  344. pars = append(pars, lastEdbData.DataTime, firstEdbInfo.EdbInfoId)
  345. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource, 0)
  346. if tmpErr != nil {
  347. return edbInfoId, tmpErr
  348. }
  349. for _, v := range firstDataList {
  350. pjDataMap[v.DataTime] = v.Value
  351. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  352. //将新的数据存入已入库指标map里面,以便后续计算
  353. nowEdbDataMap[v.DataTime] = v.Value
  354. }
  355. } else {
  356. // 获取当前指标的所有数据
  357. var condition string
  358. var pars []interface{}
  359. condition += " AND edb_info_id=? and data_time <= ?"
  360. pars = append(pars, nowEdbInfo.EdbInfoId, nowEdbInfo.CalculateFormula)
  361. nowEdbDataList, tmpErr := GetEdbDataListAll(condition, pars, nowEdbInfo.Source, nowEdbInfo.SubSource, 0)
  362. if tmpErr != nil {
  363. return edbInfoId, tmpErr
  364. }
  365. for _, v := range nowEdbDataList {
  366. nowEdbDataMap[v.DataTime] = v.Value
  367. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  368. }
  369. }
  370. }
  371. //同比值指标map
  372. tbzEdbDataMap := make(map[string]float64)
  373. //同比值日期切片列表
  374. tbzEdbDataTimeList := make([]string, 0)
  375. //同比值指标
  376. {
  377. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  378. //删除通过B指标生成的数据
  379. {
  380. //删除之前的B指标关联关系
  381. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  382. _, err = to.Raw(sql, edbInfoId, existItemB.FromEdbInfoId).Exec()
  383. if err != nil {
  384. err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  385. return
  386. }
  387. //如果同比值指标变更了,那么需要删除所有计算出来的指标数据
  388. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  389. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time > ? `, tableName)
  390. _, err = to.Raw(sql, edbInfoId, nowEdbInfo.CalculateFormula).Exec()
  391. if err != nil {
  392. err = errors.New("删除计算出来的累计值同比拼接指标数据失败,Err:" + err.Error())
  393. return
  394. }
  395. }
  396. //添加新的指标关系
  397. {
  398. calculateMappingItem := new(EdbInfoCalculateMapping)
  399. calculateMappingItem.CreateTime = time.Now()
  400. calculateMappingItem.ModifyTime = time.Now()
  401. calculateMappingItem.Sort = 1
  402. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  403. calculateMappingItem.EdbInfoId = edbInfoId
  404. calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
  405. calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
  406. calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
  407. calculateMappingItem.FromSource = secondEdbInfo.Source
  408. calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
  409. calculateMappingItem.FromTag = "B"
  410. calculateMappingItem.Source = nowEdbInfo.Source
  411. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  412. _, err = to.Insert(calculateMappingItem)
  413. if err != nil {
  414. return
  415. }
  416. }
  417. }
  418. var condition string
  419. var pars []interface{}
  420. condition += " AND data_time > ? AND edb_info_id = ? "
  421. pars = append(pars, lastDateTime, secondEdbInfo.EdbInfoId)
  422. //同比值指标的数据列表
  423. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, secondEdbInfo.SubSource, 0)
  424. if tmpErr != nil {
  425. return edbInfoId, tmpErr
  426. }
  427. for _, v := range secondDataList {
  428. tbzEdbDataMap[v.DataTime] = v.Value
  429. tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
  430. }
  431. }
  432. sort.Strings(tbzEdbDataTimeList)
  433. // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后)
  434. removeDateList := make([]string, 0)
  435. for nowEdbDate := range nowEdbDataMap {
  436. nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local)
  437. //校验日期 需要 大于 拼接前日期
  438. if startCalculationDate.Before(nowEdbDateTime) {
  439. if _, ok := tbzEdbDataMap[nowEdbDate]; !ok {
  440. // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据
  441. removeDateList = append(removeDateList, nowEdbDate)
  442. }
  443. }
  444. }
  445. //待修改的指标数据map(index:日期,value:值)
  446. updateEdbDataMap := make(map[string]float64)
  447. for _, v := range tbzEdbDataTimeList {
  448. tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  449. //获取拼接指标上一年同一天的数据
  450. var pjDataTime time.Time
  451. if tbzDataTime.Month() == 2 {
  452. pjDataTime = tbzDataTime.AddDate(0, -11, 0)
  453. pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
  454. } else {
  455. pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
  456. }
  457. //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致
  458. if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas {
  459. //获取去年今日的数据,获取到后,然后是去修改该日期的数据
  460. if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  461. tbzDataValue := tbzEdbDataMap[v] //同比值
  462. currValue := lastYearEdbDataValue * (1 + tbzDataValue/100)
  463. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数
  464. //如果计算出来的值与库里面的值不匹配,那么就去修改该值
  465. if nowEdbDataValue != currValue {
  466. //将计算后的数据存入待拼接指标map里面,以便后续计算
  467. updateEdbDataMap[v] = currValue
  468. }
  469. }
  470. } else {
  471. //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理
  472. //如果去年今日存在该数据,那么就去计算当前的数据
  473. if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  474. tbzDataValue := tbzEdbDataMap[v] //同比值
  475. currValue := pjDataValue * (1 + tbzDataValue/100)
  476. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
  477. //将计算后的数据存入已入库指标map里面,以便后续计算
  478. nowEdbDataMap[v] = currValue
  479. //将计算后的数据存入待拼接指标map里面,以便后续入库
  480. pjDataMap[v] = currValue
  481. pjEdbDataTimeList = append(pjEdbDataTimeList, v)
  482. }
  483. }
  484. }
  485. //新增的数据入库
  486. {
  487. addDataList := make([]*EdbDataCalculateLjztbpj, 0)
  488. for dataTime, dataValue := range pjDataMap {
  489. //时间戳
  490. currentDate, _ := time.Parse(utils.FormatDate, dataTime)
  491. timestamp := currentDate.UnixNano() / 1e6
  492. edbDataLjztbpj := &EdbDataCalculateLjztbpj{
  493. EdbInfoId: edbInfoId,
  494. EdbCode: nowEdbInfo.EdbCode,
  495. DataTime: dataTime,
  496. Value: dataValue,
  497. Status: 1,
  498. CreateTime: time.Now(),
  499. ModifyTime: time.Now(),
  500. DataTimestamp: timestamp,
  501. }
  502. addDataList = append(addDataList, edbDataLjztbpj)
  503. }
  504. tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0)
  505. for _, v := range addDataList {
  506. tmpAddDataList = append(tmpAddDataList, v)
  507. if len(tmpAddDataList) >= 200 {
  508. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  509. if tmpErr != nil {
  510. err = tmpErr
  511. return
  512. }
  513. //重新初始化需要加入的数据切片
  514. tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0)
  515. }
  516. }
  517. //最后如果还有需要新增的数据,那么就统一入库
  518. if len(tmpAddDataList) > 0 {
  519. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  520. if tmpErr != nil {
  521. err = tmpErr
  522. return
  523. }
  524. }
  525. }
  526. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  527. {
  528. if len(removeDateList) > 0 {
  529. removeDateStr := strings.Join(removeDateList, `","`)
  530. removeDateStr = `"` + removeDateStr + `"`
  531. //如果拼接指标变更了,那么需要删除所有的指标数据
  532. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  533. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  534. _, err = to.Raw(sql, edbInfoId).Exec()
  535. if err != nil {
  536. err = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error())
  537. return
  538. }
  539. }
  540. }
  541. //修改现有的数据中对应的值
  542. {
  543. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  544. for edbDate, edbDataValue := range updateEdbDataMap {
  545. sql = fmt.Sprintf(` UPDATE %s set value = %f,modify_time=now() WHERE edb_info_id = ? and data_time = %s `, tableName, edbDataValue, edbDate)
  546. _, err = to.Raw(sql, edbInfoId).Exec()
  547. if err != nil {
  548. err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error())
  549. return
  550. }
  551. }
  552. }
  553. //修改指标信息
  554. sql = ` UPDATE edb_info SET
  555. edb_name =?,
  556. edb_name_source=?,
  557. frequency = ?,
  558. unit = ?,
  559. classify_id = ?,
  560. calculate_formula=?,
  561. modify_time = NOW()
  562. WHERE edb_info_id = ? `
  563. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, lastDateTime.Format(utils.FormatDate), edbInfoId).Exec()
  564. if err != nil {
  565. return
  566. }
  567. return
  568. }