edb_info_calculate_ljztbpj.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. // EditCalculateLjztbpj 编辑累计值同比拼接数据
  13. func EditCalculateLjztbpj(req *EdbInfoCalculateBatchEditReq, nowEdbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (edbInfoId int, err error) {
  14. edbInfoId = req.EdbInfoId
  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. _ = to.Rollback()
  23. } else {
  24. _ = to.Commit()
  25. }
  26. }()
  27. sql := ``
  28. var existCondition string
  29. var existPars []interface{}
  30. existCondition += " AND edb_info_id=? "
  31. existPars = append(existPars, edbInfoId)
  32. //查询出所有的关联指标
  33. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  34. if err != nil {
  35. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  36. return
  37. }
  38. var existItemA, existItemB *EdbInfoCalculateMapping
  39. for _, existItem := range existList {
  40. if existItem.FromTag == "A" {
  41. existItemA = existItem
  42. } else if existItem.FromTag == "B" {
  43. existItemB = existItem
  44. }
  45. }
  46. // 原数据开始计算日期
  47. startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, nowEdbInfo.CalculateFormula, time.Local)
  48. //待拼接指标map
  49. pjDataMap := make(map[string]float64) //需要入库的数据
  50. nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新)
  51. //拼接指标的日期切片数据
  52. pjEdbDataTimeList := make([]string, 0)
  53. //最近开始的时间
  54. lastDateTime := startCalculationDate
  55. //待拼接指标数据
  56. {
  57. //如果拼接指标变更了,那么需要删除所有的指标进行重新拼接
  58. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  59. //删除旧的指标数据
  60. {
  61. //删除之前的A指标关联关系
  62. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  63. _, err = to.Raw(sql, edbInfoId, existItemA.FromEdbInfoId).Exec()
  64. if err != nil {
  65. err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  66. return
  67. }
  68. //如果拼接指标变更了,那么需要删除所有的指标数据
  69. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  70. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
  71. _, err = to.Raw(sql, edbInfoId).Exec()
  72. if err != nil {
  73. err = errors.New("删除所有的累计值同比拼接指标数据失败,Err:" + err.Error())
  74. return
  75. }
  76. }
  77. //添加新的指标关系
  78. {
  79. calculateMappingItem := new(EdbInfoCalculateMapping)
  80. calculateMappingItem.CreateTime = time.Now()
  81. calculateMappingItem.ModifyTime = time.Now()
  82. calculateMappingItem.Sort = 1
  83. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  84. calculateMappingItem.EdbInfoId = edbInfoId
  85. calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
  86. calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
  87. calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
  88. calculateMappingItem.FromSource = firstEdbInfo.Source
  89. calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
  90. calculateMappingItem.FromTag = "A"
  91. calculateMappingItem.Source = nowEdbInfo.Source
  92. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  93. _, err = to.Insert(calculateMappingItem)
  94. if err != nil {
  95. return
  96. }
  97. }
  98. var condition string
  99. var pars []interface{}
  100. //获取待拼接指标最近的个12月31日有值的年份
  101. condition += " AND data_time like ? AND edb_info_id=? "
  102. pars = append(pars, "%12-31", firstEdbInfo.EdbInfoId)
  103. //获取最新的待拼接指标
  104. lastEdbData, tmpErr := GetLastEdbData(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource)
  105. if tmpErr != nil {
  106. return edbInfoId, tmpErr
  107. }
  108. lastDateTime, _ = time.ParseInLocation(utils.FormatDate, lastEdbData.DataTime, time.Local)
  109. //获取待拼接指标的数据列表
  110. condition = ``
  111. pars = make([]interface{}, 0)
  112. condition += " AND data_time <= ? AND edb_info_id=? "
  113. pars = append(pars, lastEdbData.DataTime, firstEdbInfo.EdbInfoId)
  114. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource, 0)
  115. if tmpErr != nil {
  116. return edbInfoId, tmpErr
  117. }
  118. for _, v := range firstDataList {
  119. pjDataMap[v.DataTime] = v.Value
  120. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  121. //将新的数据存入已入库指标map里面,以便后续计算
  122. nowEdbDataMap[v.DataTime] = v.Value
  123. }
  124. } else {
  125. // 获取当前指标的所有数据
  126. var condition string
  127. var pars []interface{}
  128. condition += " AND edb_info_id=? and data_time <= ?"
  129. pars = append(pars, nowEdbInfo.EdbInfoId, nowEdbInfo.CalculateFormula)
  130. nowEdbDataList, tmpErr := GetEdbDataListAll(condition, pars, nowEdbInfo.Source, nowEdbInfo.SubSource, 0)
  131. if tmpErr != nil {
  132. return edbInfoId, tmpErr
  133. }
  134. for _, v := range nowEdbDataList {
  135. nowEdbDataMap[v.DataTime] = v.Value
  136. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  137. }
  138. }
  139. }
  140. //同比值指标map
  141. tbzEdbDataMap := make(map[string]float64)
  142. //同比值日期切片列表
  143. tbzEdbDataTimeList := make([]string, 0)
  144. //同比值指标
  145. {
  146. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  147. //删除通过B指标生成的数据
  148. {
  149. //删除之前的B指标关联关系
  150. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  151. _, err = to.Raw(sql, edbInfoId, existItemB.FromEdbInfoId).Exec()
  152. if err != nil {
  153. err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  154. return
  155. }
  156. //如果同比值指标变更了,那么需要删除所有计算出来的指标数据
  157. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  158. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time > ? `, tableName)
  159. _, err = to.Raw(sql, edbInfoId, nowEdbInfo.CalculateFormula).Exec()
  160. if err != nil {
  161. err = errors.New("删除计算出来的累计值同比拼接指标数据失败,Err:" + err.Error())
  162. return
  163. }
  164. }
  165. //添加新的指标关系
  166. {
  167. calculateMappingItem := new(EdbInfoCalculateMapping)
  168. calculateMappingItem.CreateTime = time.Now()
  169. calculateMappingItem.ModifyTime = time.Now()
  170. calculateMappingItem.Sort = 1
  171. calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
  172. calculateMappingItem.EdbInfoId = edbInfoId
  173. calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
  174. calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
  175. calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
  176. calculateMappingItem.FromSource = secondEdbInfo.Source
  177. calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
  178. calculateMappingItem.FromTag = "B"
  179. calculateMappingItem.Source = nowEdbInfo.Source
  180. calculateMappingItem.SourceName = nowEdbInfo.SourceName
  181. _, err = to.Insert(calculateMappingItem)
  182. if err != nil {
  183. return
  184. }
  185. }
  186. }
  187. var condition string
  188. var pars []interface{}
  189. condition += " AND data_time > ? AND edb_info_id = ? "
  190. pars = append(pars, lastDateTime, secondEdbInfo.EdbInfoId)
  191. //同比值指标的数据列表
  192. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, secondEdbInfo.SubSource, 0)
  193. if tmpErr != nil {
  194. return edbInfoId, tmpErr
  195. }
  196. for _, v := range secondDataList {
  197. tbzEdbDataMap[v.DataTime] = v.Value
  198. tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
  199. }
  200. }
  201. sort.Strings(tbzEdbDataTimeList)
  202. // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后)
  203. removeDateList := make([]string, 0)
  204. for nowEdbDate := range nowEdbDataMap {
  205. nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local)
  206. //校验日期 需要 大于 拼接前日期
  207. if startCalculationDate.Before(nowEdbDateTime) {
  208. if _, ok := tbzEdbDataMap[nowEdbDate]; !ok {
  209. // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据
  210. removeDateList = append(removeDateList, nowEdbDate)
  211. }
  212. }
  213. }
  214. //待修改的指标数据map(index:日期,value:值)
  215. updateEdbDataMap := make(map[string]float64)
  216. for _, v := range tbzEdbDataTimeList {
  217. tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  218. //获取拼接指标上一年同一天的数据
  219. var pjDataTime time.Time
  220. if tbzDataTime.Month() == 2 {
  221. pjDataTime = tbzDataTime.AddDate(0, -11, 0)
  222. pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
  223. } else {
  224. pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
  225. }
  226. //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致
  227. if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas {
  228. //获取去年今日的数据,获取到后,然后是去修改该日期的数据
  229. if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  230. tbzDataValue := tbzEdbDataMap[v] //同比值
  231. currValue := lastYearEdbDataValue * (1 + tbzDataValue/100)
  232. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数
  233. //如果计算出来的值与库里面的值不匹配,那么就去修改该值
  234. if nowEdbDataValue != currValue {
  235. //将计算后的数据存入待拼接指标map里面,以便后续计算
  236. updateEdbDataMap[v] = currValue
  237. }
  238. }
  239. } else {
  240. //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理
  241. //如果去年今日存在该数据,那么就去计算当前的数据
  242. if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  243. tbzDataValue := tbzEdbDataMap[v] //同比值
  244. currValue := pjDataValue * (1 + tbzDataValue/100)
  245. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
  246. //将计算后的数据存入已入库指标map里面,以便后续计算
  247. nowEdbDataMap[v] = currValue
  248. //将计算后的数据存入待拼接指标map里面,以便后续入库
  249. pjDataMap[v] = currValue
  250. pjEdbDataTimeList = append(pjEdbDataTimeList, v)
  251. }
  252. }
  253. }
  254. //新增的数据入库
  255. {
  256. addDataList := make([]*EdbDataCalculateLjztbpj, 0)
  257. for dataTime, dataValue := range pjDataMap {
  258. //时间戳
  259. currentDate, _ := time.Parse(utils.FormatDate, dataTime)
  260. timestamp := currentDate.UnixNano() / 1e6
  261. edbDataLjztbpj := &EdbDataCalculateLjztbpj{
  262. EdbInfoId: edbInfoId,
  263. EdbCode: nowEdbInfo.EdbCode,
  264. DataTime: dataTime,
  265. Value: dataValue,
  266. Status: 1,
  267. CreateTime: time.Now(),
  268. ModifyTime: time.Now(),
  269. DataTimestamp: timestamp,
  270. }
  271. addDataList = append(addDataList, edbDataLjztbpj)
  272. }
  273. tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0)
  274. for _, v := range addDataList {
  275. tmpAddDataList = append(tmpAddDataList, v)
  276. if len(tmpAddDataList) >= 200 {
  277. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  278. if tmpErr != nil {
  279. err = tmpErr
  280. return
  281. }
  282. //重新初始化需要加入的数据切片
  283. tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0)
  284. }
  285. }
  286. //最后如果还有需要新增的数据,那么就统一入库
  287. if len(tmpAddDataList) > 0 {
  288. _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  289. if tmpErr != nil {
  290. err = tmpErr
  291. return
  292. }
  293. }
  294. }
  295. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  296. {
  297. if len(removeDateList) > 0 {
  298. removeDateStr := strings.Join(removeDateList, `","`)
  299. removeDateStr = `"` + removeDateStr + `"`
  300. //如果拼接指标变更了,那么需要删除所有的指标数据
  301. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  302. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  303. _, err = to.Raw(sql, edbInfoId).Exec()
  304. if err != nil {
  305. err = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error())
  306. return
  307. }
  308. }
  309. }
  310. //修改现有的数据中对应的值
  311. {
  312. tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
  313. for edbDate, edbDataValue := range updateEdbDataMap {
  314. sql = fmt.Sprintf(` UPDATE %s set value = %f,modify_time=now() WHERE edb_info_id = ? and data_time = %s `, tableName, edbDataValue, edbDate)
  315. _, err = to.Raw(sql, edbInfoId).Exec()
  316. if err != nil {
  317. err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error())
  318. return
  319. }
  320. }
  321. }
  322. //修改指标信息
  323. sql = ` UPDATE edb_info SET
  324. edb_name =?,
  325. edb_name_source=?,
  326. frequency = ?,
  327. unit = ?,
  328. classify_id = ?,
  329. calculate_formula=?,
  330. modify_time = NOW()
  331. WHERE edb_info_id = ? `
  332. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, lastDateTime.Format(utils.FormatDate), edbInfoId).Exec()
  333. if err != nil {
  334. return
  335. }
  336. return
  337. }