mixed_table.go 24 KB

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