excel_info.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/models/data_manage/excel"
  8. "eta_gn/eta_api/models/data_manage/excel/request"
  9. "eta_gn/eta_api/models/data_manage/excel/response"
  10. "eta_gn/eta_api/models/system"
  11. "eta_gn/eta_api/services/data"
  12. "eta_gn/eta_api/services/data/data_manage_permission"
  13. "eta_gn/eta_api/utils"
  14. "fmt"
  15. "sort"
  16. "strings"
  17. "time"
  18. "github.com/xuri/excelize/v2"
  19. )
  20. func GetExcelDetailInfoByExcelInfoId(excelInfoId, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  21. errMsg = `获取失败`
  22. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  23. if err != nil {
  24. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  25. if utils.IsErrNoRow(err) {
  26. errMsg = "ETA表格被删除,请刷新页面"
  27. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  28. }
  29. return
  30. }
  31. return formatExcelInfo2Detail(excelInfo, sysUserId, lang)
  32. }
  33. func GetExcelDetailInfoByUnicode(unicode string, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  34. errMsg = `获取失败`
  35. excelInfo, err := excel.GetExcelInfoByUnicode(unicode)
  36. if err != nil {
  37. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  38. if utils.IsErrNoRow(err) {
  39. errMsg = "ETA表格被删除,请刷新页面"
  40. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  41. }
  42. return
  43. }
  44. return formatExcelInfo2Detail(excelInfo, sysUserId, lang)
  45. }
  46. func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  47. checkExcelInfo := excelInfo
  48. if excelInfo.Source == utils.BALANCE_TABLE {
  49. checkExcelInfoId := excelInfo.ExcelInfoId
  50. if excelInfo.BalanceType == 1 {
  51. checkExcelInfoId = excelInfo.RelExcelInfoId
  52. } else {
  53. if excelInfo.ParentId > 0 {
  54. checkExcelInfoId = excelInfo.ParentId
  55. }
  56. }
  57. if checkExcelInfoId != excelInfo.ExcelInfoId {
  58. checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
  59. if err != nil {
  60. errMsg = "获取平衡表格信息失败"
  61. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  62. return
  63. }
  64. }
  65. }
  66. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  67. if err != nil {
  68. err = errors.New("获取表格权限信息失败,Err" + err.Error())
  69. return
  70. }
  71. excelDetail = response.ExcelInfoDetail{
  72. ExcelInfoId: excelInfo.ExcelInfoId,
  73. Source: excelInfo.Source,
  74. ExcelType: excelInfo.ExcelType,
  75. ExcelName: excelInfo.ExcelName,
  76. UniqueCode: excelInfo.UniqueCode,
  77. ExcelClassifyId: excelInfo.ExcelClassifyId,
  78. SysUserId: excelInfo.SysUserId,
  79. SysUserRealName: excelInfo.SysUserRealName,
  80. Content: excelInfo.Content,
  81. ExcelImage: excelInfo.ExcelImage,
  82. FileUrl: excelInfo.FileUrl,
  83. Sort: excelInfo.Sort,
  84. IsDelete: excelInfo.IsDelete,
  85. ModifyTime: excelInfo.ModifyTime,
  86. CreateTime: excelInfo.CreateTime,
  87. TableData: nil,
  88. HaveOperaAuth: haveOperaAuth,
  89. ParentId: excelInfo.ParentId,
  90. BalanceType: excelInfo.BalanceType,
  91. UpdateUserId: excelInfo.UpdateUserId,
  92. UpdateUserRealName: excelInfo.UpdateUserRealName,
  93. RelExcelInfoId: excelInfo.RelExcelInfoId,
  94. SourcesFrom: excelInfo.SourcesFrom,
  95. }
  96. if !haveOperaAuth {
  97. return
  98. }
  99. switch excelInfo.Source {
  100. case utils.TIME_TABLE: // 时间序列表格
  101. var tableDataConfig TableDataConfig
  102. err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
  103. if err != nil {
  104. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  105. return
  106. }
  107. result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
  108. if tmpErr != nil {
  109. err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
  110. return
  111. }
  112. result = SetExcelByDecimalConfig(result, tableDataConfig.DecimalConfig)
  113. if len(result.EdbInfoIdList) > 0 {
  114. classifyIdList := make([]int, 0)
  115. for _, v := range result.Data {
  116. classifyIdList = append(classifyIdList, v.ClassifyId)
  117. }
  118. classifyMap := make(map[int]*data_manage.EdbClassify)
  119. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  120. if tmpErr != nil {
  121. err = errors.New("获取分类列表失败,Err:" + tmpErr.Error())
  122. return
  123. }
  124. for _, v := range classifyList {
  125. classifyMap[v.ClassifyId] = v
  126. }
  127. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  128. if tmpErr != nil {
  129. err = errors.New("获取所有有权限的指标和分类失败,Err:" + tmpErr.Error())
  130. return
  131. }
  132. for i, v := range result.Data {
  133. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  134. result.Data[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  135. }
  136. }
  137. }
  138. excelDetail.TableData = result
  139. case utils.MIXED_TABLE, utils.BALANCE_TABLE: // 混合表格 平衡表
  140. var result request.MixedTableReq
  141. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  142. if err != nil {
  143. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  144. return
  145. }
  146. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
  147. if tmpErr != nil {
  148. errMsg = "获取失败"
  149. if tmpErrMsg != `` {
  150. errMsg = tmpErrMsg
  151. }
  152. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  153. return
  154. }
  155. result.Data = newData
  156. excelDetail.TableData = result
  157. }
  158. if excelDetail.Source == utils.BALANCE_TABLE {
  159. excelDetail.Button = GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, excelDetail.HaveOperaAuth, checkExcelInfo.ExcelInfoId)
  160. }
  161. return
  162. }
  163. func SetExcelByDecimalConfig(tableData request.TableDataReq, config []request.DecimalConfig) request.TableDataReq {
  164. excelData := tableData.Data
  165. edbInfoIndex := make(map[int]int)
  166. dateIndex := make(map[string]int)
  167. for i, v := range excelData {
  168. edbInfoIndex[v.EdbInfoId] = i
  169. }
  170. for i, v := range excelData[0].Data {
  171. dateIndex[v.DataTime] = i
  172. }
  173. for _, conf := range config {
  174. if conf.Col > 0 {
  175. if v, ok := edbInfoIndex[conf.Col]; ok {
  176. excelData[v].Decimal = conf.Decimal
  177. for i := 0; i < len(excelData[v].Data); i++ {
  178. excelData[v].Data[i].Decimal = conf.Decimal
  179. }
  180. }
  181. }
  182. if conf.Row != "" {
  183. if v, ok := dateIndex[conf.Row]; ok {
  184. for i := 0; i < len(excelData); i++ {
  185. excelData[i].Data[v].Decimal = conf.Decimal
  186. }
  187. }
  188. }
  189. }
  190. tableData.Data = excelData
  191. tableData.DecimalConfig = config
  192. return tableData
  193. }
  194. func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int, haveOperaAuth bool) (button excel.ExcelInfoDetailButton) {
  195. if !haveOperaAuth {
  196. return
  197. }
  198. button.RefreshButton = true
  199. button.CopyButton = true
  200. button.DownloadButton = true
  201. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || source == utils.EXCEL_DEFAULT {
  202. button.OpButton = true
  203. button.DeleteButton = true
  204. }
  205. if source == utils.CUSTOM_ANALYSIS_TABLE {
  206. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  207. button.OpEdbButton = true // 生成、查看指标按钮
  208. button.RefreshEdbButton = true // 刷新指标按钮
  209. }
  210. }
  211. return
  212. }
  213. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  214. var dataList []*data_manage.EdbDataList
  215. resultDataList = make([]request.ManualDataReq, 0)
  216. switch edbInfo.EdbInfoType {
  217. case 0:
  218. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  219. case 1:
  220. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  221. default:
  222. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  223. }
  224. if err != nil {
  225. return
  226. }
  227. lenData := len(dataList)
  228. if lenData <= 0 {
  229. return
  230. }
  231. tmpManualDateNum := 0 // 手工数据的期数
  232. lenManualDate := len(manualDateList)
  233. if lenManualDate > 0 {
  234. sortDateList := manualDateList
  235. baseDateList := utils.StrArr{}
  236. baseDateList = append(baseDateList, sortDateList...)
  237. sort.Sort(baseDateList)
  238. sortDateList = append([]string{}, baseDateList...)
  239. lastData := dataList[lenData-1]
  240. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  241. if tmpErr != nil {
  242. err = tmpErr
  243. return
  244. }
  245. for _, tmpDateStr := range sortDateList {
  246. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  247. if tmpErr != nil {
  248. err = tmpErr
  249. return
  250. }
  251. if tmpDate.After(lastDataDate) {
  252. tmpManualDateNum++
  253. continue
  254. }
  255. break
  256. }
  257. }
  258. num = num - tmpManualDateNum
  259. if num > lenData {
  260. num = lenData
  261. }
  262. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  263. for i := 1; i <= num; i++ {
  264. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  265. dataType := 1
  266. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  267. dataType = 5
  268. }
  269. resultDataList = append(resultDataList, request.ManualDataReq{
  270. DataType: dataType,
  271. DataTime: dataList[lenData-i].DataTime,
  272. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  273. Value: fmt.Sprint(dataList[lenData-i].Value),
  274. Decimal: decimal,
  275. DataTimeType: 1,
  276. })
  277. }
  278. return
  279. }
  280. func PadFirstEdbDataList(resultDataList []request.ManualDataReq, dateList []string, sortType string, decimal int) []request.ManualDataReq {
  281. originDataNum := len(resultDataList)
  282. requsetDateNum := len(dateList)
  283. if originDataNum >= requsetDateNum {
  284. return resultDataList
  285. }
  286. padNum := requsetDateNum - originDataNum
  287. if sortType == "asc" {
  288. for i := 0; i < padNum; i++ {
  289. resultDataList = append(resultDataList, request.ManualDataReq{
  290. DataType: 0,
  291. DataTime: dateList[originDataNum+i],
  292. Decimal: decimal,
  293. ShowValue: ``,
  294. Value: ``,
  295. DataTimeType: 1,
  296. })
  297. }
  298. } else {
  299. var tmpDateList []request.ManualDataReq
  300. for i := padNum - 1; i <= 0; i-- {
  301. tmpDateList = append(tmpDateList, request.ManualDataReq{
  302. DataType: 0,
  303. DataTime: dateList[originDataNum+i],
  304. Decimal: decimal,
  305. ShowValue: ``,
  306. Value: ``,
  307. DataTimeType: 1,
  308. })
  309. resultDataList = append(tmpDateList, resultDataList...)
  310. }
  311. }
  312. return resultDataList
  313. }
  314. func GetOtherEdbDataListFollowDate(edbInfo *data_manage.EdbInfo, dateList []string, dateDecimal map[string]int) (resultDataList []request.ManualDataReq, err error) {
  315. lenDate := len(dateList)
  316. if lenDate <= 0 {
  317. return
  318. }
  319. sortDateList := dateList
  320. baseDateList := utils.StrArr{}
  321. baseDateList = append(baseDateList, sortDateList...)
  322. sort.Sort(baseDateList)
  323. sortDateList = append([]string{}, baseDateList...)
  324. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  325. if err != nil {
  326. return
  327. }
  328. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  329. if err != nil {
  330. return
  331. }
  332. var dataList []*data_manage.EdbDataList
  333. switch edbInfo.EdbInfoType {
  334. case 0:
  335. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  336. case 1:
  337. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  338. default:
  339. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  340. }
  341. if err != nil {
  342. return
  343. }
  344. baseDataList := make([]*data_manage.EdbDataList, 0)
  345. var lastData *data_manage.EdbDataList
  346. var isInsert bool
  347. for _, data := range dataList {
  348. tmpDate := data.DataTime
  349. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  350. if tmpErr != nil {
  351. err = tmpErr
  352. return
  353. }
  354. if tmpDateTime.Before(firstDateTime) {
  355. lastData = data
  356. continue
  357. }
  358. if !isInsert && lastData != nil {
  359. baseDataList = append(baseDataList, lastData)
  360. }
  361. if tmpDateTime.After(endDateTime) {
  362. baseDataList = append(baseDataList, data)
  363. break
  364. }
  365. baseDataList = append(baseDataList, data)
  366. isInsert = true
  367. }
  368. realValMap := make(map[string]string)
  369. for _, v := range baseDataList {
  370. realValMap[v.DataTime] = v.DataTime
  371. }
  372. handleDataMap := make(map[string]float64)
  373. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  374. if err != nil {
  375. return
  376. }
  377. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  378. for _, date := range sortDateList {
  379. dataType := 1
  380. if _, ok := realValMap[date]; !ok {
  381. dataType = 2
  382. } else {
  383. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  384. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  385. dataType = 5
  386. }
  387. }
  388. var value, showValue string
  389. if tmpVal, ok := handleDataMap[date]; ok {
  390. value = fmt.Sprint(tmpVal)
  391. showValue = value
  392. } else {
  393. dataType = 3
  394. }
  395. tmpData := request.ManualDataReq{
  396. DataType: dataType,
  397. DataTime: date,
  398. ShowValue: showValue,
  399. Value: value,
  400. }
  401. if v, ok := dateDecimal[date]; ok {
  402. tmpData.Decimal = v
  403. } else {
  404. tmpData.Decimal = -1
  405. }
  406. resultDataList = append(resultDataList, tmpData)
  407. }
  408. return
  409. }
  410. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  411. lenDate := len(dateList)
  412. if lenDate <= 0 {
  413. return
  414. }
  415. sortDateList := dateList
  416. baseDateList := utils.StrArr{}
  417. baseDateList = append(baseDateList, sortDateList...)
  418. sort.Sort(baseDateList)
  419. sortDateList = append([]string{}, baseDateList...)
  420. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  421. if err != nil {
  422. return
  423. }
  424. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  425. if err != nil {
  426. return
  427. }
  428. var dataList []*data_manage.EdbDataList
  429. switch edbInfo.EdbInfoType {
  430. case 0:
  431. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  432. case 1:
  433. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  434. default:
  435. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  436. }
  437. if err != nil {
  438. return
  439. }
  440. baseDataList := make([]*data_manage.EdbDataList, 0)
  441. var lastData *data_manage.EdbDataList
  442. var isInsert bool
  443. for _, data := range dataList {
  444. tmpDate := data.DataTime
  445. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  446. if tmpErr != nil {
  447. err = tmpErr
  448. return
  449. }
  450. if tmpDateTime.Before(firstDateTime) {
  451. lastData = data
  452. continue
  453. }
  454. if !isInsert && lastData != nil {
  455. baseDataList = append(baseDataList, lastData)
  456. }
  457. if tmpDateTime.After(endDateTime) {
  458. baseDataList = append(baseDataList, data)
  459. break
  460. }
  461. baseDataList = append(baseDataList, data)
  462. isInsert = true
  463. }
  464. realValMap := make(map[string]string)
  465. for _, v := range baseDataList {
  466. realValMap[v.DataTime] = v.DataTime
  467. }
  468. handleDataMap := make(map[string]float64)
  469. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  470. if err != nil {
  471. return
  472. }
  473. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  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. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  481. dataType = 5
  482. }
  483. }
  484. var value, showValue string
  485. if tmpVal, ok := handleDataMap[date]; ok {
  486. value = fmt.Sprint(tmpVal)
  487. showValue = value
  488. } else {
  489. dataType = 3
  490. }
  491. resultDataList = append(resultDataList, request.ManualDataReq{
  492. DataType: dataType,
  493. DataTime: date,
  494. Decimal: decimal,
  495. ShowValue: showValue,
  496. Value: value,
  497. })
  498. }
  499. return
  500. }
  501. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string, decimal int) (resultDataList []request.ManualDataReq, err error) {
  502. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  503. if err != nil {
  504. return
  505. }
  506. var dataList []*data_manage.EdbDataList
  507. switch edbInfo.EdbInfoType {
  508. case 0:
  509. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  510. case 1:
  511. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  512. default:
  513. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  514. }
  515. if err != nil {
  516. return
  517. }
  518. lenData := len(dataList)
  519. if lenData <= 0 {
  520. return
  521. }
  522. lastData := dataList[lenData-1]
  523. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  524. if err != nil {
  525. return
  526. }
  527. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  528. dataList = dataList[:lenData-1]
  529. lenData = len(dataList)
  530. }
  531. if num > lenData {
  532. num = lenData
  533. }
  534. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  535. for i := 1; i <= num; i++ {
  536. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  537. dataType := 1
  538. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  539. dataType = 5
  540. }
  541. resultDataList = append(resultDataList, request.ManualDataReq{
  542. DataType: dataType,
  543. DataTime: dataList[lenData-i].DataTime,
  544. Decimal: decimal,
  545. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  546. Value: fmt.Sprint(dataList[lenData-i].Value),
  547. })
  548. }
  549. return
  550. }
  551. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  552. edbIds = make([]int, 0)
  553. edbIdExist := make(map[int]bool)
  554. for _, v := range excelCodes {
  555. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  556. if e != nil {
  557. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  558. return
  559. }
  560. if detail.Source == utils.TIME_TABLE {
  561. jsonByte, e := json.Marshal(detail.TableData)
  562. if e != nil {
  563. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  564. return
  565. }
  566. var tableData request.TableDataReq
  567. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  568. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  569. return
  570. }
  571. for _, tv := range tableData.EdbInfoIdList {
  572. if edbIdExist[tv] {
  573. continue
  574. }
  575. edbIdExist[tv] = true
  576. edbIds = append(edbIds, tv)
  577. }
  578. }
  579. if detail.Source == utils.MIXED_TABLE {
  580. jsonByte, e := json.Marshal(detail.TableData)
  581. if e != nil {
  582. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  583. return
  584. }
  585. var tableData request.MixedTableReq
  586. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  587. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  588. return
  589. }
  590. if len(tableData.Data) > 0 {
  591. for _, td := range tableData.Data {
  592. for _, tv := range td {
  593. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  594. edbIdExist[tv.EdbInfoId] = true
  595. edbIds = append(edbIds, tv.EdbInfoId)
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. return
  603. }
  604. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  605. if source == `` {
  606. return ``
  607. }
  608. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  609. }
  610. func GetEdbSourceByEdbInfoIdList(edbInfoIdList []int) (sourceNameList, sourceNameEnList []string, err error) {
  611. sourceNameList = make([]string, 0)
  612. sourceNameEnList = make([]string, 0)
  613. sourceMap := make(map[int]string)
  614. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  615. if tmpErr != nil {
  616. err = tmpErr
  617. return
  618. }
  619. for _, v := range edbInfoList {
  620. if v.EdbType == 2 {
  621. baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  622. for _, baseEdbInfo := range baseEdbInfoArr {
  623. if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
  624. sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
  625. }
  626. }
  627. } else {
  628. sourceMap[v.Source] = v.SourceName
  629. }
  630. }
  631. for source, sourceName := range sourceMap {
  632. if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
  633. continue
  634. }
  635. sourceNameList = append(sourceNameList, sourceName)
  636. sourceNameEn, ok := utils.DataSourceEnMap[source]
  637. if !ok {
  638. sourceNameEn = sourceName
  639. }
  640. sourceNameEnList = append(sourceNameEnList, sourceNameEn)
  641. }
  642. conf, e := models.GetBusinessConf()
  643. if e != nil {
  644. return
  645. }
  646. if conf[models.BusinessConfCompanyName] != "" {
  647. sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
  648. sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
  649. }
  650. return
  651. }
  652. func GetCustomAnalysisOpButton(sysUser *system.Admin, belongUserId int, permissionType []int) (button excel.ExcelInfoDetailButton) {
  653. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  654. button.RefreshButton = true
  655. button.CopyButton = true
  656. button.DownloadButton = true
  657. button.OpEdbButton = true
  658. button.RefreshEdbButton = true
  659. button.OpButton = true
  660. button.DeleteButton = true
  661. return
  662. }
  663. for _, v := range permissionType {
  664. if v == 1 {
  665. button.RefreshButton = true
  666. button.CopyButton = true
  667. button.DownloadButton = true
  668. }
  669. if v == 2 {
  670. button.OpEdbButton = true
  671. button.RefreshEdbButton = true
  672. button.OpButton = true
  673. button.CopyButton = true
  674. button.DownloadButton = true
  675. }
  676. }
  677. return
  678. }
  679. func parseExcelScopeCoord(scopeList []string) (scopeCoord string, err error) {
  680. if len(scopeList) == 2 {
  681. x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
  682. if er != nil {
  683. return "", er
  684. }
  685. x2, y2, er := excelize.CellNameToCoordinates(scopeList[1])
  686. if er != nil {
  687. return "", er
  688. }
  689. scopeCoord = fmt.Sprintf("%d,%d,%d,%d", x1, y1, x2, y2)
  690. }
  691. if len(scopeList) == 1 {
  692. x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
  693. if er != nil {
  694. return "", er
  695. }
  696. scopeCoord = fmt.Sprintf("%d,%d", x1, y1)
  697. }
  698. return
  699. }
  700. func ExcelRuleFormat(req *request.ExcelRuleMappingReq, lang string) (res *excel.ExcelInfoRuleMapping, err error) {
  701. res = new(excel.ExcelInfoRuleMapping)
  702. if req.RuleType == 5 {
  703. switch req.LeftValue {
  704. case "today":
  705. res.LeftValueBack = req.LeftValue
  706. case "tomorrow":
  707. res.LeftValueBack = req.LeftValue
  708. case "last7days":
  709. res.LeftValueBack = req.LeftValue
  710. case "lastweek":
  711. res.LeftValueBack = req.LeftValue
  712. case "thisweek":
  713. res.LeftValueBack = req.LeftValue
  714. case "nextweek":
  715. res.LeftValueBack = req.LeftValue
  716. case "lastmonth":
  717. res.LeftValueBack = req.LeftValue
  718. case "thismonth":
  719. res.LeftValueBack = req.LeftValue
  720. case "nextmonth":
  721. res.LeftValueBack = req.LeftValue
  722. default:
  723. err = errors.New("发生日期规则错误")
  724. return
  725. }
  726. }
  727. switch req.LeftValueType {
  728. case 2:
  729. x, y, err := excelize.CellNameToCoordinates(req.LeftValue)
  730. if err != nil {
  731. return nil, err
  732. }
  733. res.LeftValue = req.LeftValue
  734. res.LeftValueBack = fmt.Sprintf("%d,%d", x, y)
  735. default:
  736. res.LeftValue = req.LeftValue
  737. res.LeftValueBack = req.LeftValue
  738. }
  739. switch req.RightValueType {
  740. case 2:
  741. x, y, err := excelize.CellNameToCoordinates(req.RightValue)
  742. if err != nil {
  743. return nil, err
  744. }
  745. res.RightValue = req.RightValue
  746. res.RightValueBack = fmt.Sprintf("%d,%d", x, y)
  747. default:
  748. res.RightValue = req.RightValue
  749. res.RightValueBack = req.RightValue
  750. }
  751. if res.LeftValueBack == "" {
  752. res.LeftValueBack = req.LeftValue
  753. }
  754. if res.RightValueBack == "" {
  755. res.RightValueBack = req.RightValue
  756. }
  757. res.RuleType = req.RuleType
  758. res.ExcelInfoId = req.ExcelInfoId
  759. res.ExcelInfoRuleMappingId = req.ExcelRuleMappingId
  760. res.LeftValueType = req.LeftValueType
  761. res.RightValueType = req.RightValueType
  762. res.FontColor = req.FontColor
  763. res.BackgroundColor = req.BackgroundColor
  764. res.Remark = req.Remark
  765. res.RemarkEn = req.Remark
  766. res.ScopeShow = req.Scope
  767. scopeList := strings.Split(req.Scope, ":")
  768. res.ScopeCoord, err = parseExcelScopeCoord(scopeList)
  769. return
  770. }
  771. func AddExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
  772. excelRule, err := ExcelRuleFormat(req, lang)
  773. if err != nil {
  774. return
  775. }
  776. if excelRule.ExcelInfoRuleMappingId != 0 {
  777. return errors.New("规则已存在")
  778. }
  779. excelRule.CreateTime = time.Now()
  780. _, err = excelRule.Insert()
  781. return
  782. }
  783. func ModifyExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
  784. excelInfo, err := excel.GetExcelRuleMappingById(req.ExcelRuleMappingId)
  785. if err != nil {
  786. return
  787. }
  788. editExcelInfo, err := ExcelRuleFormat(req, lang)
  789. if err != nil {
  790. return
  791. }
  792. var updateCols []string
  793. if excelInfo.LeftValue != editExcelInfo.LeftValue {
  794. updateCols = append(updateCols, "LeftValue")
  795. updateCols = append(updateCols, "LeftValueBack")
  796. }
  797. if excelInfo.LeftValueType != editExcelInfo.LeftValueType {
  798. updateCols = append(updateCols, "LeftValueType")
  799. }
  800. if excelInfo.RightValue != editExcelInfo.RightValue {
  801. updateCols = append(updateCols, "RightValue")
  802. updateCols = append(updateCols, "RightValueBack")
  803. }
  804. if excelInfo.RightValueType != editExcelInfo.RightValueType {
  805. updateCols = append(updateCols, "RightValueType")
  806. }
  807. if excelInfo.FontColor != editExcelInfo.FontColor {
  808. updateCols = append(updateCols, "FontColor")
  809. }
  810. if excelInfo.BackgroundColor != editExcelInfo.BackgroundColor {
  811. updateCols = append(updateCols, "BackgroundColor")
  812. }
  813. if excelInfo.Remark != editExcelInfo.Remark {
  814. updateCols = append(updateCols, "Remark")
  815. }
  816. if excelInfo.RemarkEn != editExcelInfo.RemarkEn {
  817. updateCols = append(updateCols, "RemarkEn")
  818. }
  819. if excelInfo.ScopeShow != editExcelInfo.ScopeShow {
  820. updateCols = append(updateCols, "ScopeCoord")
  821. updateCols = append(updateCols, "ScopeShow")
  822. }
  823. if len(updateCols) > 0 {
  824. err = editExcelInfo.Update(updateCols)
  825. }
  826. return
  827. }
  828. func GetExcelRuleList(excelInfoId int) (resp *response.ExcelRuleListResp, err error) {
  829. resp = new(response.ExcelRuleListResp)
  830. excelInfoList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  831. if err != nil {
  832. return
  833. }
  834. resp.List = excelInfoList
  835. return
  836. }
  837. func GetExcelRuleDetail(excelInfoMappingId int) (resp *excel.ExcelInfoRuleMappingView, err error) {
  838. resp = new(excel.ExcelInfoRuleMappingView)
  839. excelInfoDetail, err := excel.GetExcelRuleMappingById(excelInfoMappingId)
  840. if err != nil {
  841. return
  842. }
  843. resp = excelInfoDetail
  844. return
  845. }