excel_info.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_chart_lib/models"
  6. "eta_gn/eta_chart_lib/models/data_manage"
  7. "eta_gn/eta_chart_lib/models/data_manage/excel"
  8. "eta_gn/eta_chart_lib/models/request"
  9. "eta_gn/eta_chart_lib/services/data"
  10. "eta_gn/eta_chart_lib/utils"
  11. "fmt"
  12. "sort"
  13. "time"
  14. )
  15. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
  16. var dataList []*models.EdbDataList
  17. switch edbInfo.EdbInfoType {
  18. case 0:
  19. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  20. case 1:
  21. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  22. default:
  23. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  24. return
  25. }
  26. lenData := len(dataList)
  27. if lenData <= 0 {
  28. return
  29. }
  30. tmpManualDateNum := 0 // 手工数据的期数
  31. lenManualDate := len(manualDateList)
  32. if lenManualDate > 0 {
  33. sortDateList := manualDateList
  34. baseDateList := utils.StrArr{}
  35. baseDateList = append(baseDateList, sortDateList...)
  36. sort.Sort(baseDateList)
  37. sortDateList = append([]string{}, baseDateList...)
  38. lastData := dataList[lenData-1]
  39. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  40. if tmpErr != nil {
  41. err = tmpErr
  42. return
  43. }
  44. for _, tmpDateStr := range sortDateList {
  45. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  46. if tmpErr != nil {
  47. err = tmpErr
  48. return
  49. }
  50. if tmpDate.After(lastDataDate) {
  51. tmpManualDateNum++
  52. continue
  53. }
  54. break
  55. }
  56. }
  57. num = num - tmpManualDateNum
  58. if num > lenData {
  59. num = lenData
  60. }
  61. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  62. for i := 1; i <= num; i++ {
  63. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  64. dataType := 1
  65. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  66. dataType = 5
  67. }
  68. resultDataList = append(resultDataList, request.ManualDataReq{
  69. DataType: dataType,
  70. DataTime: dataList[lenData-i].DataTime,
  71. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  72. Value: fmt.Sprint(dataList[lenData-i].Value),
  73. DataTimeType: 1,
  74. })
  75. }
  76. return
  77. }
  78. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  79. lenDate := len(dateList)
  80. if lenDate <= 0 {
  81. return
  82. }
  83. sortDateList := dateList
  84. baseDateList := utils.StrArr{}
  85. baseDateList = append(baseDateList, sortDateList...)
  86. sort.Sort(baseDateList)
  87. sortDateList = append([]string{}, baseDateList...)
  88. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  89. if err != nil {
  90. return
  91. }
  92. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  93. if err != nil {
  94. return
  95. }
  96. var dataList []*models.EdbDataList
  97. switch edbInfo.EdbInfoType {
  98. case 0:
  99. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  100. case 1:
  101. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  102. default:
  103. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  104. return
  105. }
  106. baseDataList := make([]*models.EdbDataList, 0)
  107. var lastData *models.EdbDataList
  108. var isInsert bool
  109. for _, data := range dataList {
  110. tmpDate := data.DataTime
  111. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  112. if tmpErr != nil {
  113. err = tmpErr
  114. return
  115. }
  116. if tmpDateTime.Before(firstDateTime) {
  117. lastData = data
  118. continue
  119. }
  120. if !isInsert && lastData != nil {
  121. baseDataList = append(baseDataList, lastData)
  122. }
  123. if tmpDateTime.After(endDateTime) {
  124. baseDataList = append(baseDataList, data)
  125. break
  126. }
  127. baseDataList = append(baseDataList, data)
  128. isInsert = true
  129. }
  130. realValMap := make(map[string]string)
  131. for _, v := range baseDataList {
  132. realValMap[v.DataTime] = v.DataTime
  133. }
  134. handleDataMap := make(map[string]float64)
  135. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  136. if err != nil {
  137. return
  138. }
  139. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  140. for _, date := range sortDateList {
  141. dataType := 1
  142. if _, ok := realValMap[date]; !ok {
  143. dataType = 2
  144. } else {
  145. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  146. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  147. dataType = 5
  148. }
  149. }
  150. var value, showValue string
  151. if tmpVal, ok := handleDataMap[date]; ok {
  152. value = fmt.Sprint(tmpVal)
  153. showValue = value
  154. } else {
  155. dataType = 3
  156. }
  157. resultDataList = append(resultDataList, request.ManualDataReq{
  158. DataType: dataType,
  159. DataTime: date,
  160. ShowValue: showValue,
  161. Value: value,
  162. })
  163. }
  164. return
  165. }
  166. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  167. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  168. if err != nil {
  169. return
  170. }
  171. var dataList []*models.EdbDataList
  172. switch edbInfo.EdbInfoType {
  173. case 0:
  174. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  175. case 1:
  176. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  177. default:
  178. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  179. return
  180. }
  181. lenData := len(dataList)
  182. if lenData <= 0 {
  183. return
  184. }
  185. lastData := dataList[lenData-1]
  186. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  187. if err != nil {
  188. return
  189. }
  190. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  191. dataList = dataList[:lenData-1]
  192. lenData = len(dataList)
  193. }
  194. if num > lenData {
  195. num = lenData
  196. }
  197. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  198. for i := 1; i <= num; i++ {
  199. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  200. dataType := 1
  201. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  202. dataType = 5
  203. }
  204. resultDataList = append(resultDataList, request.ManualDataReq{
  205. DataType: dataType,
  206. DataTime: dataList[lenData-i].DataTime,
  207. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  208. Value: fmt.Sprint(dataList[lenData-i].Value),
  209. })
  210. }
  211. return
  212. }
  213. func GetExcelDetailInfoByExcelInfoId(uniqueCode, lang string) (excelDetail excel.ExcelInfoDetail, errMsg string, err error) {
  214. errMsg = `获取失败`
  215. excelInfo, err := excel.GetExcelInfoByUniqueCode(uniqueCode)
  216. if err != nil {
  217. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  218. if utils.IsErrNoRow(err) {
  219. errMsg = "ETA表格被删除,请刷新页面"
  220. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  221. }
  222. return
  223. }
  224. return formatExcelInfo2Detail(excelInfo, lang)
  225. }
  226. func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, lang string) (excelDetail excel.ExcelInfoDetail, errMsg string, err error) {
  227. excelDetail = excel.ExcelInfoDetail{
  228. ExcelInfoId: excelInfo.ExcelInfoId,
  229. Source: excelInfo.Source,
  230. ExcelType: excelInfo.ExcelType,
  231. ExcelName: excelInfo.ExcelName,
  232. UniqueCode: excelInfo.UniqueCode,
  233. ExcelClassifyId: excelInfo.ExcelClassifyId,
  234. SysUserId: excelInfo.SysUserId,
  235. SysUserRealName: excelInfo.SysUserRealName,
  236. Content: excelInfo.Content,
  237. ExcelImage: excelInfo.ExcelImage,
  238. FileUrl: excelInfo.FileUrl,
  239. Sort: excelInfo.Sort,
  240. IsDelete: excelInfo.IsDelete,
  241. ModifyTime: excelInfo.ModifyTime,
  242. CreateTime: excelInfo.CreateTime,
  243. TableData: nil,
  244. }
  245. switch excelInfo.Source {
  246. case utils.TIME_TABLE: // 时间序列表格
  247. var tableDataConfig TableDataConfig
  248. err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
  249. if err != nil {
  250. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  251. return
  252. }
  253. result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
  254. if tmpErr != nil {
  255. err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
  256. return
  257. }
  258. excelDetail.TableData = result
  259. case utils.MIXED_TABLE: // 混合表格
  260. var result request.MixedTableReq
  261. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  262. if err != nil {
  263. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  264. return
  265. }
  266. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
  267. if tmpErr != nil {
  268. errMsg = "获取失败"
  269. if tmpErrMsg != `` {
  270. errMsg = tmpErrMsg
  271. }
  272. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  273. return
  274. }
  275. result.Data = newData
  276. excelDetail.TableData = result
  277. }
  278. return
  279. }