classify.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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/data_manage"
  8. dataSourceModel "eta/eta_api/models/data_source"
  9. "eta/eta_api/services"
  10. "eta/eta_api/services/data"
  11. "eta/eta_api/services/elastic"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // AiPredictModelClassifyController AI预测模型-分类
  20. type AiPredictModelClassifyController struct {
  21. controllers.BaseAuthController
  22. }
  23. // List
  24. // @Title 分类列表
  25. // @Description 分类列表
  26. // @Param ParentId query bool false "父级ID"
  27. // @Success 200 {object} data_manage.ChartClassifyListResp
  28. // @router /classify/list [get]
  29. func (this *AiPredictModelClassifyController) List() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. if br.ErrMsg == "" {
  33. br.IsSendEmail = false
  34. }
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. parentId, _ := this.GetInt("ParentId")
  39. resp := new(aiPredictModel.AiPredictModelClassifyListResp)
  40. // (懒加载)仅查询直属分类
  41. classifyOb := new(aiPredictModel.AiPredictModelClassify)
  42. {
  43. cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ParentId)
  44. pars := make([]interface{}, 0)
  45. pars = append(pars, parentId)
  46. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", classifyOb.Cols().Sort))
  47. if e != nil {
  48. br.Msg = "获取失败"
  49. br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e)
  50. return
  51. }
  52. for _, v := range list {
  53. resp.AllNodes = append(resp.AllNodes, &aiPredictModel.AiPredictModelClassifyListItem{
  54. NodeName: v.ClassifyName,
  55. ClassifyId: v.AiPredictModelClassifyId,
  56. ClassifyName: v.ClassifyName,
  57. ParentId: v.ParentId,
  58. Level: v.Level,
  59. Sort: v.Sort,
  60. UniqueCode: v.UniqueCode,
  61. })
  62. }
  63. }
  64. // 非顶级目录查询指标
  65. indexOb := new(aiPredictModel.AiPredictModelIndex)
  66. if parentId > 0 {
  67. parentClassify, e := classifyOb.GetItemById(parentId)
  68. if e != nil {
  69. br.Msg = "父级分类不存在, 请刷新页面"
  70. return
  71. }
  72. cond := fmt.Sprintf(" AND %s = ?", indexOb.Cols().ClassifyId)
  73. pars := make([]interface{}, 0)
  74. pars = append(pars, parentId)
  75. list, e := indexOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", indexOb.Cols().Sort))
  76. if e != nil {
  77. br.Msg = "获取失败"
  78. br.ErrMsg = fmt.Sprintf("获取分类下指标失败, %v", e)
  79. return
  80. }
  81. for _, v := range list {
  82. resp.AllNodes = append(resp.AllNodes, &aiPredictModel.AiPredictModelClassifyListItem{
  83. NodeType: 1,
  84. NodeName: v.IndexName,
  85. ClassifyId: parentClassify.AiPredictModelClassifyId,
  86. ClassifyName: parentClassify.ClassifyName,
  87. IndexId: v.AiPredictModelIndexId,
  88. IndexCode: v.IndexCode,
  89. IndexName: v.IndexName,
  90. ParentId: parentId,
  91. Sort: v.Sort,
  92. UniqueCode: v.IndexCode,
  93. })
  94. }
  95. }
  96. sort.Slice(resp.AllNodes, func(i, j int) bool {
  97. return resp.AllNodes[i].Sort < resp.AllNodes[j].Sort
  98. })
  99. br.Data = resp
  100. br.Ret = 200
  101. br.Success = true
  102. br.Msg = "获取成功"
  103. }
  104. // Add
  105. // @Title 新增分类
  106. // @Description 新增分类
  107. // @Param request body aiPredictModel.AiPredictModelClassifyAddReq true "type json string"
  108. // @Success 200 Ret=200 保存成功
  109. // @router /classify/add [post]
  110. func (this *AiPredictModelClassifyController) Add() {
  111. br := new(models.BaseResponse).Init()
  112. defer func() {
  113. if br.ErrMsg == "" {
  114. br.IsSendEmail = false
  115. }
  116. this.Data["json"] = br
  117. this.ServeJSON()
  118. }()
  119. sysUser := this.SysUser
  120. if sysUser == nil {
  121. br.Msg = "请登录"
  122. br.ErrMsg = "请登录,SysUser Is Empty"
  123. br.Ret = 408
  124. return
  125. }
  126. var req aiPredictModel.AiPredictModelClassifyAddReq
  127. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  128. br.Msg = "参数解析异常"
  129. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  130. return
  131. }
  132. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  133. if req.ClassifyName == "" {
  134. br.Msg = "请输入分类名称"
  135. return
  136. }
  137. if req.ParentId < 0 {
  138. br.Msg = "请选择上级分类"
  139. return
  140. }
  141. if req.Level > 5 {
  142. br.Msg = "目前只支持6级目录"
  143. return
  144. }
  145. // 校验分类名称
  146. classifyOb := new(aiPredictModel.AiPredictModelClassify)
  147. {
  148. cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ParentId)
  149. if this.Lang == utils.EnLangVersion {
  150. cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyNameEn)
  151. } else {
  152. cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyName)
  153. }
  154. pars := make([]interface{}, 0)
  155. pars = append(pars, req.ParentId, req.ClassifyName)
  156. count, e := classifyOb.GetCountByCondition(cond, pars)
  157. if e != nil {
  158. br.Msg = "操作失败"
  159. br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
  160. return
  161. }
  162. if count > 0 {
  163. br.Msg = "分类名称已存在"
  164. return
  165. }
  166. }
  167. // 层级路径
  168. var levelPath string
  169. var rootId int
  170. if req.ParentId > 0 {
  171. parent, e := classifyOb.GetItemById(req.ParentId)
  172. if e != nil {
  173. br.Msg = "上级分类有误"
  174. br.ErrMsg = fmt.Sprintf("获取上级分类失败, %v", e)
  175. return
  176. }
  177. levelPath = parent.LevelPath
  178. rootId = parent.RootId
  179. }
  180. sortMax, e := classifyOb.GetSortMax(req.ParentId)
  181. if e != nil {
  182. br.Msg = "操作失败"
  183. br.ErrMsg = fmt.Sprintf("获取分类最大排序失败, %v", e)
  184. return
  185. }
  186. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  187. classifyOb.ParentId = req.ParentId
  188. classifyOb.ClassifyName = req.ClassifyName
  189. classifyOb.ClassifyNameEn = req.ClassifyName
  190. classifyOb.Level = req.Level + 1
  191. classifyOb.Sort = sortMax + 1
  192. classifyOb.SysUserId = sysUser.AdminId
  193. classifyOb.SysUserRealName = sysUser.RealName
  194. classifyOb.UniqueCode = utils.MD5(classifyOb.TableName() + "_" + timestamp)
  195. classifyOb.CreateTime = time.Now().Local()
  196. classifyOb.ModifyTime = time.Now().Local()
  197. if e = classifyOb.Create(); e != nil {
  198. br.Msg = "操作失败"
  199. br.ErrMsg = fmt.Sprintf("新增分类失败, %v", e)
  200. return
  201. }
  202. if req.ParentId > 0 {
  203. // 用英文逗号拼接方便查询
  204. classifyOb.LevelPath = fmt.Sprintf("%s,%d", levelPath, classifyOb.AiPredictModelClassifyId)
  205. classifyOb.RootId = rootId
  206. } else {
  207. classifyOb.LevelPath = fmt.Sprint(classifyOb.AiPredictModelClassifyId)
  208. classifyOb.RootId = classifyOb.AiPredictModelClassifyId
  209. }
  210. if e = classifyOb.Update([]string{classifyOb.Cols().LevelPath, classifyOb.Cols().RootId}); e != nil {
  211. br.Msg = "操作失败"
  212. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  213. return
  214. }
  215. br.Ret = 200
  216. br.Msg = "操作成功"
  217. br.Success = true
  218. }
  219. // Edit
  220. // @Title 修改分类
  221. // @Description 修改分类
  222. // @Param request body aiPredictModel.AiPredictModelClassifyEditReq true "type json string"
  223. // @Success 200 Ret=200 修改成功
  224. // @router /classify/edit [post]
  225. func (this *AiPredictModelClassifyController) Edit() {
  226. br := new(models.BaseResponse).Init()
  227. defer func() {
  228. if br.ErrMsg == "" {
  229. br.IsSendEmail = false
  230. }
  231. this.Data["json"] = br
  232. this.ServeJSON()
  233. }()
  234. sysUser := this.SysUser
  235. if sysUser == nil {
  236. br.Msg = "请登录"
  237. br.ErrMsg = "请登录,SysUser Is Empty"
  238. br.Ret = 408
  239. return
  240. }
  241. var req aiPredictModel.AiPredictModelClassifyEditReq
  242. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  243. br.Msg = "参数解析异常"
  244. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  245. return
  246. }
  247. if req.ClassifyId < 0 {
  248. br.Msg = "请选择分类"
  249. return
  250. }
  251. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  252. if req.ClassifyName == "" {
  253. br.Msg = "请输入分类名称"
  254. return
  255. }
  256. classifyOb := new(aiPredictModel.AiPredictModelClassify)
  257. classifyItem, e := classifyOb.GetItemById(req.ClassifyId)
  258. if e != nil {
  259. if utils.IsErrNoRow(e) {
  260. br.Msg = "分类不存在, 请刷新页面"
  261. return
  262. }
  263. br.Msg = "操作失败"
  264. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  265. return
  266. }
  267. // 校验分类名称
  268. {
  269. cond := fmt.Sprintf(" AND %s <> ?", classifyOb.Cols().PrimaryId)
  270. if this.Lang == utils.EnLangVersion {
  271. cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyNameEn)
  272. } else {
  273. cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyName)
  274. }
  275. pars := make([]interface{}, 0)
  276. pars = append(pars, req.ClassifyId, req.ClassifyName)
  277. count, e := classifyOb.GetCountByCondition(cond, pars)
  278. if e != nil {
  279. br.Msg = "操作失败"
  280. br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
  281. return
  282. }
  283. if count > 0 {
  284. br.Msg = "分类名称已存在"
  285. return
  286. }
  287. }
  288. classifyItem.ClassifyName = req.ClassifyName
  289. classifyItem.ClassifyNameEn = req.ClassifyName
  290. classifyItem.ModifyTime = time.Now().Local()
  291. updateCols := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().ModifyTime}
  292. if e = classifyItem.Update(updateCols); e != nil {
  293. br.Msg = "操作失败"
  294. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  295. return
  296. }
  297. br.Ret = 200
  298. br.Msg = "操作成功"
  299. br.Success = true
  300. }
  301. // RemoveCheck
  302. // @Title 删除校验
  303. // @Description 删除校验
  304. // @Param request body aiPredictModel.AiPredictModelClassifyRemoveReq true "type json string"
  305. // @Success 200 Ret=200 检测成功
  306. // @router /classify/remove_check [post]
  307. func (this *AiPredictModelClassifyController) RemoveCheck() {
  308. br := new(models.BaseResponse).Init()
  309. defer func() {
  310. if br.ErrMsg == "" {
  311. br.IsSendEmail = false
  312. }
  313. this.Data["json"] = br
  314. this.ServeJSON()
  315. }()
  316. var req aiPredictModel.AiPredictModelClassifyRemoveReq
  317. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  318. br.Msg = "参数解析异常"
  319. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  320. return
  321. }
  322. if req.ClassifyId < 0 && req.IndexId <= 0 {
  323. br.Msg = "参数错误"
  324. return
  325. }
  326. var deleteStatus int
  327. var tipsMsg string
  328. // 删除分类
  329. if req.ClassifyId > 0 && req.IndexId == 0 {
  330. count, err := aiPredictModel.GetAiPredictModelIndexCountByClassifyId(req.ClassifyId)
  331. if err != nil {
  332. br.Msg = "删除失败"
  333. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  334. return
  335. }
  336. if count > 0 {
  337. deleteStatus = 1
  338. tipsMsg = "该分类下关联图表不可删除"
  339. }
  340. }
  341. if deleteStatus != 1 && req.IndexId == 0 {
  342. classifyCount, err := aiPredictModel.GetAiPredictModelClassifyCountByClassifyId(req.ClassifyId)
  343. if err != nil && !utils.IsErrNoRow(err) {
  344. br.Msg = "删除失败"
  345. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  346. return
  347. }
  348. if classifyCount > 0 {
  349. deleteStatus = 2
  350. tipsMsg = "确认删除当前目录及包含的子目录吗"
  351. }
  352. }
  353. if deleteStatus == 0 {
  354. tipsMsg = "可删除,进行删除操作"
  355. }
  356. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  357. resp.DeleteStatus = deleteStatus
  358. resp.TipsMsg = tipsMsg
  359. br.Ret = 200
  360. br.Msg = "检测成功"
  361. br.Success = true
  362. br.Data = resp
  363. }
  364. // Remove
  365. // @Title 删除分类/标的
  366. // @Description 删除分类/标的
  367. // @Param request body aiPredictModel.AiPredictModelClassifyRemoveReq true "type json string"
  368. // @Success 200 Ret=200 删除成功
  369. // @router /classify/remove [post]
  370. func (this *AiPredictModelClassifyController) Remove() {
  371. br := new(models.BaseResponse).Init()
  372. defer func() {
  373. if br.ErrMsg == "" {
  374. br.IsSendEmail = false
  375. }
  376. this.Data["json"] = br
  377. this.ServeJSON()
  378. }()
  379. var req aiPredictModel.AiPredictModelClassifyRemoveReq
  380. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  381. br.Msg = "参数解析异常"
  382. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  383. return
  384. }
  385. if req.ClassifyId < 0 && req.IndexId <= 0 {
  386. br.Msg = "参数错误"
  387. return
  388. }
  389. // 删除分类
  390. if req.ClassifyId > 0 && req.IndexId == 0 {
  391. count, err := aiPredictModel.GetAiPredictModelIndexCountByClassifyId(req.ClassifyId)
  392. if err != nil && !utils.IsErrNoRow(err) {
  393. br.Msg = "删除失败"
  394. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  395. return
  396. }
  397. if count > 0 {
  398. br.Msg = "该目录下存在关联指标,不可删除"
  399. br.IsSendEmail = false
  400. return
  401. }
  402. err = aiPredictModel.RemoveAiPredictModelClassify(req.ClassifyId)
  403. if err != nil {
  404. br.Msg = "删除失败"
  405. br.ErrMsg = "删除失败,Err:" + err.Error()
  406. return
  407. }
  408. }
  409. // 删除标的
  410. if req.IndexId > 0 {
  411. indexOb := new(aiPredictModel.AiPredictModelIndex)
  412. aiIndex, e := indexOb.GetItemById(req.IndexId)
  413. if e != nil {
  414. if utils.IsErrNoRow(e) {
  415. br.Ret = 200
  416. br.Msg = "删除成功"
  417. br.Success = true
  418. return
  419. }
  420. br.Msg = "操作失败"
  421. br.ErrMsg = fmt.Sprintf("获取标的信息失败, %v", e)
  422. return
  423. }
  424. if aiIndex != nil && aiIndex.AiPredictModelIndexId <= 0 {
  425. br.Ret = 200
  426. br.Msg = "删除成功"
  427. br.Success = true
  428. return
  429. }
  430. // 获取指标图表
  431. var chartIds []int
  432. chartTypes := []int{utils.CHART_SOURCE_AI_PREDICT_MODEL_DAILY, utils.CHART_SOURCE_AI_PREDICT_MODEL_MONTHLY}
  433. for _, v := range chartTypes {
  434. ct, e := data_manage.GetAiPredictChartInfoByIndexId(v, req.IndexId)
  435. if e != nil && !utils.IsErrNoRow(e) {
  436. br.Msg = "操作失败"
  437. br.ErrMsg = fmt.Sprintf("获取标的图表信息失败, %v", e)
  438. return
  439. }
  440. if ct != nil && ct.ChartInfoId > 0 {
  441. chartIds = append(chartIds, ct.ChartInfoId)
  442. }
  443. }
  444. // 删除标的及数据
  445. if e = indexOb.RemoveIndexAndData(req.IndexId, chartIds); e != nil {
  446. br.Msg = "操作失败"
  447. br.ErrMsg = fmt.Sprintf("删除标的及数据失败, %v", e)
  448. return
  449. }
  450. // ES标记标的/图表删除
  451. go func() {
  452. indexItem := new(dataSourceModel.SearchDataSource)
  453. indexItem.PrimaryId = aiIndex.AiPredictModelIndexId
  454. indexItem.IndexName = aiIndex.IndexName
  455. indexItem.IndexCode = aiIndex.IndexCode
  456. indexItem.ClassifyId = aiIndex.ClassifyId
  457. indexItem.Source = utils.DATA_SOURCE_AI_PREDICT_MODEL
  458. indexItem.SourceName = "AI预测模型"
  459. indexItem.IsDeleted = 1
  460. indexItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, aiIndex.CreateTime)
  461. indexItem.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, aiIndex.ModifyTime)
  462. docId := fmt.Sprintf("%d-%d", indexItem.Source, indexItem.PrimaryId)
  463. if e := elastic.EsAddOrEditDataSourceIndex(utils.EsDataSourceIndexName, docId, indexItem); e != nil {
  464. utils.FileLog.Info("AI预测模型-标记删除es失败, %v", e)
  465. return
  466. }
  467. if len(chartIds) == 0 {
  468. return
  469. }
  470. for _, v := range chartIds {
  471. data.EsDeleteChartInfo(v)
  472. }
  473. }()
  474. }
  475. br.Ret = 200
  476. br.Msg = "删除成功"
  477. br.Success = true
  478. br.IsAddLog = true
  479. }
  480. // Move
  481. // @Title 移动
  482. // @Description 移动
  483. // @Success 200 {object} aiPredictModel.AiPredictModelClassifyMoveReq
  484. // @router /classify/move [post]
  485. func (this *AiPredictModelClassifyController) Move() {
  486. br := new(models.BaseResponse).Init()
  487. defer func() {
  488. if br.ErrMsg == "" {
  489. br.IsSendEmail = false
  490. }
  491. this.Data["json"] = br
  492. this.ServeJSON()
  493. }()
  494. sysUser := this.SysUser
  495. if sysUser == nil {
  496. br.Msg = "请登录"
  497. br.ErrMsg = "请登录,SysUser Is Empty"
  498. br.Ret = 408
  499. return
  500. }
  501. var req aiPredictModel.AiPredictModelClassifyMoveReq
  502. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  503. br.Msg = "参数解析异常"
  504. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  505. return
  506. }
  507. if req.ClassifyId <= 0 && req.ItemId <= 0 {
  508. br.Msg = "请选择分类或指标"
  509. return
  510. }
  511. err, errMsg := services.AiPredictModelMoveClassify(req, sysUser)
  512. if errMsg != `` {
  513. br.Msg = errMsg
  514. br.ErrMsg = errMsg
  515. if err != nil {
  516. br.ErrMsg = err.Error()
  517. } else {
  518. br.IsSendEmail = false
  519. }
  520. return
  521. }
  522. br.Ret = 200
  523. br.Success = true
  524. br.Msg = "移动成功"
  525. }