edb_info_calculate_ljztbpj.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "hongze/hz_crm_api/utils"
  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)
  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, 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)
  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, 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)
  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)
  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, 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, 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)
  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, 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)
  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)
  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. }
  569. // RefreshAllCalculateLjztbpj 刷新所有 累计值同比拼接 数据
  570. func RefreshAllCalculateLjztbpj(edbInfo *EdbInfo) (err error) {
  571. o := orm.NewOrmUsingDB("data")
  572. to, err := o.Begin()
  573. if err != nil {
  574. return
  575. }
  576. defer func() {
  577. if err != nil {
  578. _ = to.Rollback()
  579. } else {
  580. _ = to.Commit()
  581. }
  582. }()
  583. //查询当前指标现有的数据
  584. var condition string
  585. var pars []interface{}
  586. condition += " AND edb_info_id=? "
  587. pars = append(pars, edbInfo.EdbInfoId)
  588. //所有的数据
  589. dataList, err := GetAllEdbDataCalculateLjztbpjByEdbInfoId(edbInfo.EdbInfoId)
  590. if err != nil {
  591. return err
  592. }
  593. //待拼接指标map
  594. pjDataMap := make(map[string]float64) //需要入库的数据
  595. nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新)
  596. //拼接指标的日期切片数据
  597. pjEdbDataTimeList := make([]string, 0)
  598. dataMap := make(map[string]*EdbDataCalculateLjztbpj)
  599. for _, v := range dataList {
  600. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  601. dataMap[v.DataTime] = v
  602. nowEdbDataMap[v.DataTime] = v.Value
  603. }
  604. //查询关联指标信息
  605. var existCondition string
  606. var existPars []interface{}
  607. existCondition += " AND edb_info_id=? "
  608. existPars = append(existPars, edbInfo.EdbInfoId)
  609. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  610. if err != nil {
  611. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  612. return
  613. }
  614. var existItemA, existItemB *EdbInfoCalculateMapping
  615. for _, existItem := range existList {
  616. if existItem.FromTag == "A" {
  617. existItemA = existItem
  618. } else if existItem.FromTag == "B" {
  619. existItemB = existItem
  620. }
  621. }
  622. // 原数据开始计算日期
  623. startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, edbInfo.CalculateFormula, time.Local)
  624. //待拼接指标
  625. {
  626. var condition string
  627. var pars []interface{}
  628. condition += " AND data_time <= ? AND edb_info_id=? "
  629. pars = append(pars, startCalculationDate, existItemA.FromEdbInfoId)
  630. //第一个指标的数据列表
  631. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemA.FromSource, 0)
  632. if tmpErr != nil {
  633. return tmpErr
  634. }
  635. for _, v := range firstDataList {
  636. //时间戳
  637. if edbData, ok := dataMap[v.DataTime]; ok {
  638. if edbData.Value != v.Value {
  639. //更新指标数据
  640. edbData.Value = v.Value
  641. _, err = to.Update(edbData, "Value")
  642. if err != nil {
  643. return
  644. }
  645. //将新的数据存入已入库指标map里面,以便后续计算
  646. nowEdbDataMap[edbData.DataTime] = v.Value
  647. }
  648. }
  649. }
  650. }
  651. //同比值指标map
  652. tbzEdbDataMap := make(map[string]float64)
  653. //同比值日期切片列表
  654. tbzEdbDataTimeList := make([]string, 0)
  655. //同比值指标
  656. {
  657. var condition string
  658. var pars []interface{}
  659. condition += " AND data_time > ? AND edb_info_id = ? "
  660. pars = append(pars, startCalculationDate, existItemB.FromEdbInfoId)
  661. //第二个指标的数据列表
  662. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemB.FromSource, 0)
  663. if tmpErr != nil {
  664. return tmpErr
  665. }
  666. for _, v := range secondDataList {
  667. tbzEdbDataMap[v.DataTime] = v.Value
  668. tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
  669. }
  670. }
  671. sort.Strings(tbzEdbDataTimeList)
  672. // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后)
  673. removeDateList := make([]string, 0)
  674. for nowEdbDate := range nowEdbDataMap {
  675. nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local)
  676. //校验日期 需要 大于 拼接前日期
  677. if startCalculationDate.Before(nowEdbDateTime) {
  678. if _, ok := tbzEdbDataMap[nowEdbDate]; !ok {
  679. // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据
  680. removeDateList = append(removeDateList, nowEdbDate)
  681. }
  682. }
  683. }
  684. //待修改的指标数据map(index:日期,value:值)
  685. updateEdbDataMap := make(map[string]float64)
  686. for _, v := range tbzEdbDataTimeList {
  687. tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  688. //获取拼接指标上一年同一天的数据
  689. var pjDataTime time.Time
  690. if tbzDataTime.Month() == 2 {
  691. pjDataTime = tbzDataTime.AddDate(0, -11, 0)
  692. pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
  693. } else {
  694. pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
  695. }
  696. //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致
  697. if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas {
  698. //获取去年今日的数据,获取到后,然后是去修改该日期的数据
  699. if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  700. if v == "2021-08-31" {
  701. fmt.Println("进来了")
  702. }
  703. tbzDataValue := tbzEdbDataMap[v] //同比值
  704. currValue := lastYearEdbDataValue * (1 + tbzDataValue/100)
  705. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数
  706. //如果计算出来的值与库里面的值不匹配,那么就去修改该值
  707. if nowEdbDataValue != currValue {
  708. //将计算后的数据存入待拼接指标map里面,以便后续计算
  709. updateEdbDataMap[v] = currValue
  710. }
  711. }
  712. } else {
  713. //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理
  714. //如果去年今日存在该数据,那么就去计算当前的数据
  715. if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  716. tbzDataValue := tbzEdbDataMap[v] //同比值
  717. currValue := pjDataValue * (1 + tbzDataValue/100)
  718. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
  719. //将计算后的数据存入已入库指标map里面,以便后续计算
  720. nowEdbDataMap[v] = currValue
  721. //将计算后的数据存入待拼接指标map里面,以便后续入库
  722. pjDataMap[v] = currValue
  723. pjEdbDataTimeList = append(pjEdbDataTimeList, v)
  724. }
  725. }
  726. }
  727. //新增的数据入库
  728. {
  729. addDataList := make([]*EdbDataCalculateLjztbpj, 0)
  730. for dataTime, dataValue := range pjDataMap {
  731. //时间戳
  732. currentDate, _ := time.Parse(utils.FormatDate, dataTime)
  733. timestamp := currentDate.UnixNano() / 1e6
  734. edbDataLjztbpj := &EdbDataCalculateLjztbpj{
  735. EdbInfoId: edbInfo.EdbInfoId,
  736. EdbCode: edbInfo.EdbCode,
  737. DataTime: dataTime,
  738. Value: dataValue,
  739. Status: 1,
  740. CreateTime: time.Now(),
  741. ModifyTime: time.Now(),
  742. DataTimestamp: timestamp,
  743. }
  744. addDataList = append(addDataList, edbDataLjztbpj)
  745. }
  746. tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0)
  747. for _, v := range addDataList {
  748. tmpAddDataList = append(tmpAddDataList, v)
  749. if len(tmpAddDataList) >= 200 {
  750. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  751. if tmpErr != nil {
  752. err = tmpErr
  753. return
  754. }
  755. //重新初始化需要加入的数据切片
  756. tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0)
  757. }
  758. }
  759. //最后如果还有需要新增的数据,那么就统一入库
  760. if len(tmpAddDataList) > 0 {
  761. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  762. if tmpErr != nil {
  763. err = tmpErr
  764. return
  765. }
  766. }
  767. }
  768. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  769. {
  770. if len(removeDateList) > 0 {
  771. removeDateStr := strings.Join(removeDateList, `","`)
  772. removeDateStr = `"` + removeDateStr + `"`
  773. //如果拼接指标变更了,那么需要删除所有的指标数据
  774. tableName := GetEdbDataTableName(edbInfo.Source)
  775. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  776. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  777. if err != nil {
  778. err = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error())
  779. return
  780. }
  781. }
  782. }
  783. //修改现有的数据中对应的值
  784. {
  785. tableName := GetEdbDataTableName(edbInfo.Source)
  786. for edbDate, edbDataValue := range updateEdbDataMap {
  787. sql := fmt.Sprintf(` UPDATE %s set value = ?,modify_time=now() WHERE edb_info_id = ? and data_time = ? `, tableName)
  788. _, err = to.Raw(sql, edbDataValue, edbInfo.EdbInfoId, edbDate).Exec()
  789. if err != nil {
  790. err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error())
  791. return
  792. }
  793. }
  794. }
  795. return
  796. }