mixed_table.go 34 KB

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