mixed_table.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.InsertEdbCalculateDataDT: // 插入指标计算公式生成的值
  137. var config request.CalculateConf
  138. err = json.Unmarshal([]byte(cell.Value), &config)
  139. if err != nil {
  140. return
  141. }
  142. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  143. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  144. case request.DateDT: // 日期类型
  145. if cell.DataTimeType == request.EdbDateDT {
  146. var config request.EdbDateConf
  147. err = json.Unmarshal([]byte(cell.Value), &config)
  148. if err != nil {
  149. return
  150. }
  151. edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
  152. } else {
  153. date, tmpErr, tmpErrMsg := handleDate(cell.DataTimeType, cell.Value)
  154. if tmpErr != nil {
  155. err = tmpErr
  156. errMsg = tmpErrMsg
  157. return
  158. }
  159. rowList[rk].DataTime = date
  160. rowList[rk].ShowValue = date
  161. }
  162. }
  163. }
  164. configList[ck] = rowList
  165. }
  166. newConfig = configList
  167. return
  168. }
  169. // HandleDate
  170. // @Description: 日期处理
  171. // @author: Roc
  172. // @datetime2023-10-27 09:37:02
  173. // @param dataTimeType int
  174. // @param val string
  175. // @return date string
  176. // @return err error
  177. // @return errMsg string
  178. func HandleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  179. return handleDate(dataTimeType, val)
  180. }
  181. // handleDate
  182. // @Description: 日期处理
  183. // @author: Roc
  184. // @datetime2023-10-27 09:36:49
  185. // @param dataTimeType int
  186. // @param val string
  187. // @return date string
  188. // @return err error
  189. // @return errMsg string
  190. func handleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
  191. if val == `` {
  192. errMsg = "错误的日期数据"
  193. err = errors.New(errMsg)
  194. return
  195. }
  196. switch dataTimeType {
  197. case request.CustomDateT: //手动输入日期
  198. date = val
  199. case request.SystemDateT: // 系统日期
  200. date, err, errMsg = handleSystemDateT(val)
  201. case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
  202. default:
  203. errMsg = "错误的日期类型"
  204. err = errors.New(errMsg)
  205. return
  206. }
  207. return
  208. }
  209. // handleSystemDateT
  210. // @Description: 处理导入系统日期
  211. // @author: Roc
  212. // @datetime2023-10-27 09:36:21
  213. // @param confStr string
  214. // @return date string
  215. // @return err error
  216. // @return errMsg string
  217. func handleSystemDateT(confStr string) (date string, err error, errMsg string) {
  218. var config request.SystemDateConf
  219. err = json.Unmarshal([]byte(confStr), &config)
  220. if err != nil {
  221. return
  222. }
  223. switch config.Source {
  224. case request.SystemCurrDateT:
  225. date = time.Now().Format(utils.FormatDate)
  226. case request.SystemCalculateDateT:
  227. date, err, errMsg = handleSystemCalculateDateT(config.CalculateNum, config.CalculateFrequency)
  228. case request.SystemFrequencyDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  229. date, err, errMsg = handleSystemAppointDateT(config.Day, config.Frequency)
  230. default:
  231. errMsg = "错误的日期日期导入方式"
  232. err = errors.New(fmt.Sprint("错误的日期日期导入方式:", config.Source))
  233. return
  234. }
  235. return
  236. }
  237. // handleSystemCalculateDateT
  238. // @Description: 处理系统日期计算后的日期
  239. // @author: Roc
  240. // @datetime2023-10-27 09:31:22
  241. // @param num int
  242. // @param frequency string
  243. // @return date string
  244. // @return err error
  245. // @return errMsg string
  246. func handleSystemCalculateDateT(num int, frequency string) (date string, err error, errMsg string) {
  247. if err != nil {
  248. return
  249. }
  250. currDate := time.Now()
  251. switch frequency {
  252. case "", "日":
  253. date = currDate.AddDate(0, 0, num).Format(utils.FormatDate)
  254. default:
  255. errMsg = "错误的日期频度:" + frequency
  256. err = errors.New(errMsg)
  257. return
  258. }
  259. return
  260. }
  261. // handleSystemAppointDateT
  262. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  263. // @author: Roc
  264. // @datetime2023-10-27 09:31:35
  265. // @param Frequency string
  266. // @param Day string
  267. // @return date string
  268. // @return err error
  269. // @return errMsg string
  270. func handleSystemAppointDateT(appointDay, frequency string) (date string, err error, errMsg string) {
  271. currDate := time.Now()
  272. switch frequency {
  273. case "本周":
  274. day := int(currDate.Weekday())
  275. if day == 0 { // 周日
  276. day = 7
  277. }
  278. num := 0
  279. switch appointDay {
  280. case "周一":
  281. num = 1
  282. case "周二":
  283. num = 2
  284. case "周三":
  285. num = 3
  286. case "周四":
  287. num = 4
  288. case "周五":
  289. num = 5
  290. case "周六":
  291. num = 6
  292. case "周日":
  293. num = 7
  294. }
  295. day = num - day
  296. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  297. case "本旬":
  298. day := currDate.Day()
  299. var tmpDate time.Time
  300. switch appointDay {
  301. case "第一天":
  302. if day <= 10 {
  303. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  304. } else if day <= 20 {
  305. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  306. } else {
  307. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  308. }
  309. case "最后一天":
  310. if day <= 10 {
  311. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  312. } else if day <= 20 {
  313. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  314. } else {
  315. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  316. }
  317. }
  318. date = tmpDate.Format(utils.FormatDate)
  319. case "本月":
  320. var tmpDate time.Time
  321. switch appointDay {
  322. case "第一天":
  323. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  324. case "最后一天":
  325. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  326. }
  327. date = tmpDate.Format(utils.FormatDate)
  328. case "本季":
  329. month := currDate.Month()
  330. var tmpDate time.Time
  331. switch appointDay {
  332. case "第一天":
  333. if month <= 3 {
  334. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  335. } else if month <= 6 {
  336. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  337. } else if month <= 9 {
  338. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  339. } else {
  340. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  341. }
  342. case "最后一天":
  343. if month <= 3 {
  344. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  345. } else if month <= 6 {
  346. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  347. } else if month <= 9 {
  348. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  349. } else {
  350. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  351. }
  352. }
  353. date = tmpDate.Format(utils.FormatDate)
  354. default:
  355. errMsg = "错误的日期频度:" + frequency
  356. err = errors.New(errMsg)
  357. return
  358. }
  359. return
  360. }