excel_info.go 30 KB

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