mixed_table.go 23 KB

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