mixed_table.go 24 KB

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