excel_info.go 29 KB

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