time_table.go 25 KB

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