mixed_table.go 24 KB

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