mixed_table.go 30 KB

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