mixed_table.go 37 KB

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