mixed_table.go 23 KB

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