time_table.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. package excel
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/models/data_manage/excel/request"
  6. "eta_gn/eta_api/services/data"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/shopspring/decimal"
  14. "github.com/yidane/formula"
  15. )
  16. type TableDataConfig struct {
  17. EdbInfoIdList []int `description:"指标id列表,从左至右,从上到下的顺序"`
  18. Sort int `description:"日期排序,0:倒序,1:正序"`
  19. Data []ManualData `description:"数据列表"`
  20. Num int `description:"实际数据需要列出来的期数"`
  21. RemoveDate []string `description:"不展示的日期"`
  22. ManualDate []string `description:"手动配置的日期(未来的日期)"`
  23. TableEdbInfoList []TableEdbInfo `description:"表格内指标信息"`
  24. TextRowData [][]request.ManualDataReq `description:"文本列表"`
  25. DecimalConfig []request.DecimalConfig `description:"小数位数配置"`
  26. }
  27. type TableEdbInfo struct {
  28. EdbInfoId int `description:"指标ID"`
  29. Tag string `description:"标签"`
  30. EdbName string `description:"指标名称"`
  31. EdbNameEn string `description:"英文指标名称"`
  32. EdbAliasName string `description:"指标别名"`
  33. Frequency string `description:"频度"`
  34. Unit string `description:"单位"`
  35. UnitEn string `description:"英文单位"`
  36. Decimal int `description:"小数位数"`
  37. }
  38. type ManualData struct {
  39. DataType int `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算"`
  40. DataTime string `description:"所属日期"`
  41. DataTimeType int `description:"日期类型,1:实际日期;2:未来日期"`
  42. ShowValue string `description:"展示值"`
  43. Value string `description:"实际值(计算公式)"`
  44. EdbInfoId int `description:"指标id"`
  45. Tag string `description:"下标"`
  46. RelationEdbInfoList []request.RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
  47. }
  48. func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp request.TableDataReq, err error) {
  49. if len(tableDataConfig.EdbInfoIdList) <= 0 {
  50. return
  51. }
  52. if tableDataConfig.Num <= 0 {
  53. return
  54. }
  55. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  56. edbInfoIdList := make([]int, 0)
  57. tagEdbInfoIdMap := make(map[string]int)
  58. {
  59. for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  60. edbInfoIdList = append(edbInfoIdList, tableEdbInfo.EdbInfoId)
  61. tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  62. }
  63. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  64. if tmpErr != nil {
  65. err = tmpErr
  66. return
  67. }
  68. for _, v := range edbInfoList {
  69. edbInfoMap[v.EdbInfoId] = v
  70. }
  71. }
  72. manualDateMap := make(map[string]string, 0)
  73. manualDateList := make([]string, 0)
  74. for _, v := range tableDataConfig.Data {
  75. if _, ok := manualDateMap[v.DataTime]; !ok {
  76. manualDateMap[v.DataTime] = v.DataTime
  77. manualDateList = append(manualDateList, v.DataTime)
  78. }
  79. }
  80. firstEdbInfo, ok := edbInfoMap[tableDataConfig.TableEdbInfoList[0].EdbInfoId]
  81. if !ok {
  82. err = errors.New("找不到A列指标")
  83. return
  84. }
  85. baseFirstEdbInfoDataList, err := GetFirstEdbDataList(firstEdbInfo, tableDataConfig.Num, manualDateList, -1)
  86. if err != nil {
  87. return
  88. }
  89. if len(baseFirstEdbInfoDataList) <= 0 {
  90. return
  91. }
  92. firstEdbInfoDataList := make([]request.ManualDataReq, 0)
  93. if tableDataConfig.RemoveDate != nil && len(tableDataConfig.RemoveDate) > 0 {
  94. for _, v := range baseFirstEdbInfoDataList {
  95. if utils.InArrayByStr(tableDataConfig.RemoveDate, v.DataTime) {
  96. continue
  97. }
  98. firstEdbInfoDataList = append(firstEdbInfoDataList, v)
  99. }
  100. } else {
  101. firstEdbInfoDataList = baseFirstEdbInfoDataList
  102. }
  103. if len(firstEdbInfoDataList) <= 0 {
  104. return
  105. }
  106. lastRealDateTime, err := time.ParseInLocation(utils.FormatDate, firstEdbInfoDataList[0].DataTime, time.Local)
  107. if err != nil {
  108. return
  109. }
  110. dateMap := make(map[string]string)
  111. dateList := make([]string, 0)
  112. edbInfoIdDateDataMap := make(map[int]map[string]request.ManualDataReq)
  113. firstDateDataMap := make(map[string]request.ManualDataReq)
  114. for _, v := range firstEdbInfoDataList {
  115. dateList = append(dateList, v.DataTime)
  116. dateMap[v.DataTime] = v.DataTime
  117. firstDateDataMap[v.DataTime] = v
  118. }
  119. for _, manualData := range tableDataConfig.Data {
  120. if !utils.InArrayByStr(dateList, manualData.DataTime) {
  121. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, manualData.DataTime, time.Local)
  122. if tmpErr != nil {
  123. err = tmpErr
  124. return
  125. }
  126. if tmpDateTime.After(lastRealDateTime) {
  127. dateList = append(dateList, manualData.DataTime)
  128. }
  129. }
  130. }
  131. edbInfoIdDateDataMap[firstEdbInfo.EdbInfoId] = firstDateDataMap
  132. for k, edbInfoId := range tableDataConfig.EdbInfoIdList {
  133. if k == 0 {
  134. continue
  135. }
  136. tmpEdbInfo, ok := edbInfoMap[edbInfoId]
  137. if !ok {
  138. err = errors.New("找不到A列指标")
  139. return
  140. }
  141. otherDataList, tmpErr := GetOtherEdbDataList(tmpEdbInfo, dateList, -1)
  142. if tmpErr != nil {
  143. err = tmpErr
  144. return
  145. }
  146. tmpDateDataMap := make(map[string]request.ManualDataReq)
  147. for _, v := range otherDataList {
  148. tmpDateDataMap[v.DataTime] = v
  149. }
  150. edbInfoIdDateDataMap[tmpEdbInfo.EdbInfoId] = tmpDateDataMap
  151. }
  152. for _, v := range tableDataConfig.Data {
  153. tmpDate := v.DataTime
  154. if _, ok := dateMap[tmpDate]; !ok {
  155. dateMap[v.DataTime] = tmpDate
  156. }
  157. edbInfoIdDateData, ok := edbInfoIdDateDataMap[v.EdbInfoId]
  158. if !ok {
  159. edbInfoIdDateData = make(map[string]request.ManualDataReq)
  160. }
  161. tmpManualData, ok := edbInfoIdDateData[tmpDate]
  162. if !ok {
  163. edbInfoIdDateData[tmpDate] = request.ManualDataReq{
  164. DataType: v.DataType,
  165. DataTime: v.DataTime,
  166. ShowValue: v.ShowValue,
  167. Value: v.Value,
  168. }
  169. } else {
  170. if (tmpManualData.DataType == 3 || tmpManualData.DataType == 4) && tmpManualData.ShowValue == `` {
  171. tmpManualData.DataType = v.DataType
  172. tmpManualData.ShowValue = v.ShowValue
  173. tmpManualData.Value = v.Value
  174. tmpManualData.RelationEdbInfoList = v.RelationEdbInfoList
  175. edbInfoIdDateData[tmpDate] = tmpManualData
  176. }
  177. }
  178. edbInfoIdDateDataMap[v.EdbInfoId] = edbInfoIdDateData
  179. }
  180. sortDateTimeList := make([]time.Time, 0)
  181. {
  182. sortDateList := dateList
  183. if tableDataConfig.Sort == 1 {
  184. baseDateList := utils.StrArr{}
  185. baseDateList = append(baseDateList, sortDateList...)
  186. sort.Sort(baseDateList)
  187. sortDateList = append([]string{}, baseDateList...)
  188. } else {
  189. sort.Strings(sortDateList)
  190. }
  191. for _, v := range sortDateList {
  192. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, v, time.Local)
  193. if tmpErr != nil {
  194. err = tmpErr
  195. return
  196. }
  197. sortDateTimeList = append(sortDateTimeList, tmpDateTime)
  198. }
  199. }
  200. tableDataMap, textRowListDataResp := handleTable(tagEdbInfoIdMap, lastRealDateTime, sortDateTimeList, edbInfoIdDateDataMap, tableDataConfig.Data, tableDataConfig.TextRowData)
  201. data := make([]request.EdbInfoData, 0)
  202. for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  203. tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  204. manualDataReqList := make([]request.ManualDataReq, 0)
  205. tmpEdbInfoData := request.EdbInfoData{
  206. EdbInfoId: tableEdbInfo.EdbInfoId,
  207. Tag: tableEdbInfo.Tag,
  208. EdbName: tableEdbInfo.EdbName,
  209. EdbNameEn: tableEdbInfo.EdbNameEn,
  210. EdbAliasName: tableEdbInfo.EdbAliasName,
  211. Frequency: tableEdbInfo.Frequency,
  212. Decimal: tableEdbInfo.Decimal,
  213. Unit: tableEdbInfo.Unit,
  214. UnitEn: tableEdbInfo.UnitEn,
  215. Data: manualDataReqList,
  216. }
  217. edbInfo, ok := edbInfoMap[tableEdbInfo.EdbInfoId]
  218. if ok {
  219. tmpEdbInfoData.EdbName = edbInfo.EdbName
  220. tmpEdbInfoData.EdbNameEn = edbInfo.EdbNameEn
  221. tmpEdbInfoData.Frequency = edbInfo.Frequency
  222. tmpEdbInfoData.Unit = edbInfo.Unit
  223. tmpEdbInfoData.UnitEn = edbInfo.UnitEn
  224. tmpEdbInfoData.ClassifyId = edbInfo.ClassifyId
  225. tmpEdbInfoData.IsJoinPermission = edbInfo.IsJoinPermission
  226. }
  227. for index, dateTime := range sortDateTimeList {
  228. dataTimeType := 1
  229. if dateTime.After(lastRealDateTime) {
  230. dataTimeType = 2
  231. }
  232. tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  233. rowData, ok := tableDataMap[index+1]
  234. if !ok {
  235. manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  236. DataType: 3,
  237. DataTime: tmpDateTimeStr,
  238. Decimal: tableEdbInfo.Decimal,
  239. DataTimeType: dataTimeType,
  240. ShowValue: "",
  241. Value: "",
  242. RelationEdbInfoList: nil,
  243. })
  244. continue
  245. }
  246. tmpData, ok := rowData[tableEdbInfo.Tag]
  247. if !ok {
  248. manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  249. DataType: 3,
  250. DataTime: tmpDateTimeStr,
  251. Decimal: tableEdbInfo.Decimal,
  252. DataTimeType: dataTimeType,
  253. ShowValue: "",
  254. Value: "",
  255. RelationEdbInfoList: nil,
  256. })
  257. continue
  258. }
  259. tmpData.DataTimeType = dataTimeType
  260. manualDataReqList = append(manualDataReqList, tmpData)
  261. }
  262. tmpEdbInfoData.Data = manualDataReqList
  263. data = append(data, tmpEdbInfoData)
  264. }
  265. for _, d := range data {
  266. for k2, d2 := range d.Data {
  267. vf, e := strconv.ParseFloat(d2.ShowValue, 64)
  268. if e != nil {
  269. continue
  270. }
  271. d.Data[k2].ShowValue = utils.FormatTableDataShowValue(vf)
  272. }
  273. }
  274. for _, d := range textRowListDataResp {
  275. for k2, d2 := range d {
  276. vf, e := strconv.ParseFloat(d2.ShowValue, 64)
  277. if e != nil {
  278. continue
  279. }
  280. d[k2].ShowValue = utils.FormatTableDataShowValue(vf)
  281. }
  282. }
  283. resultResp = request.TableDataReq{
  284. EdbInfoIdList: edbInfoIdList,
  285. Sort: tableDataConfig.Sort,
  286. TextRowData: textRowListDataResp,
  287. Data: data,
  288. }
  289. return
  290. }
  291. func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableDataConfig, err error) {
  292. tableDataConfig.EdbInfoIdList = reqData.EdbInfoIdList
  293. tableDataConfig.Sort = reqData.Sort
  294. if len(reqData.Data) <= 0 {
  295. err = errors.New("数据不能为空")
  296. return
  297. }
  298. var startDate string
  299. var firstEdbInfoId int
  300. manualDataList := make([]ManualData, 0)
  301. tableEdbInfoList := make([]TableEdbInfo, 0)
  302. firstDateMap := make(map[string]string)
  303. manualDateMap := make(map[string]string)
  304. for _, v := range reqData.Data {
  305. tmpTableEdbInfo := TableEdbInfo{
  306. EdbInfoId: v.EdbInfoId,
  307. Tag: v.Tag,
  308. EdbName: v.EdbName,
  309. EdbNameEn: v.EdbNameEn,
  310. EdbAliasName: v.EdbAliasName,
  311. Frequency: v.Frequency,
  312. Unit: v.Unit,
  313. UnitEn: v.UnitEn,
  314. Decimal: v.Decimal,
  315. }
  316. tableEdbInfoList = append(tableEdbInfoList, tmpTableEdbInfo)
  317. if v.Tag == "A" {
  318. firstEdbInfoId = v.EdbInfoId
  319. lenData := len(v.Data)
  320. if lenData <= 0 {
  321. err = errors.New("A列不能为空")
  322. return
  323. }
  324. index := 0
  325. if reqData.Sort == 1 {
  326. index = lenData - 1
  327. }
  328. startDate = v.Data[index].DataTime
  329. for _, data := range v.Data {
  330. firstDateMap[data.DataTime] = data.DataTime
  331. if data.DataTimeType == 2 {
  332. manualDateMap[data.DataTime] = data.DataTime
  333. }
  334. }
  335. }
  336. for _, data := range v.Data {
  337. if data.DataType == 3 || data.DataType == 4 {
  338. tmpManualData := ManualData{
  339. DataType: data.DataType,
  340. DataTime: data.DataTime,
  341. DataTimeType: data.DataTimeType,
  342. ShowValue: data.ShowValue,
  343. Value: data.Value,
  344. EdbInfoId: v.EdbInfoId,
  345. Tag: v.Tag,
  346. RelationEdbInfoList: data.RelationEdbInfoList,
  347. }
  348. if data.DataType == 4 {
  349. tmpManualData.ShowValue = ``
  350. }
  351. manualDataList = append(manualDataList, tmpManualData)
  352. }
  353. }
  354. }
  355. num := len(reqData.Data[0].Data)
  356. removeDate := make([]string, 0)
  357. {
  358. firstDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  359. if tmpErr != nil {
  360. err = tmpErr
  361. return
  362. }
  363. edbInfo, tmpErr := data_manage.GetEdbInfoById(firstEdbInfoId)
  364. if tmpErr != nil {
  365. err = tmpErr
  366. return
  367. }
  368. var firstDataList []*data_manage.EdbDataList
  369. switch edbInfo.EdbInfoType {
  370. case 0:
  371. firstDataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  372. case 1:
  373. _, firstDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  374. default:
  375. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  376. }
  377. if err != nil {
  378. return
  379. }
  380. baseDataList := make([]*data_manage.EdbDataList, 0)
  381. for _, data := range firstDataList {
  382. tmpDate := data.DataTime
  383. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  384. if tmpErr != nil {
  385. err = tmpErr
  386. return
  387. }
  388. if tmpDateTime.Before(firstDateTime) {
  389. continue
  390. }
  391. baseDataList = append(baseDataList, data)
  392. }
  393. for _, tmpData := range baseDataList {
  394. if _, ok := firstDateMap[tmpData.DataTime]; !ok {
  395. removeDate = append(removeDate, tmpData.DataTime)
  396. }
  397. }
  398. }
  399. tableDataConfig.Num = num
  400. tableDataConfig.RemoveDate = removeDate
  401. tableDataConfig.Data = manualDataList
  402. tableDataConfig.TableEdbInfoList = tableEdbInfoList
  403. tableDataConfig.TextRowData = reqData.TextRowData
  404. return
  405. }
  406. func handleTable(tagEdbInfoIdMap map[string]int, lastRealDateTime time.Time, sortDateTimeList []time.Time, edbInfoIdDateDataMap map[int]map[string]request.ManualDataReq, manualDataList []ManualData, textRowData [][]request.ManualDataReq) (tableDataMap map[int]map[string]request.ManualDataReq, textRowListDataResp [][]request.ManualDataReq) {
  407. tagList := make([]string, 0)
  408. for tag, _ := range tagEdbInfoIdMap {
  409. tagList = append(tagList, tag)
  410. }
  411. sort.Strings(tagList)
  412. tableDataMap = make(map[int]map[string]request.ManualDataReq) //行、列数据
  413. dateIndexMap := make(map[string]int)
  414. for k, dateTime := range sortDateTimeList {
  415. rowDataMap := make(map[string]request.ManualDataReq)
  416. dataTimeType := 1
  417. if dateTime.After(lastRealDateTime) {
  418. dataTimeType = 2
  419. }
  420. tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  421. dateIndexMap[tmpDateTimeStr] = k + 1
  422. for _, tag := range tagList {
  423. edbInfoId, ok := tagEdbInfoIdMap[tag]
  424. if !ok { // 没有找到该指标的映射关系,那么就用空串填补
  425. rowDataMap[tag] = request.ManualDataReq{
  426. DataType: 3,
  427. DataTime: tmpDateTimeStr,
  428. DataTimeType: dataTimeType,
  429. ShowValue: "",
  430. Value: "",
  431. RelationEdbInfoList: nil,
  432. }
  433. continue
  434. }
  435. dateDataMap, ok := edbInfoIdDateDataMap[edbInfoId]
  436. if !ok { // 没有找到该指标的数据,那么就用空串填补
  437. rowDataMap[tag] = request.ManualDataReq{
  438. DataType: 3,
  439. DataTime: tmpDateTimeStr,
  440. DataTimeType: dataTimeType,
  441. ShowValue: "",
  442. Value: "",
  443. RelationEdbInfoList: nil,
  444. }
  445. continue
  446. }
  447. tmpData, ok := dateDataMap[tmpDateTimeStr]
  448. if !ok { // 该指标没有找到对应日期的数据,那么就用空串填补
  449. rowDataMap[tag] = request.ManualDataReq{
  450. DataType: 3,
  451. DataTime: tmpDateTimeStr,
  452. DataTimeType: dataTimeType,
  453. ShowValue: "",
  454. Value: "",
  455. RelationEdbInfoList: nil,
  456. }
  457. continue
  458. }
  459. tmpData.DataTimeType = dataTimeType
  460. rowDataMap[tag] = tmpData
  461. }
  462. tableDataMap[k+1] = rowDataMap
  463. }
  464. for _, manualData := range manualDataList {
  465. index, ok := dateIndexMap[manualData.DataTime]
  466. if !ok {
  467. continue
  468. }
  469. rowDataMap, ok := tableDataMap[index]
  470. if !ok {
  471. continue
  472. }
  473. tmpData, ok := rowDataMap[manualData.Tag]
  474. if !ok {
  475. continue
  476. }
  477. if utils.InArrayByInt([]int{1, 2, 5}, tmpData.DataType) {
  478. continue
  479. }
  480. if tmpData.DataType == 3 {
  481. tmpData.ShowValue = manualData.ShowValue
  482. tmpData.Value = manualData.Value
  483. tableDataMap[index][manualData.Tag] = tmpData
  484. continue
  485. }
  486. tmpData.DataType = manualData.DataType
  487. tmpData.ShowValue = ``
  488. tmpData.Value = manualData.Value
  489. tmpData.RelationEdbInfoList = manualData.RelationEdbInfoList
  490. tableDataMap[index][manualData.Tag] = tmpData
  491. }
  492. lenTableData := len(tableDataMap)
  493. firstColTextRowList := make([]request.ManualDataReq, 0)
  494. tmpTextRowList := make([][]request.ManualDataReq, 0)
  495. for k, textRowList := range textRowData {
  496. if len(tagList)+1 != len(textRowList) {
  497. continue
  498. }
  499. rowDataMap := make(map[string]request.ManualDataReq)
  500. tmpTextRow := make([]request.ManualDataReq, 0)
  501. for index, textRow := range textRowList {
  502. if index == 0 {
  503. firstColTextRowList = append(firstColTextRowList, textRow)
  504. continue
  505. }
  506. rowDataMap[tagList[index-1]] = textRow
  507. tmpTextRow = append(tmpTextRow, textRow)
  508. }
  509. tableDataMap[lenTableData+k+1] = rowDataMap
  510. tmpTextRowList = append(tmpTextRowList, tmpTextRow)
  511. }
  512. calculateCellMap := make(map[string]string)
  513. for _, manualData := range manualDataList {
  514. index, ok := dateIndexMap[manualData.DataTime]
  515. if !ok {
  516. continue
  517. }
  518. rowDataMap, ok := tableDataMap[index]
  519. if !ok {
  520. continue
  521. }
  522. colData, ok := rowDataMap[manualData.Tag]
  523. if !ok {
  524. continue
  525. }
  526. if colData.DataType != 4 {
  527. continue
  528. }
  529. tagMap := make(map[string]float64)
  530. lenRelation := len(colData.RelationEdbInfoList)
  531. replaceNum := 0
  532. for _, relation := range colData.RelationEdbInfoList {
  533. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  534. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  535. if tmpErr != nil {
  536. continue
  537. }
  538. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  539. if tmpErr != nil {
  540. continue
  541. }
  542. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  543. replaceNum++
  544. }
  545. if lenRelation != replaceNum {
  546. continue
  547. }
  548. val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  549. if err != nil {
  550. continue
  551. }
  552. colData.ShowValue = val
  553. tableDataMap[index][manualData.Tag] = colData
  554. }
  555. for k, textRow := range tmpTextRowList {
  556. index := lenTableData + k + 1
  557. rowDataMap, ok := tableDataMap[index]
  558. if !ok {
  559. continue
  560. }
  561. for colIndex, _ := range textRow {
  562. currTag := tagList[colIndex]
  563. colData, ok := rowDataMap[currTag]
  564. if !ok {
  565. continue
  566. }
  567. if colData.DataType != 4 {
  568. continue
  569. }
  570. tagMap := make(map[string]float64)
  571. lenRelation := len(colData.RelationEdbInfoList)
  572. replaceNum := 0
  573. for _, relation := range colData.RelationEdbInfoList {
  574. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  575. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  576. if tmpErr != nil {
  577. continue
  578. }
  579. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  580. if tmpErr != nil {
  581. continue
  582. }
  583. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  584. replaceNum++
  585. }
  586. if lenRelation != replaceNum {
  587. continue
  588. }
  589. val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  590. if err != nil {
  591. continue
  592. }
  593. colData.ShowValue = val
  594. tableDataMap[index][currTag] = colData
  595. }
  596. }
  597. for k, colData := range firstColTextRowList {
  598. if colData.DataType != 4 {
  599. continue
  600. }
  601. tagMap := make(map[string]float64)
  602. lenRelation := len(colData.RelationEdbInfoList)
  603. replaceNum := 0
  604. for _, relation := range colData.RelationEdbInfoList {
  605. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  606. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  607. if tmpErr != nil {
  608. continue
  609. }
  610. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  611. if tmpErr != nil {
  612. continue
  613. }
  614. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  615. replaceNum++
  616. }
  617. if lenRelation != replaceNum {
  618. continue
  619. }
  620. val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  621. if err != nil {
  622. continue
  623. }
  624. colData.ShowValue = val
  625. firstColTextRowList[k] = colData
  626. }
  627. {
  628. textRowListDataResp = make([][]request.ManualDataReq, 0)
  629. newLenTableDataMap := len(tableDataMap)
  630. firstTextRow := lenTableData + 1
  631. for i := firstTextRow; i <= newLenTableDataMap; i++ {
  632. textRowDataResp := make([]request.ManualDataReq, 0)
  633. textRowDataResp = append(textRowDataResp, firstColTextRowList[i-firstTextRow])
  634. for _, tmpTag := range tagList {
  635. textRowDataResp = append(textRowDataResp, tableDataMap[i][tmpTag])
  636. }
  637. textRowListDataResp = append(textRowListDataResp, textRowDataResp)
  638. }
  639. }
  640. return
  641. }
  642. func getCalculateValue(tableDataMap map[int]map[string]request.ManualDataReq, tag, row string, calculateCellMap map[string]string) (val string, err error) {
  643. rowInt, err := strconv.Atoi(row)
  644. if err != nil {
  645. return
  646. }
  647. cellTagName := strings.ToUpper(tag) + row
  648. val, ok := calculateCellMap[cellTagName]
  649. if ok {
  650. return
  651. }
  652. rowData, ok := tableDataMap[rowInt]
  653. if !ok {
  654. err = errors.New("查找" + row + "行的数据失败")
  655. return
  656. }
  657. colData, ok := rowData[tag]
  658. if !ok {
  659. err = errors.New("查找单元格" + tag + row + "的数据失败")
  660. return
  661. }
  662. if colData.DataType != 4 {
  663. val = colData.ShowValue
  664. return
  665. }
  666. calculateCellMap[cellTagName] = ``
  667. tagMap := make(map[string]float64)
  668. for _, relation := range colData.RelationEdbInfoList {
  669. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  670. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  671. if tmpErr != nil {
  672. err = tmpErr
  673. return
  674. }
  675. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  676. if tmpErr != nil {
  677. err = tmpErr
  678. return
  679. }
  680. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  681. }
  682. val, _, err = calculate(strings.ToUpper(colData.Value), tagMap)
  683. if err != nil {
  684. return
  685. }
  686. colData.ShowValue = val
  687. tableDataMap[rowInt][tag] = colData
  688. calculateCellMap[cellTagName] = val
  689. return
  690. }
  691. func calculate(calculateFormula string, TagMap map[string]float64) (calVal, errMsg string, err error) {
  692. if calculateFormula == "" {
  693. errMsg = "公式异常"
  694. err = errors.New(errMsg)
  695. return
  696. }
  697. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  698. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  699. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  700. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  701. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  702. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  703. formulaFormStr := utils.ReplaceFormula(TagMap, calculateFormula)
  704. if formulaFormStr == `` {
  705. errMsg = "公式异常"
  706. err = errors.New(errMsg)
  707. return
  708. }
  709. expression := formula.NewExpression(formulaFormStr)
  710. calResult, err := expression.Evaluate()
  711. if err != nil {
  712. errMsg = "计算失败"
  713. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  714. if strings.Contains(err.Error(), "divide by zero") {
  715. errMsg = "分母不能为0"
  716. err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
  717. }
  718. return
  719. }
  720. if calResult.IsNan() {
  721. errMsg = "计算失败"
  722. err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
  723. return
  724. }
  725. calVal = calResult.String()
  726. valDecimal, err := decimal.NewFromString(calVal)
  727. if err != nil {
  728. errMsg = "计算失败"
  729. err = errors.New("计算失败,结果转 Decimal 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  730. return
  731. }
  732. calVal = valDecimal.Round(4).String()
  733. return
  734. }