mixed_table.go 28 KB

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