time_table.go 27 KB

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