excel_info.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/excel"
  7. "eta/eta_api/models/data_manage/excel/request"
  8. "eta/eta_api/models/data_manage/excel/response"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/data"
  11. "eta/eta_api/services/data/data_manage_permission"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/shopspring/decimal"
  15. "github.com/yidane/formula"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // GetExcelDetailInfoByExcelInfoId 根据表格id获取表格详情
  22. func GetExcelDetailInfoByExcelInfoId(excelInfoId, sysUserId int) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  23. errMsg = `获取失败`
  24. //获取eta表格信息
  25. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  26. if err != nil {
  27. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  28. if err.Error() == utils.ErrNoRow() {
  29. errMsg = "ETA表格被删除,请刷新页面"
  30. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  31. }
  32. return
  33. }
  34. return formatExcelInfo2Detail(excelInfo, sysUserId)
  35. }
  36. // GetExcelDetailInfoByUnicode 根据表格编码获取表格详情
  37. func GetExcelDetailInfoByUnicode(unicode string, sysUserId int) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  38. errMsg = `获取失败`
  39. // 获取eta表格信息
  40. excelInfo, err := excel.GetExcelInfoByUnicode(unicode)
  41. if err != nil {
  42. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  43. if err.Error() == utils.ErrNoRow() {
  44. errMsg = "ETA表格被删除,请刷新页面"
  45. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  46. }
  47. return
  48. }
  49. return formatExcelInfo2Detail(excelInfo, sysUserId)
  50. }
  51. func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, sysUserId int) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  52. // 数据权限
  53. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelInfo.ExcelInfoId, excelInfo.ExcelClassifyId, excelInfo.IsJoinPermission, sysUserId)
  54. if err != nil {
  55. err = errors.New("获取表格权限信息失败,Err" + err.Error())
  56. return
  57. }
  58. excelDetail = response.ExcelInfoDetail{
  59. ExcelInfoId: excelInfo.ExcelInfoId,
  60. Source: excelInfo.Source,
  61. ExcelType: excelInfo.ExcelType,
  62. ExcelName: excelInfo.ExcelName,
  63. UniqueCode: excelInfo.UniqueCode,
  64. ExcelClassifyId: excelInfo.ExcelClassifyId,
  65. SysUserId: excelInfo.SysUserId,
  66. SysUserRealName: excelInfo.SysUserRealName,
  67. Content: excelInfo.Content,
  68. ExcelImage: excelInfo.ExcelImage,
  69. FileUrl: excelInfo.FileUrl,
  70. Sort: excelInfo.Sort,
  71. IsDelete: excelInfo.IsDelete,
  72. ModifyTime: excelInfo.ModifyTime,
  73. CreateTime: excelInfo.CreateTime,
  74. TableData: nil,
  75. HaveOperaAuth: haveOperaAuth,
  76. }
  77. // 无权限,不需要返回数据
  78. if !haveOperaAuth {
  79. return
  80. }
  81. switch excelInfo.Source {
  82. case utils.TIME_TABLE: // 时间序列表格
  83. var tableDataConfig TableDataConfig
  84. err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
  85. if err != nil {
  86. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  87. return
  88. }
  89. result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
  90. if tmpErr != nil {
  91. err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
  92. return
  93. }
  94. if len(result.EdbInfoIdList) > 0 {
  95. classifyIdList := make([]int, 0)
  96. for _, v := range result.Data {
  97. classifyIdList = append(classifyIdList, v.ClassifyId)
  98. }
  99. classifyMap := make(map[int]*data_manage.EdbClassify)
  100. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  101. if tmpErr != nil {
  102. err = errors.New("获取分类列表失败,Err:" + tmpErr.Error())
  103. return
  104. }
  105. for _, v := range classifyList {
  106. classifyMap[v.ClassifyId] = v
  107. }
  108. // 获取所有有权限的指标和分类
  109. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  110. if err != nil {
  111. err = errors.New("获取所有有权限的指标和分类失败,Err:" + tmpErr.Error())
  112. return
  113. }
  114. for i, v := range result.Data {
  115. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  116. result.Data[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  117. }
  118. }
  119. }
  120. excelDetail.TableData = result
  121. case utils.MIXED_TABLE: // 混合表格
  122. var result request.MixedTableReq
  123. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  124. if err != nil {
  125. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  126. return
  127. }
  128. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result)
  129. if tmpErr != nil {
  130. errMsg = "获取失败"
  131. if tmpErrMsg != `` {
  132. errMsg = tmpErrMsg
  133. }
  134. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  135. return
  136. }
  137. result.Data = newData
  138. excelDetail.TableData = result
  139. }
  140. return
  141. }
  142. // GetExcelInfoOpButton 获取ETA表格的操作权限
  143. func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int, haveOperaAuth bool) (button response.ExcelInfoDetailButton) {
  144. // 如果没有数据权限,那么直接返回
  145. if !haveOperaAuth {
  146. return
  147. }
  148. //非管理员角色查看其他用户创建的表格,可刷新、另存为、下载表格;
  149. button.RefreshButton = true
  150. button.CopyButton = true
  151. button.DownloadButton = true
  152. // 1、本用户创建的表格,可编辑、刷新、另存为、下载、删除,删除需二次确认;
  153. // 2、管理员角色对所有表格有如上权限;
  154. // 3、在线excel所有人都能编辑
  155. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || source == utils.EXCEL_DEFAULT {
  156. button.OpButton = true
  157. button.DeleteButton = true
  158. }
  159. // 自定义分析
  160. if source == utils.CUSTOM_ANALYSIS_TABLE {
  161. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  162. button.OpEdbButton = true // 生成、查看指标按钮
  163. button.RefreshEdbButton = true // 刷新指标按钮
  164. }
  165. }
  166. return
  167. }
  168. // GetFirstEdbDataList 获取第一列的数据
  169. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
  170. var dataList []*data_manage.EdbDataList
  171. switch edbInfo.EdbInfoType {
  172. case 0:
  173. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  174. case 1:
  175. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  176. default:
  177. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  178. }
  179. if err != nil {
  180. return
  181. }
  182. // 获取需要的期数
  183. lenData := len(dataList)
  184. if lenData <= 0 {
  185. return
  186. }
  187. tmpManualDateNum := 0 // 手工数据的期数
  188. lenManualDate := len(manualDateList)
  189. if lenManualDate > 0 {
  190. sortDateList := manualDateList
  191. baseDateList := utils.StrArr{}
  192. baseDateList = append(baseDateList, sortDateList...)
  193. sort.Sort(baseDateList)
  194. sortDateList = append([]string{}, baseDateList...)
  195. lastData := dataList[lenData-1]
  196. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  197. if tmpErr != nil {
  198. err = tmpErr
  199. return
  200. }
  201. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  202. for _, tmpDateStr := range sortDateList {
  203. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  204. if tmpErr != nil {
  205. err = tmpErr
  206. return
  207. }
  208. if tmpDate.After(lastDataDate) {
  209. tmpManualDateNum++
  210. continue
  211. }
  212. break
  213. }
  214. }
  215. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  216. num = num - tmpManualDateNum
  217. if num > lenData {
  218. num = lenData
  219. }
  220. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  221. for i := 1; i <= num; i++ {
  222. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  223. dataType := 1
  224. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  225. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  226. dataType = 5
  227. }
  228. resultDataList = append(resultDataList, request.ManualDataReq{
  229. DataType: dataType,
  230. DataTime: dataList[lenData-i].DataTime,
  231. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  232. Value: fmt.Sprint(dataList[lenData-i].Value),
  233. DataTimeType: 1,
  234. })
  235. }
  236. return
  237. }
  238. // GetOtherEdbDataList 获取其他列的数据
  239. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  240. lenDate := len(dateList)
  241. if lenDate <= 0 {
  242. return
  243. }
  244. sortDateList := dateList
  245. baseDateList := utils.StrArr{}
  246. baseDateList = append(baseDateList, sortDateList...)
  247. sort.Sort(baseDateList)
  248. sortDateList = append([]string{}, baseDateList...)
  249. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  250. if err != nil {
  251. return
  252. }
  253. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  254. if err != nil {
  255. return
  256. }
  257. var dataList []*data_manage.EdbDataList
  258. switch edbInfo.EdbInfoType {
  259. case 0:
  260. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  261. case 1:
  262. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  263. default:
  264. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  265. }
  266. if err != nil {
  267. return
  268. }
  269. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  270. baseDataList := make([]*data_manage.EdbDataList, 0)
  271. var lastData *data_manage.EdbDataList
  272. var isInsert bool
  273. for _, data := range dataList {
  274. tmpDate := data.DataTime
  275. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  276. if tmpErr != nil {
  277. err = tmpErr
  278. return
  279. }
  280. if tmpDateTime.Before(firstDateTime) {
  281. lastData = data
  282. continue
  283. }
  284. // 如果是第一次写入数据
  285. if !isInsert && lastData != nil {
  286. baseDataList = append(baseDataList, lastData)
  287. }
  288. if tmpDateTime.After(endDateTime) {
  289. baseDataList = append(baseDataList, data)
  290. break
  291. }
  292. baseDataList = append(baseDataList, data)
  293. isInsert = true
  294. }
  295. // 实际数据的日期map
  296. realValMap := make(map[string]string)
  297. for _, v := range baseDataList {
  298. realValMap[v.DataTime] = v.DataTime
  299. }
  300. // 插值法处理
  301. handleDataMap := make(map[string]float64)
  302. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  303. if err != nil {
  304. return
  305. }
  306. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  307. // 对于不存在的数据做补充
  308. for _, date := range sortDateList {
  309. dataType := 1
  310. if _, ok := realValMap[date]; !ok {
  311. dataType = 2
  312. } else {
  313. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  314. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  315. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  316. dataType = 5
  317. }
  318. }
  319. var value, showValue string
  320. if tmpVal, ok := handleDataMap[date]; ok {
  321. value = fmt.Sprint(tmpVal)
  322. showValue = value
  323. } else {
  324. dataType = 3
  325. }
  326. resultDataList = append(resultDataList, request.ManualDataReq{
  327. DataType: dataType,
  328. DataTime: date,
  329. ShowValue: showValue,
  330. Value: value,
  331. })
  332. }
  333. return
  334. }
  335. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  336. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  337. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  338. if err != nil {
  339. return
  340. }
  341. var dataList []*data_manage.EdbDataList
  342. switch edbInfo.EdbInfoType {
  343. case 0:
  344. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  345. case 1:
  346. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  347. default:
  348. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  349. }
  350. if err != nil {
  351. return
  352. }
  353. // 获取需要的期数
  354. lenData := len(dataList)
  355. if lenData <= 0 {
  356. return
  357. }
  358. lastData := dataList[lenData-1]
  359. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  360. if err != nil {
  361. return
  362. }
  363. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  364. dataList = dataList[:lenData-1]
  365. lenData = len(dataList)
  366. }
  367. if num > lenData {
  368. num = lenData
  369. }
  370. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  371. for i := 1; i <= num; i++ {
  372. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  373. dataType := 1
  374. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  375. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  376. dataType = 5
  377. }
  378. resultDataList = append(resultDataList, request.ManualDataReq{
  379. DataType: dataType,
  380. DataTime: dataList[lenData-i].DataTime,
  381. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  382. Value: fmt.Sprint(dataList[lenData-i].Value),
  383. })
  384. }
  385. return
  386. }
  387. type TableDataConfig struct {
  388. EdbInfoIdList []int `description:"指标id列表,从左至右,从上到下的顺序"`
  389. Sort int `description:"日期排序,0:倒序,1:正序"`
  390. Data []ManualData `description:"数据列表"`
  391. Num int `description:"实际数据需要列出来的期数"`
  392. RemoveDate []string `description:"不展示的日期"`
  393. ManualDate []string `description:"手动配置的日期(未来的日期)"`
  394. TableEdbInfoList []TableEdbInfo `description:"表格内指标信息"`
  395. TextRowData [][]request.ManualDataReq `description:"文本列表"`
  396. }
  397. type TableEdbInfo struct {
  398. EdbInfoId int `description:"指标ID"`
  399. Tag string `description:"标签"`
  400. EdbName string `description:"指标名称"`
  401. EdbAliasName string `description:"指标别名"`
  402. Frequency string `description:"频度"`
  403. Unit string `description:"单位"`
  404. }
  405. type ManualData struct {
  406. DataType int `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算"`
  407. DataTime string `description:"所属日期"`
  408. DataTimeType int `description:"日期类型,1:实际日期;2:未来日期"`
  409. ShowValue string `description:"展示值"`
  410. Value string `description:"实际值(计算公式)"`
  411. EdbInfoId int `description:"指标id"`
  412. Tag string `description:"下标"`
  413. RelationEdbInfoList []request.RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
  414. }
  415. // GetTableDataConfig 根据TableDataReq配置获取相关数据配置
  416. func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableDataConfig, err error) {
  417. // 指标数据
  418. tableDataConfig.EdbInfoIdList = reqData.EdbInfoIdList
  419. tableDataConfig.Sort = reqData.Sort
  420. if len(reqData.Data) <= 0 {
  421. err = errors.New("数据不能为空")
  422. return
  423. }
  424. // 开始日期
  425. var startDate string
  426. // A列的指标id
  427. var firstEdbInfoId int
  428. // 手工操作的数据列
  429. manualDataList := make([]ManualData, 0)
  430. // 指标配置列表
  431. tableEdbInfoList := make([]TableEdbInfo, 0)
  432. // 第一列的日期map
  433. firstDateMap := make(map[string]string)
  434. manualDateMap := make(map[string]string)
  435. for _, v := range reqData.Data {
  436. // 指标信息
  437. tmpTableEdbInfo := TableEdbInfo{
  438. EdbInfoId: v.EdbInfoId,
  439. Tag: v.Tag,
  440. EdbName: v.EdbName,
  441. EdbAliasName: v.EdbAliasName,
  442. Frequency: v.Frequency,
  443. Unit: v.Unit,
  444. }
  445. tableEdbInfoList = append(tableEdbInfoList, tmpTableEdbInfo)
  446. // 确定数据A列
  447. if v.Tag == "A" {
  448. firstEdbInfoId = v.EdbInfoId
  449. lenData := len(v.Data)
  450. if lenData <= 0 {
  451. err = errors.New("A列不能为空")
  452. return
  453. }
  454. index := 0
  455. if reqData.Sort == 1 {
  456. // 倒序
  457. index = lenData - 1
  458. }
  459. startDate = v.Data[index].DataTime
  460. // 存在的日期列表
  461. for _, data := range v.Data {
  462. firstDateMap[data.DataTime] = data.DataTime
  463. if data.DataTimeType == 2 {
  464. manualDateMap[data.DataTime] = data.DataTime
  465. }
  466. }
  467. }
  468. for _, data := range v.Data {
  469. if data.DataType == 3 || data.DataType == 4 {
  470. tmpManualData := ManualData{
  471. DataType: data.DataType,
  472. DataTime: data.DataTime,
  473. DataTimeType: data.DataTimeType,
  474. ShowValue: data.ShowValue,
  475. Value: data.Value,
  476. EdbInfoId: v.EdbInfoId,
  477. Tag: v.Tag,
  478. RelationEdbInfoList: data.RelationEdbInfoList,
  479. }
  480. if data.DataType == 4 {
  481. tmpManualData.ShowValue = ``
  482. }
  483. manualDataList = append(manualDataList, tmpManualData)
  484. }
  485. }
  486. }
  487. // 总共需要的期数
  488. num := len(reqData.Data[0].Data)
  489. removeDate := make([]string, 0)
  490. // 获取期数
  491. {
  492. firstDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  493. if tmpErr != nil {
  494. err = tmpErr
  495. return
  496. }
  497. edbInfo, tmpErr := data_manage.GetEdbInfoById(firstEdbInfoId)
  498. if tmpErr != nil {
  499. err = tmpErr
  500. return
  501. }
  502. var firstDataList []*data_manage.EdbDataList
  503. switch edbInfo.EdbInfoType {
  504. case 0:
  505. firstDataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  506. case 1:
  507. _, firstDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  508. default:
  509. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  510. }
  511. if err != nil {
  512. return
  513. }
  514. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  515. baseDataList := make([]*data_manage.EdbDataList, 0)
  516. for _, data := range firstDataList {
  517. tmpDate := data.DataTime
  518. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  519. if tmpErr != nil {
  520. err = tmpErr
  521. return
  522. }
  523. if tmpDateTime.Before(firstDateTime) {
  524. continue
  525. }
  526. baseDataList = append(baseDataList, data)
  527. }
  528. // 筛选出需要删除的日期
  529. for _, tmpData := range baseDataList {
  530. //firstDateMap{}
  531. if _, ok := firstDateMap[tmpData.DataTime]; !ok {
  532. removeDate = append(removeDate, tmpData.DataTime)
  533. }
  534. }
  535. }
  536. tableDataConfig.Num = num
  537. tableDataConfig.RemoveDate = removeDate
  538. tableDataConfig.Data = manualDataList
  539. tableDataConfig.TableEdbInfoList = tableEdbInfoList
  540. tableDataConfig.TextRowData = reqData.TextRowData
  541. return
  542. }
  543. // GetDataByTableDataConfig 根据数据配置获取表格数据
  544. func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp request.TableDataReq, err error) {
  545. // 没有选择指标的情况下,直接返回吧
  546. if len(tableDataConfig.EdbInfoIdList) <= 0 {
  547. return
  548. }
  549. // 实际期数没有的情况下,直接返回吧
  550. if tableDataConfig.Num <= 0 {
  551. return
  552. }
  553. // 获取所有的指标信息
  554. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  555. edbInfoIdList := make([]int, 0)
  556. // 标签与指标id的map
  557. tagEdbInfoIdMap := make(map[string]int)
  558. {
  559. for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  560. edbInfoIdList = append(edbInfoIdList, tableEdbInfo.EdbInfoId)
  561. tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  562. }
  563. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  564. if tmpErr != nil {
  565. err = tmpErr
  566. return
  567. }
  568. for _, v := range edbInfoList {
  569. edbInfoMap[v.EdbInfoId] = v
  570. }
  571. }
  572. manualDateMap := make(map[string]string, 0)
  573. manualDateList := make([]string, 0)
  574. for _, v := range tableDataConfig.Data {
  575. if _, ok := manualDateMap[v.DataTime]; !ok {
  576. manualDateMap[v.DataTime] = v.DataTime
  577. manualDateList = append(manualDateList, v.DataTime)
  578. }
  579. }
  580. // 寻找A列的数据列表
  581. firstEdbInfo, ok := edbInfoMap[tableDataConfig.TableEdbInfoList[0].EdbInfoId]
  582. if !ok {
  583. err = errors.New("找不到A列指标")
  584. return
  585. }
  586. baseFirstEdbInfoDataList, err := GetFirstEdbDataList(firstEdbInfo, tableDataConfig.Num, manualDateList)
  587. if err != nil {
  588. return
  589. }
  590. // A列找不到数据,那么就直接返回吧
  591. if len(baseFirstEdbInfoDataList) <= 0 {
  592. return
  593. }
  594. firstEdbInfoDataList := make([]request.ManualDataReq, 0)
  595. if tableDataConfig.RemoveDate != nil && len(tableDataConfig.RemoveDate) > 0 {
  596. for _, v := range baseFirstEdbInfoDataList {
  597. if utils.InArrayByStr(tableDataConfig.RemoveDate, v.DataTime) {
  598. continue
  599. }
  600. firstEdbInfoDataList = append(firstEdbInfoDataList, v)
  601. }
  602. } else {
  603. firstEdbInfoDataList = baseFirstEdbInfoDataList
  604. }
  605. if len(firstEdbInfoDataList) <= 0 {
  606. return
  607. }
  608. // 实际数据的最后一天
  609. lastRealDateTime, err := time.ParseInLocation(utils.FormatDate, firstEdbInfoDataList[0].DataTime, time.Local)
  610. if err != nil {
  611. return
  612. }
  613. dateMap := make(map[string]string)
  614. dateList := make([]string, 0)
  615. edbInfoIdDateDataMap := make(map[int]map[string]request.ManualDataReq)
  616. firstDateDataMap := make(map[string]request.ManualDataReq)
  617. for _, v := range firstEdbInfoDataList {
  618. dateList = append(dateList, v.DataTime)
  619. dateMap[v.DataTime] = v.DataTime
  620. firstDateDataMap[v.DataTime] = v
  621. }
  622. // 将手工数据的日期填补进去(未来的日期,过去的就不管了)
  623. for _, manualData := range tableDataConfig.Data {
  624. if !utils.InArrayByStr(dateList, manualData.DataTime) {
  625. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, manualData.DataTime, time.Local)
  626. if tmpErr != nil {
  627. err = tmpErr
  628. return
  629. }
  630. if tmpDateTime.After(lastRealDateTime) {
  631. dateList = append(dateList, manualData.DataTime)
  632. }
  633. }
  634. }
  635. edbInfoIdDateDataMap[firstEdbInfo.EdbInfoId] = firstDateDataMap
  636. for k, edbInfoId := range tableDataConfig.EdbInfoIdList {
  637. if k == 0 {
  638. continue
  639. }
  640. tmpEdbInfo, ok := edbInfoMap[edbInfoId]
  641. if !ok {
  642. err = errors.New("找不到A列指标")
  643. return
  644. }
  645. otherDataList, tmpErr := GetOtherEdbDataList(tmpEdbInfo, dateList)
  646. if tmpErr != nil {
  647. err = tmpErr
  648. return
  649. }
  650. tmpDateDataMap := make(map[string]request.ManualDataReq)
  651. for _, v := range otherDataList {
  652. tmpDateDataMap[v.DataTime] = v
  653. }
  654. edbInfoIdDateDataMap[tmpEdbInfo.EdbInfoId] = tmpDateDataMap
  655. }
  656. for _, v := range tableDataConfig.Data {
  657. tmpDate := v.DataTime
  658. if _, ok := dateMap[tmpDate]; !ok {
  659. dateMap[v.DataTime] = tmpDate
  660. }
  661. edbInfoIdDateData, ok := edbInfoIdDateDataMap[v.EdbInfoId]
  662. if !ok {
  663. edbInfoIdDateData = make(map[string]request.ManualDataReq)
  664. }
  665. // 判断是否存在该日期的数据(不存在,那么插入数据吧,存在就不管了)
  666. tmpManualData, ok := edbInfoIdDateData[tmpDate]
  667. if !ok {
  668. edbInfoIdDateData[tmpDate] = request.ManualDataReq{
  669. DataType: v.DataType,
  670. DataTime: v.DataTime,
  671. ShowValue: v.ShowValue,
  672. Value: v.Value,
  673. }
  674. } else {
  675. if (tmpManualData.DataType == 3 || tmpManualData.DataType == 4) && tmpManualData.ShowValue == `` {
  676. tmpManualData.DataType = v.DataType
  677. tmpManualData.ShowValue = v.ShowValue
  678. tmpManualData.Value = v.Value
  679. tmpManualData.RelationEdbInfoList = v.RelationEdbInfoList
  680. edbInfoIdDateData[tmpDate] = tmpManualData
  681. }
  682. }
  683. edbInfoIdDateDataMap[v.EdbInfoId] = edbInfoIdDateData
  684. }
  685. // 获取数据的日期排序
  686. sortDateTimeList := make([]time.Time, 0)
  687. {
  688. sortDateList := dateList
  689. if tableDataConfig.Sort == 1 {
  690. baseDateList := utils.StrArr{}
  691. baseDateList = append(baseDateList, sortDateList...)
  692. sort.Sort(baseDateList)
  693. sortDateList = append([]string{}, baseDateList...)
  694. } else {
  695. sort.Strings(sortDateList)
  696. }
  697. for _, v := range sortDateList {
  698. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, v, time.Local)
  699. if tmpErr != nil {
  700. err = tmpErr
  701. return
  702. }
  703. sortDateTimeList = append(sortDateTimeList, tmpDateTime)
  704. }
  705. }
  706. // 数据处理,处理成表格的数据格式
  707. tableDataMap, textRowListDataResp := handleTable(tagEdbInfoIdMap, lastRealDateTime, sortDateTimeList, edbInfoIdDateDataMap, tableDataConfig.Data, tableDataConfig.TextRowData)
  708. data := make([]request.EdbInfoData, 0)
  709. for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  710. tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  711. manualDataReqList := make([]request.ManualDataReq, 0)
  712. tmpEdbInfoData := request.EdbInfoData{
  713. EdbInfoId: tableEdbInfo.EdbInfoId,
  714. Tag: tableEdbInfo.Tag,
  715. EdbName: tableEdbInfo.EdbName,
  716. EdbAliasName: tableEdbInfo.EdbAliasName,
  717. Frequency: tableEdbInfo.Frequency,
  718. Unit: tableEdbInfo.Unit,
  719. Data: manualDataReqList,
  720. }
  721. edbInfo, ok := edbInfoMap[tableEdbInfo.EdbInfoId]
  722. if ok {
  723. tmpEdbInfoData.EdbName = edbInfo.EdbName
  724. tmpEdbInfoData.Frequency = edbInfo.Frequency
  725. tmpEdbInfoData.Unit = edbInfo.Unit
  726. tmpEdbInfoData.ClassifyId = edbInfo.ClassifyId
  727. tmpEdbInfoData.IsJoinPermission = edbInfo.IsJoinPermission
  728. }
  729. for index, dateTime := range sortDateTimeList {
  730. dataTimeType := 1
  731. if dateTime.After(lastRealDateTime) {
  732. dataTimeType = 2
  733. }
  734. tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  735. rowData, ok := tableDataMap[index+1]
  736. if !ok {
  737. manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  738. DataType: 3,
  739. DataTime: tmpDateTimeStr,
  740. DataTimeType: dataTimeType,
  741. ShowValue: "",
  742. Value: "",
  743. RelationEdbInfoList: nil,
  744. })
  745. continue
  746. }
  747. tmpData, ok := rowData[tableEdbInfo.Tag]
  748. if !ok {
  749. manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  750. DataType: 3,
  751. DataTime: tmpDateTimeStr,
  752. DataTimeType: dataTimeType,
  753. ShowValue: "",
  754. Value: "",
  755. RelationEdbInfoList: nil,
  756. })
  757. continue
  758. }
  759. tmpData.DataTimeType = dataTimeType
  760. manualDataReqList = append(manualDataReqList, tmpData)
  761. }
  762. tmpEdbInfoData.Data = manualDataReqList
  763. data = append(data, tmpEdbInfoData)
  764. }
  765. // 处理一下数据格式
  766. for _, d := range data {
  767. for k2, d2 := range d.Data {
  768. // 可能有ShowValue非数值, 转换一下报错则continue
  769. vf, e := strconv.ParseFloat(d2.ShowValue, 64)
  770. if e != nil {
  771. continue
  772. }
  773. d.Data[k2].ShowValue = utils.FormatTableDataShowValue(vf)
  774. }
  775. }
  776. for _, d := range textRowListDataResp {
  777. for k2, d2 := range d {
  778. // 可能有ShowValue非数值, 转换一下报错则continue
  779. vf, e := strconv.ParseFloat(d2.ShowValue, 64)
  780. if e != nil {
  781. continue
  782. }
  783. d[k2].ShowValue = utils.FormatTableDataShowValue(vf)
  784. }
  785. }
  786. resultResp = request.TableDataReq{
  787. EdbInfoIdList: edbInfoIdList,
  788. Sort: tableDataConfig.Sort,
  789. TextRowData: textRowListDataResp,
  790. Data: data,
  791. }
  792. return
  793. }
  794. // handleTable 表格数据处理
  795. 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) {
  796. tagList := make([]string, 0)
  797. for tag, _ := range tagEdbInfoIdMap {
  798. tagList = append(tagList, tag)
  799. }
  800. sort.Strings(tagList)
  801. tableDataMap = make(map[int]map[string]request.ManualDataReq) //行、列数据
  802. // 日期与行的关系
  803. dateIndexMap := make(map[string]int)
  804. for k, dateTime := range sortDateTimeList {
  805. rowDataMap := make(map[string]request.ManualDataReq)
  806. dataTimeType := 1
  807. if dateTime.After(lastRealDateTime) {
  808. dataTimeType = 2
  809. }
  810. tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  811. dateIndexMap[tmpDateTimeStr] = k + 1
  812. for _, tag := range tagList {
  813. edbInfoId, ok := tagEdbInfoIdMap[tag]
  814. if !ok { // 没有找到该指标的映射关系,那么就用空串填补
  815. rowDataMap[tag] = request.ManualDataReq{
  816. DataType: 3,
  817. DataTime: tmpDateTimeStr,
  818. DataTimeType: dataTimeType,
  819. ShowValue: "",
  820. Value: "",
  821. RelationEdbInfoList: nil,
  822. }
  823. continue
  824. }
  825. // 获取指标的数据map
  826. dateDataMap, ok := edbInfoIdDateDataMap[edbInfoId]
  827. if !ok { // 没有找到该指标的数据,那么就用空串填补
  828. rowDataMap[tag] = request.ManualDataReq{
  829. DataType: 3,
  830. DataTime: tmpDateTimeStr,
  831. DataTimeType: dataTimeType,
  832. ShowValue: "",
  833. Value: "",
  834. RelationEdbInfoList: nil,
  835. }
  836. continue
  837. }
  838. // 获取指标该日期的数据
  839. tmpData, ok := dateDataMap[tmpDateTimeStr]
  840. if !ok { // 该指标没有找到对应日期的数据,那么就用空串填补
  841. rowDataMap[tag] = request.ManualDataReq{
  842. DataType: 3,
  843. DataTime: tmpDateTimeStr,
  844. DataTimeType: dataTimeType,
  845. ShowValue: "",
  846. Value: "",
  847. RelationEdbInfoList: nil,
  848. }
  849. continue
  850. }
  851. tmpData.DataTimeType = dataTimeType
  852. rowDataMap[tag] = tmpData
  853. }
  854. tableDataMap[k+1] = rowDataMap
  855. }
  856. // 替换手工设置的数据
  857. for _, manualData := range manualDataList {
  858. // 找不到该日期,说明这日期过期了,不处理
  859. index, ok := dateIndexMap[manualData.DataTime]
  860. if !ok {
  861. continue
  862. }
  863. // 获取对应行的数据
  864. rowDataMap, ok := tableDataMap[index]
  865. if !ok {
  866. continue
  867. }
  868. // 找到对应的单元格
  869. tmpData, ok := rowDataMap[manualData.Tag]
  870. if !ok {
  871. continue
  872. }
  873. // 如果该单元格实际有数据(包含预测值),或者插值法补充了数据的话,那么就不用手动填入的数据
  874. if utils.InArrayByInt([]int{1, 2, 5}, tmpData.DataType) {
  875. continue
  876. }
  877. // 手工填写的数字
  878. if tmpData.DataType == 3 {
  879. tmpData.ShowValue = manualData.ShowValue
  880. tmpData.Value = manualData.Value
  881. tableDataMap[index][manualData.Tag] = tmpData
  882. //edbInfoIdDateDataMap[manualData.EdbInfoId][manualData.DataTime] = tmpData
  883. continue
  884. }
  885. // 公式
  886. tmpData.DataType = manualData.DataType
  887. tmpData.ShowValue = ``
  888. tmpData.Value = manualData.Value
  889. tmpData.RelationEdbInfoList = manualData.RelationEdbInfoList
  890. tableDataMap[index][manualData.Tag] = tmpData
  891. }
  892. // 文本行的列表插入
  893. lenTableData := len(tableDataMap)
  894. // 文本行第一列的数据列表(可能多行)
  895. firstColTextRowList := make([]request.ManualDataReq, 0)
  896. // 参与计算的文本行列表数据
  897. tmpTextRowList := make([][]request.ManualDataReq, 0)
  898. for k, textRowList := range textRowData {
  899. // 判断列数是否匹配,不匹配的话那么过滤
  900. if len(tagList)+1 != len(textRowList) {
  901. continue
  902. }
  903. rowDataMap := make(map[string]request.ManualDataReq)
  904. tmpTextRow := make([]request.ManualDataReq, 0)
  905. for index, textRow := range textRowList {
  906. // 移除第一列,因为第一列是日期列
  907. if index == 0 {
  908. firstColTextRowList = append(firstColTextRowList, textRow)
  909. continue
  910. }
  911. rowDataMap[tagList[index-1]] = textRow
  912. tmpTextRow = append(tmpTextRow, textRow)
  913. }
  914. tableDataMap[lenTableData+k+1] = rowDataMap
  915. tmpTextRowList = append(tmpTextRowList, tmpTextRow)
  916. }
  917. // 参与计算的单元格
  918. calculateCellMap := make(map[string]string)
  919. // 计算手工填写的单元格
  920. for _, manualData := range manualDataList {
  921. // 找不到该日期,说明这日期过期了,不处理
  922. index, ok := dateIndexMap[manualData.DataTime]
  923. if !ok {
  924. continue
  925. }
  926. // 获取对应行的数据
  927. rowDataMap, ok := tableDataMap[index]
  928. if !ok {
  929. continue
  930. }
  931. // 找到对应的单元格
  932. colData, ok := rowDataMap[manualData.Tag]
  933. if !ok {
  934. continue
  935. }
  936. // 如果该单元格不是计算公式的单元格,那么直接退出当前循环即可
  937. if colData.DataType != 4 {
  938. continue
  939. }
  940. tagMap := make(map[string]float64)
  941. lenRelation := len(colData.RelationEdbInfoList)
  942. replaceNum := 0
  943. for _, relation := range colData.RelationEdbInfoList {
  944. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  945. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  946. if tmpErr != nil {
  947. continue
  948. }
  949. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  950. if tmpErr != nil {
  951. continue
  952. }
  953. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  954. replaceNum++
  955. }
  956. // 如果替换的数据与关联的不一致,那么就退出当前循环
  957. if lenRelation != replaceNum {
  958. continue
  959. }
  960. // 计算
  961. val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  962. // 计算失败,退出循环
  963. if err != nil {
  964. continue
  965. }
  966. // 重新赋值
  967. colData.ShowValue = val
  968. tableDataMap[index][manualData.Tag] = colData
  969. }
  970. // 计算文本行的单元格
  971. for k, textRow := range tmpTextRowList {
  972. // 获取对应行的数据
  973. index := lenTableData + k + 1
  974. rowDataMap, ok := tableDataMap[index]
  975. if !ok {
  976. continue
  977. }
  978. for colIndex, _ := range textRow {
  979. currTag := tagList[colIndex]
  980. // 找到对应的单元格
  981. colData, ok := rowDataMap[currTag]
  982. if !ok {
  983. continue
  984. }
  985. // 如果该单元格不是计算公式的单元格,那么直接退出当前循环即可
  986. if colData.DataType != 4 {
  987. continue
  988. }
  989. tagMap := make(map[string]float64)
  990. lenRelation := len(colData.RelationEdbInfoList)
  991. replaceNum := 0
  992. for _, relation := range colData.RelationEdbInfoList {
  993. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  994. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  995. if tmpErr != nil {
  996. continue
  997. }
  998. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  999. if tmpErr != nil {
  1000. continue
  1001. }
  1002. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  1003. replaceNum++
  1004. }
  1005. // 如果替换的数据与关联的不一致,那么就退出当前循环
  1006. if lenRelation != replaceNum {
  1007. continue
  1008. }
  1009. // 计算
  1010. val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  1011. // 计算失败,退出循环
  1012. if err != nil {
  1013. continue
  1014. }
  1015. // 重新赋值
  1016. colData.ShowValue = val
  1017. tableDataMap[index][currTag] = colData
  1018. }
  1019. }
  1020. // 计算文本行第一列的数据值(多行)
  1021. for k, colData := range firstColTextRowList {
  1022. // 如果该单元格不是计算公式的单元格,那么直接退出当前循环即可
  1023. if colData.DataType != 4 {
  1024. continue
  1025. }
  1026. tagMap := make(map[string]float64)
  1027. lenRelation := len(colData.RelationEdbInfoList)
  1028. replaceNum := 0
  1029. for _, relation := range colData.RelationEdbInfoList {
  1030. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  1031. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  1032. if tmpErr != nil {
  1033. continue
  1034. }
  1035. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  1036. if tmpErr != nil {
  1037. continue
  1038. }
  1039. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  1040. replaceNum++
  1041. }
  1042. // 如果替换的数据与关联的不一致,那么就退出当前循环
  1043. if lenRelation != replaceNum {
  1044. continue
  1045. }
  1046. // 计算
  1047. val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  1048. // 计算失败,退出循环
  1049. if err != nil {
  1050. continue
  1051. }
  1052. // 重新赋值
  1053. colData.ShowValue = val
  1054. firstColTextRowList[k] = colData
  1055. }
  1056. {
  1057. // 文本行的数据处理返回
  1058. textRowListDataResp = make([][]request.ManualDataReq, 0)
  1059. newLenTableDataMap := len(tableDataMap)
  1060. // 文本行的第一行所在的位置
  1061. firstTextRow := lenTableData + 1
  1062. for i := firstTextRow; i <= newLenTableDataMap; i++ {
  1063. textRowDataResp := make([]request.ManualDataReq, 0)
  1064. textRowDataResp = append(textRowDataResp, firstColTextRowList[i-firstTextRow])
  1065. for _, tmpTag := range tagList {
  1066. textRowDataResp = append(textRowDataResp, tableDataMap[i][tmpTag])
  1067. }
  1068. textRowListDataResp = append(textRowListDataResp, textRowDataResp)
  1069. }
  1070. }
  1071. return
  1072. }
  1073. // getCalculateValue 获取公式计算的结果
  1074. func getCalculateValue(tableDataMap map[int]map[string]request.ManualDataReq, tag, row string, calculateCellMap map[string]string) (val string, err error) {
  1075. rowInt, err := strconv.Atoi(row)
  1076. if err != nil {
  1077. return
  1078. }
  1079. // 单元格的标签名
  1080. cellTagName := strings.ToUpper(tag) + row
  1081. val, ok := calculateCellMap[cellTagName]
  1082. if ok {
  1083. return
  1084. }
  1085. // 查找行数据
  1086. rowData, ok := tableDataMap[rowInt]
  1087. if !ok {
  1088. err = errors.New("查找" + row + "行的数据失败")
  1089. return
  1090. }
  1091. // 查找单元格数据
  1092. colData, ok := rowData[tag]
  1093. if !ok {
  1094. err = errors.New("查找单元格" + tag + row + "的数据失败")
  1095. return
  1096. }
  1097. // 如果不是计算单元格
  1098. if colData.DataType != 4 {
  1099. val = colData.ShowValue
  1100. return
  1101. }
  1102. // 如果是计算单元格
  1103. calculateCellMap[cellTagName] = ``
  1104. tagMap := make(map[string]float64)
  1105. for _, relation := range colData.RelationEdbInfoList {
  1106. relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  1107. valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  1108. if tmpErr != nil {
  1109. err = tmpErr
  1110. return
  1111. }
  1112. tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  1113. if tmpErr != nil {
  1114. err = tmpErr
  1115. return
  1116. }
  1117. tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  1118. }
  1119. // 计算
  1120. val, _, err = calculate(strings.ToUpper(colData.Value), tagMap)
  1121. if err != nil {
  1122. return
  1123. }
  1124. // 重新赋值
  1125. colData.ShowValue = val
  1126. tableDataMap[rowInt][tag] = colData
  1127. calculateCellMap[cellTagName] = val
  1128. return
  1129. }
  1130. // calculate 公式计算
  1131. func calculate(calculateFormula string, TagMap map[string]float64) (calVal, errMsg string, err error) {
  1132. if calculateFormula == "" {
  1133. errMsg = "公式异常"
  1134. err = errors.New(errMsg)
  1135. return
  1136. }
  1137. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  1138. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  1139. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  1140. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  1141. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  1142. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  1143. formulaFormStr := utils.ReplaceFormula(TagMap, calculateFormula)
  1144. //计算公式异常,那么就移除该指标
  1145. if formulaFormStr == `` {
  1146. errMsg = "公式异常"
  1147. err = errors.New(errMsg)
  1148. return
  1149. }
  1150. expression := formula.NewExpression(formulaFormStr)
  1151. calResult, err := expression.Evaluate()
  1152. if err != nil {
  1153. errMsg = "计算失败"
  1154. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  1155. // 分母为0的报错
  1156. if strings.Contains(err.Error(), "divide by zero") {
  1157. errMsg = "分母不能为0"
  1158. err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
  1159. }
  1160. return
  1161. }
  1162. // 如果计算结果是NAN,那么就提示报错
  1163. if calResult.IsNan() {
  1164. errMsg = "计算失败"
  1165. err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
  1166. return
  1167. }
  1168. calVal = calResult.String()
  1169. // 转Decimal然后四舍五入
  1170. valDecimal, err := decimal.NewFromString(calVal)
  1171. if err != nil {
  1172. errMsg = "计算失败"
  1173. err = errors.New("计算失败,结果转 Decimal 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  1174. return
  1175. }
  1176. calVal = valDecimal.Round(4).String()
  1177. return
  1178. }
  1179. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  1180. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int) (edbIds []int, err error) {
  1181. edbIds = make([]int, 0)
  1182. edbIdExist := make(map[int]bool)
  1183. for _, v := range excelCodes {
  1184. // 表格详情
  1185. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId)
  1186. if e != nil {
  1187. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  1188. return
  1189. }
  1190. // 自定义表格
  1191. if detail.Source == utils.TIME_TABLE {
  1192. jsonByte, e := json.Marshal(detail.TableData)
  1193. if e != nil {
  1194. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  1195. return
  1196. }
  1197. var tableData request.TableDataReq
  1198. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  1199. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  1200. return
  1201. }
  1202. for _, tv := range tableData.EdbInfoIdList {
  1203. if edbIdExist[tv] {
  1204. continue
  1205. }
  1206. edbIdExist[tv] = true
  1207. edbIds = append(edbIds, tv)
  1208. }
  1209. }
  1210. // 混合表格
  1211. if detail.Source == utils.MIXED_TABLE {
  1212. jsonByte, e := json.Marshal(detail.TableData)
  1213. if e != nil {
  1214. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  1215. return
  1216. }
  1217. var tableData request.MixedTableReq
  1218. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  1219. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  1220. return
  1221. }
  1222. if len(tableData.Data) > 0 {
  1223. for _, td := range tableData.Data {
  1224. for _, tv := range td {
  1225. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  1226. edbIdExist[tv.EdbInfoId] = true
  1227. edbIds = append(edbIds, tv.EdbInfoId)
  1228. }
  1229. }
  1230. }
  1231. }
  1232. }
  1233. }
  1234. return
  1235. }
  1236. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  1237. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  1238. if source == `` {
  1239. return ``
  1240. }
  1241. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  1242. }