excel_info.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 err != 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. switch edbInfo.EdbInfoType {
  232. case 0:
  233. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  234. case 1:
  235. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  236. default:
  237. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  238. }
  239. if err != nil {
  240. return
  241. }
  242. // 获取需要的期数
  243. lenData := len(dataList)
  244. if lenData <= 0 {
  245. return
  246. }
  247. tmpManualDateNum := 0 // 手工数据的期数
  248. lenManualDate := len(manualDateList)
  249. if lenManualDate > 0 {
  250. sortDateList := manualDateList
  251. baseDateList := utils.StrArr{}
  252. baseDateList = append(baseDateList, sortDateList...)
  253. sort.Sort(baseDateList)
  254. sortDateList = append([]string{}, baseDateList...)
  255. lastData := dataList[lenData-1]
  256. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  257. if tmpErr != nil {
  258. err = tmpErr
  259. return
  260. }
  261. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  262. for _, tmpDateStr := range sortDateList {
  263. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  264. if tmpErr != nil {
  265. err = tmpErr
  266. return
  267. }
  268. if tmpDate.After(lastDataDate) {
  269. tmpManualDateNum++
  270. continue
  271. }
  272. break
  273. }
  274. }
  275. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  276. num = num - tmpManualDateNum
  277. if num > lenData {
  278. num = lenData
  279. }
  280. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  281. for i := 1; i <= num; i++ {
  282. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  283. dataType := 1
  284. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  285. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  286. dataType = 5
  287. }
  288. resultDataList = append(resultDataList, request.ManualDataReq{
  289. DataType: dataType,
  290. DataTime: dataList[lenData-i].DataTime,
  291. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  292. Value: fmt.Sprint(dataList[lenData-i].Value),
  293. Decimal: decimal,
  294. DataTimeType: 1,
  295. })
  296. }
  297. return
  298. }
  299. // PadFirstEdbDataList 补齐第一列的数据
  300. func PadFirstEdbDataList(resultDataList []request.ManualDataReq, dateList []string, sortType string, decimal int) []request.ManualDataReq {
  301. originDataNum := len(resultDataList)
  302. requsetDateNum := len(dateList)
  303. if originDataNum >= requsetDateNum {
  304. return resultDataList
  305. }
  306. padNum := requsetDateNum - originDataNum
  307. if sortType == "asc" {
  308. for i := 0; i < padNum; i++ {
  309. resultDataList = append(resultDataList, request.ManualDataReq{
  310. DataType: 0,
  311. DataTime: dateList[originDataNum+i],
  312. Decimal: decimal,
  313. ShowValue: ``,
  314. Value: ``,
  315. DataTimeType: 1,
  316. })
  317. }
  318. } else {
  319. var tmpDateList []request.ManualDataReq
  320. for i := padNum - 1; i <= 0; i-- {
  321. tmpDateList = append(tmpDateList, request.ManualDataReq{
  322. DataType: 0,
  323. DataTime: dateList[originDataNum+i],
  324. Decimal: decimal,
  325. ShowValue: ``,
  326. Value: ``,
  327. DataTimeType: 1,
  328. })
  329. resultDataList = append(tmpDateList, resultDataList...)
  330. }
  331. }
  332. return resultDataList
  333. }
  334. // GetOtherEdbDataListFollowDate 获取其他列的数据,并设置日期的小数位数
  335. func GetOtherEdbDataListFollowDate(edbInfo *data_manage.EdbInfo, dateList []string, dateDecimal map[string]int) (resultDataList []request.ManualDataReq, err error) {
  336. lenDate := len(dateList)
  337. if lenDate <= 0 {
  338. return
  339. }
  340. sortDateList := dateList
  341. baseDateList := utils.StrArr{}
  342. baseDateList = append(baseDateList, sortDateList...)
  343. sort.Sort(baseDateList)
  344. sortDateList = append([]string{}, baseDateList...)
  345. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  346. if err != nil {
  347. return
  348. }
  349. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  350. if err != nil {
  351. return
  352. }
  353. var dataList []*data_manage.EdbDataList
  354. switch edbInfo.EdbInfoType {
  355. case 0:
  356. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  357. case 1:
  358. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  359. default:
  360. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  361. }
  362. if err != nil {
  363. return
  364. }
  365. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  366. baseDataList := make([]*data_manage.EdbDataList, 0)
  367. var lastData *data_manage.EdbDataList
  368. var isInsert bool
  369. for _, data := range dataList {
  370. tmpDate := data.DataTime
  371. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  372. if tmpErr != nil {
  373. err = tmpErr
  374. return
  375. }
  376. if tmpDateTime.Before(firstDateTime) {
  377. lastData = data
  378. continue
  379. }
  380. // 如果是第一次写入数据
  381. if !isInsert && lastData != nil {
  382. baseDataList = append(baseDataList, lastData)
  383. }
  384. if tmpDateTime.After(endDateTime) {
  385. baseDataList = append(baseDataList, data)
  386. break
  387. }
  388. baseDataList = append(baseDataList, data)
  389. isInsert = true
  390. }
  391. // 实际数据的日期map
  392. realValMap := make(map[string]string)
  393. for _, v := range baseDataList {
  394. realValMap[v.DataTime] = v.DataTime
  395. }
  396. // 插值法处理
  397. handleDataMap := make(map[string]float64)
  398. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  399. if err != nil {
  400. return
  401. }
  402. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  403. // 对于不存在的数据做补充
  404. for _, date := range sortDateList {
  405. dataType := 1
  406. if _, ok := realValMap[date]; !ok {
  407. dataType = 2
  408. } else {
  409. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  410. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  411. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  412. dataType = 5
  413. }
  414. }
  415. var value, showValue string
  416. if tmpVal, ok := handleDataMap[date]; ok {
  417. value = fmt.Sprint(tmpVal)
  418. showValue = value
  419. } else {
  420. dataType = 3
  421. }
  422. tmpData := request.ManualDataReq{
  423. DataType: dataType,
  424. DataTime: date,
  425. ShowValue: showValue,
  426. Value: value,
  427. }
  428. if v, ok := dateDecimal[date]; ok {
  429. tmpData.Decimal = v
  430. } else {
  431. tmpData.Decimal = -1
  432. }
  433. resultDataList = append(resultDataList, tmpData)
  434. }
  435. return
  436. }
  437. // GetOtherEdbDataList 获取其他列的数据
  438. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  439. lenDate := len(dateList)
  440. if lenDate <= 0 {
  441. return
  442. }
  443. sortDateList := dateList
  444. baseDateList := utils.StrArr{}
  445. baseDateList = append(baseDateList, sortDateList...)
  446. sort.Sort(baseDateList)
  447. sortDateList = append([]string{}, baseDateList...)
  448. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  449. if err != nil {
  450. return
  451. }
  452. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  453. if err != nil {
  454. return
  455. }
  456. var dataList []*data_manage.EdbDataList
  457. switch edbInfo.EdbInfoType {
  458. case 0:
  459. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  460. case 1:
  461. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  462. default:
  463. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  464. }
  465. if err != nil {
  466. return
  467. }
  468. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  469. baseDataList := make([]*data_manage.EdbDataList, 0)
  470. var lastData *data_manage.EdbDataList
  471. var isInsert bool
  472. for _, data := range dataList {
  473. tmpDate := data.DataTime
  474. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  475. if tmpErr != nil {
  476. err = tmpErr
  477. return
  478. }
  479. if tmpDateTime.Before(firstDateTime) {
  480. lastData = data
  481. continue
  482. }
  483. // 如果是第一次写入数据
  484. if !isInsert && lastData != nil {
  485. baseDataList = append(baseDataList, lastData)
  486. }
  487. if tmpDateTime.After(endDateTime) {
  488. baseDataList = append(baseDataList, data)
  489. break
  490. }
  491. baseDataList = append(baseDataList, data)
  492. isInsert = true
  493. }
  494. // 实际数据的日期map
  495. realValMap := make(map[string]string)
  496. for _, v := range baseDataList {
  497. realValMap[v.DataTime] = v.DataTime
  498. }
  499. // 插值法处理
  500. handleDataMap := make(map[string]float64)
  501. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  502. if err != nil {
  503. return
  504. }
  505. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  506. // 对于不存在的数据做补充
  507. for _, date := range sortDateList {
  508. dataType := 1
  509. if _, ok := realValMap[date]; !ok {
  510. dataType = 2
  511. } else {
  512. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  513. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  514. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  515. dataType = 5
  516. }
  517. }
  518. var value, showValue string
  519. if tmpVal, ok := handleDataMap[date]; ok {
  520. value = fmt.Sprint(tmpVal)
  521. showValue = value
  522. } else {
  523. dataType = 3
  524. }
  525. resultDataList = append(resultDataList, request.ManualDataReq{
  526. DataType: dataType,
  527. DataTime: date,
  528. Decimal: decimal,
  529. ShowValue: showValue,
  530. Value: value,
  531. })
  532. }
  533. return
  534. }
  535. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  536. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  537. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  538. if err != nil {
  539. return
  540. }
  541. var dataList []*data_manage.EdbDataList
  542. switch edbInfo.EdbInfoType {
  543. case 0:
  544. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  545. case 1:
  546. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  547. default:
  548. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  549. }
  550. if err != nil {
  551. return
  552. }
  553. // 获取需要的期数
  554. lenData := len(dataList)
  555. if lenData <= 0 {
  556. return
  557. }
  558. lastData := dataList[lenData-1]
  559. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  560. if err != nil {
  561. return
  562. }
  563. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  564. dataList = dataList[:lenData-1]
  565. lenData = len(dataList)
  566. }
  567. if num > lenData {
  568. num = lenData
  569. }
  570. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  571. for i := 1; i <= num; i++ {
  572. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  573. dataType := 1
  574. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  575. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  576. dataType = 5
  577. }
  578. resultDataList = append(resultDataList, request.ManualDataReq{
  579. DataType: dataType,
  580. DataTime: dataList[lenData-i].DataTime,
  581. Decimal: decimal,
  582. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  583. Value: fmt.Sprint(dataList[lenData-i].Value),
  584. })
  585. }
  586. return
  587. }
  588. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  589. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  590. edbIds = make([]int, 0)
  591. edbIdExist := make(map[int]bool)
  592. for _, v := range excelCodes {
  593. // 表格详情
  594. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  595. if e != nil {
  596. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  597. return
  598. }
  599. // 自定义表格
  600. if detail.Source == utils.TIME_TABLE {
  601. jsonByte, e := json.Marshal(detail.TableData)
  602. if e != nil {
  603. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  604. return
  605. }
  606. var tableData request.TableDataReq
  607. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  608. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  609. return
  610. }
  611. for _, tv := range tableData.EdbInfoIdList {
  612. if edbIdExist[tv] {
  613. continue
  614. }
  615. edbIdExist[tv] = true
  616. edbIds = append(edbIds, tv)
  617. }
  618. }
  619. // 混合表格
  620. if detail.Source == utils.MIXED_TABLE {
  621. jsonByte, e := json.Marshal(detail.TableData)
  622. if e != nil {
  623. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  624. return
  625. }
  626. var tableData request.MixedTableReq
  627. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  628. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  629. return
  630. }
  631. if len(tableData.Data) > 0 {
  632. for _, td := range tableData.Data {
  633. for _, tv := range td {
  634. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  635. edbIdExist[tv.EdbInfoId] = true
  636. edbIds = append(edbIds, tv.EdbInfoId)
  637. }
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return
  644. }
  645. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  646. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  647. if source == `` {
  648. return ``
  649. }
  650. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  651. }
  652. // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
  653. func GetEdbSourceByEdbInfoIdList(edbInfoIdList []int) (sourceNameList, sourceNameEnList []string, err error) {
  654. sourceNameList = make([]string, 0)
  655. sourceNameEnList = make([]string, 0)
  656. sourceMap := make(map[int]string)
  657. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  658. if tmpErr != nil {
  659. err = tmpErr
  660. return
  661. }
  662. for _, v := range edbInfoList {
  663. // 指标类型:1:基础指标,2:计算指标
  664. if v.EdbType == 2 {
  665. //sourceMap[0] = "弘则研究"
  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. resp.List = excelInfoList
  899. return
  900. }
  901. // GetExcelRuleDetail 获取规则列表
  902. func GetExcelRuleDetail(excelInfoMappingId int) (resp *excel.ExcelInfoRuleMappingView, err error) {
  903. resp = new(excel.ExcelInfoRuleMappingView)
  904. excelInfoDetail, err := excel.GetExcelRuleMappingById(excelInfoMappingId)
  905. if err != nil {
  906. return
  907. }
  908. resp = excelInfoDetail
  909. return
  910. }