classify.go 14 KB

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