mixed_table.go 37 KB

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