mixed_table.go 38 KB

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