mixed_table.go 24 KB

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