edb_info_calculate.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_ETA_mobile_api/utils"
  6. "strings"
  7. "time"
  8. )
  9. type EdbInfoCalculateSaveReq struct {
  10. EdbName string `description:"指标名称"`
  11. Frequency string `description:"频率"`
  12. Unit string `description:"单位"`
  13. ClassifyId int `description:"分类id"`
  14. CalculateFormula string `description:"计算公式"`
  15. Calendar string `description:"公历/农历"`
  16. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  17. }
  18. // EdbInfoFromTag 计算指标的关联指标
  19. type EdbInfoFromTag struct {
  20. EdbInfoId int `description:"指标id"`
  21. FromTag string `description:"指标对应标签"`
  22. MoveValue int `description:"移动的值"`
  23. }
  24. type EdbInfoCalculate struct {
  25. EdbInfoCalculateId int `orm:"column(edb_info_calculate_id);pk"`
  26. EdbInfoId int `description:"指标id"`
  27. EdbCode string `description:"指标编码"`
  28. FromEdbInfoId int `description:"计算指标id"`
  29. FromEdbCode string `description:"计算指标编码"`
  30. FromEdbName string `description:"计算指标名称"`
  31. FromSource int `description:"计算指标来源"`
  32. FromSourceName string `description:"计算指标来源名称"`
  33. FromTag string `description:"来源指标标签"`
  34. Sort int `description:"计算指标名称排序"`
  35. CreateTime time.Time `description:"创建时间"`
  36. ModifyTime time.Time `description:"修改时间"`
  37. }
  38. func AddEdbInfoCalculateMulti(items []*EdbInfoCalculate) (err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. _, err = o.InsertMulti(1, items)
  41. return
  42. }
  43. type FromEdbInfoData struct {
  44. EdbInfoId int `description:"指标id"`
  45. EdbName string `description:"指标名称"`
  46. DataList []*EdbInfoSearchData
  47. }
  48. type EdbInfoCalculateDetail struct {
  49. EdbInfoCalculateId int `orm:"column(edb_info_calculate_id);pk"`
  50. EdbInfoId int `description:"指标id"`
  51. EdbCode string `description:"指标编码"`
  52. FromEdbInfoId int `description:"计算指标id"`
  53. FromEdbCode string `description:"计算指标编码"`
  54. FromEdbName string `description:"计算指标名称"`
  55. FromSource int `description:"计算指标来源"`
  56. FromSourceName string `description:"计算指标来源名称"`
  57. FromTag string `description:"来源指标标签"`
  58. MoveValue int `description:"移动的值,小于0是提前,0是不变,大于0是滞后"`
  59. Sort int `description:"计算指标名称排序"`
  60. CreateTime time.Time `description:"创建时间"`
  61. ModifyTime time.Time `description:"修改时间"`
  62. StartDate string `description:"开始日期"`
  63. EndDate string `description:"结束日期"`
  64. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  65. }
  66. func GetEdbInfoCalculateDetail(edbInfoId, source int) (list []*EdbInfoCalculateDetail, err error) {
  67. o := orm.NewOrmUsingDB("data")
  68. //calculateTableName := GetEdbInfoCalculateTableName(source)
  69. //sql := ` SELECT a.*,b.start_date,b.end_date,b.edb_type FROM %s AS a
  70. // INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  71. // WHERE a.edb_info_id=? ORDER BY sort ASC `
  72. //
  73. //sql = fmt.Sprintf(sql, calculateTableName)
  74. sql := ` SELECT a.edb_info_calculate_mapping_id,a.edb_info_id,a.source,a.source_name,a.edb_code,a.from_edb_info_id,a.from_edb_code,a.from_source,a.from_source_name,a.sort,a.create_time,a.modify_time,a.from_tag,a.move_value,b.edb_name_source as from_edb_name,b.start_date,b.end_date,b.edb_type FROM edb_info_calculate_mapping AS a
  75. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  76. WHERE a.edb_info_id=? ORDER BY sort ASC `
  77. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  78. return
  79. }
  80. type CalculateDetailResp struct {
  81. EdbInfoDetail *EdbInfo
  82. CalculateList []*EdbInfoCalculateDetail
  83. }
  84. type EdbInfoCalculateEditReq struct {
  85. EdbInfoId int `description:"指标id"`
  86. EdbName string `description:"指标名称"`
  87. Frequency string `description:"频率"`
  88. Unit string `description:"单位"`
  89. ClassifyId int `description:"分类id"`
  90. CalculateFormula string `description:"计算公式"`
  91. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  92. }
  93. func GetCalculateEdbInfo(edbInfoId int) (from_edb_info_id string, err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. sql := ` SELECT GROUP_CONCAT(from_edb_info_id ORDER BY sort ASC SEPARATOR ',') AS from_edb_info_id
  96. FROM edb_info_calculate_mapping
  97. WHERE edb_info_id=?
  98. GROUP BY edb_info_id
  99. `
  100. err = o.Raw(sql, edbInfoId).QueryRow(&from_edb_info_id)
  101. return
  102. }
  103. func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. to, err := o.Begin()
  106. if err != nil {
  107. return
  108. }
  109. defer func() {
  110. if err != nil {
  111. _ = to.Rollback()
  112. } else {
  113. _ = to.Commit()
  114. }
  115. }()
  116. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  117. _, err = to.Raw(sql, edbInfoId).Exec()
  118. if err != nil {
  119. return
  120. }
  121. sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
  122. _, err = to.Raw(sql, edbInfoId).Exec()
  123. return
  124. }
  125. func DeleteCalculateData(edbInfoId int) (err error) {
  126. o := orm.NewOrmUsingDB("data")
  127. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  128. _, err = o.Raw(sql, edbInfoId).Exec()
  129. return
  130. }
  131. type EdbInfoCalculateBatchSaveReq struct {
  132. EdbInfoId int `description:"指标id"`
  133. EdbName string `description:"指标名称"`
  134. Frequency string `description:"频度"`
  135. Unit string `description:"单位"`
  136. ClassifyId int `description:"分类id"`
  137. Formula string `description:"N值/移动天数"`
  138. FromEdbInfoId int `description:"计算来源指标id"`
  139. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  140. CalculateFormula string `description:"计算公式"`
  141. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  142. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  143. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  144. Calendar string `description:"公历/农历"`
  145. }
  146. type EdbInfoCalculateBatchEditReq struct {
  147. EdbName string `description:"指标名称"`
  148. Frequency string `description:"频度"`
  149. Unit string `description:"单位"`
  150. ClassifyId int `description:"分类id"`
  151. Formula string `description:"N值"`
  152. EdbInfoId int `description:"编辑指标id"`
  153. FromEdbInfoId int `description:"计算来源指标id"`
  154. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  155. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  156. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  157. Calendar string `description:"公历/农历"`
  158. EdbInfoIdArr []EdbInfoFromTag
  159. }
  160. // EdbInfoCalculateBatchSaveReqByEdbLib edb指标库的请求逻辑
  161. type EdbInfoCalculateBatchSaveReqByEdbLib struct {
  162. AdminId int `description:"添加人id"`
  163. AdminName string `description:"添加人名称"`
  164. EdbInfoId int `description:"指标id"`
  165. EdbName string `description:"指标名称"`
  166. Frequency string `description:"频度"`
  167. Unit string `description:"单位"`
  168. ClassifyId int `description:"分类id"`
  169. Formula string `description:"N值/移动天数"`
  170. FromEdbInfoId int `description:"计算来源指标id"`
  171. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  172. CalculateFormula string `description:"计算公式"`
  173. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  174. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  175. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  176. Calendar string `description:"公历/农历"`
  177. }
  178. // EdbInfoCalculateBatchEditReq 编辑计算指标的请求参数
  179. type EdbInfoCalculateBatchEditReqByEdbLib struct {
  180. EdbName string `description:"指标名称"`
  181. Frequency string `description:"频度"`
  182. Unit string `description:"单位"`
  183. ClassifyId int `description:"分类id"`
  184. Formula string `description:"N值"`
  185. EdbInfoId int `description:"编辑指标id"`
  186. FromEdbInfoId int `description:"计算来源指标id"`
  187. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  188. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  189. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  190. Calendar string `description:"公历/农历"`
  191. EdbInfoIdArr []EdbInfoFromTag
  192. }
  193. func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*EdbInfo, err error) {
  194. o := orm.NewOrmUsingDB("data")
  195. //calculateTableName := GetEdbInfoCalculateTableName(source)
  196. //sql := ` SELECT b.* FROM %s AS a
  197. // INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  198. // WHERE a.edb_info_id=? ORDER BY sort ASC `
  199. //sql = fmt.Sprintf(sql, calculateTableName)
  200. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  201. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  202. WHERE a.edb_info_id=? ORDER BY sort ASC `
  203. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  204. return
  205. }
  206. // 指标运算
  207. func AddCalculate(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo []*EdbInfoCalculateDetail, edbCode, uniqueCode, formulaStr string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  208. o := orm.NewOrmUsingDB("data")
  209. to, err := o.Begin()
  210. if err != nil {
  211. return
  212. }
  213. defer func() {
  214. if err != nil {
  215. _ = to.Rollback()
  216. } else {
  217. _ = to.Commit()
  218. }
  219. }()
  220. if req.EdbInfoId <= 0 {
  221. edbInfo := new(EdbInfo)
  222. edbInfo.Source = utils.DATA_SOURCE_CALCULATE
  223. edbInfo.SourceName = "指标运算"
  224. edbInfo.EdbCode = edbCode
  225. edbInfo.EdbName = req.EdbName
  226. edbInfo.EdbNameSource = req.EdbName
  227. edbInfo.Frequency = req.Frequency
  228. edbInfo.Unit = req.Unit
  229. edbInfo.ClassifyId = req.ClassifyId
  230. edbInfo.SysUserId = sysUserId
  231. edbInfo.SysUserRealName = sysUserRealName
  232. edbInfo.CreateTime = time.Now()
  233. edbInfo.ModifyTime = time.Now()
  234. edbInfo.UniqueCode = uniqueCode
  235. edbInfo.CalculateFormula = req.CalculateFormula
  236. edbInfo.EdbType = 2
  237. newEdbInfoId, err := to.Insert(edbInfo)
  238. if err != nil {
  239. return int(newEdbInfoId), err
  240. }
  241. edbInfoId = int(newEdbInfoId)
  242. //处理同名指标
  243. {
  244. edbNameList, err := GetEdbInfoByName(req.EdbName)
  245. if err != nil {
  246. return edbInfoId, err
  247. }
  248. if len(edbNameList) >= 2 {
  249. for _, v := range edbNameList {
  250. edbName := v.EdbName + "(" + v.SourceName + ")"
  251. err = ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  252. if err != nil {
  253. return edbInfoId, err
  254. }
  255. }
  256. }
  257. }
  258. //calculateList := make([]*EdbInfoCalculate, 0)
  259. edbInfoList := make([]*EdbInfo, 0)
  260. for _, v := range req.EdbInfoIdArr {
  261. edbInfo, err := GetEdbInfoById(v.EdbInfoId)
  262. if err != nil {
  263. return edbInfoId, err
  264. }
  265. //calculateItem := new(EdbInfoCalculate)
  266. //calculateItem.CreateTime = time.Now()
  267. //calculateItem.ModifyTime = time.Now()
  268. //calculateItem.Sort = k + 1
  269. //calculateItem.EdbCode = edbCode
  270. //calculateItem.EdbInfoId = int(edbInfoId)
  271. //calculateItem.FromEdbInfoId = edbInfo.EdbInfoId
  272. //calculateItem.FromEdbCode = edbInfo.EdbCode
  273. //calculateItem.FromEdbName = edbInfo.EdbName
  274. //calculateItem.FromSource = edbInfo.Source
  275. //calculateItem.FromSourceName = edbInfo.SourceName
  276. //calculateItem.FromTag = v.FromTag
  277. //calculateList = append(calculateList, calculateItem)
  278. edbInfoList = append(edbInfoList, edbInfo)
  279. }
  280. //if len(calculateList) > 0 {
  281. // _, err = to.InsertMulti(1, calculateList)
  282. // if err != nil {
  283. // return edbInfoId, err
  284. // }
  285. //}
  286. } else {
  287. edbInfoId = req.EdbInfoId
  288. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE)
  289. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  290. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  291. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  292. }
  293. //计算数据
  294. saveDataMap := make(map[string]map[int]float64)
  295. for _, v := range fromEdbInfo {
  296. var condition string
  297. var pars []interface{}
  298. condition += " AND edb_info_id=? "
  299. pars = append(pars, v.FromEdbInfoId)
  300. dataList, err := GetEdbDataListAll(condition, pars, v.FromSource, 1)
  301. if err != nil {
  302. return edbInfoId, err
  303. }
  304. dataMap := make(map[string]float64)
  305. for _, dv := range dataList {
  306. if val, ok := saveDataMap[dv.DataTime]; ok {
  307. if _, ok := val[v.EdbInfoId]; !ok {
  308. val[v.EdbInfoId] = dv.Value
  309. }
  310. } else {
  311. temp := make(map[int]float64)
  312. temp[v.EdbInfoId] = dv.Value
  313. saveDataMap[dv.DataTime] = temp
  314. }
  315. }
  316. item := new(CalculateItems)
  317. item.EdbInfoId = v.EdbInfoId
  318. item.DataMap = dataMap
  319. }
  320. //formulaMap := CheckFormula(formulaStr)
  321. //addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  322. //nowStr := time.Now().Format(utils.FormatDateTime)
  323. //var isAdd bool
  324. //
  325. //for sk, sv := range saveDataMap {
  326. // formulaStr = strings.ToUpper(formulaStr)
  327. // formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  328. // if formulaFormStr != "" {
  329. // utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  330. // expression := formula.NewExpression(formulaFormStr)
  331. // calResult, err := expression.Evaluate()
  332. // if err != nil {
  333. // err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  334. // fmt.Println(err)
  335. // return err
  336. // }
  337. // calVal, err := calResult.Float64()
  338. // if err != nil {
  339. // err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  340. // fmt.Println(err)
  341. // return err
  342. // }
  343. //
  344. // //需要存入的数据
  345. // {
  346. // dataTime, _ := time.Parse(utils.FormatDate, sk)
  347. // timestamp := dataTime.UnixNano() / 1e6
  348. // timeStr := fmt.Sprintf("%d", timestamp)
  349. // addSql += "("
  350. // addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  351. // "," + "'" + nowStr + "'" + "," + "1"
  352. // addSql += "," + "'" + timeStr + "'"
  353. // addSql += "),"
  354. // isAdd = true
  355. // }
  356. // }
  357. //}
  358. //if isAdd {
  359. // addSql = strings.TrimRight(addSql, ",")
  360. // AddEdbDataCalculateBySql(addSql)
  361. // if err != nil {
  362. // fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  363. // return
  364. // }
  365. //}
  366. return
  367. }
  368. type CalculateItems struct {
  369. EdbInfoId int
  370. DataMap map[string]float64
  371. }
  372. func CheckFormula(formula string) map[string]string {
  373. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  374. str := strings.ToUpper(formula)
  375. for _, v := range mathFormula {
  376. str = strings.Replace(str, v, "", -1)
  377. }
  378. str = strings.Replace(str, "(", "", -1)
  379. str = strings.Replace(str, ")", "", -1)
  380. byteMap := make(map[string]string)
  381. for i := 0; i < len(str); i++ {
  382. byteInt := str[i]
  383. if byteInt >= 65 && byteInt <= 90 {
  384. byteStr := string(byteInt)
  385. if _, ok := byteMap[byteStr]; !ok {
  386. byteMap[byteStr] = byteStr
  387. }
  388. }
  389. }
  390. return byteMap
  391. }
  392. func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  393. funMap := GetFormulaMap()
  394. for k, v := range funMap {
  395. formulaStr = strings.Replace(formulaStr, k, v, -1)
  396. }
  397. replaceCount := 0
  398. for dk, dv := range edbInfoIdArr {
  399. if dk == 0 {
  400. dKey := edbInfoIdBytes[dk]
  401. if _, ok := formulaMap[dKey]; ok { //公式中存在
  402. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  403. dvStr := fmt.Sprintf("%v", val)
  404. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  405. replaceCount++
  406. }
  407. }
  408. }
  409. if dk == 1 {
  410. dKey := edbInfoIdBytes[dk]
  411. if _, ok := formulaMap[dKey]; ok { //公式中存在
  412. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  413. dvStr := fmt.Sprintf("%v", val)
  414. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  415. replaceCount++
  416. }
  417. }
  418. }
  419. if dk == 2 {
  420. dKey := edbInfoIdBytes[dk]
  421. if _, ok := formulaMap[dKey]; ok { //公式中存在
  422. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  423. dvStr := fmt.Sprintf("%v", val)
  424. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  425. replaceCount++
  426. }
  427. }
  428. }
  429. if dk == 3 {
  430. dKey := edbInfoIdBytes[dk]
  431. if _, ok := formulaMap[dKey]; ok { //公式中存在
  432. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  433. dvStr := fmt.Sprintf("%v", val)
  434. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  435. replaceCount++
  436. }
  437. }
  438. }
  439. if dk == 4 {
  440. dKey := edbInfoIdBytes[dk]
  441. if _, ok := formulaMap[dKey]; ok { //公式中存在
  442. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  443. dvStr := fmt.Sprintf("%v", val)
  444. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  445. replaceCount++
  446. }
  447. }
  448. }
  449. if dk == 5 {
  450. dKey := edbInfoIdBytes[dk]
  451. if _, ok := formulaMap[dKey]; ok { //公式中存在
  452. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  453. dvStr := fmt.Sprintf("%v", val)
  454. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  455. replaceCount++
  456. }
  457. }
  458. }
  459. if dk == 6 {
  460. dKey := edbInfoIdBytes[dk]
  461. if _, ok := formulaMap[dKey]; ok { //公式中存在
  462. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  463. dvStr := fmt.Sprintf("%v", val)
  464. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  465. replaceCount++
  466. }
  467. }
  468. }
  469. if dk == 7 {
  470. dKey := edbInfoIdBytes[dk]
  471. if _, ok := formulaMap[dKey]; ok { //公式中存在
  472. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  473. dvStr := fmt.Sprintf("%v", val)
  474. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  475. replaceCount++
  476. }
  477. }
  478. }
  479. if dk == 8 {
  480. dKey := edbInfoIdBytes[dk]
  481. if _, ok := formulaMap[dKey]; ok { //公式中存在
  482. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  483. dvStr := fmt.Sprintf("%v", val)
  484. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  485. replaceCount++
  486. }
  487. }
  488. }
  489. }
  490. for k, v := range funMap {
  491. formulaStr = strings.Replace(formulaStr, v, k, -1)
  492. }
  493. if replaceCount == len(formulaMap) {
  494. return formulaStr
  495. } else {
  496. return ""
  497. }
  498. }
  499. func GetFormulaMap() map[string]string {
  500. funMap := make(map[string]string)
  501. funMap["MAX"] = "[@@]"
  502. funMap["MIN"] = "[@!]"
  503. funMap["ABS"] = "[@#]"
  504. funMap["CEIL"] = "[@$]"
  505. funMap["COS"] = "[@%]"
  506. funMap["FLOOR"] = "[@^]"
  507. funMap["MOD"] = "[@&]"
  508. funMap["POW"] = "[@*]"
  509. funMap["ROUND"] = "[@(]"
  510. return funMap
  511. }
  512. // SaveAdjustEdbInfoReq 保存数据调整请求参数(请求指标服务)
  513. type SaveAdjustEdbInfoReq struct {
  514. AdminId int `description:"添加人id"`
  515. AdminName string `description:"添加人名称"`
  516. EdbInfoId int `description:"指标id"`
  517. FromEdbInfoId int `description:"来源指标id"`
  518. EdbName string `description:"指标名称"`
  519. Frequency string `description:"频度"`
  520. Unit string `description:"单位"`
  521. ClassifyId int `description:"分类id"`
  522. DataList []SaveAdjustEdbDataReq `description:"指标对应的数据值"`
  523. }
  524. // SaveAdjustEdbDataReq 保存数据调整请求的数据的参数
  525. type SaveAdjustEdbDataReq struct {
  526. Date string `description:"数据日期"`
  527. Value float64 `description:"数据值"`
  528. }
  529. // BatchEdbInfoCalculateBatchSaveReq 批量添加 计算指标
  530. type BatchEdbInfoCalculateBatchSaveReq struct {
  531. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  532. CalculateInfo EdbInfoCalculateBatchSaveReq
  533. }
  534. // BatchEdbInfoCalculateBatchSaveResp 批量添加 计算指标 返回数据
  535. type BatchEdbInfoCalculateBatchSaveResp struct {
  536. Fail []BatchEdbInfoCalculateBatchSaveFailResp `description:"添加失败的指标"`
  537. Success []BatchEdbInfoCalculateBatchSaveSuccessResp `description:"添加成功的指标"`
  538. }
  539. // BatchEdbInfoCalculateBatchSaveFailResp 添加失败的指标信息
  540. type BatchEdbInfoCalculateBatchSaveFailResp struct {
  541. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  542. Msg string `description:"用户提示信息"`
  543. ErrMsg string `description:"错误信息,内部查看"`
  544. }
  545. // BatchEdbInfoCalculateBatchSaveSuccessResp 添加成功的指标信息
  546. type BatchEdbInfoCalculateBatchSaveSuccessResp struct {
  547. ClassifyId int `description:"分类id"`
  548. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  549. EdbInfoId int `description:"指标ID"`
  550. UniqueCode string `description:"指标唯一编码"`
  551. }