excel_info.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/data_manage/excel"
  8. "eta/eta_api/models/data_manage/excel/request"
  9. "eta/eta_api/models/data_manage/excel/response"
  10. "eta/eta_api/models/system"
  11. "eta/eta_api/services/data"
  12. "eta/eta_api/services/data/data_manage_permission"
  13. "eta/eta_api/utils"
  14. "fmt"
  15. "sort"
  16. "strings"
  17. "time"
  18. "github.com/xuri/excelize/v2"
  19. )
  20. // GetExcelDetailInfoByExcelInfoId 根据表格id获取表格详情
  21. func GetExcelDetailInfoByExcelInfoId(excelInfoId, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  22. errMsg = `获取失败`
  23. //获取eta表格信息
  24. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  25. if err != nil {
  26. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  27. if err.Error() == utils.ErrNoRow() {
  28. errMsg = "ETA表格被删除,请刷新页面"
  29. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  30. }
  31. return
  32. }
  33. return formatExcelInfo2Detail(excelInfo, sysUserId, lang)
  34. }
  35. // GetExcelDetailInfoByUnicode 根据表格编码获取表格详情
  36. func GetExcelDetailInfoByUnicode(unicode string, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  37. errMsg = `获取失败`
  38. // 获取eta表格信息
  39. excelInfo, err := excel.GetExcelInfoByUnicode(unicode)
  40. if err != nil {
  41. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  42. if err.Error() == utils.ErrNoRow() {
  43. errMsg = "ETA表格被删除,请刷新页面"
  44. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  45. }
  46. return
  47. }
  48. return formatExcelInfo2Detail(excelInfo, sysUserId, lang)
  49. }
  50. func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  51. checkExcelInfo := excelInfo
  52. if excelInfo.Source == utils.BALANCE_TABLE {
  53. checkExcelInfoId := excelInfo.ExcelInfoId
  54. if excelInfo.BalanceType == 1 {
  55. checkExcelInfoId = excelInfo.RelExcelInfoId
  56. } else {
  57. if excelInfo.ParentId > 0 {
  58. checkExcelInfoId = excelInfo.ParentId
  59. }
  60. }
  61. if checkExcelInfoId != excelInfo.ExcelInfoId {
  62. checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
  63. if err != nil {
  64. errMsg = "获取平衡表格信息失败"
  65. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  66. return
  67. }
  68. }
  69. }
  70. // 数据权限
  71. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  72. if err != nil {
  73. err = errors.New("获取表格权限信息失败,Err" + err.Error())
  74. return
  75. }
  76. excelDetail = response.ExcelInfoDetail{
  77. ExcelInfoId: excelInfo.ExcelInfoId,
  78. Source: excelInfo.Source,
  79. ExcelType: excelInfo.ExcelType,
  80. ExcelName: excelInfo.ExcelName,
  81. UniqueCode: excelInfo.UniqueCode,
  82. ExcelClassifyId: excelInfo.ExcelClassifyId,
  83. SysUserId: excelInfo.SysUserId,
  84. SysUserRealName: excelInfo.SysUserRealName,
  85. Content: excelInfo.Content,
  86. ExcelImage: excelInfo.ExcelImage,
  87. FileUrl: excelInfo.FileUrl,
  88. Sort: excelInfo.Sort,
  89. IsDelete: excelInfo.IsDelete,
  90. ModifyTime: excelInfo.ModifyTime,
  91. CreateTime: excelInfo.CreateTime,
  92. TableData: nil,
  93. HaveOperaAuth: haveOperaAuth,
  94. ParentId: excelInfo.ParentId,
  95. BalanceType: excelInfo.BalanceType,
  96. UpdateUserId: excelInfo.UpdateUserId,
  97. UpdateUserRealName: excelInfo.UpdateUserRealName,
  98. RelExcelInfoId: excelInfo.RelExcelInfoId,
  99. SourcesFrom: excelInfo.SourcesFrom,
  100. }
  101. // 无权限,不需要返回数据
  102. if !haveOperaAuth {
  103. return
  104. }
  105. switch excelInfo.Source {
  106. case utils.TIME_TABLE: // 时间序列表格
  107. var tableDataConfig TableDataConfig
  108. err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
  109. if err != nil {
  110. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  111. return
  112. }
  113. result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
  114. if tmpErr != nil {
  115. err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
  116. return
  117. }
  118. result = SetExcelByDecimalConfig(result, tableDataConfig.DecimalConfig)
  119. if len(result.EdbInfoIdList) > 0 {
  120. classifyIdList := make([]int, 0)
  121. for _, v := range result.Data {
  122. classifyIdList = append(classifyIdList, v.ClassifyId)
  123. }
  124. classifyMap := make(map[int]*data_manage.EdbClassify)
  125. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  126. if tmpErr != nil {
  127. err = errors.New("获取分类列表失败,Err:" + tmpErr.Error())
  128. return
  129. }
  130. for _, v := range classifyList {
  131. classifyMap[v.ClassifyId] = v
  132. }
  133. // 获取所有有权限的指标和分类
  134. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  135. if tmpErr != nil {
  136. err = errors.New("获取所有有权限的指标和分类失败,Err:" + tmpErr.Error())
  137. return
  138. }
  139. for i, v := range result.Data {
  140. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  141. result.Data[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  142. }
  143. }
  144. for i, edbData := range result.Data {
  145. for j := range edbData.Data {
  146. if result.Data[i].Data[j].Width == 0 {
  147. result.Data[i].Data[j].Width = 140
  148. }
  149. if result.Data[i].Data[j].Height == 0 {
  150. result.Data[i].Data[j].Height = 35
  151. }
  152. }
  153. }
  154. setTextCellDefaultSize(result.TextRowData)
  155. }
  156. excelDetail.TableData = result
  157. case utils.MIXED_TABLE, utils.BALANCE_TABLE: // 混合表格 平衡表
  158. var result request.MixedTableReq
  159. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  160. if err != nil {
  161. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  162. return
  163. }
  164. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
  165. if tmpErr != nil {
  166. errMsg = "获取失败"
  167. if tmpErrMsg != `` {
  168. errMsg = tmpErrMsg
  169. }
  170. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  171. return
  172. }
  173. result.Data = newData
  174. excelDetail.TableData = result
  175. }
  176. if excelDetail.Source == utils.BALANCE_TABLE {
  177. excelDetail.Button = GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, excelDetail.HaveOperaAuth, checkExcelInfo.ExcelInfoId)
  178. }
  179. return
  180. }
  181. // 设置默认的文本单元格大小
  182. func setTextCellDefaultSize(textRowData [][]request.ManualDataReq) {
  183. for i := range textRowData {
  184. for j := range textRowData[i] {
  185. if textRowData[i][j].Width == 0 {
  186. textRowData[i][j].Width = 140
  187. }
  188. if textRowData[i][j].Height == 0 {
  189. textRowData[i][j].Height = 35
  190. }
  191. }
  192. }
  193. }
  194. // SetExcelByDecimalConfig 设置表格的小数位
  195. func SetExcelByDecimalConfig(tableData request.TableDataReq, config []request.DecimalConfig) request.TableDataReq {
  196. excelData := tableData.Data
  197. edbInfoIndex := make(map[int]int)
  198. dateIndex := make(map[string]int)
  199. for i, v := range excelData {
  200. edbInfoIndex[v.EdbInfoId] = i
  201. }
  202. for i, v := range excelData[0].Data {
  203. dateIndex[v.DataTime] = i
  204. }
  205. for _, conf := range config {
  206. if conf.Col > 0 {
  207. if v, ok := edbInfoIndex[conf.Col]; ok {
  208. excelData[v].Decimal = conf.Decimal
  209. for i := 0; i < len(excelData[v].Data); i++ {
  210. excelData[v].Data[i].Decimal = conf.Decimal
  211. }
  212. }
  213. }
  214. if conf.Row != "" {
  215. if v, ok := dateIndex[conf.Row]; ok {
  216. for i := 0; i < len(excelData); i++ {
  217. excelData[i].Data[v].Decimal = conf.Decimal
  218. }
  219. }
  220. }
  221. }
  222. tableData.Data = excelData
  223. tableData.DecimalConfig = config
  224. return tableData
  225. }
  226. // GetExcelInfoOpButton 获取ETA表格的操作权限
  227. func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int, haveOperaAuth bool) (button excel.ExcelInfoDetailButton) {
  228. // 如果没有数据权限,那么直接返回
  229. if !haveOperaAuth {
  230. return
  231. }
  232. //非管理员角色查看其他用户创建的表格,可刷新、另存为、下载表格;
  233. button.RefreshButton = true
  234. button.CopyButton = true
  235. button.DownloadButton = true
  236. // 1、本用户创建的表格,可编辑、刷新、另存为、下载、删除,删除需二次确认;
  237. // 2、管理员角色对所有表格有如上权限;
  238. // 3、在线excel所有人都能编辑
  239. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || source == utils.EXCEL_DEFAULT {
  240. button.OpButton = true
  241. button.DeleteButton = true
  242. }
  243. // 自定义分析
  244. if source == utils.CUSTOM_ANALYSIS_TABLE {
  245. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  246. button.OpEdbButton = true // 生成、查看指标按钮
  247. button.RefreshEdbButton = true // 刷新指标按钮
  248. }
  249. }
  250. return
  251. }
  252. // GetFirstEdbDataList 获取第一列的数据
  253. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  254. var dataList []*data_manage.EdbDataList
  255. resultDataList = make([]request.ManualDataReq, 0)
  256. switch edbInfo.EdbInfoType {
  257. case 0:
  258. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  259. case 1:
  260. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  261. default:
  262. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  263. }
  264. if err != nil {
  265. return
  266. }
  267. // 获取需要的期数
  268. lenData := len(dataList)
  269. if lenData <= 0 {
  270. return
  271. }
  272. tmpManualDateNum := 0 // 手工数据的期数
  273. lenManualDate := len(manualDateList)
  274. if lenManualDate > 0 {
  275. sortDateList := manualDateList
  276. baseDateList := utils.StrArr{}
  277. baseDateList = append(baseDateList, sortDateList...)
  278. sort.Sort(baseDateList)
  279. sortDateList = append([]string{}, baseDateList...)
  280. lastData := dataList[lenData-1]
  281. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  282. if tmpErr != nil {
  283. err = tmpErr
  284. return
  285. }
  286. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  287. for _, tmpDateStr := range sortDateList {
  288. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  289. if tmpErr != nil {
  290. err = tmpErr
  291. return
  292. }
  293. if tmpDate.After(lastDataDate) {
  294. tmpManualDateNum++
  295. continue
  296. }
  297. break
  298. }
  299. }
  300. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  301. num = num - tmpManualDateNum
  302. if num > lenData {
  303. num = lenData
  304. }
  305. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  306. for i := 1; i <= num; i++ {
  307. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  308. dataType := 1
  309. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  310. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  311. dataType = 5
  312. }
  313. resultDataList = append(resultDataList, request.ManualDataReq{
  314. DataType: dataType,
  315. DataTime: dataList[lenData-i].DataTime,
  316. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  317. Value: fmt.Sprint(dataList[lenData-i].Value),
  318. Decimal: decimal,
  319. DataTimeType: 1,
  320. })
  321. }
  322. return
  323. }
  324. // PadFirstEdbDataList 补齐第一列的数据
  325. func PadFirstEdbDataList(resultDataList []request.ManualDataReq, dateList []string, sortType string, decimal int) []request.ManualDataReq {
  326. originDataNum := len(resultDataList)
  327. requsetDateNum := len(dateList)
  328. if originDataNum >= requsetDateNum {
  329. return resultDataList
  330. }
  331. padNum := requsetDateNum - originDataNum
  332. if sortType == "asc" {
  333. for i := 0; i < padNum; i++ {
  334. resultDataList = append(resultDataList, request.ManualDataReq{
  335. DataType: 0,
  336. DataTime: dateList[originDataNum+i],
  337. Decimal: decimal,
  338. ShowValue: ``,
  339. Value: ``,
  340. DataTimeType: 1,
  341. })
  342. }
  343. } else {
  344. var tmpDateList []request.ManualDataReq
  345. for i := padNum - 1; i <= 0; i-- {
  346. tmpDateList = append(tmpDateList, request.ManualDataReq{
  347. DataType: 0,
  348. DataTime: dateList[originDataNum+i],
  349. Decimal: decimal,
  350. ShowValue: ``,
  351. Value: ``,
  352. DataTimeType: 1,
  353. })
  354. resultDataList = append(tmpDateList, resultDataList...)
  355. }
  356. }
  357. return resultDataList
  358. }
  359. // GetOtherEdbDataListFollowDate 获取其他列的数据,并设置日期的小数位数
  360. func GetOtherEdbDataListFollowDate(edbInfo *data_manage.EdbInfo, dateList []string, dateDecimal map[string]int) (resultDataList []request.ManualDataReq, err error) {
  361. lenDate := len(dateList)
  362. if lenDate <= 0 {
  363. return
  364. }
  365. sortDateList := dateList
  366. baseDateList := utils.StrArr{}
  367. baseDateList = append(baseDateList, sortDateList...)
  368. sort.Sort(baseDateList)
  369. sortDateList = append([]string{}, baseDateList...)
  370. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  371. if err != nil {
  372. return
  373. }
  374. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  375. if err != nil {
  376. return
  377. }
  378. var dataList []*data_manage.EdbDataList
  379. switch edbInfo.EdbInfoType {
  380. case 0:
  381. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  382. case 1:
  383. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  384. default:
  385. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  386. }
  387. if err != nil {
  388. return
  389. }
  390. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  391. baseDataList := make([]*data_manage.EdbDataList, 0)
  392. var lastData *data_manage.EdbDataList
  393. var isInsert bool
  394. for _, data := range dataList {
  395. tmpDate := data.DataTime
  396. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  397. if tmpErr != nil {
  398. err = tmpErr
  399. return
  400. }
  401. if tmpDateTime.Before(firstDateTime) {
  402. lastData = data
  403. continue
  404. }
  405. // 如果是第一次写入数据
  406. if !isInsert && lastData != nil {
  407. baseDataList = append(baseDataList, lastData)
  408. }
  409. if tmpDateTime.After(endDateTime) {
  410. baseDataList = append(baseDataList, data)
  411. break
  412. }
  413. baseDataList = append(baseDataList, data)
  414. isInsert = true
  415. }
  416. // 实际数据的日期map
  417. realValMap := make(map[string]string)
  418. for _, v := range baseDataList {
  419. realValMap[v.DataTime] = v.DataTime
  420. }
  421. // 插值法处理
  422. handleDataMap := make(map[string]float64)
  423. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  424. if err != nil {
  425. return
  426. }
  427. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  428. // 对于不存在的数据做补充
  429. for _, date := range sortDateList {
  430. dataType := 1
  431. if _, ok := realValMap[date]; !ok {
  432. dataType = 2
  433. } else {
  434. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  435. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  436. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  437. dataType = 5
  438. }
  439. }
  440. var value, showValue string
  441. if tmpVal, ok := handleDataMap[date]; ok {
  442. value = fmt.Sprint(tmpVal)
  443. showValue = value
  444. } else {
  445. dataType = 3
  446. }
  447. tmpData := request.ManualDataReq{
  448. DataType: dataType,
  449. DataTime: date,
  450. ShowValue: showValue,
  451. Value: value,
  452. }
  453. if v, ok := dateDecimal[date]; ok {
  454. tmpData.Decimal = v
  455. } else {
  456. tmpData.Decimal = -1
  457. }
  458. resultDataList = append(resultDataList, tmpData)
  459. }
  460. return
  461. }
  462. // GetOtherEdbDataList 获取其他列的数据
  463. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  464. lenDate := len(dateList)
  465. if lenDate <= 0 {
  466. return
  467. }
  468. sortDateList := dateList
  469. baseDateList := utils.StrArr{}
  470. baseDateList = append(baseDateList, sortDateList...)
  471. sort.Sort(baseDateList)
  472. sortDateList = append([]string{}, baseDateList...)
  473. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  474. if err != nil {
  475. return
  476. }
  477. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  478. if err != nil {
  479. return
  480. }
  481. var dataList []*data_manage.EdbDataList
  482. switch edbInfo.EdbInfoType {
  483. case 0:
  484. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  485. case 1:
  486. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  487. default:
  488. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  489. }
  490. if err != nil {
  491. return
  492. }
  493. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  494. baseDataList := make([]*data_manage.EdbDataList, 0)
  495. var lastData *data_manage.EdbDataList
  496. var isInsert bool
  497. for _, data := range dataList {
  498. tmpDate := data.DataTime
  499. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  500. if tmpErr != nil {
  501. err = tmpErr
  502. return
  503. }
  504. if tmpDateTime.Before(firstDateTime) {
  505. lastData = data
  506. continue
  507. }
  508. // 如果是第一次写入数据
  509. if !isInsert && lastData != nil {
  510. baseDataList = append(baseDataList, lastData)
  511. }
  512. if tmpDateTime.After(endDateTime) {
  513. baseDataList = append(baseDataList, data)
  514. break
  515. }
  516. baseDataList = append(baseDataList, data)
  517. isInsert = true
  518. }
  519. // 实际数据的日期map
  520. realValMap := make(map[string]string)
  521. for _, v := range baseDataList {
  522. realValMap[v.DataTime] = v.DataTime
  523. }
  524. // 插值法处理
  525. handleDataMap := make(map[string]float64)
  526. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  527. if err != nil {
  528. return
  529. }
  530. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  531. // 对于不存在的数据做补充
  532. for _, date := range sortDateList {
  533. dataType := 1
  534. if _, ok := realValMap[date]; !ok {
  535. dataType = 2
  536. } else {
  537. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  538. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  539. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  540. dataType = 5
  541. }
  542. }
  543. var value, showValue string
  544. if tmpVal, ok := handleDataMap[date]; ok {
  545. value = fmt.Sprint(tmpVal)
  546. showValue = value
  547. } else {
  548. dataType = 3
  549. }
  550. resultDataList = append(resultDataList, request.ManualDataReq{
  551. DataType: dataType,
  552. DataTime: date,
  553. Decimal: decimal,
  554. ShowValue: showValue,
  555. Value: value,
  556. })
  557. }
  558. return
  559. }
  560. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  561. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  562. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  563. if err != nil {
  564. return
  565. }
  566. var dataList []*data_manage.EdbDataList
  567. switch edbInfo.EdbInfoType {
  568. case 0:
  569. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  570. case 1:
  571. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  572. default:
  573. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  574. }
  575. if err != nil {
  576. return
  577. }
  578. // 获取需要的期数
  579. lenData := len(dataList)
  580. if lenData <= 0 {
  581. return
  582. }
  583. lastData := dataList[lenData-1]
  584. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  585. if err != nil {
  586. return
  587. }
  588. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  589. dataList = dataList[:lenData-1]
  590. lenData = len(dataList)
  591. }
  592. if num > lenData {
  593. num = lenData
  594. }
  595. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  596. for i := 1; i <= num; i++ {
  597. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  598. dataType := 1
  599. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  600. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  601. dataType = 5
  602. }
  603. resultDataList = append(resultDataList, request.ManualDataReq{
  604. DataType: dataType,
  605. DataTime: dataList[lenData-i].DataTime,
  606. Decimal: decimal,
  607. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  608. Value: fmt.Sprint(dataList[lenData-i].Value),
  609. })
  610. }
  611. return
  612. }
  613. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  614. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  615. edbIds = make([]int, 0)
  616. edbIdExist := make(map[int]bool)
  617. for _, v := range excelCodes {
  618. // 表格详情
  619. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  620. if e != nil {
  621. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  622. return
  623. }
  624. // 自定义表格
  625. if detail.Source == utils.TIME_TABLE {
  626. jsonByte, e := json.Marshal(detail.TableData)
  627. if e != nil {
  628. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  629. return
  630. }
  631. var tableData request.TableDataReq
  632. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  633. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  634. return
  635. }
  636. for _, tv := range tableData.EdbInfoIdList {
  637. if edbIdExist[tv] {
  638. continue
  639. }
  640. edbIdExist[tv] = true
  641. edbIds = append(edbIds, tv)
  642. }
  643. }
  644. // 混合表格
  645. if detail.Source == utils.MIXED_TABLE {
  646. jsonByte, e := json.Marshal(detail.TableData)
  647. if e != nil {
  648. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  649. return
  650. }
  651. var tableData request.MixedTableReq
  652. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  653. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  654. return
  655. }
  656. if len(tableData.Data) > 0 {
  657. for _, td := range tableData.Data {
  658. for _, tv := range td {
  659. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  660. edbIdExist[tv.EdbInfoId] = true
  661. edbIds = append(edbIds, tv.EdbInfoId)
  662. }
  663. }
  664. }
  665. }
  666. }
  667. }
  668. return
  669. }
  670. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  671. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  672. if source == `` {
  673. return ``
  674. }
  675. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  676. }
  677. // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
  678. func GetEdbSourceByEdbInfoIdList(edbInfoIdList []int) (sourceNameList, sourceNameEnList []string, err error) {
  679. sourceNameList = make([]string, 0)
  680. sourceNameEnList = make([]string, 0)
  681. sourceMap := make(map[int]string)
  682. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  683. if tmpErr != nil {
  684. err = tmpErr
  685. return
  686. }
  687. for _, v := range edbInfoList {
  688. // 指标类型:1:基础指标,2:计算指标
  689. if v.EdbType == 2 {
  690. //sourceMap[0] = "弘则研究"
  691. baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  692. for _, baseEdbInfo := range baseEdbInfoArr {
  693. if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
  694. sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
  695. }
  696. }
  697. } else {
  698. sourceMap[v.Source] = v.SourceName
  699. }
  700. }
  701. for source, sourceName := range sourceMap {
  702. if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
  703. continue
  704. }
  705. sourceNameList = append(sourceNameList, sourceName)
  706. sourceNameEn, ok := utils.DataSourceEnMap[source]
  707. if !ok {
  708. sourceNameEn = sourceName
  709. }
  710. sourceNameEnList = append(sourceNameEnList, sourceNameEn)
  711. }
  712. //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
  713. //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
  714. // 图表来源
  715. conf, e := models.GetBusinessConf()
  716. if e != nil {
  717. return
  718. }
  719. if conf[models.BusinessConfCompanyName] != "" {
  720. sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
  721. sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
  722. }
  723. return
  724. }
  725. // GetCustomAnalysisOpButton 获取自定义分析按钮权限
  726. func GetCustomAnalysisOpButton(sysUser *system.Admin, belongUserId int, permissionType []int) (button excel.ExcelInfoDetailButton) {
  727. // 管理员/所属人所有权限
  728. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  729. button.RefreshButton = true
  730. button.CopyButton = true
  731. button.DownloadButton = true
  732. button.OpEdbButton = true
  733. button.RefreshEdbButton = true
  734. button.OpButton = true
  735. button.DeleteButton = true
  736. return
  737. }
  738. // 非管理员-被分享人
  739. for _, v := range permissionType {
  740. if v == 1 {
  741. button.RefreshButton = true
  742. button.CopyButton = true
  743. button.DownloadButton = true
  744. }
  745. if v == 2 {
  746. button.OpEdbButton = true
  747. button.RefreshEdbButton = true
  748. button.OpButton = true
  749. button.CopyButton = true
  750. button.DownloadButton = true
  751. }
  752. }
  753. return
  754. }
  755. func parseExcelScopeCoord(scopeList []string) (scopeCoord string, err error) {
  756. if len(scopeList) == 2 {
  757. x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
  758. if er != nil {
  759. return "", er
  760. }
  761. x2, y2, er := excelize.CellNameToCoordinates(scopeList[1])
  762. if er != nil {
  763. return "", er
  764. }
  765. scopeCoord = fmt.Sprintf("%d,%d,%d,%d", x1, y1, x2, y2)
  766. }
  767. if len(scopeList) == 1 {
  768. x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
  769. if er != nil {
  770. return "", er
  771. }
  772. scopeCoord = fmt.Sprintf("%d,%d", x1, y1)
  773. }
  774. return
  775. }
  776. func ExcelRuleFormat(req *request.ExcelRuleMappingReq, lang string) (res *excel.ExcelInfoRuleMapping, err error) {
  777. res = new(excel.ExcelInfoRuleMapping)
  778. if req.RuleType == 5 {
  779. switch req.LeftValue {
  780. // 今天
  781. case "today":
  782. res.LeftValueBack = req.LeftValue
  783. // 明天
  784. case "tomorrow":
  785. res.LeftValueBack = req.LeftValue
  786. // 最近7天
  787. case "last7days":
  788. res.LeftValueBack = req.LeftValue
  789. // 上周
  790. case "lastweek":
  791. res.LeftValueBack = req.LeftValue
  792. // 本周
  793. case "thisweek":
  794. res.LeftValueBack = req.LeftValue
  795. // 下周
  796. case "nextweek":
  797. res.LeftValueBack = req.LeftValue
  798. // 上月
  799. case "lastmonth":
  800. res.LeftValueBack = req.LeftValue
  801. // 本月
  802. case "thismonth":
  803. res.LeftValueBack = req.LeftValue
  804. // 下月
  805. case "nextmonth":
  806. res.LeftValueBack = req.LeftValue
  807. default:
  808. err = errors.New("发生日期规则错误")
  809. return
  810. }
  811. }
  812. // 格式化条件值
  813. switch req.LeftValueType {
  814. // 坐标
  815. case 2:
  816. x, y, err := excelize.CellNameToCoordinates(req.LeftValue)
  817. if err != nil {
  818. return nil, err
  819. }
  820. res.LeftValue = req.LeftValue
  821. res.LeftValueBack = fmt.Sprintf("%d,%d", x, y)
  822. default:
  823. res.LeftValue = req.LeftValue
  824. res.LeftValueBack = req.LeftValue
  825. }
  826. switch req.RightValueType {
  827. // 坐标
  828. case 2:
  829. x, y, err := excelize.CellNameToCoordinates(req.RightValue)
  830. if err != nil {
  831. return nil, err
  832. }
  833. res.RightValue = req.RightValue
  834. res.RightValueBack = fmt.Sprintf("%d,%d", x, y)
  835. default:
  836. res.RightValue = req.RightValue
  837. res.RightValueBack = req.RightValue
  838. }
  839. if res.LeftValueBack == "" {
  840. res.LeftValueBack = req.LeftValue
  841. }
  842. if res.RightValueBack == "" {
  843. res.RightValueBack = req.RightValue
  844. }
  845. res.RuleType = req.RuleType
  846. res.ExcelInfoId = req.ExcelInfoId
  847. res.ExcelInfoRuleMappingId = req.ExcelRuleMappingId
  848. res.LeftValueType = req.LeftValueType
  849. res.RightValueType = req.RightValueType
  850. res.FontColor = req.FontColor
  851. res.BackgroundColor = req.BackgroundColor
  852. res.Remark = req.Remark
  853. res.RemarkEn = req.Remark
  854. res.ScopeShow = req.Scope
  855. scopeList := strings.Split(req.Scope, ":")
  856. res.ScopeCoord, err = parseExcelScopeCoord(scopeList)
  857. return
  858. }
  859. func AddExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
  860. excelRule, err := ExcelRuleFormat(req, lang)
  861. if err != nil {
  862. return
  863. }
  864. if excelRule.ExcelInfoRuleMappingId != 0 {
  865. return errors.New("规则已存在")
  866. }
  867. excelRule.CreateTime = time.Now()
  868. _, err = excelRule.Insert()
  869. return
  870. }
  871. func ModifyExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
  872. excelInfo, err := excel.GetExcelRuleMappingById(req.ExcelRuleMappingId)
  873. if err != nil {
  874. return
  875. }
  876. editExcelInfo, err := ExcelRuleFormat(req, lang)
  877. if err != nil {
  878. return
  879. }
  880. var updateCols []string
  881. if excelInfo.LeftValue != editExcelInfo.LeftValue {
  882. updateCols = append(updateCols, "LeftValue")
  883. updateCols = append(updateCols, "LeftValueBack")
  884. }
  885. if excelInfo.LeftValueType != editExcelInfo.LeftValueType {
  886. updateCols = append(updateCols, "LeftValueType")
  887. }
  888. if excelInfo.RightValue != editExcelInfo.RightValue {
  889. updateCols = append(updateCols, "RightValue")
  890. updateCols = append(updateCols, "RightValueBack")
  891. }
  892. if excelInfo.RightValueType != editExcelInfo.RightValueType {
  893. updateCols = append(updateCols, "RightValueType")
  894. }
  895. if excelInfo.FontColor != editExcelInfo.FontColor {
  896. updateCols = append(updateCols, "FontColor")
  897. }
  898. if excelInfo.BackgroundColor != editExcelInfo.BackgroundColor {
  899. updateCols = append(updateCols, "BackgroundColor")
  900. }
  901. if excelInfo.Remark != editExcelInfo.Remark {
  902. updateCols = append(updateCols, "Remark")
  903. }
  904. if excelInfo.RemarkEn != editExcelInfo.RemarkEn {
  905. updateCols = append(updateCols, "RemarkEn")
  906. }
  907. if excelInfo.ScopeShow != editExcelInfo.ScopeShow {
  908. updateCols = append(updateCols, "ScopeCoord")
  909. updateCols = append(updateCols, "ScopeShow")
  910. }
  911. if len(updateCols) > 0 {
  912. err = editExcelInfo.Update(updateCols)
  913. }
  914. return
  915. }
  916. // GetExcelRuleList 获取规则列表
  917. func GetExcelRuleList(excelInfoId int) (resp *response.ExcelRuleListResp, err error) {
  918. resp = new(response.ExcelRuleListResp)
  919. excelInfoList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  920. if err != nil {
  921. return
  922. }
  923. resp.List = excelInfoList
  924. return
  925. }
  926. // GetExcelRuleDetail 获取规则列表
  927. func GetExcelRuleDetail(excelInfoMappingId int) (resp *excel.ExcelInfoRuleMappingView, err error) {
  928. resp = new(excel.ExcelInfoRuleMappingView)
  929. excelInfoDetail, err := excel.GetExcelRuleMappingById(excelInfoMappingId)
  930. if err != nil {
  931. return
  932. }
  933. resp = excelInfoDetail
  934. return
  935. }