excel_info.go 30 KB

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