time_table.go 28 KB

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