mixed_table.go 38 KB

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