index.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. package ai_predict_model
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. aiPredictModel "eta/eta_api/models/ai_predict_model"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "github.com/tealeg/xlsx"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // AiPredictModelIndexController AI预测模型标的
  19. type AiPredictModelIndexController struct {
  20. controllers.BaseAuthController
  21. }
  22. // List
  23. // @Title 标的列表
  24. // @Description 标的列表
  25. // @Param PageSize query int true "每页数据条数"
  26. // @Param CurrentIndex query int true "当前页页码,从1开始"
  27. // @Param ClassifyId query int false "分类id"
  28. // @Param IndexId query int false "模型标的ID"
  29. // @Param Keyword query string false "搜索关键词"
  30. // @Success 200 {object} data_manage.ChartListResp
  31. // @router /index/list [get]
  32. func (this *AiPredictModelIndexController) List() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. sysUser := this.SysUser
  39. if sysUser == nil {
  40. br.Msg = "请登录"
  41. br.ErrMsg = "请登录,SysUser Is Empty"
  42. br.Ret = 408
  43. return
  44. }
  45. pageSize, _ := this.GetInt("PageSize")
  46. currentIndex, _ := this.GetInt("CurrentIndex")
  47. classifyId, _ := this.GetInt("ClassifyId")
  48. indexId, _ := this.GetInt("IndexId")
  49. keyword := this.GetString("KeyWord")
  50. keyword = strings.TrimSpace(keyword)
  51. resp := new(aiPredictModel.AiPredictModelIndexPageListResp)
  52. // 分页
  53. var startSize int
  54. if pageSize <= 0 {
  55. pageSize = utils.PageSize20
  56. }
  57. if currentIndex <= 0 {
  58. currentIndex = 1
  59. }
  60. startSize = paging.StartIndex(currentIndex, pageSize)
  61. // 分类
  62. classifyIdName := make(map[int]string)
  63. {
  64. classifyOb := new(aiPredictModel.AiPredictModelClassify)
  65. list, e := classifyOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  66. if e != nil {
  67. br.Msg = "获取失败"
  68. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  69. return
  70. }
  71. for _, v := range list {
  72. classifyIdName[v.AiPredictModelClassifyId] = v.ClassifyName
  73. }
  74. }
  75. // 筛选条件
  76. indexOb := new(aiPredictModel.AiPredictModelIndex)
  77. var cond string
  78. var pars []interface{}
  79. {
  80. if indexId > 0 {
  81. cond += fmt.Sprintf(" AND %s = ?", indexOb.Cols().PrimaryId)
  82. pars = append(pars, indexId)
  83. }
  84. if classifyId > 0 {
  85. cond += fmt.Sprintf(" AND %s = ?", indexOb.Cols().ClassifyId)
  86. pars = append(pars, classifyId)
  87. }
  88. if keyword != "" {
  89. cond += fmt.Sprintf(" AND %s LIKE ?", indexOb.Cols().IndexName)
  90. pars = append(pars, fmt.Sprint("%", keyword, "%"))
  91. }
  92. }
  93. // 获取列表
  94. total, e := indexOb.GetCountByCondition(cond, pars)
  95. if e != nil {
  96. br.Msg = "获取失败"
  97. br.ErrMsg = fmt.Sprintf("获取标的总数失败, %v", e)
  98. return
  99. }
  100. list, e := indexOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, pageSize)
  101. if e != nil {
  102. br.Msg = "获取失败"
  103. br.ErrMsg = fmt.Sprintf("获取分页列表失败, %v", e)
  104. return
  105. }
  106. pageList := make([]*aiPredictModel.AiPredictModelIndexItem, 0)
  107. for _, v := range list {
  108. t := v.Format2Item()
  109. t.ClassifyName = classifyIdName[v.ClassifyId]
  110. pageList = append(pageList, t)
  111. }
  112. page := paging.GetPaging(currentIndex, pageSize, total)
  113. resp.Paging = page
  114. resp.List = pageList
  115. br.Data = resp
  116. br.Ret = 200
  117. br.Success = true
  118. br.Msg = "获取成功"
  119. }
  120. // Import
  121. // @Title 导入标的和数据
  122. // @Description 导入标的和数据
  123. // @Param IndexFile query file true "标的文件"
  124. // @Success 200 Ret=200 录入成功
  125. // @router /index/import [post]
  126. func (this *AiPredictModelIndexController) Import() {
  127. br := new(models.BaseResponse).Init()
  128. defer func() {
  129. if br.ErrMsg == "" {
  130. br.IsSendEmail = false
  131. }
  132. this.Data["json"] = br
  133. this.ServeJSON()
  134. }()
  135. sysUser := this.SysUser
  136. if sysUser == nil {
  137. br.Msg = "请登录"
  138. br.ErrMsg = "请登录,SysUser Is Empty"
  139. br.Ret = 408
  140. return
  141. }
  142. file, _, e := this.GetFile("IndexFile")
  143. if e != nil {
  144. br.Msg = "导入失败"
  145. br.ErrMsg = fmt.Sprintf("获取文件失败, %v", e)
  146. return
  147. }
  148. path := "./static/ai_predict_model_temp_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  149. defer func() {
  150. _ = file.Close()
  151. _ = os.Remove(path)
  152. }()
  153. if e = this.SaveToFile("IndexFile", path); e != nil {
  154. br.Msg = "导入失败"
  155. br.ErrMsg = fmt.Sprintf("保存文件失败, %v", e)
  156. return
  157. }
  158. xlFile, e := xlsx.OpenFile(path)
  159. if e != nil {
  160. br.Msg = "导入失败"
  161. br.ErrMsg = fmt.Sprintf("打开excel文件失败, %v", e)
  162. return
  163. }
  164. // 获取分类和用户,遍历时校验
  165. classifyNameId := make(map[string]int)
  166. adminNameId := make(map[string]int)
  167. {
  168. classifyOb := new(aiPredictModel.AiPredictModelClassify)
  169. classifyCond := fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().ParentId)
  170. classifyPars := make([]interface{}, 0)
  171. classifyPars = append(classifyPars, 0) // 只取一级分类(临时过渡方案,业务端只会加一级)
  172. classifies, e := classifyOb.GetItemsByCondition(classifyCond, classifyPars, []string{}, "")
  173. if e != nil {
  174. br.Msg = "导入失败"
  175. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  176. return
  177. }
  178. for _, v := range classifies {
  179. classifyNameId[v.ClassifyName] = v.AiPredictModelClassifyId
  180. }
  181. admins, e := system.GetSysAdminList(``, make([]interface{}, 0), []string{}, "")
  182. if e != nil {
  183. br.Msg = "导入失败"
  184. br.ErrMsg = fmt.Sprintf("获取用户失败, %v", e)
  185. return
  186. }
  187. for _, v := range admins {
  188. adminNameId[v.RealName] = v.AdminId
  189. }
  190. }
  191. // 遍历sheet页
  192. // 列表页:预测标的|分类|模型框架|创建人|预测日期|预测值|预测频度|方向准确率|绝对偏差
  193. type ImportDataColKey struct {
  194. IndexName string
  195. ColKey int
  196. DataDate time.Time
  197. }
  198. imports := make(map[string]*aiPredictModel.AiPredictModelImportData)
  199. importsData := make(map[string]map[time.Time]*aiPredictModel.AiPredictModelData)
  200. importsDailyData := make(map[string]map[time.Time]*aiPredictModel.AiPredictModelData)
  201. for sheetKey, sheet := range xlFile.Sheets {
  202. maxRow := sheet.MaxRow
  203. // 列表页
  204. if sheetKey == 0 {
  205. for i := 0; i < maxRow; i++ {
  206. // 忽略首行标题
  207. if i < 1 {
  208. continue
  209. }
  210. row := sheet.Row(i)
  211. cells := row.Cells
  212. if len(cells) < 9 {
  213. continue
  214. }
  215. // 标的名称
  216. indexName := strings.TrimSpace(cells[0].String())
  217. if indexName == "" {
  218. continue
  219. }
  220. if imports[indexName] == nil {
  221. imports[indexName] = new(aiPredictModel.AiPredictModelImportData)
  222. imports[indexName].Index = new(aiPredictModel.AiPredictModelIndex)
  223. imports[indexName].Data = make([]*aiPredictModel.AiPredictModelData, 0)
  224. }
  225. imports[indexName].Index.IndexName = indexName
  226. imports[indexName].Index.CreateTime = time.Now()
  227. imports[indexName].Index.ModifyTime = time.Now()
  228. // 分类
  229. classifyName := strings.TrimSpace(cells[1].String())
  230. if classifyNameId[classifyName] <= 0 {
  231. br.Msg = fmt.Sprintf("分类:%s不存在", classifyName)
  232. return
  233. }
  234. imports[indexName].Index.ClassifyId = classifyNameId[classifyName]
  235. // 创建人
  236. adminName := strings.TrimSpace(cells[3].String())
  237. if adminNameId[adminName] <= 0 {
  238. br.Msg = fmt.Sprintf("创建人:%s不存在", adminName)
  239. return
  240. }
  241. imports[indexName].Index.SysUserId = adminNameId[adminName]
  242. imports[indexName].Index.SysUserRealName = adminName
  243. // 其余信息
  244. imports[indexName].Index.ModelFramework = strings.TrimSpace(cells[2].String())
  245. strDate := strings.TrimSpace(cells[4].String())
  246. predictDate, _ := time.Parse("2006/01/02", strDate)
  247. if predictDate.IsZero() {
  248. predictDate, _ = time.Parse("01-02-06", strDate)
  249. if predictDate.IsZero() {
  250. predictDate, _ = time.Parse("2006/1/2", strDate)
  251. }
  252. }
  253. imports[indexName].Index.PredictDate = predictDate
  254. strVal := strings.TrimSpace(cells[5].String())
  255. if strVal == "" {
  256. continue
  257. }
  258. predictVal, _ := strconv.ParseFloat(strVal, 64)
  259. imports[indexName].Index.PredictValue = predictVal
  260. imports[indexName].Index.PredictFrequency = strings.TrimSpace(cells[6].String())
  261. imports[indexName].Index.DirectionAccuracy = strings.TrimSpace(cells[7].String())
  262. imports[indexName].Index.AbsoluteDeviation = strings.TrimSpace(cells[8].String())
  263. }
  264. }
  265. // 月度数据页
  266. if sheetKey == 1 {
  267. // 每五列为一个指标的数据
  268. colKeys := make(map[int]*ImportDataColKey) // 每一列对应的指标名称以及对应的字段序号
  269. for i := 0; i < maxRow; i++ {
  270. // 首行为指标名称
  271. if i == 0 {
  272. nameCol := 0
  273. row := sheet.Row(i)
  274. for ck, cell := range row.Cells {
  275. nameCol += 1
  276. if nameCol > 5 {
  277. nameCol = 1
  278. }
  279. if nameCol == 1 {
  280. // nameCol=1时为指标/数据行则为日期
  281. indexName := strings.TrimSpace(cell.String())
  282. if indexName == "" {
  283. continue
  284. }
  285. importsData[indexName] = make(map[time.Time]*aiPredictModel.AiPredictModelData)
  286. colKeys[ck] = &ImportDataColKey{
  287. ColKey: 1,
  288. IndexName: indexName,
  289. }
  290. // 后面四列分别对应: 实际值|预测值|方向|偏差率, 这里直接加无须考虑是否会越界
  291. colKeys[ck+1] = &ImportDataColKey{
  292. ColKey: 2,
  293. IndexName: indexName,
  294. }
  295. colKeys[ck+2] = &ImportDataColKey{
  296. ColKey: 3,
  297. IndexName: indexName,
  298. }
  299. colKeys[ck+3] = &ImportDataColKey{
  300. ColKey: 4,
  301. IndexName: indexName,
  302. }
  303. colKeys[ck+4] = &ImportDataColKey{
  304. ColKey: 5,
  305. IndexName: indexName,
  306. }
  307. continue
  308. }
  309. }
  310. continue
  311. }
  312. // 第二行为标题,跳过
  313. if i == 1 {
  314. continue
  315. }
  316. // 剩余为数据行
  317. row := sheet.Row(i)
  318. for ck, cell := range row.Cells {
  319. if colKeys[ck] == nil {
  320. continue
  321. }
  322. if colKeys[ck].IndexName == "" {
  323. continue
  324. }
  325. switch colKeys[ck].ColKey {
  326. case 1:
  327. // 日期列
  328. strDate := strings.TrimSpace(cell.String())
  329. dataDate, _ := time.Parse("2006/01/02", strDate)
  330. if dataDate.IsZero() {
  331. dataDate, _ = time.Parse("01-02-06", strDate)
  332. if dataDate.IsZero() {
  333. dataDate, _ = time.Parse("2006/1/2", strDate)
  334. if dataDate.IsZero() {
  335. continue
  336. }
  337. }
  338. }
  339. colKeys[ck].DataDate = dataDate
  340. colKeys[ck+1].DataDate = dataDate
  341. colKeys[ck+2].DataDate = dataDate
  342. colKeys[ck+3].DataDate = dataDate
  343. colKeys[ck+4].DataDate = dataDate
  344. importRow := imports[colKeys[ck].IndexName]
  345. if importRow == nil {
  346. continue
  347. }
  348. // 新增当前日期数据
  349. importsData[colKeys[ck].IndexName][dataDate] = new(aiPredictModel.AiPredictModelData)
  350. importsData[colKeys[ck].IndexName][dataDate].DataTime = dataDate
  351. importsData[colKeys[ck].IndexName][dataDate].CreateTime = time.Now()
  352. importsData[colKeys[ck].IndexName][dataDate].ModifyTime = time.Now()
  353. importsData[colKeys[ck].IndexName][dataDate].Source = aiPredictModel.ModelDataSourceMonthly
  354. case 2, 3:
  355. // 实际值和预测值, 可能为空
  356. dataDate := colKeys[ck].DataDate
  357. if importsData[colKeys[ck].IndexName][dataDate] == nil {
  358. continue
  359. }
  360. strVal := strings.TrimSpace(cell.String())
  361. if strVal == "" {
  362. continue
  363. }
  364. val, _ := strconv.ParseFloat(strVal, 64)
  365. if colKeys[ck].ColKey == 2 {
  366. importsData[colKeys[ck].IndexName][dataDate].Value.Valid = true
  367. importsData[colKeys[ck].IndexName][dataDate].Value.Float64 = val
  368. } else {
  369. importsData[colKeys[ck].IndexName][dataDate].PredictValue.Valid = true
  370. importsData[colKeys[ck].IndexName][dataDate].PredictValue.Float64 = val
  371. }
  372. case 4, 5:
  373. // 方向/偏差率
  374. dataDate := colKeys[ck].DataDate
  375. if importsData[colKeys[ck].IndexName][dataDate] == nil {
  376. continue
  377. }
  378. str := strings.TrimSpace(cell.String())
  379. if str == "" {
  380. continue
  381. }
  382. if colKeys[ck].ColKey == 4 {
  383. importsData[colKeys[ck].IndexName][dataDate].Direction = str
  384. } else {
  385. importsData[colKeys[ck].IndexName][dataDate].DeviationRate = str
  386. }
  387. default:
  388. continue
  389. }
  390. }
  391. }
  392. }
  393. // 日度数据页
  394. if sheetKey == 2 {
  395. // 每3列为一个指标的数据
  396. colKeys := make(map[int]*ImportDataColKey) // 每一列对应的指标名称以及对应的字段序号
  397. for i := 0; i < maxRow; i++ {
  398. // 首行为指标名称
  399. if i == 0 {
  400. nameCol := 0
  401. row := sheet.Row(i)
  402. for ck, cell := range row.Cells {
  403. nameCol += 1
  404. if nameCol > 3 {
  405. nameCol = 1
  406. }
  407. if nameCol == 1 {
  408. // nameCol=1时为指标/数据行则为日期
  409. indexName := strings.TrimSpace(cell.String())
  410. if indexName == "" {
  411. continue
  412. }
  413. importsDailyData[indexName] = make(map[time.Time]*aiPredictModel.AiPredictModelData)
  414. colKeys[ck] = &ImportDataColKey{
  415. ColKey: 1,
  416. IndexName: indexName,
  417. }
  418. // 后面两列分别对应: 实际值|预测值, 这里直接加无须考虑是否会越界
  419. colKeys[ck+1] = &ImportDataColKey{
  420. ColKey: 2,
  421. IndexName: indexName,
  422. }
  423. colKeys[ck+2] = &ImportDataColKey{
  424. ColKey: 3,
  425. IndexName: indexName,
  426. }
  427. continue
  428. }
  429. }
  430. continue
  431. }
  432. // 第二行为标题,遇到"预测值"单元格,需要取出其中的值作为预测图例名称
  433. if i == 1 {
  434. row := sheet.Row(i)
  435. for ck, cell := range row.Cells {
  436. if colKeys[ck] == nil {
  437. continue
  438. }
  439. if colKeys[ck].IndexName == "" {
  440. continue
  441. }
  442. if colKeys[ck].ColKey != 3 {
  443. continue
  444. }
  445. if imports[colKeys[ck].IndexName] != nil && imports[colKeys[ck].IndexName].Index != nil {
  446. var extraConfig aiPredictModel.AiPredictModelIndexExtraConfig
  447. extraConfig.DailyChart.PredictLegendName = strings.TrimSpace(cell.String())
  448. b, _ := json.Marshal(extraConfig)
  449. imports[colKeys[ck].IndexName].Index.ExtraConfig = string(b)
  450. }
  451. }
  452. continue
  453. }
  454. // 剩余为数据行
  455. row := sheet.Row(i)
  456. for ck, cell := range row.Cells {
  457. if colKeys[ck] == nil {
  458. continue
  459. }
  460. if colKeys[ck].IndexName == "" {
  461. continue
  462. }
  463. switch colKeys[ck].ColKey {
  464. case 1:
  465. // 日期列
  466. strDate := strings.TrimSpace(cell.String())
  467. dataDate, _ := time.Parse("2006/01/02", strDate)
  468. if dataDate.IsZero() {
  469. dataDate, _ = time.Parse("01-02-06", strDate)
  470. if dataDate.IsZero() {
  471. dataDate, _ = time.Parse("2006/1/2", strDate)
  472. if dataDate.IsZero() {
  473. continue
  474. }
  475. }
  476. }
  477. colKeys[ck].DataDate = dataDate
  478. colKeys[ck+1].DataDate = dataDate
  479. colKeys[ck+2].DataDate = dataDate
  480. importRow := imports[colKeys[ck].IndexName]
  481. if importRow == nil {
  482. continue
  483. }
  484. // 新增当前日期数据
  485. importsDailyData[colKeys[ck].IndexName][dataDate] = new(aiPredictModel.AiPredictModelData)
  486. importsDailyData[colKeys[ck].IndexName][dataDate].DataTime = dataDate
  487. importsDailyData[colKeys[ck].IndexName][dataDate].CreateTime = time.Now()
  488. importsDailyData[colKeys[ck].IndexName][dataDate].ModifyTime = time.Now()
  489. importsDailyData[colKeys[ck].IndexName][dataDate].Source = aiPredictModel.ModelDataSourceDaily
  490. case 2, 3:
  491. // 实际值和预测值, 可能为空
  492. dataDate := colKeys[ck].DataDate
  493. if importsDailyData[colKeys[ck].IndexName][dataDate] == nil {
  494. continue
  495. }
  496. strVal := strings.TrimSpace(cell.String())
  497. if strVal == "" {
  498. continue
  499. }
  500. val, _ := strconv.ParseFloat(strVal, 64)
  501. if colKeys[ck].ColKey == 2 {
  502. importsDailyData[colKeys[ck].IndexName][dataDate].Value.Valid = true
  503. importsDailyData[colKeys[ck].IndexName][dataDate].Value.Float64 = val
  504. } else {
  505. importsDailyData[colKeys[ck].IndexName][dataDate].PredictValue.Valid = true
  506. importsDailyData[colKeys[ck].IndexName][dataDate].PredictValue.Float64 = val
  507. }
  508. default:
  509. continue
  510. }
  511. }
  512. }
  513. }
  514. }
  515. for indexName, v := range importsData {
  516. if imports[indexName] == nil {
  517. continue
  518. }
  519. for _, dateData := range v {
  520. imports[indexName].Data = append(imports[indexName].Data, dateData)
  521. }
  522. }
  523. for indexName, v := range importsDailyData {
  524. if imports[indexName] == nil {
  525. continue
  526. }
  527. for _, dateData := range v {
  528. imports[indexName].Data = append(imports[indexName].Data, dateData)
  529. }
  530. }
  531. importIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0)
  532. for _, v := range imports {
  533. importIndexes = append(importIndexes, v)
  534. }
  535. // 导入指标
  536. if e = services.ImportAiPredictModelIndexAndData(importIndexes); e != nil {
  537. br.Msg = "操作失败"
  538. br.ErrMsg = fmt.Sprintf("导入指标数据失败, %v", e)
  539. return
  540. }
  541. br.Ret = 200
  542. br.Success = true
  543. br.Msg = "操作成功"
  544. }
  545. // Detail
  546. // @Title 标的详情
  547. // @Description 标的详情
  548. // @Param IndexId query int true "标的ID"
  549. // @Success 200 {object} data_manage.ChartListResp
  550. // @router /index/detail [get]
  551. func (this *AiPredictModelIndexController) Detail() {
  552. br := new(models.BaseResponse).Init()
  553. defer func() {
  554. if br.ErrMsg == "" {
  555. br.IsSendEmail = false
  556. }
  557. this.Data["json"] = br
  558. this.ServeJSON()
  559. }()
  560. sysUser := this.SysUser
  561. if sysUser == nil {
  562. br.Msg = "请登录"
  563. br.ErrMsg = "请登录,SysUser Is Empty"
  564. br.Ret = 408
  565. return
  566. }
  567. indexId, _ := this.GetInt("IndexId")
  568. if indexId <= 0 {
  569. br.Msg = "参数有误"
  570. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", indexId)
  571. return
  572. }
  573. resp := new(aiPredictModel.AiPredictModelDetailResp)
  574. indexOb := new(aiPredictModel.AiPredictModelIndex)
  575. indexItem, e := indexOb.GetItemById(indexId)
  576. if e != nil {
  577. if e.Error() == utils.ErrNoRow() {
  578. br.Msg = "标的已被删除,请刷新页面"
  579. return
  580. }
  581. br.Msg = "获取失败"
  582. br.ErrMsg = fmt.Sprintf("获取标的失败, %v", e)
  583. return
  584. }
  585. // 获取标的数据
  586. monthData, dailyData := make([]*aiPredictModel.AiPredictModelData, 0), make([]*aiPredictModel.AiPredictModelData, 0)
  587. {
  588. tableData := make([]*aiPredictModel.AiPredictModelDataItem, 0)
  589. dataOb := new(aiPredictModel.AiPredictModelData)
  590. dataCond := fmt.Sprintf(` AND %s = ?`, dataOb.Cols().IndexCode)
  591. dataPars := make([]interface{}, 0)
  592. dataPars = append(dataPars, indexItem.IndexCode)
  593. list, e := dataOb.GetItemsByCondition(dataCond, dataPars, []string{}, fmt.Sprintf("%s DESC", dataOb.Cols().DataTime))
  594. if e != nil {
  595. br.Msg = "获取失败"
  596. br.ErrMsg = fmt.Sprintf("获取标的数据失败, %v", e)
  597. return
  598. }
  599. // tableData取月度数据,最多显示10条
  600. count, limit := 0, 10
  601. for _, v := range list {
  602. // 日度数据
  603. if v.Source == aiPredictModel.ModelDataSourceDaily {
  604. dailyData = append(dailyData, v)
  605. continue
  606. }
  607. // 月度数据
  608. if count < limit {
  609. tableData = append(tableData, v.Format2Item())
  610. count += 1
  611. }
  612. monthData = append(monthData, v)
  613. }
  614. resp.TableData = tableData
  615. }
  616. // 月度图表
  617. if len(monthData) > 0 {
  618. chartDetail, e := services.GetAiPredictChartDetailByData(indexItem, monthData, aiPredictModel.ModelDataSourceMonthly)
  619. if e != nil {
  620. br.Msg = "获取失败"
  621. br.ErrMsg = fmt.Sprintf("获取月度图表失败, %v", e)
  622. return
  623. }
  624. resp.ChartView = chartDetail
  625. }
  626. // 日度图表
  627. if len(dailyData) > 0 {
  628. dailyChartDetail, e := services.GetAiPredictChartDetailByData(indexItem, dailyData, aiPredictModel.ModelDataSourceDaily)
  629. if e != nil {
  630. br.Msg = "获取失败"
  631. br.ErrMsg = fmt.Sprintf("获取日度图表失败, %v", e)
  632. return
  633. }
  634. resp.DailyChartView = dailyChartDetail
  635. }
  636. br.Data = resp
  637. br.Ret = 200
  638. br.Success = true
  639. br.Msg = "获取成功"
  640. }
  641. // Save
  642. // @Title 保存标的
  643. // @Description 保存标的
  644. // @Param request body aiPredictModel.AiPredictModelIndexSaveReq true "type json string"
  645. // @Success 200 Ret=200 保存成功
  646. // @router /index/save [post]
  647. func (this *AiPredictModelIndexController) Save() {
  648. br := new(models.BaseResponse).Init()
  649. defer func() {
  650. if br.ErrMsg == "" {
  651. br.IsSendEmail = false
  652. }
  653. this.Data["json"] = br
  654. this.ServeJSON()
  655. }()
  656. sysUser := this.SysUser
  657. if sysUser == nil {
  658. br.Msg = "请登录"
  659. br.ErrMsg = "请登录,SysUser Is Empty"
  660. br.Ret = 408
  661. return
  662. }
  663. var req aiPredictModel.AiPredictModelIndexSaveReq
  664. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  665. br.Msg = "参数解析异常"
  666. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  667. return
  668. }
  669. if req.IndexId < 0 {
  670. br.Msg = "参数有误"
  671. br.ErrMsg = fmt.Sprintf("标的ID有误, IndexId: %d", req.IndexId)
  672. return
  673. }
  674. indexOb := new(aiPredictModel.AiPredictModelIndex)
  675. indexItem, e := indexOb.GetItemById(req.IndexId)
  676. if e != nil {
  677. if e.Error() == utils.ErrNoRow() {
  678. br.Msg = "标的已被删除,请刷新页面"
  679. return
  680. }
  681. br.Msg = "操作失败"
  682. br.ErrMsg = fmt.Sprintf("获取标的失败, %v", e)
  683. return
  684. }
  685. var extraConfig aiPredictModel.AiPredictModelIndexExtraConfig
  686. if indexItem.ExtraConfig != "" {
  687. if e = json.Unmarshal([]byte(indexItem.ExtraConfig), &extraConfig); e != nil {
  688. br.Msg = "操作失败"
  689. br.ErrMsg = fmt.Sprintf("标的配置解析失败, %v", e)
  690. return
  691. }
  692. }
  693. if req.MonthlyChart != nil {
  694. extraConfig.MonthlyChart.LeftMin = req.MonthlyChart.LeftMin
  695. extraConfig.MonthlyChart.LeftMax = req.MonthlyChart.LeftMax
  696. extraConfig.MonthlyChart.Unit = req.MonthlyChart.Unit
  697. }
  698. if req.DailyChart != nil {
  699. extraConfig.DailyChart.LeftMin = req.DailyChart.LeftMin
  700. extraConfig.DailyChart.LeftMax = req.DailyChart.LeftMax
  701. extraConfig.DailyChart.Unit = req.DailyChart.Unit
  702. }
  703. configByte, _ := json.Marshal(extraConfig)
  704. indexItem.ExtraConfig = string(configByte)
  705. indexItem.ModifyTime = time.Now()
  706. updateCols := []string{indexOb.Cols().ExtraConfig, indexOb.Cols().ModifyTime}
  707. if e = indexItem.Update(updateCols); e != nil {
  708. br.Msg = "操作失败"
  709. br.ErrMsg = fmt.Sprintf("保存标的失败, %v", e)
  710. return
  711. }
  712. br.Ret = 200
  713. br.Msg = "操作成功"
  714. br.Success = true
  715. }
  716. // DashboardSave
  717. // @Title 保存看板
  718. // @Description 保存看板
  719. // @Param request body aiPredictModel.AiPredictModelDashboardSaveReq true "type json string"
  720. // @Success 200 Ret=200 新增成功
  721. // @router /index/dashboard/save [post]
  722. func (this *AiPredictModelIndexController) DashboardSave() {
  723. br := new(models.BaseResponse).Init()
  724. defer func() {
  725. if br.ErrMsg == "" {
  726. br.IsSendEmail = false
  727. }
  728. this.Data["json"] = br
  729. this.ServeJSON()
  730. }()
  731. sysUser := this.SysUser
  732. if sysUser == nil {
  733. br.Msg = "请登录"
  734. br.ErrMsg = "请登录,SysUser Is Empty"
  735. br.Ret = 408
  736. return
  737. }
  738. var req aiPredictModel.AiPredictModelDashboardSaveReq
  739. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  740. br.Msg = "参数解析异常"
  741. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  742. return
  743. }
  744. if req.IndexId <= 0 {
  745. br.Msg = "参数有误"
  746. br.ErrMsg = fmt.Sprintf("参数有误, %d", req.IndexId)
  747. return
  748. }
  749. req.DashboardName = strings.TrimSpace(req.DashboardName)
  750. indexOb := new(aiPredictModel.AiPredictModelIndex)
  751. _, e := indexOb.GetItemById(req.IndexId)
  752. if e != nil {
  753. if e.Error() == utils.ErrNoRow() {
  754. br.Msg = "标的已被删除,请刷新页面"
  755. return
  756. }
  757. br.Msg = "操作失败"
  758. br.ErrMsg = fmt.Sprintf("获取标的失败, %v", e)
  759. return
  760. }
  761. // 获取看板
  762. var isUpdate bool
  763. var updateCols []string
  764. dashboardItem := new(aiPredictModel.AiPredictModelDashboard)
  765. if req.IndexId > 0 {
  766. cond := fmt.Sprintf(" AND %s = ?", dashboardItem.Cols().AiPredictModelIndexId)
  767. pars := make([]interface{}, 0)
  768. pars = append(pars, req.IndexId)
  769. item, e := dashboardItem.GetItemByCondition(cond, pars, "")
  770. if e != nil && e.Error() != utils.ErrNoRow() {
  771. br.Msg = "操作失败"
  772. br.ErrMsg = fmt.Sprintf("获取标的看板失败, %v", e)
  773. return
  774. }
  775. if item != nil {
  776. isUpdate = true
  777. dashboardItem = item
  778. dashboardItem.DashboardName = req.DashboardName
  779. dashboardItem.ModifyTime = time.Now()
  780. updateCols = append(updateCols, dashboardItem.Cols().DashboardName, dashboardItem.Cols().ModifyTime)
  781. }
  782. }
  783. if !isUpdate {
  784. dashboardItem.AiPredictModelIndexId = req.IndexId
  785. dashboardItem.DashboardName = req.DashboardName
  786. dashboardItem.SysUserId = sysUser.AdminId
  787. dashboardItem.SysUserRealName = sysUser.RealName
  788. dashboardItem.CreateTime = time.Now()
  789. dashboardItem.ModifyTime = time.Now()
  790. }
  791. // 详情
  792. dashboardDetails := make([]*aiPredictModel.AiPredictModelDashboardDetail, 0)
  793. for i, v := range req.List {
  794. t := &aiPredictModel.AiPredictModelDashboardDetail{
  795. Type: v.Type,
  796. UniqueCode: v.UniqueCode,
  797. Sort: i + 1,
  798. CreateTime: time.Now(),
  799. ModifyTime: time.Now(),
  800. }
  801. dashboardDetails = append(dashboardDetails, t)
  802. }
  803. // 保存
  804. if e := dashboardItem.SaveIndexDashboard(dashboardItem, dashboardDetails, isUpdate, updateCols); e != nil {
  805. br.Msg = "操作失败"
  806. br.ErrMsg = fmt.Sprintf("保存标的看板失败, %v", e)
  807. return
  808. }
  809. br.Ret = 200
  810. br.Success = true
  811. br.Msg = "操作成功"
  812. }
  813. // DashboardDetail
  814. // @Title 看板详情
  815. // @Description 看板详情
  816. // @Param IndexId query int true "标的ID"
  817. // @Success 200 {object} aiPredictModel.AiPredictModelDashboardDetailResp
  818. // @router /index/dashboard/detail [get]
  819. func (this *AiPredictModelIndexController) DashboardDetail() {
  820. br := new(models.BaseResponse).Init()
  821. defer func() {
  822. if br.ErrMsg == "" {
  823. br.IsSendEmail = false
  824. }
  825. this.Data["json"] = br
  826. this.ServeJSON()
  827. }()
  828. sysUser := this.SysUser
  829. if sysUser == nil {
  830. br.Msg = "请登录"
  831. br.ErrMsg = "请登录,SysUser Is Empty"
  832. br.Ret = 408
  833. return
  834. }
  835. indexId, _ := this.GetInt("IndexId")
  836. resp := new(aiPredictModel.AiPredictModelDashboardDetailResp)
  837. indexOb := new(aiPredictModel.AiPredictModelIndex)
  838. indexItem, e := indexOb.GetItemById(indexId)
  839. if e != nil {
  840. if e.Error() == utils.ErrNoRow() {
  841. br.Msg = "标的已被删除,请刷新页面"
  842. return
  843. }
  844. br.Msg = "获取失败"
  845. br.ErrMsg = fmt.Sprintf("获取标的失败, %v", e)
  846. return
  847. }
  848. resp.CreateUserId = indexItem.SysUserId
  849. resp.CreateUserRealName = indexItem.SysUserRealName
  850. // 获取标的看板
  851. dashboardOb := new(aiPredictModel.AiPredictModelDashboard)
  852. dashboardItem := new(aiPredictModel.AiPredictModelDashboardItem)
  853. {
  854. cond := fmt.Sprintf(" AND %s = ?", dashboardOb.Cols().AiPredictModelIndexId)
  855. pars := make([]interface{}, 0)
  856. pars = append(pars, indexId)
  857. item, e := dashboardOb.GetItemByCondition(cond, pars, "")
  858. if e != nil && e.Error() != utils.ErrNoRow() {
  859. br.Msg = "操作失败"
  860. br.ErrMsg = fmt.Sprintf("获取标的看板失败, %v", e)
  861. return
  862. }
  863. if item != nil {
  864. dashboardItem = item.Format2Item()
  865. }
  866. }
  867. // 获取看板详情
  868. dashboardDetails := make([]*aiPredictModel.AiPredictModelDashboardDetailItem, 0)
  869. if dashboardItem.DashboardId > 0 {
  870. detailOb := new(aiPredictModel.AiPredictModelDashboardDetail)
  871. cond := fmt.Sprintf(" AND %s = ?", detailOb.Cols().AiPredictModelDashboardId)
  872. pars := make([]interface{}, 0)
  873. pars = append(pars, dashboardItem.DashboardId)
  874. list, e := detailOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", detailOb.Cols().Sort))
  875. if e != nil {
  876. br.Msg = "获取失败"
  877. br.ErrMsg = fmt.Sprintf("获取看板详情失败, %v", e)
  878. return
  879. }
  880. for _, v := range list {
  881. dashboardDetails = append(dashboardDetails, v.Format2Item())
  882. }
  883. }
  884. resp.AiPredictModelDashboardItem = dashboardItem
  885. resp.List = dashboardDetails
  886. br.Data = resp
  887. br.Ret = 200
  888. br.Success = true
  889. br.Msg = "获取成功"
  890. }