base_from_ths_hf_classify.go 13 KB

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