mixed_table.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, errMsg string) {
  15. // 单元格关系配置x信息
  16. cellRelationConfMap := make(map[string]request.CellRelationConf)
  17. cellRelationConfList := make([]request.CellRelationConf, 0)
  18. if cellRelationConf != `` {
  19. err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
  20. if err != nil {
  21. return
  22. }
  23. for _, v := range cellRelationConfList {
  24. cellRelationConfMap[v.Key] = v
  25. }
  26. }
  27. // 找出所有的关联指标id
  28. config, edbInfoIdList, _, err, errMsg := handleConfig(config)
  29. if err != nil {
  30. return
  31. }
  32. // 查询所有关联的指标信息
  33. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  34. if err != nil {
  35. return
  36. }
  37. // 指标信息map
  38. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  39. // 日度指标数据map
  40. edbDataListMap := make(map[int]map[string]float64)
  41. // 月度指标数据map
  42. edbMonthDataListMap := make(map[int]map[string]float64)
  43. for _, edbInfo := range edbInfoList {
  44. edbInfoMap[edbInfo.EdbInfoId] = edbInfo
  45. dataList := make([]*data_manage.EdbDataList, 0)
  46. switch edbInfo.EdbInfoType {
  47. case 0:
  48. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  49. case 1:
  50. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  51. default:
  52. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  53. }
  54. dateValMap := make(map[string]float64)
  55. monthValMap := make(map[string]float64)
  56. for _, data := range dataList {
  57. // 日度数据
  58. dateValMap[data.DataTime] = data.Value
  59. // 月度数据(取该月份的第一个数据)
  60. yearMonth := strings.Join(strings.Split(data.DataTime, "-")[0:2], "-")
  61. if _, ok := monthValMap[yearMonth]; !ok {
  62. monthValMap[yearMonth] = data.Value
  63. }
  64. }
  65. edbDataListMap[edbInfo.EdbInfoId] = dateValMap
  66. edbMonthDataListMap[edbInfo.EdbInfoId] = monthValMap
  67. }
  68. // 处理指定指标的日期
  69. for k, row := range config {
  70. for i, cell := range row {
  71. // 单元格是日期类型,且是日导入指标日期(指标库的最新日期)
  72. if cell.DataType == request.DateDT && cell.DataTimeType == request.EdbDateDT {
  73. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  74. cell.ShowValue = edbInfo.EndDate
  75. cell.DataTime = edbInfo.EndDate
  76. config[k][i] = cell
  77. }
  78. }
  79. row[i] = cell
  80. }
  81. config[k] = row
  82. }
  83. for k, row := range config {
  84. for i, cell := range row {
  85. switch cell.DataType {
  86. case request.EdbDT: // 指标类型
  87. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  88. cell.ShowValue = edbInfo.EdbName
  89. }
  90. case request.InsertDataDT, request.PopInsertDataDT: // 数据类型
  91. tmpDateList := strings.Split(cell.DataTime, "-")
  92. tmpDateValMap := make(map[string]float64)
  93. if len(tmpDateList) == 2 {
  94. //月度数据
  95. if dateValMap, ok := edbMonthDataListMap[cell.EdbInfoId]; ok {
  96. tmpDateValMap = dateValMap
  97. }
  98. } else {
  99. // 日度数据
  100. if dateValMap, ok := edbDataListMap[cell.EdbInfoId]; ok {
  101. tmpDateValMap = dateValMap
  102. }
  103. }
  104. if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
  105. //cell.ShowValue = fmt.Sprint(val)
  106. cell.ShowValue = utils.FormatTableDataShowValue(val)
  107. }
  108. }
  109. row[i] = cell
  110. }
  111. config[k] = row
  112. }
  113. newMixedTableCellDataList = config
  114. return
  115. }
  116. // handleConfig
  117. // @Description: 处理混合表格配置
  118. // @author: Roc
  119. // @datetime2023-10-27 13:24:53
  120. // @param configList [][]request.MixedTableCellDataReq
  121. // @return newConfig [][]request.MixedTableCellDataReq
  122. // @return edbInfoIdList []int
  123. // @return dataEdbInfoIdList []int
  124. // @return err error
  125. // @return errMsg string
  126. func handleConfig(configList [][]request.MixedTableCellDataReq) (newConfig [][]request.MixedTableCellDataReq, edbInfoIdList []int, dataEdbInfoIdList []int, err error, errMsg string) {
  127. edbInfoIdList = make([]int, 0)
  128. dataEdbInfoIdList = make([]int, 0)
  129. for ck, rowList := range configList {
  130. for rk, cell := range rowList {
  131. switch cell.DataType {
  132. case request.EdbDT: // 指标信息
  133. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  134. case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
  135. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  136. case request.DateDT: // 日期类型
  137. if cell.DataTimeType == request.EdbDateDT {
  138. var config request.EdbDateConf
  139. err = json.Unmarshal([]byte(cell.Value), &config)
  140. if err != nil {
  141. return
  142. }
  143. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  144. } else {
  145. date, tmpErr, tmpErrMsg := handleDate(cell.DataTimeType, cell.Value)
  146. if tmpErr != nil {
  147. err = tmpErr
  148. errMsg = tmpErrMsg
  149. return
  150. }
  151. rowList[rk].DataTime = date
  152. rowList[rk].ShowValue = date
  153. }
  154. }
  155. }
  156. configList[ck] = rowList
  157. }
  158. newConfig = configList
  159. return
  160. }
  161. // HandleDate
  162. // @Description: 日期处理
  163. // @author: Roc
  164. // @datetime2023-10-27 09:37:02
  165. // @param dataTimeType int
  166. // @param val string
  167. // @return date string
  168. // @return err error
  169. // @return errMsg string
  170. func HandleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  171. return handleDate(dataTimeType, val)
  172. }
  173. // handleDate
  174. // @Description: 日期处理
  175. // @author: Roc
  176. // @datetime2023-10-27 09:36:49
  177. // @param dataTimeType int
  178. // @param val string
  179. // @return date string
  180. // @return err error
  181. // @return errMsg string
  182. func handleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  183. if val == `` {
  184. errMsg = "错误的日期数据"
  185. err = errors.New(errMsg)
  186. return
  187. }
  188. switch dataTimeType {
  189. case request.CustomDateT: //手动输入日期
  190. date = val
  191. case request.SystemDateT: // 系统日期
  192. date, err, errMsg = handleSystemDateT(val)
  193. case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
  194. default:
  195. errMsg = "错误的日期类型"
  196. err = errors.New(errMsg)
  197. return
  198. }
  199. return
  200. }
  201. // handleSystemDateT
  202. // @Description: 处理导入系统日期
  203. // @author: Roc
  204. // @datetime2023-10-27 09:36:21
  205. // @param confStr string
  206. // @return date string
  207. // @return err error
  208. // @return errMsg string
  209. func handleSystemDateT(confStr string) (date string, err error, errMsg string) {
  210. var config request.SystemDateConf
  211. err = json.Unmarshal([]byte(confStr), &config)
  212. if err != nil {
  213. return
  214. }
  215. switch config.Source {
  216. case request.SystemCurrDateT:
  217. date = time.Now().Format(utils.FormatDate)
  218. case request.SystemCalculateDateT:
  219. date, err, errMsg = handleSystemCalculateDateT(config.CalculateNum, config.CalculateFrequency)
  220. case request.SystemFrequencyDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  221. date, err, errMsg = handleSystemAppointDateT(config.Day, config.Frequency)
  222. default:
  223. errMsg = "错误的日期日期导入方式"
  224. err = errors.New(fmt.Sprint("错误的日期日期导入方式:", config.Source))
  225. return
  226. }
  227. return
  228. }
  229. // handleSystemCalculateDateT
  230. // @Description: 处理系统日期计算后的日期
  231. // @author: Roc
  232. // @datetime2023-10-27 09:31:22
  233. // @param num int
  234. // @param frequency string
  235. // @return date string
  236. // @return err error
  237. // @return errMsg string
  238. func handleSystemCalculateDateT(num int, frequency string) (date string, err error, errMsg string) {
  239. if err != nil {
  240. return
  241. }
  242. currDate := time.Now()
  243. switch frequency {
  244. case "", "日":
  245. date = currDate.AddDate(0, 0, num).Format(utils.FormatDate)
  246. default:
  247. errMsg = "错误的日期频度:" + frequency
  248. err = errors.New(errMsg)
  249. return
  250. }
  251. return
  252. }
  253. // handleSystemAppointDateT
  254. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  255. // @author: Roc
  256. // @datetime2023-10-27 09:31:35
  257. // @param Frequency string
  258. // @param Day string
  259. // @return date string
  260. // @return err error
  261. // @return errMsg string
  262. func handleSystemAppointDateT(appointDay, frequency string) (date string, err error, errMsg string) {
  263. currDate := time.Now()
  264. switch frequency {
  265. case "本周":
  266. day := int(currDate.Weekday())
  267. if day == 0 { // 周日
  268. day = 7
  269. }
  270. num := 0
  271. switch appointDay {
  272. case "周一":
  273. num = 1
  274. case "周二":
  275. num = 2
  276. case "周三":
  277. num = 3
  278. case "周四":
  279. num = 4
  280. case "周五":
  281. num = 5
  282. case "周六":
  283. num = 6
  284. case "周日":
  285. num = 7
  286. }
  287. day = num - day
  288. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  289. case "本旬":
  290. day := currDate.Day()
  291. var tmpDate time.Time
  292. switch appointDay {
  293. case "第一天":
  294. if day <= 10 {
  295. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  296. } else if day <= 20 {
  297. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  298. } else {
  299. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  300. }
  301. case "最后一天":
  302. if day <= 10 {
  303. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  304. } else if day <= 20 {
  305. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  306. } else {
  307. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  308. }
  309. }
  310. date = tmpDate.Format(utils.FormatDate)
  311. case "本月":
  312. var tmpDate time.Time
  313. switch appointDay {
  314. case "第一天":
  315. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  316. case "最后一天":
  317. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  318. }
  319. date = tmpDate.Format(utils.FormatDate)
  320. case "本季":
  321. month := currDate.Month()
  322. var tmpDate time.Time
  323. switch appointDay {
  324. case "第一天":
  325. if month <= 3 {
  326. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  327. } else if month <= 6 {
  328. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  329. } else if month <= 9 {
  330. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  331. } else {
  332. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  333. }
  334. case "最后一天":
  335. if month <= 3 {
  336. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  337. } else if month <= 6 {
  338. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  339. } else if month <= 9 {
  340. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  341. } else {
  342. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  343. }
  344. }
  345. date = tmpDate.Format(utils.FormatDate)
  346. default:
  347. errMsg = "错误的日期频度:" + frequency
  348. err = errors.New(errMsg)
  349. return
  350. }
  351. return
  352. }