index.go 27 KB

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