mixed_table.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/excel/request"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // GetMixedTableCellData 获取混合表格数据
  14. func GetMixedTableCellData(cellRelationConf string, config [][]request.MixedTableCellDataReq) (newMixedTableCellDataList [][]request.MixedTableCellDataReq, err error) {
  15. newMixedTableCellDataList = config
  16. // 单元格关系配置x信息
  17. cellRelationConfMap := make(map[string]request.CellRelationConf)
  18. cellRelationConfList := make([]request.CellRelationConf, 0)
  19. if cellRelationConf != `` {
  20. err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
  21. if err != nil {
  22. return
  23. }
  24. for _, v := range cellRelationConfList {
  25. cellRelationConfMap[v.Key] = v
  26. }
  27. }
  28. // 找出所有的关联指标id
  29. config, edbInfoIdList, _ := handleConfig(config)
  30. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  31. if err != nil {
  32. return
  33. }
  34. // 指标信息map
  35. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  36. // 日度指标数据map
  37. edbDataListMap := make(map[int]map[string]float64)
  38. // 月度指标数据map
  39. edbMonthDataListMap := make(map[int]map[string]float64)
  40. for _, edbInfo := range edbInfoList {
  41. edbInfoMap[edbInfo.EdbInfoId] = edbInfo
  42. dataList := make([]*data_manage.EdbDataList, 0)
  43. switch edbInfo.EdbInfoType {
  44. case 0:
  45. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  46. case 1:
  47. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  48. default:
  49. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  50. }
  51. dateValMap := make(map[string]float64)
  52. monthValMap := make(map[string]float64)
  53. for _, data := range dataList {
  54. // 日度数据
  55. dateValMap[data.DataTime] = data.Value
  56. // 月度数据(取该月份的第一个数据)
  57. yearMonth := strings.Join(strings.Split(data.DataTime, "-")[0:2], "-")
  58. if _, ok := monthValMap[yearMonth]; !ok {
  59. monthValMap[yearMonth] = data.Value
  60. }
  61. }
  62. edbDataListMap[edbInfo.EdbInfoId] = dateValMap
  63. edbMonthDataListMap[edbInfo.EdbInfoId] = monthValMap
  64. }
  65. for k, row := range newMixedTableCellDataList {
  66. for i, cell := range row {
  67. if cell.DataType == request.EdbDT {
  68. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  69. cell.ShowValue = edbInfo.EdbName
  70. }
  71. } else if utils.InArrayByInt([]int{request.InsertDataDT, request.PopInsertDataDT}, cell.DataType) {
  72. tmpDateList := strings.Split(cell.DataTime, "-")
  73. tmpDateValMap := make(map[string]float64)
  74. if len(tmpDateList) == 2 {
  75. //月度数据
  76. if dateValMap, ok := edbMonthDataListMap[cell.EdbInfoId]; ok {
  77. tmpDateValMap = dateValMap
  78. }
  79. } else {
  80. // 日度数据
  81. if dateValMap, ok := edbDataListMap[cell.EdbInfoId]; ok {
  82. tmpDateValMap = dateValMap
  83. }
  84. }
  85. if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
  86. //cell.ShowValue = fmt.Sprint(val)
  87. cell.ShowValue = utils.FormatTableDataShowValue(val)
  88. }
  89. }
  90. row[i] = cell
  91. }
  92. newMixedTableCellDataList[k] = row
  93. }
  94. return
  95. }
  96. func handleConfig(config [][]request.MixedTableCellDataReq) ([][]request.MixedTableCellDataReq, []int, []int) {
  97. edbInfoIdList := make([]int, 0)
  98. dataEdbInfoIdList := make([]int, 0)
  99. for _, row := range config {
  100. for _, cell := range row {
  101. switch cell.DataType {
  102. case request.EdbDT: // 指标信息
  103. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  104. case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
  105. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  106. case request.DateDT: // 日期类型
  107. }
  108. }
  109. }
  110. return config, edbInfoIdList, dataEdbInfoIdList
  111. }
  112. func HandleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  113. return handleDate(dataTimeType, val)
  114. }
  115. func handleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  116. if val == `` {
  117. errMsg = "错误的日期数据"
  118. err = errors.New(errMsg)
  119. return
  120. }
  121. switch dataTimeType {
  122. case request.CustomDateT: //手动输入日期
  123. date = val
  124. case request.SystemDateT: // 系统日期
  125. date = time.Now().Format(utils.FormatDate)
  126. case request.SystemCalculateDateT: // 系统日期计算后的日期
  127. date, err, errMsg = handleSystemCalculateDateT(val)
  128. case request.SystemAppointDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  129. date, err, errMsg = handleSystemAppointDateT(val)
  130. case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
  131. default:
  132. errMsg = "错误的日期类型"
  133. err = errors.New(errMsg)
  134. return
  135. }
  136. return
  137. }
  138. // handleSystemCalculateDateT
  139. // @Description: 处理系统日期计算后的日期
  140. // @author: Roc
  141. // @datetime2023-10-26 11:12:56
  142. // @param val string
  143. // @return date string
  144. // @return err error
  145. // @return errMsg string
  146. func handleSystemCalculateDateT(val string) (date string, err error, errMsg string) {
  147. type SystemCalculateVal struct {
  148. Num int
  149. Frequency string
  150. }
  151. var config SystemCalculateVal
  152. err = json.Unmarshal([]byte(val), &config)
  153. if err != nil {
  154. return
  155. }
  156. currDate := time.Now()
  157. switch config.Frequency {
  158. case "", "日":
  159. date = currDate.AddDate(0, 0, config.Num).Format(utils.FormatDate)
  160. default:
  161. errMsg = "错误的日期频度:" + config.Frequency
  162. err = errors.New(errMsg)
  163. return
  164. }
  165. return
  166. }
  167. // handleSystemAppointDateT
  168. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  169. // @author: Roc
  170. // @datetime2023-10-26 11:13:52
  171. // @param val string
  172. // @return date string
  173. // @return err error
  174. // @return errMsg string
  175. func handleSystemAppointDateT(val string) (date string, err error, errMsg string) {
  176. type SystemCalculateVal struct {
  177. Frequency string
  178. Day string
  179. }
  180. var config SystemCalculateVal
  181. err = json.Unmarshal([]byte(val), &config)
  182. if err != nil {
  183. return
  184. }
  185. currDate := time.Now()
  186. switch config.Frequency {
  187. case "本周":
  188. day := currDate.Day()
  189. num := 0
  190. switch config.Day {
  191. case "周一":
  192. num = 1
  193. case "周二":
  194. num = 2
  195. case "周三":
  196. num = 3
  197. case "周四":
  198. num = 4
  199. case "周五":
  200. num = 5
  201. case "周六":
  202. num = 6
  203. case "周日":
  204. num = 0
  205. }
  206. day = day - num
  207. default:
  208. errMsg = "错误的日期频度:" + config.Frequency
  209. err = errors.New(errMsg)
  210. return
  211. }
  212. return
  213. }