mixed_table.go 38 KB

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