time_table.go 27 KB

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