excel_info.go 46 KB

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