excel_info.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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: // 混合表格
  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. return
  1347. }