mixed_table.go 13 KB

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