mixed_table.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/excel/request"
  7. "eta/eta_api/services/data"
  8. excel2 "eta/eta_api/services/data/excel"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // GetSystemDate
  16. // @Title 获取系统日期(包含计算日期)
  17. // @Description 获取系统日期(包含计算日期)
  18. // @Param request body request.MixedTableCellDataReq true "type json string"
  19. // @router /excel_info/get_system_date [post]
  20. func (c *ExcelInfoController) GetSystemDate() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. c.Data["json"] = br
  24. c.ServeJSON()
  25. }()
  26. sysUser := c.SysUser
  27. if sysUser == nil {
  28. br.Msg = "请登录"
  29. br.ErrMsg = "请登录,SysUser Is Empty"
  30. br.Ret = 408
  31. return
  32. }
  33. var req request.MixedTableCellDataReq
  34. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  35. if err != nil {
  36. br.Msg = "参数解析异常!"
  37. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  38. return
  39. }
  40. var config request.EdbDateConf
  41. err = json.Unmarshal([]byte(req.Value), &config)
  42. if err != nil {
  43. br.Msg = "参数解析异常!"
  44. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  45. return
  46. }
  47. date, _, err, errMsg := excel2.HandleDate(req.DataTimeType, req.Value)
  48. if err != nil {
  49. br.Msg = "获取系统日期失败"
  50. if errMsg != `` {
  51. br.Msg = errMsg
  52. }
  53. br.ErrMsg = "获取系统日期失败,Err:" + err.Error()
  54. return
  55. }
  56. if req.DataTimeType == request.EdbDateDT {
  57. edbInfo, err := data_manage.GetEdbInfoById(config.EdbInfoId)
  58. if err != nil {
  59. br.Msg = "获取指标信息失败!"
  60. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  61. return
  62. }
  63. dataList := make([]*data_manage.EdbDataList, 0)
  64. switch edbInfo.EdbInfoType {
  65. case 0:
  66. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  67. case 1:
  68. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  69. default:
  70. br.Msg = "指标类型异常!"
  71. br.ErrMsg = "指标类型异常,Err:" + strconv.Itoa(edbInfo.EdbInfoType)
  72. return
  73. }
  74. if req.DataTime == `` { //选择前移几期数
  75. date, err = excel2.GetEdbDateByMoveForward(req.Value, dataList)
  76. if err != nil {
  77. br.Msg = "查询指标前移日期失败!"
  78. br.ErrMsg = "查询指标前移日期失败,Err:" + err.Error()
  79. return
  80. }
  81. } else {
  82. date = req.DataTime //选择表格中的日期
  83. }
  84. // 开始做日期变换
  85. date, err = excel2.HandleMixTableDateChange(date, req.Value)
  86. if err != nil {
  87. br.Msg = "日期变换失败!"
  88. br.ErrMsg = "日期变换失败,Err:" + err.Error()
  89. return
  90. }
  91. }
  92. type resp struct {
  93. Date string
  94. }
  95. br.Ret = 200
  96. br.Success = true
  97. br.Msg = "获取系统日期成功"
  98. br.Data = resp{
  99. Date: date,
  100. }
  101. }
  102. // CalculateData
  103. // @Title 公式计算(混合表格)
  104. // @Description 公式计算(混合表格)
  105. // @Param request body request.CalculateConf true "type json string"
  106. // @router /excel_info/mixed/calculate [post]
  107. func (c *ExcelInfoController) CalculateData() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. c.Data["json"] = br
  111. c.ServeJSON()
  112. }()
  113. sysUser := c.SysUser
  114. if sysUser == nil {
  115. br.Msg = "请登录"
  116. br.ErrMsg = "请登录,SysUser Is Empty"
  117. br.Ret = 408
  118. return
  119. }
  120. requestBody := string(c.Ctx.Input.RequestBody)
  121. var req request.CalculateConf
  122. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  123. if err != nil {
  124. br.Msg = "参数解析异常!"
  125. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  126. return
  127. }
  128. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  129. if err != nil {
  130. br.Msg = "获取指标信息失败!"
  131. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  132. return
  133. }
  134. dataList := make([]*data_manage.EdbDataList, 0)
  135. switch edbInfo.EdbInfoType {
  136. case 0:
  137. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  138. case 1:
  139. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  140. default:
  141. br.Msg = "指标类型异常!"
  142. br.ErrMsg = "指标类型异常,Err:" + strconv.Itoa(edbInfo.EdbInfoType)
  143. return
  144. }
  145. //获取所有数据,计算所有数据
  146. //获取部分数据,计算部分数据
  147. // BaseCalculate 数据计算的结构体
  148. type BaseCalculate struct {
  149. DataList []*data_manage.EdbDataList
  150. Frequency string `description:"需要转换的频度"`
  151. Formula interface{}
  152. Calendar string `description:"公历/农历"`
  153. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  154. MoveFrequency string `description:"移动频度"`
  155. FromFrequency string `description:"来源的频度"`
  156. Source int `description:"1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值"`
  157. }
  158. req2 := &BaseCalculate{
  159. DataList: dataList,
  160. Frequency: req.Frequency,
  161. Formula: req.Formula,
  162. Calendar: req.Calendar,
  163. MoveType: req.MoveType,
  164. MoveFrequency: req.MoveFrequency,
  165. FromFrequency: edbInfo.Frequency,
  166. Source: req.Source,
  167. }
  168. // 调用指标库去更新
  169. reqJson, tmpErr := json.Marshal(req2)
  170. if tmpErr != nil {
  171. err = tmpErr
  172. return
  173. }
  174. respItem, tmpErr := data.BaseCalculate(string(reqJson))
  175. if tmpErr != nil {
  176. br.Msg = "计算指标失败"
  177. br.ErrMsg = tmpErr.Error()
  178. return
  179. }
  180. if respItem.Ret != 200 {
  181. br.Msg = respItem.Msg
  182. br.ErrMsg = respItem.ErrMsg
  183. return
  184. }
  185. // 数据处理成需要返回的样式
  186. num := 5
  187. lenDate := len(respItem.Data.DateList)
  188. if lenDate < 5 {
  189. num = lenDate
  190. }
  191. dataListResp := make([]*data_manage.EdbDataList, 0)
  192. var newDate string
  193. if req.DataTime == `` { //选择前移几期数
  194. newDate, err = excel2.GetEdbDateByMoveForwardByDateList(requestBody, respItem.Data.DateList)
  195. if err != nil {
  196. return
  197. }
  198. } else {
  199. newDate = req.DataTime //选择表格中的日期
  200. if strings.Count(newDate, "-") == 1 {
  201. newDate = newDate + "-01"
  202. // 查找这个月早的时间作为起始时间
  203. for _, v := range respItem.Data.DateList {
  204. if v >= newDate {
  205. newDate = v
  206. }
  207. }
  208. }
  209. }
  210. // 开始做日期变换
  211. newDate, err = excel2.HandleMixTableDateChange(newDate, requestBody)
  212. if err != nil {
  213. return
  214. }
  215. var finalVal float64
  216. if req.DataTime == `` {
  217. for i := 1; i <= num; i++ {
  218. date := respItem.Data.DateList[lenDate-i]
  219. val, ok := respItem.Data.DataMap[date]
  220. if !ok {
  221. continue
  222. }
  223. if date == newDate {
  224. finalVal = val
  225. }
  226. dataListResp = append(dataListResp, &data_manage.EdbDataList{
  227. Value: val,
  228. DataTime: date,
  229. })
  230. }
  231. } else {
  232. // todo 如果选择了表格中的日期应该如何处理
  233. if val, ok := respItem.Data.DataMap[newDate]; ok {
  234. finalVal = val
  235. for i, tmpDate := range respItem.Data.DateList {
  236. if tmpDate == newDate {
  237. if i+3 <= lenDate {
  238. t1Date := respItem.Data.DateList[i+2]
  239. if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
  240. // 当前日期
  241. dataListResp = append(dataListResp, &data_manage.EdbDataList{
  242. Value: tmpVal,
  243. DataTime: t1Date,
  244. })
  245. }
  246. }
  247. if i+2 <= lenDate {
  248. t1Date := respItem.Data.DateList[i+1]
  249. if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
  250. // 当前日期
  251. dataListResp = append(dataListResp, &data_manage.EdbDataList{
  252. Value: tmpVal,
  253. DataTime: t1Date,
  254. })
  255. }
  256. }
  257. // 当前日期
  258. dataListResp = append(dataListResp, &data_manage.EdbDataList{
  259. Value: val,
  260. DataTime: newDate,
  261. })
  262. if i >= 1 {
  263. t1Date := respItem.Data.DateList[i-1]
  264. if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
  265. // 当前日期
  266. dataListResp = append(dataListResp, &data_manage.EdbDataList{
  267. Value: tmpVal,
  268. DataTime: t1Date,
  269. })
  270. }
  271. }
  272. if i >= 2 {
  273. t1Date := respItem.Data.DateList[i-2]
  274. if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
  275. // 当前日期
  276. dataListResp = append(dataListResp, &data_manage.EdbDataList{
  277. Value: tmpVal,
  278. DataTime: t1Date,
  279. })
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. resp := data_manage.BeforeAndAfterDateDataResp{
  287. List: dataListResp,
  288. Date: newDate,
  289. //ShowValue: utils.FormatTableDataShowValue(finalVal),
  290. ShowValue: finalVal,
  291. }
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "计算成功"
  295. br.Data = resp
  296. }
  297. // GetMixDateCalculate
  298. // @Title 获取混合表格日期计算
  299. // @Description 获取混合表格日期计算
  300. // @Param request body request.MixedTableCellDataReq true "type json string"
  301. // @router /excel_info/mixed/date_calculate [post]
  302. func (c *ExcelInfoController) GetMixDateCalculate() {
  303. br := new(models.BaseResponse).Init()
  304. defer func() {
  305. c.Data["json"] = br
  306. c.ServeJSON()
  307. }()
  308. sysUser := c.SysUser
  309. if sysUser == nil {
  310. br.Msg = "请登录"
  311. br.ErrMsg = "请登录,SysUser Is Empty"
  312. br.Ret = 408
  313. return
  314. }
  315. var req request.MixedDateCalculateReq
  316. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  317. if err != nil {
  318. br.Msg = "参数解析异常!"
  319. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  320. return
  321. }
  322. valMap := make(map[string]int)
  323. for _, v := range req.DateList {
  324. // 查找单元格数据
  325. // 如果不是基础计算单元格,直接返回
  326. _, err = time.ParseInLocation(utils.FormatDate, v.Date, time.Local)
  327. if err != nil {
  328. br.Msg = "日期计算失败!"
  329. br.ErrMsg = fmt.Sprintf("%s 的单元格非日期类型, Err: %s", v.Date, err.Error())
  330. return
  331. }
  332. // todo 把日期转换成excel里的天数
  333. realDiffDay := utils.GetDaysDiff1900(v.Date)
  334. valMap[strings.ToUpper(v.Tag)] = realDiffDay
  335. }
  336. // 计算
  337. val, errMsg, err := excel2.DateCalculateFormula(valMap, strings.ToUpper(req.Formula))
  338. if err != nil {
  339. br.Msg = "日期计算失败"
  340. br.ErrMsg = errMsg
  341. return
  342. }
  343. showValue := utils.FormatTableDataShowValue(val)
  344. type resp struct {
  345. ShowValue string
  346. }
  347. br.Ret = 200
  348. br.Success = true
  349. br.Msg = "操作成功"
  350. br.Data = resp{
  351. ShowValue: showValue,
  352. }
  353. }
  354. // GetBaseEdbInfo
  355. // @Title 获取指标的基本信息
  356. // @Description 获取指标的基本信息
  357. // @Param EdbInfoIds string "指标ID用英文逗号分割"
  358. // @router /excel_info/base_edb_info [get]
  359. func (c *ExcelInfoController) GetBaseEdbInfo() {
  360. br := new(models.BaseResponse).Init()
  361. defer func() {
  362. c.Data["json"] = br
  363. c.ServeJSON()
  364. }()
  365. sysUser := c.SysUser
  366. if sysUser == nil {
  367. br.Msg = "请登录"
  368. br.ErrMsg = "请登录,SysUser Is Empty"
  369. br.Ret = 408
  370. return
  371. }
  372. edbInfoIds := c.GetString("EdbInfoIds")
  373. if edbInfoIds == "" {
  374. br.Msg = "请输入指标ID"
  375. return
  376. }
  377. ids := strings.Split(edbInfoIds, ",")
  378. edbIds := make([]int, 0)
  379. for _, v := range ids {
  380. id, err := strconv.Atoi(v)
  381. if err != nil {
  382. br.Msg = "指标ID格式错误"
  383. return
  384. }
  385. edbIds = append(edbIds, id)
  386. }
  387. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbIds)
  388. if err != nil {
  389. br.Msg = "获取指标信息失败!"
  390. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  391. return
  392. }
  393. list := make([]*data_manage.BaseEdbNameItem, 0)
  394. for _, v := range edbInfoList {
  395. tmp := new(data_manage.BaseEdbNameItem)
  396. tmp.EdbInfoId = v.EdbInfoId
  397. tmp.EdbInfoType = v.EdbInfoType
  398. tmp.EdbCode = v.EdbCode
  399. tmp.EdbName = v.EdbName
  400. tmp.Source = v.Source
  401. tmp.SourceName = v.SourceName
  402. tmp.Frequency = v.Frequency
  403. tmp.Unit = v.Unit
  404. list = append(list, tmp)
  405. }
  406. resp := data_manage.BaseEdbInfoResp{
  407. List: list,
  408. }
  409. br.Ret = 200
  410. br.Success = true
  411. br.Msg = "查询成功"
  412. br.Data = resp
  413. }