predict_edb_classify.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "hongze/hz_crm_api/controllers"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/data_manage"
  7. "hongze/hz_crm_api/models/system"
  8. "hongze/hz_crm_api/services/data"
  9. "hongze/hz_crm_api/utils"
  10. )
  11. // PredictEdbClassifyController 数据管理-预测指标分类模块
  12. type PredictEdbClassifyController struct {
  13. controllers.BaseAuthController
  14. }
  15. // List
  16. // @Title 预测指标分类列表
  17. // @Description 预测指标分类列表接口
  18. // @Param IsOnlyMe query bool false "是否只看我的"
  19. // @Success 200 {object} data_manage.EdbClassifyListResp
  20. // @router /predict_classify/list [get]
  21. func (this *PredictEdbClassifyController) List() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. isOnlyMe, _ := this.GetBool("IsOnlyMe")
  28. rootList, err := data_manage.GetEdbClassifyByParentId(0, 1)
  29. if err != nil && err.Error() != utils.ErrNoRow() {
  30. br.Msg = "获取失败"
  31. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  32. return
  33. }
  34. // 获取当前账号的不可见指标
  35. obj := data_manage.EdbInfoNoPermissionAdmin{}
  36. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  37. if err != nil && err.Error() != utils.ErrNoRow() {
  38. br.Msg = "获取失败"
  39. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  40. return
  41. }
  42. noPermissionEdbInfoIdMap := make(map[int]bool)
  43. for _, v := range confList {
  44. noPermissionEdbInfoIdMap[v.EdbInfoId] = true
  45. }
  46. allEdbInfo, err := data_manage.GetPredictEdbInfoAll(1)
  47. if err != nil && err.Error() != utils.ErrNoRow() {
  48. br.Msg = "获取失败"
  49. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  50. return
  51. }
  52. edbInfoMap := make(map[int][]*data_manage.EdbClassifyItems)
  53. for _, v := range allEdbInfo {
  54. // 如果指标不可见,那么就不返回该指标
  55. if _, ok := noPermissionEdbInfoIdMap[v.EdbInfoId]; ok {
  56. continue
  57. }
  58. button := data.GetPredictEdbOpButton(this.SysUser, v.SysUserId)
  59. button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
  60. button.OpButton = false
  61. button.DeleteButton = false
  62. v.Button = button
  63. if isOnlyMe {
  64. if v.SysUserId == this.SysUser.AdminId {
  65. edbInfoMap[v.ClassifyId] = append(edbInfoMap[v.ClassifyId], v)
  66. }
  67. } else {
  68. edbInfoMap[v.ClassifyId] = append(edbInfoMap[v.ClassifyId], v)
  69. }
  70. }
  71. rootChildMap := make(map[int][]*data_manage.EdbClassifyItems)
  72. for _, v := range rootList {
  73. button := data.GetPredictEdbClassifyOpButton(this.SysUser, v.SysUserId)
  74. v.Button = button
  75. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  76. if existItems, ok := edbInfoMap[v.ClassifyId]; ok {
  77. v.Children = existItems
  78. } else {
  79. items := make([]*data_manage.EdbClassifyItems, 0)
  80. v.Children = items
  81. }
  82. }
  83. nodeAll := make([]*data_manage.EdbClassifyItems, 0)
  84. for _, v := range rootList {
  85. nodeAll = append(nodeAll, v)
  86. }
  87. // 是否允许添加分类
  88. canOpClassify := true
  89. // 如果不是 超管 或者 ficc管理员,那么就没有权限
  90. if this.SysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && this.SysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN {
  91. canOpClassify = false
  92. }
  93. language := `CN`
  94. // 指标显示的语言
  95. {
  96. configDetail, _ := system.GetConfigDetailByCode(this.SysUser.AdminId, system.PredictEdbLanguageVar)
  97. if configDetail != nil {
  98. language = configDetail.ConfigValue
  99. } else {
  100. configDetail, _ = system.GetDefaultConfigDetailByCode(system.PredictEdbLanguageVar)
  101. if configDetail != nil {
  102. language = configDetail.ConfigValue
  103. }
  104. }
  105. }
  106. resp := data_manage.EdbClassifyListResp{
  107. AllNodes: nodeAll,
  108. CanOpClassify: canOpClassify,
  109. Language: language,
  110. }
  111. br.Ret = 200
  112. br.Success = true
  113. br.Msg = "获取成功"
  114. br.Data = resp
  115. }
  116. // Items
  117. // @Title 获取所有预测指标分类接口-不包含指标
  118. // @Description 获取所有预测指标分类接口-不包含指标
  119. // @Success 200 {object} data_manage.EdbClassifyListResp
  120. // @router /predict_classify/items [get]
  121. func (this *PredictEdbClassifyController) Items() {
  122. br := new(models.BaseResponse).Init()
  123. defer func() {
  124. this.Data["json"] = br
  125. this.ServeJSON()
  126. }()
  127. rootList, err := data_manage.GetEdbClassifyByParentId(0, 1)
  128. if err != nil {
  129. br.Msg = "获取失败"
  130. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  131. return
  132. }
  133. resp := new(data_manage.EdbClassifyListResp)
  134. resp.AllNodes = rootList
  135. br.Ret = 200
  136. br.Success = true
  137. br.Msg = "获取成功"
  138. br.Data = resp
  139. }
  140. // Add
  141. // @Title 新增预测指标分类
  142. // @Description 新增预测指标分类接口
  143. // @Param request body data_manage.AddEdbClassifyReq true "type json string"
  144. // @Success 200 Ret=200 保存成功
  145. // @router /predict_classify/add [post]
  146. func (this *PredictEdbClassifyController) Add() {
  147. br := new(models.BaseResponse).Init()
  148. defer func() {
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. var req data_manage.AddEdbClassifyReq
  153. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  154. if err != nil {
  155. br.Msg = "参数解析异常!"
  156. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  157. return
  158. }
  159. if req.ClassifyName == "" {
  160. br.Msg = "请输入分类名称"
  161. br.IsSendEmail = false
  162. return
  163. }
  164. if req.ParentId < 0 {
  165. br.Msg = "参数错误"
  166. br.IsSendEmail = false
  167. return
  168. }
  169. if this.SysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && this.SysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN {
  170. br.Msg = "不允许添加分类"
  171. br.IsSendEmail = false
  172. return
  173. }
  174. // 添加指标分类
  175. _, err, errMsg := data.AddEdbClassify(req.ClassifyName, req.ParentId, req.Level, 1, this.SysUser.AdminId, this.SysUser.AdminName)
  176. if errMsg != `` {
  177. br.Msg = errMsg
  178. br.ErrMsg = errMsg
  179. if err != nil {
  180. br.ErrMsg = errMsg + ";Err:" + err.Error()
  181. } else {
  182. br.IsSendEmail = false
  183. }
  184. return
  185. }
  186. br.Ret = 200
  187. br.Msg = "保存成功"
  188. br.Success = true
  189. br.IsAddLog = true
  190. }
  191. // Edit
  192. // @Title 修改预测指标分类
  193. // @Description 修改预测指标分类接口
  194. // @Param request body data_manage.EditEdbClassifyReq true "type json string"
  195. // @Success 200 Ret=200 修改成功
  196. // @router /predict_classify/edit [post]
  197. func (this *PredictEdbClassifyController) Edit() {
  198. br := new(models.BaseResponse).Init()
  199. defer func() {
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. var req data_manage.EditEdbClassifyReq
  204. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  205. if err != nil {
  206. br.Msg = "参数解析异常!"
  207. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  208. return
  209. }
  210. if req.ClassifyName == "" {
  211. br.Msg = "请输入分类名称"
  212. br.IsSendEmail = false
  213. return
  214. }
  215. if req.ClassifyId < 0 {
  216. br.Msg = "参数错误"
  217. br.IsSendEmail = false
  218. return
  219. }
  220. err, errMsg := data.EditEdbClassify(req.ClassifyId, req.ClassifyName)
  221. if errMsg != `` {
  222. br.Msg = errMsg
  223. br.ErrMsg = errMsg
  224. if err != nil {
  225. br.ErrMsg = errMsg + ";Err:" + err.Error()
  226. } else {
  227. br.IsSendEmail = false
  228. }
  229. return
  230. }
  231. br.Ret = 200
  232. br.Msg = "保存成功"
  233. br.Success = true
  234. }
  235. // DeleteCheck
  236. // @Title 删除检测接口
  237. // @Description 删除检测接口
  238. // @Param request body data_manage.ClassifyDeleteCheckReq true "type json string"
  239. // @Success 200 Ret=200 检测成功
  240. // @router /predict_classify/delete/check [post]
  241. func (this *PredictEdbClassifyController) DeleteCheck() {
  242. br := new(models.BaseResponse).Init()
  243. defer func() {
  244. this.Data["json"] = br
  245. this.ServeJSON()
  246. }()
  247. var req data_manage.ClassifyDeleteCheckReq
  248. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  249. if err != nil {
  250. br.Msg = "参数解析异常!"
  251. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  252. return
  253. }
  254. if req.ClassifyId < 0 && req.EdbInfoId <= 0 {
  255. br.Msg = "参数错误"
  256. br.IsSendEmail = false
  257. return
  258. }
  259. deleteStatus, tipsMsg, err, errMsg := data.DeleteCheck(req.ClassifyId, req.EdbInfoId)
  260. if errMsg != `` {
  261. br.Msg = errMsg
  262. br.ErrMsg = errMsg
  263. if err != nil {
  264. br.ErrMsg = err.Error()
  265. } else {
  266. br.IsSendEmail = false
  267. }
  268. return
  269. }
  270. resp := new(data_manage.ClassifyDeleteCheckResp)
  271. resp.DeleteStatus = deleteStatus
  272. resp.TipsMsg = tipsMsg
  273. br.Ret = 200
  274. br.Msg = "检测成功"
  275. br.Success = true
  276. br.Data = resp
  277. }
  278. // Delete
  279. // @Title 删除分类/指标
  280. // @Description 删除分类/指标接口
  281. // @Param request body data_manage.DeleteEdbClassifyReq true "type json string"
  282. // @Success 200 Ret=200 新增成功
  283. // @router /predict_classify/delete [post]
  284. func (this *PredictEdbClassifyController) Delete() {
  285. br := new(models.BaseResponse).Init()
  286. defer func() {
  287. this.Data["json"] = br
  288. this.ServeJSON()
  289. }()
  290. sysUser := this.SysUser
  291. if sysUser == nil {
  292. br.Msg = "请登录"
  293. br.ErrMsg = "请登录,SysUser Is Empty"
  294. br.Ret = 408
  295. return
  296. }
  297. var req data_manage.DeleteEdbClassifyReq
  298. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  299. if err != nil {
  300. br.Msg = "参数解析异常!"
  301. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  302. return
  303. }
  304. if req.ClassifyId < 0 && req.EdbInfoId <= 0 {
  305. br.Msg = "参数错误"
  306. br.IsSendEmail = false
  307. return
  308. }
  309. nextItem, err, errMsg := data.Delete(req.ClassifyId, req.EdbInfoId, sysUser.AdminId, sysUser.RealName, string(this.Ctx.Input.RequestBody), this.Ctx.Input.URI())
  310. if errMsg != `` {
  311. br.Msg = errMsg
  312. br.ErrMsg = errMsg
  313. if err != nil {
  314. br.ErrMsg = err.Error()
  315. } else {
  316. br.IsSendEmail = false
  317. }
  318. return
  319. }
  320. resp := data_manage.AddEdbInfoResp{}
  321. if nextItem != nil {
  322. resp = data_manage.AddEdbInfoResp{
  323. EdbInfoId: nextItem.EdbInfoId,
  324. UniqueCode: nextItem.UniqueCode,
  325. }
  326. }
  327. br.Ret = 200
  328. br.Msg = "删除成功"
  329. br.Success = true
  330. br.IsAddLog = true
  331. br.Data = resp
  332. }
  333. // Move
  334. // EdbClassifyMove
  335. // @Title 分类移动接口
  336. // @Description 分类移动接口
  337. // @Success 200 {object} data_manage.MoveEdbClassifyReq
  338. // @router /predict_classify/move [post]
  339. func (this *PredictEdbClassifyController) Move() {
  340. br := new(models.BaseResponse).Init()
  341. defer func() {
  342. this.Data["json"] = br
  343. this.ServeJSON()
  344. }()
  345. sysUser := this.SysUser
  346. if sysUser == nil {
  347. br.Msg = "请登录"
  348. br.ErrMsg = "请登录,SysUser Is Empty"
  349. br.Ret = 408
  350. return
  351. }
  352. var req data_manage.MoveEdbClassifyReq
  353. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  354. if err != nil {
  355. br.Msg = "参数解析异常!"
  356. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  357. return
  358. }
  359. if req.ClassifyId <= 0 {
  360. br.Msg = "参数错误"
  361. br.ErrMsg = "分类id小于等于0"
  362. return
  363. }
  364. err, errMsg := data.MoveEdbClassify(req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId)
  365. if errMsg != `` {
  366. br.Msg = errMsg
  367. br.ErrMsg = errMsg
  368. if err != nil {
  369. br.ErrMsg = err.Error()
  370. } else {
  371. br.IsSendEmail = false
  372. }
  373. return
  374. }
  375. ////判断分类是否存在
  376. //edbClassifyInfo, err := data_manage.GetEdbClassifyById(req.ClassifyId)
  377. //if err != nil {
  378. // br.Msg = "移动失败"
  379. // br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  380. // return
  381. //}
  382. //updateCol := make([]string, 0)
  383. //
  384. ////判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  385. //if edbClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  386. // parentEdbClassifyInfo, err := data_manage.GetEdbClassifyById(req.ParentClassifyId)
  387. // if err != nil {
  388. // br.Msg = "移动失败"
  389. // br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  390. // return
  391. // }
  392. // edbClassifyInfo.ParentId = parentEdbClassifyInfo.ClassifyId
  393. // edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
  394. // edbClassifyInfo.ModifyTime = time.Now()
  395. // updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  396. //}
  397. //
  398. ////如果有传入 上一个兄弟节点分类id
  399. //if req.PrevClassifyId > 0 {
  400. // prevClassify, err := data_manage.GetEdbClassifyById(req.PrevClassifyId)
  401. // if err != nil {
  402. // br.Msg = "移动失败"
  403. // br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  404. // return
  405. // }
  406. //
  407. // //如果是移动在两个兄弟节点之间
  408. // if req.NextClassifyId > 0 {
  409. // //下一个兄弟节点
  410. // nextClassify, err := data_manage.GetEdbClassifyById(req.NextClassifyId)
  411. // if err != nil {
  412. // br.Msg = "移动失败"
  413. // br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  414. // return
  415. // }
  416. // //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  417. // if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == edbClassifyInfo.Sort {
  418. // //变更兄弟节点的排序
  419. // updateSortStr := `sort + 2`
  420. // _ = data_manage.UpdateEdbClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  421. // } else {
  422. // //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  423. // if nextClassify.Sort-prevClassify.Sort == 1 {
  424. // //变更兄弟节点的排序
  425. // updateSortStr := `sort + 1`
  426. // _ = data_manage.UpdateEdbClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  427. // }
  428. // }
  429. // }
  430. //
  431. // edbClassifyInfo.Sort = prevClassify.Sort + 1
  432. // edbClassifyInfo.ModifyTime = time.Now()
  433. // updateCol = append(updateCol, "Sort", "ModifyTime")
  434. //} else {
  435. // firstClassify, err := data_manage.GetFirstEdbClassifyByParentId(edbClassifyInfo.ParentId)
  436. // if err != nil && err.Error() != utils.ErrNoRow() {
  437. // br.Msg = "移动失败"
  438. // br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  439. // return
  440. // }
  441. //
  442. // //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  443. // if firstClassify != nil && firstClassify.Sort == 0 {
  444. // updateSortStr := ` sort + 1 `
  445. // _ = data_manage.UpdateEdbClassifySortByParentId(firstClassify.ParentId, firstClassify.ClassifyId-1, 0, updateSortStr)
  446. // }
  447. //
  448. // edbClassifyInfo.Sort = 0 //那就是排在第一位
  449. // edbClassifyInfo.ModifyTime = time.Now()
  450. // updateCol = append(updateCol, "Sort", "ModifyTime")
  451. //}
  452. //
  453. ////更新
  454. //if len(updateCol) > 0 {
  455. // err = edbClassifyInfo.Update(updateCol)
  456. // if err != nil {
  457. // br.Msg = "移动失败"
  458. // br.ErrMsg = "修改失败,Err:" + err.Error()
  459. // return
  460. // }
  461. //}
  462. br.Ret = 200
  463. br.Success = true
  464. br.Msg = "移动成功"
  465. }