mixed_table.go 22 KB

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