mixed_table.go 37 KB

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