mixed_table.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. package table
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/models"
  6. "eta/eta_chart_lib/models/data_manage"
  7. "eta/eta_chart_lib/models/request"
  8. "eta/eta_chart_lib/services/data"
  9. "eta/eta_chart_lib/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "github.com/yidane/formula"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // BaseCalculate
  19. // @Description: 指标数据计算请求
  20. type BaseCalculate struct {
  21. DataList []*models.EdbDataList
  22. Frequency string `description:"需要转换的频度"`
  23. Formula interface{}
  24. Calendar string `description:"公历/农历"`
  25. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  26. MoveFrequency string `description:"移动频度"`
  27. FromFrequency string `description:"来源的频度"`
  28. Source int `description:"1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值"`
  29. }
  30. // Cell
  31. // @Description: 单元格位置
  32. type Cell struct {
  33. Column int `description:"行"`
  34. Row int `description:"列"`
  35. CellInfo request.MixedTableCellDataReq `description:"对应的单元格信息"`
  36. }
  37. // GetMixedTableCellData 获取混合表格数据
  38. func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCellDataList [][]request.MixedTableCellDataReq, err error, errMsg string) {
  39. cellRelationConf := mixedTableReq.CellRelation
  40. config := mixedTableReq.Data
  41. // 单元格关系配置x信息
  42. cellRelationConfMap := make(map[string]request.CellRelationConf)
  43. cellRelationConfList := make([]request.CellRelationConf, 0)
  44. if cellRelationConf != `` {
  45. err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
  46. if err != nil {
  47. return
  48. }
  49. for _, v := range cellRelationConfList {
  50. cellRelationConfMap[v.Key] = v
  51. }
  52. }
  53. // 找出所有的关联指标id
  54. config, edbInfoIdList, _, err, errMsg := handleConfig(config)
  55. if err != nil {
  56. return
  57. }
  58. // 查询所有关联的指标信息
  59. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  60. if err != nil {
  61. return
  62. }
  63. // 指标信息map
  64. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  65. // 日度指标数据map
  66. edbDayDataListMap := make(map[int]map[string]float64)
  67. // 月度指标数据map
  68. edbMonthDataListMap := make(map[int]map[string]string)
  69. // 日度指标数据map
  70. edbDataListMap := make(map[int][]*models.EdbDataList)
  71. for _, edbInfo := range edbInfoList {
  72. edbInfoMap[edbInfo.EdbInfoId] = edbInfo
  73. dataList := make([]*models.EdbDataList, 0)
  74. switch edbInfo.EdbInfoType {
  75. case 0:
  76. dataList, _ = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  77. case 1:
  78. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  79. default:
  80. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  81. }
  82. dateValMap := make(map[string]float64)
  83. monthDateMap := make(map[string]string)
  84. for _, tmpData := range dataList {
  85. // 日度数据
  86. dateValMap[tmpData.DataTime] = tmpData.Value
  87. // 月度数据(取该月份的第一个数据)
  88. yearMonth := strings.Join(strings.Split(tmpData.DataTime, "-")[0:2], "-")
  89. if _, ok := monthDateMap[yearMonth]; !ok {
  90. // 存最早的时间
  91. monthDateMap[yearMonth] = tmpData.DataTime
  92. }
  93. }
  94. edbDayDataListMap[edbInfo.EdbInfoId] = dateValMap
  95. edbMonthDataListMap[edbInfo.EdbInfoId] = monthDateMap
  96. edbDataListMap[edbInfo.EdbInfoId] = dataList
  97. }
  98. // 单元格实际绑定的信息map
  99. cellDataRelationMap := make(map[string]request.MixedTableCellDataReq, 0)
  100. // 处理指定指标的日期
  101. for k, row := range config {
  102. for i, cell := range row {
  103. // 单元格是日期类型,且是日导入指标日期(指标库的最新日期)
  104. if cell.DataType == request.DateDT && cell.DataTimeType == request.EdbDateDT {
  105. // 指标id是在配置里面
  106. var edbDateConfig request.EdbDateConf
  107. err = json.Unmarshal([]byte(cell.Value), &edbDateConfig)
  108. if err != nil {
  109. return
  110. }
  111. if dataList, ok := edbDataListMap[edbDateConfig.EdbInfoId]; ok {
  112. // todo 获取配置信息,根据配置信息进行日期变换, 是否需要更新当前记录,将历史记录逐渐转换成新的记录
  113. var newDate string
  114. newDate, err = GetEdbDateByMoveForward(config[k][i].Value, dataList)
  115. if err != nil {
  116. return
  117. }
  118. newDate, err = HandleMixTableDateChange(newDate, config[k][i].Value)
  119. if err != nil {
  120. return
  121. }
  122. cell.ShowValue = newDate
  123. cell.DataTime = newDate
  124. config[k][i] = cell
  125. }
  126. }
  127. row[i] = cell
  128. cellDataRelationMap[cell.Uid] = cell
  129. }
  130. config[k] = row
  131. }
  132. // 指标计算的结果map
  133. edbSourceDataMap := make(map[string]data.BaseCalculateDataResp)
  134. // 单元格对应的key与他的值(只处理数据类型)
  135. cellKeyVal := make(map[string]float64)
  136. // 基础计算单元格的位置信息
  137. calculateCellMap := make(map[string]Cell)
  138. calculateChainList := make([]string, 0)
  139. dateCalculateList := make([]string, 0)
  140. showStyleList := make([]string, 0)
  141. // 处理单元格中的数据类型(除去基础计算,因为这个是依赖于其他)
  142. for k, row := range config {
  143. for i, cell := range row {
  144. switch cell.DataType {
  145. case request.EdbDT: // 指标类型
  146. if cell.Value == `` {
  147. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  148. cell.ShowValue = edbInfo.EdbName
  149. }
  150. } else {
  151. cell.ShowValue = cell.Value
  152. }
  153. case request.InsertDataDT, request.PopInsertDataDT: // 数据类型
  154. // 数值先清空
  155. cell.ShowValue = ``
  156. //cell.Value = ``
  157. // 日期关系配置不存在,则默认最新数据
  158. if relationConf, ok := cellRelationConfMap[cell.Uid]; ok { //表示表格日期
  159. if relationConf.RelationDate.Key == `` {
  160. // 日期关系配置未绑定
  161. continue
  162. }
  163. // 配置
  164. relationCell, ok := cellDataRelationMap[relationConf.RelationDate.Key]
  165. if !ok {
  166. // 找不到对应日期的单元格
  167. continue
  168. }
  169. // 确实是找到了这个关联日期的单元格,那么通过日期重新获取数据值
  170. tmpDateValMap := make(map[string]float64)
  171. // 日度数据
  172. if dateValMap, ok := edbDayDataListMap[cell.EdbInfoId]; ok {
  173. tmpDateValMap = dateValMap
  174. }
  175. // todo 根据配置进行日期变换
  176. relationDate := relationCell.DataTime
  177. if strings.Contains(cell.Value, "{") {
  178. relationDate, err = HandleMixTableDateChange(relationDate, cell.Value)
  179. if err != nil {
  180. return
  181. }
  182. } else {
  183. cell.Value = ""
  184. }
  185. if val, ok2 := tmpDateValMap[relationDate]; ok2 {
  186. //cell.ShowValue = fmt.Sprint(val)
  187. cellKeyVal[cell.Uid] = val
  188. cell.ShowValue = utils.FormatMixTableDataShowValue(val)
  189. }
  190. } else {
  191. // 如果不是取得一个关联的日期,那么就是指定日期
  192. // 如果没有指定日期,则默认最新数据
  193. if cell.DataTime == `` {
  194. // 指标的最新日期
  195. if dateValList, ok := edbDataListMap[cell.EdbInfoId]; ok {
  196. tmpLenData := len(dateValList)
  197. if tmpLenData > 0 {
  198. //做期数前移动和日期变换
  199. if !strings.Contains(cell.Value, "{") {
  200. cell.Value = ""
  201. }
  202. var newDate string
  203. newDate, err = GetEdbDateByMoveForward(cell.Value, dateValList)
  204. if err != nil {
  205. return
  206. }
  207. newDate, err = HandleMixTableDateChange(newDate, cell.Value)
  208. if err != nil {
  209. return
  210. }
  211. var finalVal float64
  212. for _, v := range dateValList {
  213. if v.DataTime == newDate {
  214. finalVal = v.Value
  215. break
  216. }
  217. }
  218. cellKeyVal[cell.Uid] = finalVal
  219. cell.ShowValue = utils.FormatMixTableDataShowValue(finalVal)
  220. }
  221. }
  222. } else {
  223. tmpDateList := strings.Split(cell.DataTime, "-")
  224. tmpDateValMap := make(map[string]float64)
  225. var newDate string
  226. if len(tmpDateList) == 2 {
  227. //月度数据
  228. if dateMap, ok1 := edbMonthDataListMap[cell.EdbInfoId]; ok1 {
  229. if d, ok2 := dateMap[cell.DataTime]; ok2 {
  230. newDate = d
  231. }
  232. }
  233. } else {
  234. // 日度数据
  235. newDate = cell.DataTime
  236. }
  237. // 日期变换后才能确定最后的时间
  238. //做期数前移动和日期变换
  239. if !strings.Contains(cell.Value, "{") {
  240. cell.Value = ""
  241. }
  242. newDate, err = HandleMixTableDateChange(newDate, cell.Value)
  243. if err != nil {
  244. return
  245. }
  246. if dateValMap, ok3 := edbDayDataListMap[cell.EdbInfoId]; ok3 {
  247. tmpDateValMap = dateValMap
  248. if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
  249. //cell.ShowValue = fmt.Sprint(val)
  250. cellKeyVal[cell.Uid] = val
  251. cell.ShowValue = utils.FormatMixTableDataShowValue(val)
  252. }
  253. }
  254. }
  255. }
  256. case request.CustomTextDT: //自定义文本
  257. if cell.Value == `` {
  258. continue
  259. }
  260. // 处理看下能否转成float,如果可以的话,说明这个也是可以参与计算的
  261. tmpDeci, tmpErr := decimal.NewFromString(cell.Value)
  262. if tmpErr == nil {
  263. tmpVal, _ := tmpDeci.Float64()
  264. cellKeyVal[cell.Uid] = tmpVal
  265. }
  266. case request.FormulateCalculateDataDT: // 公式计算(A+B这种)
  267. calculateChainList = append(calculateChainList, cell.Uid)
  268. case request.InsertEdbCalculateDataDT: // 插入指标系统计算公式生成的值
  269. // 处理value的值
  270. if !strings.Contains(cell.Value, "EdbInfoId") && cell.EdbInfoId > 0 {
  271. cell.Value, _ = fixCalculateValueConfig(cell.Value, cell.EdbInfoId)
  272. row[i] = cell
  273. }
  274. // 日期
  275. var cellDateTime string
  276. // 日期关系配置不存在,则默认最新数据
  277. // 从绑定的单元格中获取数据的日期
  278. if relationConf, ok := cellRelationConfMap[cell.Uid]; ok {
  279. if relationConf.RelationDate.Key == `` {
  280. // 日期关系配置未绑定
  281. continue
  282. }
  283. // 配置
  284. relationCell, ok := cellDataRelationMap[relationConf.RelationDate.Key]
  285. if !ok {
  286. // 找不到对应日期的单元格
  287. continue
  288. }
  289. cellDateTime = relationCell.DataTime
  290. }
  291. var tmpDataMap map[string]float64
  292. key := utils.MD5(cell.Value)
  293. respItemData, ok := edbSourceDataMap[key]
  294. if !ok {
  295. // 对应的配置值
  296. var tmpConfig request.CalculateConf
  297. err = json.Unmarshal([]byte(cell.Value), &tmpConfig)
  298. if err != nil {
  299. return
  300. }
  301. tmpDataList, ok := edbDataListMap[tmpConfig.EdbInfoId]
  302. if !ok {
  303. continue
  304. }
  305. edbInfo, ok := edbInfoMap[tmpConfig.EdbInfoId]
  306. if !ok {
  307. continue
  308. }
  309. req2 := &BaseCalculate{
  310. DataList: tmpDataList,
  311. Frequency: tmpConfig.Frequency,
  312. Formula: tmpConfig.Formula,
  313. Calendar: tmpConfig.Calendar,
  314. MoveType: tmpConfig.MoveType,
  315. MoveFrequency: tmpConfig.MoveFrequency,
  316. FromFrequency: edbInfo.Frequency,
  317. Source: tmpConfig.Source,
  318. }
  319. // 调用指标库去更新
  320. reqJson, tmpErr := json.Marshal(req2)
  321. if tmpErr != nil {
  322. utils.FileLog.Error(fmt.Sprintf("计算失败1,配置信息;%s;错误原因:%s", cell.Value, tmpErr.Error()))
  323. err = tmpErr
  324. return
  325. }
  326. respItem, tmpErr := data.BaseCalculate(string(reqJson))
  327. if tmpErr != nil {
  328. utils.FileLog.Error(fmt.Sprintf("计算失败2,配置信息;%s;错误原因:%s", cell.Value, tmpErr.Error()))
  329. err = tmpErr
  330. return
  331. }
  332. if respItem.Ret != 200 {
  333. utils.FileLog.Error(fmt.Sprintf("计算失败3,配置信息;%s;原因:%s;错误原因:%s", cell.Value, respItem.Msg, respItem.ErrMsg))
  334. continue
  335. }
  336. //tmpDataMap = respItem.Data.DataMap
  337. // 计算结果存一份,万一存在重复的计算方式,那么省的重新计算一下
  338. edbSourceDataMap[key] = respItem.Data
  339. respItemData = respItem.Data
  340. }
  341. lenDataList := len(respItemData.DateList)
  342. tmpDataMap = respItemData.DataMap
  343. if cellDateTime == `` && lenDataList > 0 {
  344. //判断是否需要做期数前移动
  345. cellDateTime = respItemData.DateList[lenDataList-1]
  346. cellDateTime, err = GetEdbDateByMoveForwardByDateList(cell.Value, respItemData.DateList)
  347. if err != nil {
  348. utils.FileLog.Error(fmt.Sprintf("日期前移失败,配置信息;%s", cell.Value))
  349. continue
  350. }
  351. }
  352. // 进行日期变换
  353. cellDateTime, err = HandleMixTableDateChange(cellDateTime, cell.Value)
  354. if err != nil {
  355. utils.FileLog.Error(fmt.Sprintf("日期变换失败,配置信息;%s, 日期:%s", cell.Value, cellDateTime))
  356. continue
  357. }
  358. val := tmpDataMap[cellDateTime]
  359. cellKeyVal[cell.Uid] = val
  360. cell.ShowValue = utils.FormatMixTableDataShowValue(val)
  361. case request.DateCalculateDataDT: //日期计算
  362. dateCalculateList = append(dateCalculateList, cell.Uid)
  363. // 遍历数组,根据公式进行计算,并将得到的结果放到对应的单元格中
  364. }
  365. row[i] = cell
  366. }
  367. config[k] = row
  368. }
  369. // 处理指定指标的日期
  370. for k, row := range config {
  371. for i, cell := range row {
  372. calculateCellMap[cell.Uid] = Cell{
  373. Column: k,
  374. Row: i,
  375. CellInfo: cell,
  376. }
  377. cell.ShowFormatValue = cell.ShowValue
  378. if cell.ShowStyle != `` {
  379. showStyleList = append(showStyleList, cell.Uid)
  380. }
  381. row[i] = cell
  382. cellDataRelationMap[cell.Uid] = cell
  383. }
  384. config[k] = row
  385. }
  386. // 公式链计算
  387. if len(calculateChainList) > 0 {
  388. for _, cellKey := range calculateChainList {
  389. // 查找这个单元格的位置,直接map找了,而不是遍历整个单元格
  390. cellPosition, ok := calculateCellMap[cellKey]
  391. if !ok {
  392. utils.FileLog.Error("找不到单元格位置:", cellKey)
  393. continue
  394. }
  395. cell := config[cellPosition.Column][cellPosition.Row]
  396. if cell.DataType != request.FormulateCalculateDataDT { // 判断公式计算(A+B这种)类型,不是的话也过滤了
  397. continue
  398. }
  399. val, has, tmpErr, tmpErrMsg := getCalculateValueByCell(calculateCellMap, cellKey, cellKeyVal)
  400. if tmpErr != nil {
  401. errMsg = tmpErrMsg
  402. err = tmpErr
  403. return
  404. }
  405. if !has {
  406. continue
  407. }
  408. cellKeyVal[cell.Uid] = val
  409. cell.ShowValue = utils.FormatMixTableDataShowValue(val)
  410. config[cellPosition.Column][cellPosition.Row] = cell
  411. }
  412. }
  413. // 日期计算
  414. config, err, errMsg = handlerDateCalculate(dateCalculateList, calculateCellMap, config)
  415. if err != nil {
  416. return
  417. }
  418. // 格式化展示
  419. config, err, errMsg = handleMixCellShowStyle(showStyleList, calculateCellMap, config)
  420. if err != nil {
  421. return
  422. }
  423. newMixedTableCellDataList = config
  424. return
  425. }
  426. // getCalculateValue 获取公式计算的结果
  427. func getCalculateValueByCell(calculateCellMap map[string]Cell, key string, cellKeyValMap map[string]float64) (val float64, has bool, err error, errMsg string) {
  428. // 单元格的标签名
  429. val, ok := cellKeyValMap[key]
  430. if ok {
  431. has = true
  432. return
  433. }
  434. // 查找单元格数据
  435. cell, ok := calculateCellMap[key]
  436. if !ok {
  437. err = errors.New("查找单元格" + key + "的数据失败")
  438. return
  439. }
  440. colData := cell.CellInfo
  441. // 如果不是基础计算单元格,直接返回
  442. if colData.DataType != request.FormulateCalculateDataDT {
  443. return
  444. }
  445. // 如果是计算单元格
  446. tagList := make([]utils.CellPosition, 0)
  447. // 计算单元格relationCellList
  448. var relationCellList []request.RelationCell
  449. if colData.Extra == `` {
  450. err = errors.New(colData.Uid + "没有绑定关系")
  451. return
  452. }
  453. err = json.Unmarshal([]byte(colData.Extra), &relationCellList)
  454. if err != nil {
  455. return
  456. }
  457. for _, relation := range relationCellList {
  458. //relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  459. tmpVal, _, tmpErr, tmpErrMsg := getCalculateValueByCell(calculateCellMap, relation.Key, cellKeyValMap)
  460. if tmpErr != nil {
  461. errMsg = tmpErrMsg
  462. err = tmpErr
  463. return
  464. }
  465. cellKeyValMap[relation.Key] = tmpVal
  466. rowInt, tmpErr := strconv.Atoi(relation.Row)
  467. if tmpErr != nil {
  468. err = tmpErr
  469. return
  470. }
  471. tagList = append(tagList, utils.CellPosition{
  472. Tag: relation.Tag,
  473. Row: rowInt,
  474. Value: tmpVal,
  475. })
  476. }
  477. // 计算
  478. val, errMsg, err = calculateByCellList(strings.ToUpper(colData.Value), tagList)
  479. if err != nil {
  480. return
  481. }
  482. // 重新赋值
  483. has = true
  484. cellKeyValMap[key] = val
  485. return
  486. }
  487. // handleConfig
  488. // @Description: 处理混合表格配置
  489. // @author: Roc
  490. // @datetime2023-10-27 13:24:53
  491. // @param configList [][]request.MixedTableCellDataReq
  492. // @return newConfig [][]request.MixedTableCellDataReq
  493. // @return edbInfoIdList []int
  494. // @return dataEdbInfoIdList []int
  495. // @return err error
  496. // @return errMsg string
  497. func handleConfig(configList [][]request.MixedTableCellDataReq) (newConfig [][]request.MixedTableCellDataReq, edbInfoIdList []int, dataEdbInfoIdList []int, err error, errMsg string) {
  498. edbInfoIdList = make([]int, 0)
  499. dataEdbInfoIdList = make([]int, 0)
  500. for ck, rowList := range configList {
  501. for rk, cell := range rowList {
  502. switch cell.DataType {
  503. case request.EdbDT: // 指标信息
  504. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  505. case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
  506. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  507. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  508. case request.InsertEdbCalculateDataDT: // 插入指标计算公式生成的值
  509. var config request.CalculateConf
  510. err = json.Unmarshal([]byte(cell.Value), &config)
  511. if err != nil {
  512. return
  513. }
  514. if config.EdbInfoId == 0 && cell.EdbInfoId > 0 {
  515. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  516. } else {
  517. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  518. }
  519. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  520. case request.DateDT: // 日期类型
  521. date, newVal, tmpErr, tmpErrMsg := handleDate(cell.DataTimeType, cell.Value)
  522. if tmpErr != nil {
  523. err = tmpErr
  524. errMsg = tmpErrMsg
  525. return
  526. }
  527. rowList[rk].DataTime = date
  528. rowList[rk].ShowValue = date
  529. rowList[rk].Value = newVal //兼容原有的历史数据中系统导入日期
  530. // 指标日期类型的单元格需要额外将指标id取出来
  531. if cell.DataTimeType == request.EdbDateDT {
  532. var config request.EdbDateConf
  533. err = json.Unmarshal([]byte(cell.Value), &config)
  534. if err != nil {
  535. return
  536. }
  537. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  538. }
  539. }
  540. }
  541. configList[ck] = rowList
  542. }
  543. newConfig = configList
  544. return
  545. }
  546. // HandleDate
  547. // @Description: 日期处理
  548. // @author: Roc
  549. // @datetime2023-10-27 09:37:02
  550. // @param dataTimeType int
  551. // @param val string
  552. // @return date string
  553. // @return err error
  554. // @return errMsg string
  555. func HandleDate(dataTimeType int, val string) (date string, newVal string, err error, errMsg string) {
  556. return handleDate(dataTimeType, val)
  557. }
  558. // handleDate
  559. // @Description: 日期处理
  560. // @author: Roc
  561. // @datetime2023-10-27 09:36:49
  562. // @param dataTimeType int
  563. // @param val string
  564. // @return date string
  565. // @return err error
  566. // @return errMsg string
  567. func handleDate(dataTimeType int, val string) (date string, newVal string, err error, errMsg string) {
  568. newVal = val
  569. if val == `` {
  570. errMsg = "错误的日期数据"
  571. err = errors.New(errMsg)
  572. return
  573. }
  574. switch dataTimeType {
  575. case request.CustomDateT: //手动输入日期
  576. /*if !strings.Contains(val, "{") {
  577. newVal, err, errMsg = handleOldCustomerDateT(val)
  578. if err != nil {
  579. return
  580. }
  581. }
  582. date, err = HandleMixTableDateChange("", newVal)
  583. if err != nil {
  584. return
  585. }*/
  586. date = val
  587. return
  588. case request.SystemDateT: // 系统日期
  589. date = time.Now().Format(utils.FormatDate)
  590. newVal, err, errMsg = handleOldSystemDateT(val)
  591. if err != nil {
  592. return
  593. }
  594. date, err = HandleMixTableDateChange(date, newVal)
  595. if err != nil {
  596. return
  597. }
  598. case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
  599. default:
  600. errMsg = "错误的日期类型"
  601. err = errors.New(errMsg)
  602. return
  603. }
  604. return
  605. }
  606. // handleSystemCalculateDateT
  607. // @Description: 处理系统日期计算后的日期
  608. // @author: Roc
  609. // @datetime2023-10-27 09:31:22
  610. // @param num int
  611. // @param frequency string
  612. // @return date string
  613. // @return err error
  614. // @return errMsg string
  615. func handleSystemCalculateDateT(num int, frequency string) (date string, err error, errMsg string) {
  616. if err != nil {
  617. return
  618. }
  619. currDate := time.Now()
  620. switch frequency {
  621. case "", "日":
  622. date = currDate.AddDate(0, 0, num).Format(utils.FormatDate)
  623. default:
  624. errMsg = "错误的日期频度:" + frequency
  625. err = errors.New(errMsg)
  626. return
  627. }
  628. return
  629. }
  630. // handleSystemAppointDateT
  631. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  632. // @author: Roc
  633. // @datetime2023-10-27 09:31:35
  634. // @param Frequency string
  635. // @param Day string
  636. // @return date string
  637. // @return err error
  638. // @return errMsg string
  639. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  640. //currDate := time.Now()
  641. switch frequency {
  642. case "本周":
  643. day := int(currDate.Weekday())
  644. if day == 0 { // 周日
  645. day = 7
  646. }
  647. num := 0
  648. switch appointDay {
  649. case "周一":
  650. num = 1
  651. case "周二":
  652. num = 2
  653. case "周三":
  654. num = 3
  655. case "周四":
  656. num = 4
  657. case "周五":
  658. num = 5
  659. case "周六":
  660. num = 6
  661. case "周日":
  662. num = 7
  663. }
  664. day = num - day
  665. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  666. case "本旬":
  667. day := currDate.Day()
  668. var tmpDate time.Time
  669. switch appointDay {
  670. case "第一天":
  671. if day <= 10 {
  672. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  673. } else if day <= 20 {
  674. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  675. } else {
  676. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  677. }
  678. case "最后一天":
  679. if day <= 10 {
  680. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  681. } else if day <= 20 {
  682. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  683. } else {
  684. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  685. }
  686. }
  687. date = tmpDate.Format(utils.FormatDate)
  688. case "本月":
  689. var tmpDate time.Time
  690. switch appointDay {
  691. case "第一天":
  692. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  693. case "最后一天":
  694. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  695. }
  696. date = tmpDate.Format(utils.FormatDate)
  697. case "本季":
  698. month := currDate.Month()
  699. var tmpDate time.Time
  700. switch appointDay {
  701. case "第一天":
  702. if month <= 3 {
  703. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  704. } else if month <= 6 {
  705. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  706. } else if month <= 9 {
  707. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  708. } else {
  709. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  710. }
  711. case "最后一天":
  712. if month <= 3 {
  713. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  714. } else if month <= 6 {
  715. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  716. } else if month <= 9 {
  717. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  718. } else {
  719. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  720. }
  721. }
  722. date = tmpDate.Format(utils.FormatDate)
  723. case "本半年":
  724. month := currDate.Month()
  725. var tmpDate time.Time
  726. switch appointDay {
  727. case "第一天":
  728. if month <= 6 {
  729. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  730. } else {
  731. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  732. }
  733. case "最后一天":
  734. if month <= 6 {
  735. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  736. } else {
  737. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  738. }
  739. }
  740. date = tmpDate.Format(utils.FormatDate)
  741. case "本年":
  742. var tmpDate time.Time
  743. switch appointDay {
  744. case "第一天":
  745. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  746. case "最后一天":
  747. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  748. }
  749. date = tmpDate.Format(utils.FormatDate)
  750. default:
  751. errMsg = "错误的日期频度:" + frequency
  752. err = errors.New(errMsg)
  753. return
  754. }
  755. return
  756. }
  757. // calculateByCellList
  758. // @Description: 根据单元格来进行公式计算
  759. // @author: Roc
  760. // @datetime2023-11-14 16:17:38
  761. // @param calculateFormula string
  762. // @param tagList []utils.CellPosition
  763. // @return calVal string
  764. // @return errMsg string
  765. // @return err error
  766. func calculateByCellList(calculateFormula string, tagList []utils.CellPosition) (calVal float64, errMsg string, err error) {
  767. if calculateFormula == "" {
  768. errMsg = "公式异常"
  769. err = errors.New(errMsg)
  770. return
  771. }
  772. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  773. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  774. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  775. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  776. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  777. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  778. rowList := make([]int, 0)
  779. rowListMap := make(map[int][]utils.CellPosition)
  780. for _, v := range tagList {
  781. tmpRowList, ok := rowListMap[v.Row]
  782. if !ok {
  783. rowList = append(rowList, v.Row)
  784. tmpRowList = make([]utils.CellPosition, 0)
  785. }
  786. tmpRowList = append(tmpRowList, v)
  787. rowListMap[v.Row] = tmpRowList
  788. }
  789. sort.Ints(rowList)
  790. list := make([]utils.CellPosition, 0)
  791. for _, row := range rowList {
  792. list = append(list, rowListMap[row]...)
  793. }
  794. formulaFormStr := utils.ReplaceFormulaByCellList(list, calculateFormula)
  795. //计算公式异常,那么就移除该指标
  796. if formulaFormStr == `` {
  797. errMsg = "公式异常"
  798. err = errors.New(errMsg)
  799. return
  800. }
  801. expression := formula.NewExpression(formulaFormStr)
  802. calResult, err := expression.Evaluate()
  803. if err != nil {
  804. errMsg = "计算失败"
  805. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  806. // 分母为0的报错
  807. if strings.Contains(err.Error(), "divide by zero") {
  808. errMsg = "分母不能为0"
  809. err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
  810. }
  811. return
  812. }
  813. // 如果计算结果是NAN,那么就提示报错
  814. if calResult.IsNan() {
  815. errMsg = "计算失败"
  816. err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
  817. return
  818. }
  819. calVal, err = calResult.Float64()
  820. if err != nil {
  821. return
  822. }
  823. // 转Decimal然后四舍五入
  824. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  825. return
  826. }
  827. func GetEdbDateByMoveForward(conf string, edbDataList []*models.EdbDataList) (date string, err error) {
  828. dateList := make([]string, 0)
  829. for _, v := range edbDataList {
  830. dateList = append(dateList, v.DataTime)
  831. }
  832. date, err = GetEdbDateByMoveForwardByDateList(conf, dateList)
  833. return
  834. }
  835. func GetEdbDateByMoveForwardByDateList(conf string, dateList []string) (date string, err error) {
  836. moveForward := 0
  837. if conf != "" {
  838. var edbDateConf request.EdbDateChangeConf
  839. err = json.Unmarshal([]byte(conf), &edbDateConf)
  840. if err != nil {
  841. err = fmt.Errorf("日期变换配置json解析失败失败: %s", err.Error())
  842. return
  843. }
  844. moveForward = edbDateConf.MoveForward
  845. }
  846. // 根据日期进行排序
  847. index := len(dateList) - 1 - moveForward
  848. for k, v := range dateList {
  849. if k == index {
  850. date = v
  851. return
  852. }
  853. }
  854. return
  855. }
  856. // HandleMixTableDateChange 处理表格中的日期变换
  857. func HandleMixTableDateChange(date, conf string) (newDate string, err error) {
  858. newDate = date
  859. if conf == "" {
  860. return
  861. }
  862. var edbDateConf request.EdbDateConf
  863. err = json.Unmarshal([]byte(conf), &edbDateConf)
  864. if err != nil {
  865. err = fmt.Errorf("日期变换配置json解析失败失败: %s, Err:%s", conf, err.Error())
  866. return
  867. }
  868. if newDate != "" {
  869. if len(edbDateConf.DateChange) > 0 {
  870. var dateTime time.Time
  871. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  872. if err != nil {
  873. err = fmt.Errorf("日期解析失败: %s", err.Error())
  874. return
  875. }
  876. for _, v := range edbDateConf.DateChange {
  877. if v.ChangeType == 1 {
  878. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  879. newDate = dateTime.Format(utils.FormatDate)
  880. } else if v.ChangeType == 2 {
  881. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  882. if err != nil {
  883. return
  884. }
  885. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  886. if err != nil {
  887. err = fmt.Errorf("日期解析失败: %s", err.Error())
  888. return
  889. }
  890. }
  891. }
  892. }
  893. }
  894. return
  895. }
  896. // handleOldSystemDateT
  897. // @Description: 历史数据中的导入系统日期
  898. // @author: Roc
  899. // @datetime2023-10-27 09:36:21
  900. // @param confStr string
  901. // @return date string
  902. // @return err error
  903. // @return errMsg string
  904. func handleOldSystemDateT(confStr string) (newConf string, err error, errMsg string) {
  905. newConf = confStr
  906. var config request.SystemDateConf
  907. err = json.Unmarshal([]byte(confStr), &config)
  908. if err != nil {
  909. return
  910. }
  911. newConfig := new(request.EdbDateConf)
  912. dateChange := new(request.EdbDateConfDateChange)
  913. dateChangeList := make([]*request.EdbDateConfDateChange, 0)
  914. switch config.Source {
  915. case request.SystemCurrDateT:
  916. return
  917. case request.SystemCalculateDateT:
  918. // todo 是否直接更新该excel记录,
  919. dateChange.Day = config.CalculateNum
  920. dateChange.ChangeType = 1
  921. dateChangeList = append(dateChangeList, dateChange)
  922. newConfig.DateChange = dateChangeList
  923. newConfByte, e := json.Marshal(newConfig)
  924. if e != nil {
  925. err = fmt.Errorf("日期计算额外配置,json序列化失败: %s", e.Error())
  926. return
  927. }
  928. newConf = string(newConfByte)
  929. return
  930. case request.SystemFrequencyDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  931. dateChange.FrequencyDay = config.Day
  932. dateChange.Frequency = config.Frequency
  933. dateChange.ChangeType = 1
  934. dateChangeList = append(dateChangeList, dateChange)
  935. newConfig.DateChange = dateChangeList
  936. newConfByte, e := json.Marshal(newConfig)
  937. if e != nil {
  938. err = fmt.Errorf("日期计算额外配置,json序列化失败: %s", e.Error())
  939. return
  940. }
  941. newConf = string(newConfByte)
  942. return
  943. default:
  944. //errMsg = "错误的日期日期导入方式"
  945. //err = errors.New(fmt.Sprint("错误的日期日期导入方式:", config.Source))
  946. return
  947. }
  948. return
  949. }
  950. // handlerDateCalculate 处理日期计算
  951. func handlerDateCalculate(dateCalculateList []string, calculateCellMap map[string]Cell, oldConfig [][]request.MixedTableCellDataReq) (config [][]request.MixedTableCellDataReq, err error, errMsg string) {
  952. config = oldConfig
  953. if len(dateCalculateList) == 0 {
  954. return
  955. }
  956. if len(dateCalculateList) > 0 {
  957. for _, cellKey := range dateCalculateList {
  958. // 查找这个单元格的位置,直接map找了,而不是遍历整个单元格
  959. cellPosition, ok := calculateCellMap[cellKey]
  960. if !ok {
  961. utils.FileLog.Error("找不到单元格位置:", cellKey)
  962. continue
  963. }
  964. cell := config[cellPosition.Column][cellPosition.Row]
  965. if cell.DataType != request.DateCalculateDataDT { // 判断公式计算(A+B这种)类型,不是的话也过滤了
  966. continue
  967. }
  968. val, tmpErr, tmpErrMsg := DateCalculatePrepare(calculateCellMap, cell.Value)
  969. if tmpErr != nil {
  970. errMsg = tmpErrMsg
  971. err = tmpErr
  972. return
  973. }
  974. cell.ShowValue = utils.FormatMixTableDataShowValue(val)
  975. config[cellPosition.Column][cellPosition.Row] = cell
  976. }
  977. }
  978. return
  979. }
  980. // DateCalculatePrepare 单个单元格的日期计算
  981. func DateCalculatePrepare(calculateCellMap map[string]Cell, config string) (val float64, err error, errMsg string) {
  982. var edbDateConf request.MixDateCalculateConf
  983. err = json.Unmarshal([]byte(config), &edbDateConf)
  984. if err != nil {
  985. err = fmt.Errorf("日期计算配置json解析失败失败: %s, Err:%s", config, err.Error())
  986. return
  987. }
  988. if len(edbDateConf.RelationCellList) == 0 {
  989. err = fmt.Errorf("日期计算 未配置日期单元格失败: %s", config)
  990. return
  991. }
  992. valMap := make(map[string]int)
  993. for _, v := range edbDateConf.RelationCellList {
  994. // 查找单元格数据
  995. cell, ok := calculateCellMap[v.Uid]
  996. if !ok {
  997. err = fmt.Errorf("查找单元格:%s 的数据失败", v.Uid)
  998. return
  999. }
  1000. colData := cell.CellInfo
  1001. // 如果不是基础计算单元格,直接返回
  1002. _, err = time.ParseInLocation(utils.FormatDate, colData.ShowValue, time.Local)
  1003. if err != nil {
  1004. err = fmt.Errorf("%s 的单元格非日期类型, Err: %s", colData.ShowValue, err.Error())
  1005. return
  1006. }
  1007. // todo 把日期转换成excel里的天数
  1008. realDiffDay := utils.GetDaysDiff1900(colData.ShowValue)
  1009. valMap[strings.ToUpper(v.Tag)] = realDiffDay
  1010. }
  1011. // 计算
  1012. val, errMsg, err = DateCalculateFormula(valMap, strings.ToUpper(edbDateConf.Formula))
  1013. if err != nil {
  1014. return
  1015. }
  1016. return
  1017. }
  1018. func DateCalculateFormula(valTagMap map[string]int, calculateFormula string) (calVal float64, errMsg string, err error) {
  1019. if calculateFormula == "" {
  1020. errMsg = "公式异常"
  1021. err = errors.New(errMsg)
  1022. return
  1023. }
  1024. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  1025. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  1026. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  1027. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  1028. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  1029. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  1030. formulaFormStr := utils.ReplaceFormulaByTagMap(valTagMap, calculateFormula)
  1031. if formulaFormStr == `` {
  1032. errMsg = "公式异常"
  1033. err = errors.New(errMsg)
  1034. return
  1035. }
  1036. fmt.Println("公式:" + formulaFormStr)
  1037. expression := formula.NewExpression(formulaFormStr)
  1038. calResult, err := expression.Evaluate()
  1039. if err != nil {
  1040. errMsg = "计算失败"
  1041. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  1042. // 分母为0的报错
  1043. if strings.Contains(err.Error(), "divide by zero") {
  1044. errMsg = "分母不能为0"
  1045. err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
  1046. }
  1047. return
  1048. }
  1049. // 如果计算结果是NAN,那么就提示报错
  1050. if calResult.IsNan() {
  1051. errMsg = "计算失败"
  1052. err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
  1053. return
  1054. }
  1055. calVal, err = calResult.Float64()
  1056. if err != nil {
  1057. return
  1058. }
  1059. // 转Decimal然后四舍五入
  1060. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  1061. return
  1062. }
  1063. // handleMixCellShowStyle 处理混合表格中,显示计算的逻辑
  1064. func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]Cell, oldConfig [][]request.MixedTableCellDataReq) (config [][]request.MixedTableCellDataReq, err error, errMsg string) {
  1065. config = oldConfig
  1066. if len(showStyleList) == 0 {
  1067. return
  1068. }
  1069. if len(showStyleList) > 0 {
  1070. for _, cellKey := range showStyleList {
  1071. // 查找这个单元格的位置,直接map找了,而不是遍历整个单元格
  1072. cellPosition, ok := calculateCellMap[cellKey]
  1073. if !ok {
  1074. utils.FileLog.Error("找不到单元格位置:", cellKey)
  1075. continue
  1076. }
  1077. cell := config[cellPosition.Column][cellPosition.Row]
  1078. val := cell.ShowValue
  1079. isPercent := false
  1080. if strings.Contains(val, "%") {
  1081. isPercent = true
  1082. val = strings.Trim(val, "%")
  1083. }
  1084. _, e := strconv.ParseFloat(val, 64) // 将字符串转换成float类型
  1085. if e != nil { // 如果没有错误发生则返回true,说明该字符串是一个合法的数字
  1086. continue
  1087. }
  1088. var styleConf request.MixCellShowStyle
  1089. err = json.Unmarshal([]byte(cell.ShowStyle), &styleConf)
  1090. if err != nil {
  1091. err = fmt.Errorf("日期计算配置json解析失败失败: %s, Err:%s", config, err.Error())
  1092. return
  1093. }
  1094. if styleConf.Pn != 0 || styleConf.Nt != "" {
  1095. val = changePointDecimalPlaces(val, styleConf.Pn, styleConf.Nt, isPercent)
  1096. cell.ShowFormatValue = val
  1097. } else {
  1098. cell.ShowFormatValue = cell.ShowValue
  1099. }
  1100. config[cellPosition.Column][cellPosition.Row] = cell
  1101. }
  1102. }
  1103. return
  1104. }
  1105. // changePointDecimalPlaces 小数点位数加减和百分比格式
  1106. func changePointDecimalPlaces(str string, changeNum int, numberType string, isPercent bool) (newStr string) {
  1107. newStr = str
  1108. // 把字符串转成浮点数
  1109. val, _ := strconv.ParseFloat(str, 64)
  1110. if isPercent {
  1111. if numberType == "number" { //百分数转成小数
  1112. val = val / 100
  1113. isPercent = false
  1114. }
  1115. } else {
  1116. if numberType == "percent" {
  1117. val = val * 100
  1118. }
  1119. }
  1120. newStr = fmt.Sprintf("%v", val)
  1121. var decimalPlaces int // 计算小数位数
  1122. dotIndex := strings.Index(newStr, ".") // 查找小数点的位置
  1123. if dotIndex == -1 {
  1124. decimalPlaces = 0
  1125. } else {
  1126. decimalPlaces = len(newStr) - dotIndex - 1
  1127. }
  1128. decimalPlaces += changeNum
  1129. if decimalPlaces < 0 {
  1130. decimalPlaces = 0
  1131. }
  1132. val, _ = decimal.NewFromFloat(val).Round(int32(decimalPlaces)).Float64()
  1133. newStr = strconv.FormatFloat(val, 'f', decimalPlaces, 64)
  1134. if numberType == "percent" || isPercent {
  1135. newStr += "%"
  1136. }
  1137. return
  1138. }
  1139. func fixCalculateValueConfig(conf string, edbInfoId int) (newConf string, err error) {
  1140. newConf = conf
  1141. if edbInfoId == 0 {
  1142. return
  1143. }
  1144. var tmpConfig request.CalculateConf
  1145. err = json.Unmarshal([]byte(conf), &tmpConfig)
  1146. if err != nil {
  1147. return
  1148. }
  1149. if tmpConfig.EdbInfoId == 0 {
  1150. tmpConfig.EdbInfoId = edbInfoId
  1151. newConfByte, _ := json.Marshal(tmpConfig)
  1152. newConf = string(newConfByte)
  1153. return
  1154. }
  1155. return
  1156. }