excel_info.go 45 KB

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