base_from_calculate.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "github.com/yidane/formula"
  8. "hongze/hongze_edb_lib/services"
  9. "hongze/hongze_edb_lib/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // EdbInfoCalculateSaveReq 计算(运算)指标请求参数
  15. type EdbInfoCalculateSaveReq struct {
  16. AdminId int `description:"添加人id"`
  17. AdminName string `description:"添加人名称"`
  18. EdbInfoId int `description:"指标id"`
  19. EdbName string `description:"指标名称"`
  20. Frequency string `description:"频率"`
  21. Unit string `description:"单位"`
  22. ClassifyId int `description:"分类id"`
  23. CalculateFormula string `description:"计算公式"`
  24. EdbInfoIdArr []EdbInfoFromTag
  25. }
  26. // EdbInfoFromTag 计算指标的关联指标
  27. type EdbInfoFromTag struct {
  28. EdbInfoId int `description:"指标id"`
  29. FromTag string `description:"指标对应标签"`
  30. }
  31. // CalculateItems 计算(运算)指标信息
  32. type CalculateItems struct {
  33. EdbInfoId int
  34. DataMap map[string]float64
  35. }
  36. // AddCalculate 新增计算(运算)指标
  37. func AddCalculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  38. o := orm.NewOrm()
  39. defer func() {
  40. if err != nil {
  41. utils.FileLog.Info(fmt.Sprintf("Calculate Err:%s" + err.Error()))
  42. }
  43. }()
  44. saveDataMap := make(map[string]map[int]float64)
  45. for _, v := range edbInfoIdArr {
  46. var condition string
  47. var pars []interface{}
  48. condition += " AND edb_info_id=? "
  49. pars = append(pars, v.EdbInfoId)
  50. dataList, err := GetEdbDataListAll(condition, pars, v.Source, 1)
  51. if err != nil {
  52. return err
  53. }
  54. dataMap := make(map[string]float64)
  55. for _, dv := range dataList {
  56. if val, ok := saveDataMap[dv.DataTime]; ok {
  57. if _, ok := val[v.EdbInfoId]; !ok {
  58. val[v.EdbInfoId] = dv.Value
  59. }
  60. } else {
  61. temp := make(map[int]float64)
  62. temp[v.EdbInfoId] = dv.Value
  63. saveDataMap[dv.DataTime] = temp
  64. }
  65. }
  66. item := new(CalculateItems)
  67. item.EdbInfoId = v.EdbInfoId
  68. item.DataMap = dataMap
  69. }
  70. formulaMap := services.CheckFormula(formulaStr)
  71. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  72. nowStr := time.Now().Format(utils.FormatDateTime)
  73. var isAdd bool
  74. for sk, sv := range saveDataMap {
  75. formulaStr = strings.ToUpper(formulaStr)
  76. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  77. if formulaStr == "" {
  78. return
  79. }
  80. if formulaFormStr != "" {
  81. expression := formula.NewExpression(formulaFormStr)
  82. calResult, err := expression.Evaluate()
  83. if err != nil {
  84. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  85. fmt.Println(err)
  86. return err
  87. }
  88. calVal, err := calResult.Float64()
  89. if err != nil {
  90. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  91. fmt.Println(err)
  92. return err
  93. }
  94. //需要存入的数据
  95. {
  96. dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  97. timestamp := dataTime.UnixNano() / 1e6
  98. timeStr := fmt.Sprintf("%d", timestamp)
  99. addSql += "("
  100. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  101. "," + "'" + nowStr + "'" + "," + "'" + timeStr + "'"
  102. addSql += "),"
  103. isAdd = true
  104. }
  105. } else {
  106. fmt.Println("formulaFormStr is empty")
  107. }
  108. }
  109. if isAdd {
  110. addSql = strings.TrimRight(addSql, ",")
  111. _, err = o.Raw(addSql).Exec()
  112. if err != nil {
  113. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  114. //errMsg = " tx.Exec Err :" + err.Error()
  115. return
  116. }
  117. }
  118. return
  119. }
  120. // AddCalculateInfo 新增计算(运算)指标
  121. func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbInfoCalculateMapping, edbInfoList []*EdbInfo, edbCode, uniqueCode string, edbInfoIdBytes []string) (edbInfo *EdbInfo, err error) {
  122. o := orm.NewOrm()
  123. to, err := o.Begin()
  124. if err != nil {
  125. return
  126. }
  127. defer func() {
  128. if err != nil {
  129. fmt.Println("AddCalculateHcz,Err:" + err.Error())
  130. _ = to.Rollback()
  131. } else {
  132. _ = to.Commit()
  133. }
  134. }()
  135. edbInfo = &EdbInfo{
  136. EdbInfoId: 0,
  137. SourceName: "指标运算",
  138. Source: utils.DATA_SOURCE_CALCULATE,
  139. EdbCode: edbCode,
  140. EdbName: req.EdbName,
  141. EdbNameSource: req.EdbName,
  142. Frequency: req.Frequency,
  143. Unit: req.Unit,
  144. StartDate: "",
  145. EndDate: "",
  146. ClassifyId: req.ClassifyId,
  147. SysUserId: req.AdminId,
  148. SysUserRealName: req.AdminName,
  149. UniqueCode: uniqueCode,
  150. CreateTime: time.Now(),
  151. ModifyTime: time.Now(),
  152. MinValue: 0,
  153. MaxValue: 0,
  154. CalculateFormula: req.CalculateFormula,
  155. EdbType: 2,
  156. Sort: 0,
  157. MoveType: 0,
  158. MoveFrequency: "",
  159. NoUpdate: 0,
  160. ServerUrl: "",
  161. EdbInfoType: 0,
  162. EdbNameEn: "",
  163. UnitEn: "",
  164. LatestDate: "",
  165. LatestValue: 0,
  166. ChartImage: "",
  167. }
  168. lastId, err := to.Insert(edbInfo)
  169. if err != nil {
  170. return
  171. }
  172. edbInfo.EdbInfoId = int(lastId)
  173. //关联关系表
  174. {
  175. //处理关联指标
  176. for _, v := range calculateMappingList {
  177. v.EdbCode = edbCode
  178. v.EdbInfoId = edbInfo.EdbInfoId
  179. }
  180. if len(calculateMappingList) > 0 {
  181. _, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
  182. if err != nil {
  183. return
  184. }
  185. }
  186. }
  187. //计算数据
  188. err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes)
  189. return
  190. }
  191. // EditCalculateInfo 编辑计算(运算)指标
  192. func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap map[string]string, edbInfoIdBytes []string, needCalculate bool) (err error, errMsg string) {
  193. o := orm.NewOrm()
  194. to, err := o.Begin()
  195. if err != nil {
  196. return
  197. }
  198. defer func() {
  199. if err != nil {
  200. fmt.Println("AddCalculateHcz,Err:" + err.Error())
  201. _ = to.Rollback()
  202. } else {
  203. _ = to.Commit()
  204. }
  205. }()
  206. // 修改指标信息
  207. edbInfo.EdbName = req.EdbName
  208. edbInfo.EdbNameSource = req.EdbName
  209. edbInfo.Frequency = req.Frequency
  210. edbInfo.Unit = req.Unit
  211. edbInfo.ClassifyId = req.ClassifyId
  212. edbInfo.CalculateFormula = req.CalculateFormula
  213. edbInfo.ModifyTime = time.Now()
  214. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
  215. if err != nil {
  216. return
  217. }
  218. if needCalculate {
  219. // 清除计算数据
  220. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  221. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  222. if err != nil {
  223. return
  224. }
  225. // 清除关联关系表
  226. sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
  227. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  228. if err != nil {
  229. return
  230. }
  231. edbInfoList := make([]*EdbInfo, 0)
  232. calculateMappingList := make([]*EdbInfoCalculateMapping, 0)
  233. for k, v := range req.EdbInfoIdArr {
  234. fromEdbInfo, tmpErr := GetEdbInfoById(v.EdbInfoId)
  235. if tmpErr != nil {
  236. err = tmpErr
  237. return
  238. }
  239. edbInfoList = append(edbInfoList, fromEdbInfo)
  240. //关联关系表
  241. {
  242. calculateMappingItem := &EdbInfoCalculateMapping{
  243. EdbInfoCalculateMappingId: 0,
  244. EdbInfoId: req.EdbInfoId,
  245. Source: edbInfo.Source,
  246. SourceName: edbInfo.SourceName,
  247. EdbCode: edbInfo.EdbCode,
  248. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  249. FromEdbCode: fromEdbInfo.EdbCode,
  250. FromEdbName: fromEdbInfo.EdbName,
  251. FromSource: fromEdbInfo.Source,
  252. FromSourceName: fromEdbInfo.SourceName,
  253. FromTag: v.FromTag,
  254. Sort: k + 1,
  255. CreateTime: time.Now(),
  256. ModifyTime: time.Now(),
  257. }
  258. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  259. }
  260. }
  261. //预先计算,判断公式是否正常
  262. ok, _ := CheckFormula2(edbInfoList, formulaMap, req.CalculateFormula, edbInfoIdBytes)
  263. if !ok {
  264. errMsg = "生成计算指标失败,请使用正确的计算公式"
  265. err = errors.New(errMsg)
  266. return
  267. }
  268. //关联关系表
  269. {
  270. if len(calculateMappingList) > 0 {
  271. _, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
  272. if err != nil {
  273. return
  274. }
  275. }
  276. }
  277. //计算数据
  278. err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes)
  279. }
  280. return
  281. }
  282. // GetCalculateEdbInfo 获取计算指标的关联指标id
  283. func GetCalculateEdbInfo(edbInfoId int) (from_edb_info_id string, err error) {
  284. o := orm.NewOrm()
  285. sql := ` SELECT GROUP_CONCAT(from_edb_info_id ORDER BY sort ASC SEPARATOR ',') AS from_edb_info_id
  286. FROM edb_info_calculate_mapping
  287. WHERE edb_info_id=?
  288. GROUP BY edb_info_id
  289. `
  290. err = o.Raw(sql, edbInfoId).QueryRow(&from_edb_info_id)
  291. return
  292. }
  293. // DeleteCalculateEdbInfo 删除指标信息
  294. func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
  295. o := orm.NewOrm()
  296. to, err := o.Begin()
  297. if err != nil {
  298. return
  299. }
  300. defer func() {
  301. if err != nil {
  302. _ = to.Rollback()
  303. } else {
  304. _ = to.Commit()
  305. }
  306. }()
  307. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  308. _, err = to.Raw(sql, edbInfoId).Exec()
  309. if err != nil {
  310. return
  311. }
  312. sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
  313. _, err = to.Raw(sql, edbInfoId).Exec()
  314. return
  315. }
  316. // RefreshAllCalculate 刷新全部数据
  317. func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  318. o := orm.NewOrm()
  319. to, err := o.Begin()
  320. if err != nil {
  321. return
  322. }
  323. defer func() {
  324. if err != nil {
  325. fmt.Println("RefreshAllCalculate,Err:" + err.Error())
  326. _ = to.Rollback()
  327. } else {
  328. _ = to.Commit()
  329. }
  330. }()
  331. fmt.Println(startDate, endDate)
  332. //计算数据
  333. err = refreshAllCalculate(to, edbInfoIdArr, edbInfoId, source, edbCode, formulaStr, startDate, endDate, edbInfoIdBytes)
  334. return
  335. }
  336. // refreshAllCalculate 刷新全部数据
  337. func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  338. realSaveDataMap := make(map[string]map[int]float64)
  339. saveDataMap := make(map[string]map[int]float64)
  340. dateList := make([]string, 0)
  341. for edbInfoIndex, v := range edbInfoIdArr {
  342. var condition string
  343. var pars []interface{}
  344. condition += " AND edb_info_id=? "
  345. pars = append(pars, v.EdbInfoId)
  346. if startDate != "" {
  347. condition += " AND data_time>=? "
  348. pars = append(pars, startDate)
  349. }
  350. //if endDate != "" {
  351. // condition += " AND data_time<=? "
  352. // pars = append(pars, endDate)
  353. //}
  354. //fmt.Println("v.Source:", v.Source)
  355. dataList, err := GetEdbDataListAllByTo(to, condition, pars, v.Source, 1)
  356. if err != nil {
  357. return err
  358. }
  359. for _, dv := range dataList {
  360. if val, ok := realSaveDataMap[dv.DataTime]; ok {
  361. if _, ok := val[v.EdbInfoId]; !ok {
  362. val[v.EdbInfoId] = dv.Value
  363. }
  364. } else {
  365. temp := make(map[int]float64)
  366. temp[v.EdbInfoId] = dv.Value
  367. realSaveDataMap[dv.DataTime] = temp
  368. }
  369. // saveDataMap
  370. if val, ok := saveDataMap[dv.DataTime]; ok {
  371. if _, ok := val[v.EdbInfoId]; !ok {
  372. val[v.EdbInfoId] = dv.Value
  373. }
  374. } else {
  375. temp2 := make(map[int]float64)
  376. temp2[v.EdbInfoId] = dv.Value
  377. saveDataMap[dv.DataTime] = temp2
  378. }
  379. // 以第一个指标的日期作为基准日期
  380. if edbInfoIndex == 0 {
  381. dateList = append(dateList, dv.DataTime)
  382. }
  383. }
  384. }
  385. //数据处理,将日期内不全的数据做补全
  386. handleDateSaveDataMap(dateList, realSaveDataMap, saveDataMap, edbInfoIdArr)
  387. formulaMap := services.CheckFormula(formulaStr)
  388. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  389. var isAdd bool
  390. //获取指标所有数据
  391. dataList := make([]*EdbData, 0)
  392. dataTableName := GetEdbDataTableName(source)
  393. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  394. sql = fmt.Sprintf(sql, dataTableName)
  395. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  396. if err != nil {
  397. return err
  398. }
  399. dataMap := make(map[string]string)
  400. removeDateMap := make(map[string]string) //需要移除的日期
  401. for _, v := range dataList {
  402. dataMap[v.DataTime] = v.Value
  403. removeDateMap[v.DataTime] = v.DataTime
  404. }
  405. edbInfoIdStr := strconv.Itoa(edbInfoId)
  406. existDataMap := make(map[string]string)
  407. for sk, sv := range saveDataMap {
  408. //fmt.Println(sk, sv)
  409. formulaStr = strings.ToUpper(formulaStr)
  410. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  411. //计算公式异常,那么就移除该指标
  412. if formulaFormStr == `` {
  413. //removeDateList = append(removeDateList, sk)
  414. continue
  415. }
  416. //utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
  417. fmt.Println(fmt.Sprintf("%s:formulaFormStr:%s", sk, formulaFormStr))
  418. expression := formula.NewExpression(formulaFormStr)
  419. calResult, err := expression.Evaluate()
  420. if err != nil {
  421. // 分母为0的报错
  422. if strings.Contains(err.Error(), "divide by zero") {
  423. //removeDateList = append(removeDateList, sk)
  424. continue
  425. }
  426. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  427. fmt.Println(err)
  428. return err
  429. }
  430. // 如果计算结果是NAN,那么就退出当前循环
  431. if calResult.IsNan() {
  432. continue
  433. }
  434. calVal, err := calResult.Float64()
  435. if err != nil {
  436. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  437. fmt.Println(err)
  438. return err
  439. }
  440. // 有计算出来值,那么就从待删除指标中移除
  441. delete(removeDateMap, sk)
  442. saveValue := decimal.NewFromFloat(calVal).RoundCeil(4).String() //utils.SubFloatToString(calVal, 4)
  443. if existVal, ok := dataMap[sk]; !ok {
  444. dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  445. timestamp := dataTime.UnixNano() / 1e6
  446. timeStr := fmt.Sprintf("%d", timestamp)
  447. if _, existOk := existDataMap[sk]; !existOk {
  448. addSql += GetAddSql(edbInfoIdStr, edbCode, sk, timeStr, saveValue)
  449. isAdd = true
  450. }
  451. existDataMap[sk] = sk
  452. } else {
  453. existValDecimal, err := decimal.NewFromString(existVal)
  454. existStr := existValDecimal.String()
  455. if existStr != saveValue {
  456. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  457. sql = fmt.Sprintf(sql, dataTableName)
  458. _, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
  459. if err != nil {
  460. return err
  461. }
  462. }
  463. }
  464. }
  465. if isAdd {
  466. addSql = strings.TrimRight(addSql, ",")
  467. _, err = to.Raw(addSql).Exec()
  468. if err != nil {
  469. fmt.Println("RefreshAllCalculate add Err", err.Error())
  470. return
  471. }
  472. }
  473. if len(removeDateMap) > 0 {
  474. removeDateList := make([]string, 0) //需要移除的日期
  475. for k := range removeDateMap {
  476. removeDateList = append(removeDateList, k)
  477. }
  478. removeDateStr := strings.Join(removeDateList, `","`)
  479. removeDateStr = `"` + removeDateStr + `"`
  480. //如果拼接指标变更了,那么需要删除所有的指标数据
  481. tableName := GetEdbDataTableName(source)
  482. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  483. _, err = to.Raw(sql, edbInfoId).Exec()
  484. if err != nil {
  485. err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
  486. return
  487. }
  488. }
  489. return
  490. }
  491. // ReplaceFormula 替换计算方式
  492. func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  493. funMap := GetFormulaMap()
  494. for k, v := range funMap {
  495. formulaStr = strings.Replace(formulaStr, k, v, -1)
  496. }
  497. replaceCount := 0
  498. for dk, dv := range edbInfoIdArr {
  499. var isReplace bool
  500. formulaStr, isReplace = GetFormulaReplace(dk, dv.EdbInfoId, formulaStr, edbInfoIdBytes, formulaMap, valArr)
  501. if isReplace {
  502. replaceCount++
  503. }
  504. }
  505. for k, v := range funMap {
  506. formulaStr = strings.Replace(formulaStr, v, k, -1)
  507. }
  508. if replaceCount == len(formulaMap) {
  509. return formulaStr
  510. } else {
  511. return ""
  512. }
  513. }
  514. func GetFormulaReplace(index, edbInfoId int, formulaStr string, edbInfoIdBytes []string, formulaMap map[string]string, valArr map[int]float64) (formulaResult string, isReplace bool) {
  515. formulaResult = formulaStr
  516. dKey := edbInfoIdBytes[index]
  517. if _, ok := formulaMap[dKey]; ok { //公式中存在
  518. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  519. dvStr := fmt.Sprintf("%v", val)
  520. formulaResult = strings.Replace(formulaStr, dKey, dvStr, -1)
  521. isReplace = true
  522. }
  523. }
  524. return
  525. }
  526. // GetFormulaMap 获取计算公式的map
  527. func GetFormulaMap() map[string]string {
  528. funMap := make(map[string]string)
  529. funMap["MAX"] = "[@@]"
  530. funMap["MIN"] = "[@!]"
  531. funMap["ABS"] = "[@#]"
  532. funMap["CEIL"] = "[@$]"
  533. funMap["COS"] = "[@%]"
  534. funMap["FLOOR"] = "[@^]"
  535. funMap["MOD"] = "[@&]"
  536. funMap["POW"] = "[@*]"
  537. funMap["ROUND"] = "[@(]"
  538. return funMap
  539. }
  540. // EdbInfoCalculateBatchSaveReq 添加计算指标的请求参数
  541. type EdbInfoCalculateBatchSaveReq struct {
  542. AdminId int `description:"添加人id"`
  543. AdminName string `description:"添加人名称"`
  544. EdbInfoId int `description:"指标id"`
  545. EdbName string `description:"指标名称"`
  546. Frequency string `description:"频度"`
  547. Unit string `description:"单位"`
  548. ClassifyId int `description:"分类id"`
  549. Formula string `description:"N值/移动天数"`
  550. FromEdbInfoId int `description:"计算来源指标id"`
  551. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:升频"`
  552. CalculateFormula string `description:"计算公式"`
  553. EdbInfoIdArr []EdbInfoCalculateEdbInfoIdReq `description:"关联指标列表"`
  554. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  555. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  556. Calendar string `description:"公历/农历"`
  557. }
  558. // EdbInfoCalculateEdbInfoIdReq 新增/编辑请求 关联的指标列表
  559. type EdbInfoCalculateEdbInfoIdReq struct {
  560. EdbInfoId int `description:"指标id"`
  561. FromTag string `description:"指标对应标签"`
  562. MoveValue int `description:"移动的值"`
  563. }
  564. // EdbInfoCalculateBatchEditReq 编辑计算指标的请求参数
  565. type EdbInfoCalculateBatchEditReq struct {
  566. EdbName string `description:"指标名称"`
  567. Frequency string `description:"频度"`
  568. Unit string `description:"单位"`
  569. ClassifyId int `description:"分类id"`
  570. Formula string `description:"N值"`
  571. EdbInfoId int `description:"编辑指标id"`
  572. FromEdbInfoId int `description:"计算来源指标id"`
  573. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:升频"`
  574. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  575. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  576. EdbInfoIdArr []struct {
  577. EdbInfoId int `description:"指标id"`
  578. FromTag string `description:"指标对应标签"`
  579. MoveValue int `description:"移动的值"`
  580. }
  581. Calendar string `description:"公历/农历" orm:"default(公历)"`
  582. }
  583. // CheckFormula2 校验公式是否正常(比如说除法的分母不能为0之类的,实际上就是用预设的字段数据做一次计算)
  584. func CheckFormula2(edbInfoArr []*EdbInfo, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) (ok bool, err error) {
  585. valArr := make(map[int]float64)
  586. for _, v := range edbInfoArr {
  587. valArr[v.EdbInfoId] = 100
  588. }
  589. formulaStr = strings.ToUpper(formulaStr)
  590. formulaFormStr := ReplaceFormula(edbInfoArr, valArr, formulaMap, formulaStr, edbInfoIdBytes)
  591. if formulaFormStr == "" {
  592. return
  593. }
  594. expression := formula.NewExpression(formulaFormStr)
  595. _, err = expression.Evaluate()
  596. if err != nil {
  597. } else {
  598. ok = true
  599. }
  600. return
  601. }
  602. // 处理整个数据
  603. func handleDateSaveDataMap(dateList []string, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*EdbInfo) {
  604. for _, date := range dateList {
  605. tmpDataMap := realSaveDataMap[date]
  606. for _, edbInfo := range edbInfoIdArr {
  607. tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
  608. // 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
  609. if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
  610. //day := 0
  611. //switch edbInfo.Frequency {
  612. //case "周度":
  613. // day = 7
  614. //case "旬度":
  615. // day = 15
  616. //case "月度":
  617. // day = 30
  618. //case "季度":
  619. // day = 90
  620. //case "年度":
  621. // day = 365
  622. //}
  623. // 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
  624. day := 35
  625. handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, day)
  626. }
  627. }
  628. }
  629. }
  630. // handleDateDataMap 处理单个日期的数据
  631. func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
  632. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  633. // 后一天
  634. nextDateDayStr := currDate.AddDate(0, 0, 1).Format(utils.FormatDate)
  635. // 前一天
  636. preDateDayStr := currDate.AddDate(0, 0, -1).Format(utils.FormatDate)
  637. for i := 1; i <= day; i++ {
  638. // 下个日期的数据
  639. {
  640. if i >= 1 {
  641. nextDateDayStr = currDate.AddDate(0, 0, i).Format(utils.FormatDate)
  642. }
  643. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  644. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  645. saveDataMap[date][edbInfoId] = val
  646. return
  647. }
  648. }
  649. }
  650. // 上个日期的数据
  651. {
  652. if i >= 1 {
  653. preDateDayStr = currDate.AddDate(0, 0, -i).Format(utils.FormatDate)
  654. }
  655. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  656. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  657. saveDataMap[date][edbInfoId] = val
  658. return
  659. }
  660. }
  661. }
  662. }
  663. }