mixed_table.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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.EdbDataItemList
  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. 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][]models.EdbDataItemList)
  70. for _, edbInfo := range edbInfoList {
  71. edbInfoMap[edbInfo.EdbInfoId] = edbInfo
  72. dataList := make([]*models.EdbDataList, 0)
  73. switch edbInfo.EdbInfoType {
  74. case 0:
  75. dataList, _ = models.GetEdbDataList(edbInfo.Source, 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. tmpDataList := make([]models.EdbDataItemList, 0)
  95. for _, v := range dataList {
  96. tmpDataList = append(tmpDataList, models.EdbDataItemList{
  97. EdbDataId: v.EdbDataId,
  98. EdbInfoId: v.EdbInfoId,
  99. DataTime: v.DataTime,
  100. DataTimestamp: v.DataTimestamp,
  101. Value: v.Value,
  102. })
  103. }
  104. edbDataListMap[edbInfo.EdbInfoId] = tmpDataList
  105. }
  106. // 单元格实际绑定的信息map
  107. cellDataRelationMap := make(map[string]request.MixedTableCellDataReq, 0)
  108. // 处理指定指标的日期
  109. for k, row := range config {
  110. for i, cell := range row {
  111. // 单元格是日期类型,且是日导入指标日期(指标库的最新日期)
  112. if cell.DataType == request.DateDT && cell.DataTimeType == request.EdbDateDT {
  113. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  114. cell.ShowValue = edbInfo.EndDate
  115. cell.DataTime = edbInfo.EndDate
  116. config[k][i] = cell
  117. }
  118. }
  119. row[i] = cell
  120. cellDataRelationMap[cell.Uid] = cell
  121. }
  122. config[k] = row
  123. }
  124. // 指标计算的结果map
  125. edbSourceDataMap := make(map[string]map[string]float64)
  126. // 单元格对应的key与他的值(只处理数据类型)
  127. cellKeyVal := make(map[string]float64)
  128. // 基础计算单元格的位置信息
  129. calculateCellMap := make(map[string]Cell)
  130. calculateChainList := make([]string, 0)
  131. // 处理单元格中的数据类型(除去基础计算,因为这个是依赖于其他)
  132. for k, row := range config {
  133. for i, cell := range row {
  134. switch cell.DataType {
  135. case request.EdbDT: // 指标类型
  136. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  137. cell.ShowValue = edbInfo.EdbName
  138. }
  139. case request.InsertDataDT, request.PopInsertDataDT: // 数据类型
  140. if cell.DataTime == `` {
  141. // 指标的最新日期
  142. if dateValList, ok := edbDataListMap[cell.EdbInfoId]; ok {
  143. tmpLenData := len(dateValList)
  144. if tmpLenData > 0 {
  145. cellKeyVal[cell.Uid] = dateValList[tmpLenData-1].Value
  146. //cell.ShowValue = utils.FormatTableDataShowValue(dateValList[tmpLenData-1].Value)
  147. cell.ShowValue = fmt.Sprint(dateValList[tmpLenData-1].Value)
  148. }
  149. }
  150. } else {
  151. tmpDateList := strings.Split(cell.DataTime, "-")
  152. tmpDateValMap := make(map[string]float64)
  153. if len(tmpDateList) == 2 {
  154. //月度数据
  155. if dateValMap, ok := edbMonthDataListMap[cell.EdbInfoId]; ok {
  156. tmpDateValMap = dateValMap
  157. }
  158. } else {
  159. // 日度数据
  160. if dateValMap, ok := edbDayDataListMap[cell.EdbInfoId]; ok {
  161. tmpDateValMap = dateValMap
  162. }
  163. }
  164. if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
  165. //cell.ShowValue = fmt.Sprint(val)
  166. cellKeyVal[cell.Uid] = val
  167. //cell.ShowValue = utils.FormatTableDataShowValue(val)
  168. cell.ShowValue = fmt.Sprint(val)
  169. }
  170. }
  171. case request.CustomTextDT: //自定义文本
  172. if cell.Value == `` {
  173. continue
  174. }
  175. // 处理看下能否转成float,如果可以的话,说明这个也是可以参与计算的
  176. tmpDeci, tmpErr := decimal.NewFromString(cell.Value)
  177. if tmpErr == nil {
  178. tmpVal, _ := tmpDeci.Float64()
  179. cellKeyVal[cell.Uid] = tmpVal
  180. calculateCellMap[cell.Uid] = Cell{
  181. Column: k,
  182. Row: i,
  183. CellInfo: cell,
  184. }
  185. }
  186. case request.FormulateCalculateDataDT: // 公式计算(A+B这种)
  187. calculateCellMap[cell.Uid] = Cell{
  188. Column: k,
  189. Row: i,
  190. CellInfo: cell,
  191. }
  192. calculateChainList = append(calculateChainList, cell.Uid)
  193. case request.InsertEdbCalculateDataDT: // 插入指标系统计算公式生成的值
  194. // 日期
  195. var cellDateTime string
  196. // 日期关系配置不存在,则默认最新数据
  197. if relationConf, ok := cellRelationConfMap[cell.Uid]; ok {
  198. if relationConf.RelationDate.Key == `` {
  199. // 日期关系配置未绑定
  200. continue
  201. }
  202. // 配置
  203. relationCell, ok := cellDataRelationMap[relationConf.RelationDate.Key]
  204. if !ok {
  205. // 找不到对应日期的单元格
  206. continue
  207. }
  208. cellDateTime = relationCell.DataTime
  209. }
  210. var tmpDataMap map[string]float64
  211. key := utils.MD5(cell.Value)
  212. tmpDataMap, ok := edbSourceDataMap[key]
  213. if !ok {
  214. // 对应的配置值
  215. var tmpConfig request.CalculateConf
  216. err = json.Unmarshal([]byte(cell.Value), &tmpConfig)
  217. if err != nil {
  218. return
  219. }
  220. tmpDataList, ok := edbDataListMap[tmpConfig.EdbInfoId]
  221. if !ok {
  222. continue
  223. }
  224. edbInfo, ok := edbInfoMap[tmpConfig.EdbInfoId]
  225. if !ok {
  226. continue
  227. }
  228. req2 := &BaseCalculate{
  229. DataList: tmpDataList,
  230. Frequency: tmpConfig.Frequency,
  231. Formula: tmpConfig.Formula,
  232. Calendar: tmpConfig.Calendar,
  233. MoveType: tmpConfig.MoveType,
  234. MoveFrequency: tmpConfig.MoveFrequency,
  235. FromFrequency: edbInfo.Frequency,
  236. Source: tmpConfig.Source,
  237. }
  238. // 调用指标库去更新
  239. reqJson, tmpErr := json.Marshal(req2)
  240. if tmpErr != nil {
  241. utils.FileLog.Error(fmt.Sprintf("计算失败1,配置信息;%s;错误原因:%s", cell.Value, tmpErr.Error()))
  242. err = tmpErr
  243. return
  244. }
  245. respItem, tmpErr := data.BaseCalculate(string(reqJson))
  246. if tmpErr != nil {
  247. utils.FileLog.Error(fmt.Sprintf("计算失败2,配置信息;%s;错误原因:%s", cell.Value, tmpErr.Error()))
  248. err = tmpErr
  249. return
  250. }
  251. if respItem.Ret != 200 {
  252. utils.FileLog.Error(fmt.Sprintf("计算失败3,配置信息;%s;原因:%s;错误原因:%s", cell.Value, respItem.Msg, respItem.ErrMsg))
  253. continue
  254. }
  255. tmpDataMap = respItem.Data.DataMap
  256. // 计算结果存一份,万一存在重复的计算方式,那么省的重新计算一下
  257. edbSourceDataMap[key] = tmpDataMap
  258. lenDataList := len(respItem.Data.DateList)
  259. if cellDateTime == `` && lenDataList > 0 {
  260. cellDateTime = respItem.Data.DateList[lenDataList-1]
  261. }
  262. }
  263. val := tmpDataMap[cellDateTime]
  264. cellKeyVal[cell.Uid] = val
  265. //cell.ShowValue = utils.FormatTableDataShowValue(val)
  266. cell.ShowValue = fmt.Sprint(val)
  267. }
  268. row[i] = cell
  269. }
  270. config[k] = row
  271. }
  272. // 公式链计算
  273. if len(calculateChainList) > 0 {
  274. for _, cellKey := range calculateChainList {
  275. // 查找这个单元格的位置,直接map找了,而不是遍历整个单元格
  276. cellPosition, ok := calculateCellMap[cellKey]
  277. if !ok {
  278. utils.FileLog.Error("找不到单元格位置:", cellKey)
  279. continue
  280. }
  281. cell := config[cellPosition.Column][cellPosition.Row]
  282. if cell.DataType != request.FormulateCalculateDataDT { // 判断公式计算(A+B这种)类型,不是的话也过滤了
  283. continue
  284. }
  285. val, tmpErr, has := getCalculateValueByCell(calculateCellMap, cellKey, cellKeyVal)
  286. if tmpErr != nil {
  287. err = tmpErr
  288. return
  289. }
  290. if !has {
  291. continue
  292. }
  293. cellKeyVal[cell.Uid] = val
  294. //cell.ShowValue = utils.FormatTableDataShowValue(val)
  295. cell.ShowValue = fmt.Sprint(val)
  296. config[cellPosition.Column][cellPosition.Row] = cell
  297. }
  298. }
  299. newMixedTableCellDataList = config
  300. return
  301. }
  302. // getCalculateValue 获取公式计算的结果
  303. func getCalculateValueByCell(calculateCellMap map[string]Cell, key string, cellKeyValMap map[string]float64) (val float64, err error, has bool) {
  304. // 单元格的标签名
  305. val, ok := cellKeyValMap[key]
  306. if ok {
  307. has = true
  308. return
  309. }
  310. // 查找单元格数据
  311. cell, ok := calculateCellMap[key]
  312. if !ok {
  313. err = errors.New("查找单元格" + key + "的数据失败")
  314. return
  315. }
  316. colData := cell.CellInfo
  317. // 如果不是基础计算单元格,直接返回
  318. if colData.DataType != request.FormulateCalculateDataDT {
  319. return
  320. }
  321. // 如果是计算单元格
  322. tagList := make([]utils.CellPosition, 0)
  323. // 计算单元格relationCellList
  324. var relationCellList []request.RelationCell
  325. if colData.Extra == `` {
  326. err = errors.New(colData.Uid + "没有绑定关系")
  327. return
  328. }
  329. err = json.Unmarshal([]byte(colData.Extra), &relationCellList)
  330. if err != nil {
  331. return
  332. }
  333. for _, relation := range relationCellList {
  334. //relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  335. tmpVal, tmpErr, _ := getCalculateValueByCell(calculateCellMap, relation.Key, cellKeyValMap)
  336. if tmpErr != nil {
  337. err = tmpErr
  338. return
  339. }
  340. cellKeyValMap[relation.Key] = tmpVal
  341. rowInt, tmpErr := strconv.Atoi(relation.Row)
  342. if tmpErr != nil {
  343. err = tmpErr
  344. return
  345. }
  346. tagList = append(tagList, utils.CellPosition{
  347. Tag: relation.Tag,
  348. Row: rowInt,
  349. Value: tmpVal,
  350. })
  351. }
  352. // 计算
  353. val, _, err = calculateByCellList(strings.ToUpper(colData.Value), tagList)
  354. if err != nil {
  355. return
  356. }
  357. // 重新赋值
  358. has = true
  359. cellKeyValMap[key] = val
  360. return
  361. }
  362. // handleConfig
  363. // @Description: 处理混合表格配置
  364. // @author: Roc
  365. // @datetime2023-10-27 13:24:53
  366. // @param configList [][]request.MixedTableCellDataReq
  367. // @return newConfig [][]request.MixedTableCellDataReq
  368. // @return edbInfoIdList []int
  369. // @return dataEdbInfoIdList []int
  370. // @return err error
  371. // @return errMsg string
  372. func handleConfig(configList [][]request.MixedTableCellDataReq) (newConfig [][]request.MixedTableCellDataReq, edbInfoIdList []int, dataEdbInfoIdList []int, err error, errMsg string) {
  373. edbInfoIdList = make([]int, 0)
  374. dataEdbInfoIdList = make([]int, 0)
  375. for ck, rowList := range configList {
  376. for rk, cell := range rowList {
  377. switch cell.DataType {
  378. case request.EdbDT: // 指标信息
  379. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  380. case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
  381. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  382. case request.InsertEdbCalculateDataDT: // 插入指标计算公式生成的值
  383. var config request.CalculateConf
  384. err = json.Unmarshal([]byte(cell.Value), &config)
  385. if err != nil {
  386. return
  387. }
  388. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  389. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  390. case request.DateDT: // 日期类型
  391. if cell.DataTimeType == request.EdbDateDT {
  392. var config request.EdbDateConf
  393. err = json.Unmarshal([]byte(cell.Value), &config)
  394. if err != nil {
  395. return
  396. }
  397. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  398. } else {
  399. date, tmpErr, tmpErrMsg := handleDate(cell.DataTimeType, cell.Value)
  400. if tmpErr != nil {
  401. err = tmpErr
  402. errMsg = tmpErrMsg
  403. return
  404. }
  405. rowList[rk].DataTime = date
  406. rowList[rk].ShowValue = date
  407. }
  408. }
  409. }
  410. configList[ck] = rowList
  411. }
  412. newConfig = configList
  413. return
  414. }
  415. // HandleDate
  416. // @Description: 日期处理
  417. // @author: Roc
  418. // @datetime2023-10-27 09:37:02
  419. // @param dataTimeType int
  420. // @param val string
  421. // @return date string
  422. // @return err error
  423. // @return errMsg string
  424. func HandleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  425. return handleDate(dataTimeType, val)
  426. }
  427. // handleDate
  428. // @Description: 日期处理
  429. // @author: Roc
  430. // @datetime2023-10-27 09:36:49
  431. // @param dataTimeType int
  432. // @param val string
  433. // @return date string
  434. // @return err error
  435. // @return errMsg string
  436. func handleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  437. if val == `` {
  438. errMsg = "错误的日期数据"
  439. err = errors.New(errMsg)
  440. return
  441. }
  442. switch dataTimeType {
  443. case request.CustomDateT: //手动输入日期
  444. date = val
  445. case request.SystemDateT: // 系统日期
  446. date, err, errMsg = handleSystemDateT(val)
  447. case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
  448. default:
  449. errMsg = "错误的日期类型"
  450. err = errors.New(errMsg)
  451. return
  452. }
  453. return
  454. }
  455. // handleSystemDateT
  456. // @Description: 处理导入系统日期
  457. // @author: Roc
  458. // @datetime2023-10-27 09:36:21
  459. // @param confStr string
  460. // @return date string
  461. // @return err error
  462. // @return errMsg string
  463. func handleSystemDateT(confStr string) (date string, err error, errMsg string) {
  464. var config request.SystemDateConf
  465. err = json.Unmarshal([]byte(confStr), &config)
  466. if err != nil {
  467. return
  468. }
  469. switch config.Source {
  470. case request.SystemCurrDateT:
  471. date = time.Now().Format(utils.FormatDate)
  472. case request.SystemCalculateDateT:
  473. date, err, errMsg = handleSystemCalculateDateT(config.CalculateNum, config.CalculateFrequency)
  474. case request.SystemFrequencyDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  475. date, err, errMsg = handleSystemAppointDateT(config.Day, config.Frequency)
  476. default:
  477. errMsg = "错误的日期日期导入方式"
  478. err = errors.New(fmt.Sprint("错误的日期日期导入方式:", config.Source))
  479. return
  480. }
  481. return
  482. }
  483. // handleSystemCalculateDateT
  484. // @Description: 处理系统日期计算后的日期
  485. // @author: Roc
  486. // @datetime2023-10-27 09:31:22
  487. // @param num int
  488. // @param frequency string
  489. // @return date string
  490. // @return err error
  491. // @return errMsg string
  492. func handleSystemCalculateDateT(num int, frequency string) (date string, err error, errMsg string) {
  493. if err != nil {
  494. return
  495. }
  496. currDate := time.Now()
  497. switch frequency {
  498. case "", "日":
  499. date = currDate.AddDate(0, 0, num).Format(utils.FormatDate)
  500. default:
  501. errMsg = "错误的日期频度:" + frequency
  502. err = errors.New(errMsg)
  503. return
  504. }
  505. return
  506. }
  507. // handleSystemAppointDateT
  508. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  509. // @author: Roc
  510. // @datetime2023-10-27 09:31:35
  511. // @param Frequency string
  512. // @param Day string
  513. // @return date string
  514. // @return err error
  515. // @return errMsg string
  516. func handleSystemAppointDateT(appointDay, frequency string) (date string, err error, errMsg string) {
  517. currDate := time.Now()
  518. switch frequency {
  519. case "本周":
  520. day := int(currDate.Weekday())
  521. if day == 0 { // 周日
  522. day = 7
  523. }
  524. num := 0
  525. switch appointDay {
  526. case "周一":
  527. num = 1
  528. case "周二":
  529. num = 2
  530. case "周三":
  531. num = 3
  532. case "周四":
  533. num = 4
  534. case "周五":
  535. num = 5
  536. case "周六":
  537. num = 6
  538. case "周日":
  539. num = 7
  540. }
  541. day = num - day
  542. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  543. case "本旬":
  544. day := currDate.Day()
  545. var tmpDate time.Time
  546. switch appointDay {
  547. case "第一天":
  548. if day <= 10 {
  549. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  550. } else if day <= 20 {
  551. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  552. } else {
  553. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  554. }
  555. case "最后一天":
  556. if day <= 10 {
  557. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  558. } else if day <= 20 {
  559. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  560. } else {
  561. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  562. }
  563. }
  564. date = tmpDate.Format(utils.FormatDate)
  565. case "本月":
  566. var tmpDate time.Time
  567. switch appointDay {
  568. case "第一天":
  569. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  570. case "最后一天":
  571. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  572. }
  573. date = tmpDate.Format(utils.FormatDate)
  574. case "本季":
  575. month := currDate.Month()
  576. var tmpDate time.Time
  577. switch appointDay {
  578. case "第一天":
  579. if month <= 3 {
  580. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  581. } else if month <= 6 {
  582. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  583. } else if month <= 9 {
  584. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  585. } else {
  586. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  587. }
  588. case "最后一天":
  589. if month <= 3 {
  590. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  591. } else if month <= 6 {
  592. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  593. } else if month <= 9 {
  594. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  595. } else {
  596. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  597. }
  598. }
  599. date = tmpDate.Format(utils.FormatDate)
  600. case "本半年":
  601. month := currDate.Month()
  602. var tmpDate time.Time
  603. switch appointDay {
  604. case "第一天":
  605. if month <= 6 {
  606. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  607. } else {
  608. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  609. }
  610. case "最后一天":
  611. if month <= 6 {
  612. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  613. } else {
  614. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  615. }
  616. }
  617. date = tmpDate.Format(utils.FormatDate)
  618. case "本年":
  619. var tmpDate time.Time
  620. switch appointDay {
  621. case "第一天":
  622. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  623. case "最后一天":
  624. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  625. }
  626. date = tmpDate.Format(utils.FormatDate)
  627. default:
  628. errMsg = "错误的日期频度:" + frequency
  629. err = errors.New(errMsg)
  630. return
  631. }
  632. return
  633. }
  634. // calculateByCellList
  635. // @Description: 根据单元格来进行公式计算
  636. // @author: Roc
  637. // @datetime2023-11-14 16:17:38
  638. // @param calculateFormula string
  639. // @param tagList []utils.CellPosition
  640. // @return calVal string
  641. // @return errMsg string
  642. // @return err error
  643. func calculateByCellList(calculateFormula string, tagList []utils.CellPosition) (calVal float64, errMsg string, err error) {
  644. if calculateFormula == "" {
  645. errMsg = "公式异常"
  646. err = errors.New(errMsg)
  647. return
  648. }
  649. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  650. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  651. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  652. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  653. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  654. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  655. rowList := make([]int, 0)
  656. rowListMap := make(map[int][]utils.CellPosition)
  657. for _, v := range tagList {
  658. tmpRowList, ok := rowListMap[v.Row]
  659. if !ok {
  660. rowList = append(rowList, v.Row)
  661. tmpRowList = make([]utils.CellPosition, 0)
  662. }
  663. tmpRowList = append(tmpRowList, v)
  664. rowListMap[v.Row] = tmpRowList
  665. }
  666. sort.Ints(rowList)
  667. list := make([]utils.CellPosition, 0)
  668. for _, row := range rowList {
  669. list = append(list, rowListMap[row]...)
  670. }
  671. formulaFormStr := utils.ReplaceFormulaByCellList(list, calculateFormula)
  672. //计算公式异常,那么就移除该指标
  673. if formulaFormStr == `` {
  674. errMsg = "公式异常"
  675. err = errors.New(errMsg)
  676. return
  677. }
  678. expression := formula.NewExpression(formulaFormStr)
  679. calResult, err := expression.Evaluate()
  680. if err != nil {
  681. errMsg = "计算失败"
  682. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  683. // 分母为0的报错
  684. if strings.Contains(err.Error(), "divide by zero") {
  685. errMsg = "分母不能为0"
  686. err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
  687. }
  688. return
  689. }
  690. // 如果计算结果是NAN,那么就提示报错
  691. if calResult.IsNan() {
  692. errMsg = "计算失败"
  693. err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
  694. return
  695. }
  696. calVal, err = calResult.Float64()
  697. if err != nil {
  698. return
  699. }
  700. // 转Decimal然后四舍五入
  701. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  702. return
  703. }