excel_info.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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) (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. DataTimeType: 1,
  261. })
  262. }
  263. return
  264. }
  265. // PadFirstEdbDataList 补齐第一列的数据
  266. func PadFirstEdbDataList(resultDataList []request.ManualDataReq, dateList []string, sortType string) []request.ManualDataReq {
  267. originDataNum := len(resultDataList)
  268. requsetDateNum := len(dateList)
  269. if originDataNum >= requsetDateNum {
  270. return resultDataList
  271. }
  272. padNum := requsetDateNum - originDataNum
  273. if sortType == "asc" {
  274. for i := 0; i < padNum; i++ {
  275. resultDataList = append(resultDataList, request.ManualDataReq{
  276. DataType: 0,
  277. DataTime: dateList[originDataNum+i],
  278. ShowValue: ``,
  279. Value: ``,
  280. DataTimeType: 1,
  281. })
  282. }
  283. } else {
  284. var tmpDateList []request.ManualDataReq
  285. for i := padNum - 1; i <= 0; i-- {
  286. tmpDateList = append(tmpDateList, request.ManualDataReq{
  287. DataType: 0,
  288. DataTime: dateList[originDataNum+i],
  289. ShowValue: ``,
  290. Value: ``,
  291. DataTimeType: 1,
  292. })
  293. resultDataList = append(tmpDateList, resultDataList...)
  294. }
  295. }
  296. return resultDataList
  297. }
  298. // GetOtherEdbDataList 获取其他列的数据
  299. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  300. lenDate := len(dateList)
  301. if lenDate <= 0 {
  302. return
  303. }
  304. sortDateList := dateList
  305. baseDateList := utils.StrArr{}
  306. baseDateList = append(baseDateList, sortDateList...)
  307. sort.Sort(baseDateList)
  308. sortDateList = append([]string{}, baseDateList...)
  309. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  310. if err != nil {
  311. return
  312. }
  313. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  314. if err != nil {
  315. return
  316. }
  317. var dataList []*data_manage.EdbDataList
  318. switch edbInfo.EdbInfoType {
  319. case 0:
  320. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  321. case 1:
  322. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  323. default:
  324. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  325. }
  326. if err != nil {
  327. return
  328. }
  329. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  330. baseDataList := make([]*data_manage.EdbDataList, 0)
  331. var lastData *data_manage.EdbDataList
  332. var isInsert bool
  333. for _, data := range dataList {
  334. tmpDate := data.DataTime
  335. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  336. if tmpErr != nil {
  337. err = tmpErr
  338. return
  339. }
  340. if tmpDateTime.Before(firstDateTime) {
  341. lastData = data
  342. continue
  343. }
  344. // 如果是第一次写入数据
  345. if !isInsert && lastData != nil {
  346. baseDataList = append(baseDataList, lastData)
  347. }
  348. if tmpDateTime.After(endDateTime) {
  349. baseDataList = append(baseDataList, data)
  350. break
  351. }
  352. baseDataList = append(baseDataList, data)
  353. isInsert = true
  354. }
  355. // 实际数据的日期map
  356. realValMap := make(map[string]string)
  357. for _, v := range baseDataList {
  358. realValMap[v.DataTime] = v.DataTime
  359. }
  360. // 插值法处理
  361. handleDataMap := make(map[string]float64)
  362. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  363. if err != nil {
  364. return
  365. }
  366. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  367. // 对于不存在的数据做补充
  368. for _, date := range sortDateList {
  369. dataType := 1
  370. if _, ok := realValMap[date]; !ok {
  371. dataType = 2
  372. } else {
  373. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  374. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  375. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  376. dataType = 5
  377. }
  378. }
  379. var value, showValue string
  380. if tmpVal, ok := handleDataMap[date]; ok {
  381. value = fmt.Sprint(tmpVal)
  382. showValue = value
  383. } else {
  384. dataType = 3
  385. }
  386. resultDataList = append(resultDataList, request.ManualDataReq{
  387. DataType: dataType,
  388. DataTime: date,
  389. ShowValue: showValue,
  390. Value: value,
  391. })
  392. }
  393. return
  394. }
  395. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  396. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  397. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  398. if err != nil {
  399. return
  400. }
  401. var dataList []*data_manage.EdbDataList
  402. switch edbInfo.EdbInfoType {
  403. case 0:
  404. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  405. case 1:
  406. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  407. default:
  408. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  409. }
  410. if err != nil {
  411. return
  412. }
  413. // 获取需要的期数
  414. lenData := len(dataList)
  415. if lenData <= 0 {
  416. return
  417. }
  418. lastData := dataList[lenData-1]
  419. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  420. if err != nil {
  421. return
  422. }
  423. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  424. dataList = dataList[:lenData-1]
  425. lenData = len(dataList)
  426. }
  427. if num > lenData {
  428. num = lenData
  429. }
  430. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  431. for i := 1; i <= num; i++ {
  432. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  433. dataType := 1
  434. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  435. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  436. dataType = 5
  437. }
  438. resultDataList = append(resultDataList, request.ManualDataReq{
  439. DataType: dataType,
  440. DataTime: dataList[lenData-i].DataTime,
  441. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  442. Value: fmt.Sprint(dataList[lenData-i].Value),
  443. })
  444. }
  445. return
  446. }
  447. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  448. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  449. edbIds = make([]int, 0)
  450. edbIdExist := make(map[int]bool)
  451. for _, v := range excelCodes {
  452. // 表格详情
  453. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  454. if e != nil {
  455. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  456. return
  457. }
  458. // 自定义表格
  459. if detail.Source == utils.TIME_TABLE {
  460. jsonByte, e := json.Marshal(detail.TableData)
  461. if e != nil {
  462. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  463. return
  464. }
  465. var tableData request.TableDataReq
  466. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  467. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  468. return
  469. }
  470. for _, tv := range tableData.EdbInfoIdList {
  471. if edbIdExist[tv] {
  472. continue
  473. }
  474. edbIdExist[tv] = true
  475. edbIds = append(edbIds, tv)
  476. }
  477. }
  478. // 混合表格
  479. if detail.Source == utils.MIXED_TABLE {
  480. jsonByte, e := json.Marshal(detail.TableData)
  481. if e != nil {
  482. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  483. return
  484. }
  485. var tableData request.MixedTableReq
  486. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  487. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  488. return
  489. }
  490. if len(tableData.Data) > 0 {
  491. for _, td := range tableData.Data {
  492. for _, tv := range td {
  493. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  494. edbIdExist[tv.EdbInfoId] = true
  495. edbIds = append(edbIds, tv.EdbInfoId)
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. return
  503. }
  504. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  505. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  506. if source == `` {
  507. return ``
  508. }
  509. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  510. }
  511. // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
  512. func GetEdbSourceByEdbInfoIdList(edbInfoIdList []int) (sourceNameList, sourceNameEnList []string, err error) {
  513. sourceNameList = make([]string, 0)
  514. sourceNameEnList = make([]string, 0)
  515. sourceMap := make(map[int]string)
  516. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  517. if tmpErr != nil {
  518. err = tmpErr
  519. return
  520. }
  521. for _, v := range edbInfoList {
  522. // 指标类型:1:基础指标,2:计算指标
  523. if v.EdbType == 2 {
  524. //sourceMap[0] = "弘则研究"
  525. baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  526. for _, baseEdbInfo := range baseEdbInfoArr {
  527. if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
  528. sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
  529. }
  530. }
  531. } else {
  532. sourceMap[v.Source] = v.SourceName
  533. }
  534. }
  535. for source, sourceName := range sourceMap {
  536. if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
  537. continue
  538. }
  539. sourceNameList = append(sourceNameList, sourceName)
  540. sourceNameEn, ok := utils.DataSourceEnMap[source]
  541. if !ok {
  542. sourceNameEn = sourceName
  543. }
  544. sourceNameEnList = append(sourceNameEnList, sourceNameEn)
  545. }
  546. //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
  547. //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
  548. // 图表来源
  549. conf, e := models.GetBusinessConf()
  550. if e != nil {
  551. return
  552. }
  553. if conf[models.BusinessConfCompanyName] != "" {
  554. sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
  555. sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
  556. }
  557. return
  558. }
  559. // GetCustomAnalysisOpButton 获取自定义分析按钮权限
  560. func GetCustomAnalysisOpButton(sysUser *system.Admin, belongUserId int, permissionType []int) (button excel.ExcelInfoDetailButton) {
  561. // 管理员/所属人所有权限
  562. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  563. button.RefreshButton = true
  564. button.CopyButton = true
  565. button.DownloadButton = true
  566. button.OpEdbButton = true
  567. button.RefreshEdbButton = true
  568. button.OpButton = true
  569. button.DeleteButton = true
  570. return
  571. }
  572. // 非管理员-被分享人
  573. for _, v := range permissionType {
  574. if v == 1 {
  575. button.RefreshButton = true
  576. button.CopyButton = true
  577. button.DownloadButton = true
  578. }
  579. if v == 2 {
  580. button.OpEdbButton = true
  581. button.RefreshEdbButton = true
  582. button.OpButton = true
  583. button.CopyButton = true
  584. button.DownloadButton = true
  585. }
  586. }
  587. return
  588. }
  589. func parseExcelScopeCoord(scopeList []string) (scopeCoord string, err error) {
  590. if len(scopeList) == 2 {
  591. x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
  592. if er != nil {
  593. return "", er
  594. }
  595. x2, y2, er := excelize.CellNameToCoordinates(scopeList[1])
  596. if er != nil {
  597. return "", er
  598. }
  599. scopeCoord = fmt.Sprintf("%d,%d,%d,%d", x1, y1, x2, y2)
  600. }
  601. if len(scopeList) == 1 {
  602. x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
  603. if er != nil {
  604. return "", er
  605. }
  606. scopeCoord = fmt.Sprintf("%d,%d", x1, y1)
  607. }
  608. return
  609. }
  610. func ExcelRuleFormat(req *request.ExcelRuleMappingReq, lang string) (res *excel.ExcelInfoRuleMapping, err error) {
  611. res = new(excel.ExcelInfoRuleMapping)
  612. if req.RuleType == 5 {
  613. switch req.LeftValue {
  614. // 今天
  615. case "today":
  616. res.LeftValueShow = "今天"
  617. // 明天
  618. case "tomorrow":
  619. res.LeftValueShow = "明天"
  620. // 最近7天
  621. case "last7days":
  622. res.LeftValueShow = "最近7天"
  623. // 上周
  624. case "lastweek":
  625. res.LeftValueShow = "上周"
  626. // 本周
  627. case "thisweek":
  628. res.LeftValueShow = "本周"
  629. // 下周
  630. case "nextweek":
  631. res.LeftValueShow = "下周"
  632. // 上月
  633. case "lastmonth":
  634. res.LeftValueShow = "上月"
  635. // 本月
  636. case "thismonth":
  637. res.LeftValueShow = "本月"
  638. // 下月
  639. case "nextmonth":
  640. res.LeftValueShow = "下月"
  641. default:
  642. err = errors.New("发生日期规则错误")
  643. return
  644. }
  645. }
  646. // 格式化条件值
  647. switch req.LeftValueType {
  648. // 坐标
  649. case 2:
  650. x, y, err := excelize.CellNameToCoordinates(req.LeftValue)
  651. if err != nil {
  652. return nil, err
  653. }
  654. res.LeftValue = fmt.Sprintf("%d,%d", x, y)
  655. default:
  656. res.LeftValue = req.LeftValue
  657. }
  658. switch req.RightValueType {
  659. // 坐标
  660. case 2:
  661. x, y, err := excelize.CellNameToCoordinates(req.RightValue)
  662. if err != nil {
  663. return nil, err
  664. }
  665. res.RightValue = fmt.Sprintf("%d,%d", x, y)
  666. res.RightValueShow = req.RightValue
  667. default:
  668. res.RightValue = req.RightValue
  669. res.RightValueShow = req.RightValue
  670. }
  671. if res.LeftValueShow == "" {
  672. res.LeftValueShow = req.LeftValue
  673. }
  674. if res.RightValueShow == "" {
  675. res.RightValueShow = req.RightValue
  676. }
  677. res.RuleType = req.RuleType
  678. res.ExcelInfoId = req.ExcelInfoId
  679. res.ExcelInfoRuleMappingId = req.ExcelRuleMappingId
  680. res.LeftValueType = req.LeftValueType
  681. res.RightValueType = req.RightValueType
  682. res.FontColor = req.FontColor
  683. res.BackgroundColor = req.BackgroundColor
  684. res.Remark = req.Remark
  685. res.RemarkEn = req.Remark
  686. res.ScopeShow = req.Scope
  687. scopeList := strings.Split(req.Scope, ":")
  688. res.ScopeCoord, err = parseExcelScopeCoord(scopeList)
  689. return
  690. }
  691. func AddExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
  692. excelRule, err := ExcelRuleFormat(req, lang)
  693. if err != nil {
  694. return
  695. }
  696. if excelRule.ExcelInfoRuleMappingId != 0 {
  697. return errors.New("规则已存在")
  698. }
  699. excelRule.CreateTime = time.Now()
  700. _, err = excelRule.Insert()
  701. return
  702. }
  703. func ModifyExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
  704. excelInfo, err := excel.GetExcelRuleMappingById(req.ExcelRuleMappingId)
  705. if err != nil {
  706. return
  707. }
  708. editExcelInfo, err := ExcelRuleFormat(req, lang)
  709. if err != nil {
  710. return
  711. }
  712. var updateCols []string
  713. if excelInfo.LeftValue != editExcelInfo.LeftValue {
  714. updateCols = append(updateCols, "LeftValue")
  715. updateCols = append(updateCols, "LeftValueShow")
  716. }
  717. if excelInfo.LeftValueType != editExcelInfo.LeftValueType {
  718. updateCols = append(updateCols, "LeftValueType")
  719. }
  720. if excelInfo.RightValue != editExcelInfo.RightValue {
  721. updateCols = append(updateCols, "RightValue")
  722. updateCols = append(updateCols, "RightValueShow")
  723. }
  724. if excelInfo.RightValueType != editExcelInfo.RightValueType {
  725. updateCols = append(updateCols, "RightValueType")
  726. }
  727. if excelInfo.FontColor != editExcelInfo.FontColor {
  728. updateCols = append(updateCols, "FontColor")
  729. }
  730. if excelInfo.BackgroundColor != editExcelInfo.BackgroundColor {
  731. updateCols = append(updateCols, "BackgroundColor")
  732. }
  733. if excelInfo.Remark != editExcelInfo.Remark {
  734. updateCols = append(updateCols, "Remark")
  735. }
  736. if excelInfo.RemarkEn != editExcelInfo.RemarkEn {
  737. updateCols = append(updateCols, "RemarkEn")
  738. }
  739. if excelInfo.ScopeShow != editExcelInfo.ScopeShow {
  740. updateCols = append(updateCols, "ScopeCoord")
  741. updateCols = append(updateCols, "ScopeShow")
  742. }
  743. if len(updateCols) > 0 {
  744. err = editExcelInfo.Update(updateCols)
  745. }
  746. return
  747. }
  748. // GetExcelRuleList 获取规则列表
  749. func GetExcelRuleList(excelInfoId int) (resp *response.ExcelRuleListResp, err error) {
  750. resp = new(response.ExcelRuleListResp)
  751. excelInfoList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  752. if err != nil {
  753. return
  754. }
  755. for _, v := range excelInfoList {
  756. switch v.RuleType {
  757. // 大于
  758. case 1:
  759. v.LeftValueShow = "大于" + v.LeftValueShow
  760. // 小于
  761. case 2:
  762. v.LeftValueShow = "小于" + v.LeftValueShow
  763. // 介于
  764. case 3:
  765. v.LeftValueShow = "介于" + v.LeftValueShow + "到" + v.RightValueShow
  766. // 等于
  767. case 4:
  768. v.LeftValueShow = "等于" + v.LeftValueShow
  769. }
  770. }
  771. resp.List = excelInfoList
  772. return
  773. }
  774. // GetExcelRuleDetail 获取规则列表
  775. func GetExcelRuleDetail(excelInfoMappingId int) (resp *excel.ExcelInfoRuleMappingView, err error) {
  776. resp = new(excel.ExcelInfoRuleMappingView)
  777. excelInfoDetail, err := excel.GetExcelRuleMappingById(excelInfoMappingId)
  778. if err != nil {
  779. return
  780. }
  781. resp = excelInfoDetail
  782. return
  783. }