mixed_table.go 11 KB

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