index.go 29 KB

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