excel_classify.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage/excel"
  7. "eta/eta_api/models/data_manage/excel/request"
  8. response2 "eta/eta_api/models/data_manage/excel/response"
  9. "eta/eta_api/services/data/data_manage_permission"
  10. excel2 "eta/eta_api/services/data/excel"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "sort"
  14. "strconv"
  15. "time"
  16. )
  17. // ExcelClassifyController ETA表格分类
  18. type ExcelClassifyController struct {
  19. controllers.BaseAuthController
  20. }
  21. // List
  22. // @Title excel表格分类列表
  23. // @Description excel表格分类列表接口
  24. // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1"
  25. // @Param IsShowMe query bool true "是否只看我的,true、false"
  26. // @Success 200 {object} response.ExcelClassifyListResp
  27. // @router /excel_classify/list [get]
  28. func (this *ExcelClassifyController) List() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. source, _ := this.GetInt("Source")
  35. if source <= 0 {
  36. source = utils.EXCEL_DEFAULT
  37. }
  38. //只看我的
  39. isShowMe, _ := this.GetBool("IsShowMe")
  40. // 自定义分析只看自己的
  41. //if source == utils.CUSTOM_ANALYSIS_TABLE {
  42. // br = AnalysisClassifyList(this)
  43. // return
  44. //}
  45. showUserId := 0
  46. if isShowMe {
  47. showUserId = this.SysUser.AdminId
  48. }
  49. classifyList, err := excel.GetExcelClassifyBySource(source)
  50. if err != nil {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  53. return
  54. }
  55. // 分类map
  56. classifyMap := make(map[int]*excel.ExcelClassifyItems)
  57. for _, v := range classifyList {
  58. classifyMap[v.ExcelClassifyId] = v
  59. }
  60. // 获取二级分类
  61. // 获取三级分类
  62. // 根据来源获取所有excel表格(无内容)
  63. allExcelInfo := make([]*excel.ExcelClassifyItems, 0)
  64. if source == utils.BALANCE_TABLE {
  65. //找到当前协作人相关的表格ID
  66. var excelIds []int
  67. if isShowMe {
  68. obj := new(excel.ExcelWorker)
  69. existList, err := obj.GetBySysUserId(this.SysUser.AdminId)
  70. if err != nil {
  71. br.Msg = "获取表格协作人失败!"
  72. br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
  73. return
  74. }
  75. if len(existList) > 0 {
  76. for _, v := range existList {
  77. excelIds = append(excelIds, v.ExcelInfoId)
  78. }
  79. }
  80. }
  81. allExcelInfo, err = excel.GetBalanceNoContentExcelInfoAll(source, excelIds, showUserId)
  82. if err != nil && err.Error() != utils.ErrNoRow() {
  83. br.Msg = "获取失败"
  84. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  85. return
  86. }
  87. } else {
  88. allExcelInfo, err = excel.GetNoContentExcelInfoAll(source, showUserId)
  89. if err != nil && err.Error() != utils.ErrNoRow() {
  90. br.Msg = "获取失败"
  91. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  92. return
  93. }
  94. }
  95. // 获取所有有权限的指标和分类
  96. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  97. if err != nil {
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  100. return
  101. }
  102. ExcelInfoMap := make(map[int][]*excel.ExcelClassifyItems)
  103. for _, v := range allExcelInfo {
  104. // 数据权限
  105. if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
  106. v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
  107. }
  108. if source == utils.BALANCE_TABLE && isShowMe && !v.HaveOperaAuth { // 过滤我不可编辑的表格
  109. continue
  110. }
  111. ExcelInfoMap[v.ExcelClassifyId] = append(ExcelInfoMap[v.ExcelClassifyId], v)
  112. }
  113. classifyListMap := make(map[int][]*excel.ExcelClassifyItems)
  114. for _, v := range classifyList {
  115. // 数据权限
  116. v.HaveOperaAuth = data_manage_permission.CheckExcelClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ExcelClassifyId, permissionClassifyIdList)
  117. if existItems, ok := ExcelInfoMap[v.ExcelClassifyId]; ok {
  118. v.Children = existItems
  119. }
  120. }
  121. for _, v := range classifyList {
  122. if v.ParentId > 0 {
  123. classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v)
  124. }
  125. }
  126. //组装三级分类
  127. for key, classify := range classifyList {
  128. subList, ok := classifyListMap[classify.ExcelClassifyId]
  129. if ok {
  130. classifyList[key].Children = append(classifyList[key].Children, subList...)
  131. sort.Slice(classifyList[key].Children, func(i, j int) bool {
  132. return excel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j])
  133. })
  134. }
  135. }
  136. nodeAll := make([]*excel.ExcelClassifyItems, 0)
  137. for _, v := range classifyList {
  138. if v.ParentId == 0 {
  139. sort.Slice(v.Children, func(i, j int) bool { return excel.ExcelClassifyItemBySort(v.Children[i], v.Children[j]) })
  140. nodeAll = append(nodeAll, v)
  141. }
  142. }
  143. if isShowMe {
  144. nodeAll = excel2.GetClassifyListRemoveNoExcel(nodeAll)
  145. }
  146. resp := response2.ExcelClassifyListResp{
  147. AllNodes: nodeAll,
  148. }
  149. br.Ret = 200
  150. br.Success = true
  151. br.Msg = "获取成功"
  152. br.Data = resp
  153. }
  154. func AnalysisClassifyList(this *ExcelClassifyController) (br *models.BaseResponse) {
  155. br = new(models.BaseResponse).Init()
  156. defer func() {
  157. this.Data["json"] = br
  158. this.ServeJSON()
  159. }()
  160. source, _ := this.GetInt("Source")
  161. if source <= 0 {
  162. source = utils.EXCEL_DEFAULT
  163. }
  164. //只看我的
  165. showUserId := this.SysUser.AdminId
  166. classifyList, err := excel.GetExcelClassifyBySource(source)
  167. if err != nil {
  168. br.Msg = "获取失败"
  169. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  170. return
  171. }
  172. // 分类map
  173. classifyMap := make(map[int]*excel.ExcelClassifyItems)
  174. for _, v := range classifyList {
  175. classifyMap[v.ExcelClassifyId] = v
  176. }
  177. // 获取二级分类
  178. // 获取三级分类
  179. // 根据来源获取所有excel表格(无内容)
  180. allExcelInfo, err := excel.GetNoContentExcelInfoAll(source, showUserId)
  181. if err != nil && err.Error() != utils.ErrNoRow() {
  182. br.Msg = "获取失败"
  183. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  184. return
  185. }
  186. // 获取所有有权限的指标和分类
  187. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  188. if err != nil {
  189. br.Msg = "获取失败"
  190. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  191. return
  192. }
  193. ExcelInfoMap := make(map[int][]*excel.ExcelClassifyItems)
  194. for _, v := range allExcelInfo {
  195. // 数据权限
  196. if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
  197. v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
  198. }
  199. ExcelInfoMap[v.ExcelClassifyId] = append(ExcelInfoMap[v.ExcelClassifyId], v)
  200. }
  201. classifyListMap := make(map[int][]*excel.ExcelClassifyItems)
  202. for _, v := range classifyList {
  203. // 数据权限
  204. v.HaveOperaAuth = data_manage_permission.CheckExcelClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ExcelClassifyId, permissionClassifyIdList)
  205. if existItems, ok := ExcelInfoMap[v.ExcelClassifyId]; ok {
  206. v.Children = existItems
  207. }
  208. }
  209. for _, v := range classifyList {
  210. if v.ParentId > 0 {
  211. classifyListMap[v.ParentId] = append(classifyListMap[v.ParentId], v)
  212. }
  213. }
  214. //组装三级分类
  215. for key, classify := range classifyList {
  216. subList, ok := classifyListMap[classify.ExcelClassifyId]
  217. if ok {
  218. classifyList[key].Children = append(classifyList[key].Children, subList...)
  219. sort.Slice(classifyList[key].Children, func(i, j int) bool {
  220. return excel.ExcelClassifyItemBySort(classifyList[key].Children[i], classifyList[key].Children[j])
  221. })
  222. }
  223. }
  224. nodeAll := make([]*excel.ExcelClassifyItems, 0)
  225. for _, v := range classifyList {
  226. if v.ParentId == 0 {
  227. sort.Slice(v.Children, func(i, j int) bool { return excel.ExcelClassifyItemBySort(v.Children[i], v.Children[j]) })
  228. nodeAll = append(nodeAll, v)
  229. }
  230. }
  231. // 自定义分析只看自己的分类
  232. nodeAll = removeNodesWithAnalysisClassify(nodeAll, this.SysUser.AdminId)
  233. resp := response2.ExcelClassifyListResp{
  234. AllNodes: nodeAll,
  235. }
  236. br.Ret = 200
  237. br.Success = true
  238. br.Msg = "获取成功"
  239. br.Data = resp
  240. return
  241. }
  242. // removeNodesWithAnalysisClassify
  243. // @Description: 自定义分析只看自己的分类,所以需要移除分类
  244. // @author: Roc
  245. // @datetime 2024-04-09 13:46:51
  246. // @param nodes []*excel.ExcelClassifyItems
  247. // @param adminId int
  248. // @return []*excel.ExcelClassifyItems
  249. func removeNodesWithAnalysisClassify(nodes []*excel.ExcelClassifyItems, adminId int) []*excel.ExcelClassifyItems {
  250. var newListNode []*excel.ExcelClassifyItems
  251. for _, node := range nodes {
  252. if node.Children != nil {
  253. node.Children = removeNodesWithAnalysisClassify(node.Children, adminId)
  254. }
  255. // 如果是ETA表格,那么就保留
  256. if node.ExcelInfoId > 0 {
  257. newListNode = append(newListNode, node)
  258. continue
  259. }
  260. // 如果是公共分类,且名下的ETA表格不为空,那么就保留
  261. if node.SysUserId == 0 && node.Children != nil && len(node.Children) > 0 {
  262. newListNode = append(newListNode, node)
  263. continue
  264. }
  265. // 如果是自己的分类,那么就保留
  266. if node.SysUserId == adminId {
  267. newListNode = append(newListNode, node)
  268. continue
  269. }
  270. }
  271. return newListNode
  272. }
  273. // ExcelClassifyItems
  274. // @Title 获取所有excel表格分类接口-不包含表格
  275. // @Description 获取所有excel表格分类接口-不包含表格
  276. // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1"
  277. // @Success 200 {object} response.ExcelClassifyListResp
  278. // @router /excel_classify/items [get]
  279. func (this *ExcelClassifyController) ExcelClassifyItems() {
  280. br := new(models.BaseResponse).Init()
  281. defer func() {
  282. this.Data["json"] = br
  283. this.ServeJSON()
  284. }()
  285. source, _ := this.GetInt("Source")
  286. if source <= 0 {
  287. source = utils.EXCEL_DEFAULT
  288. }
  289. classifyList, err := excel.GetExcelClassifyBySource(source)
  290. if err != nil {
  291. br.Msg = "获取失败"
  292. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  293. return
  294. }
  295. // 获取二级分类
  296. // 获取三级分类
  297. classifyMap := make(map[int][]*excel.ExcelClassifyItems)
  298. for _, v := range classifyList {
  299. if v.ParentId > 0 {
  300. classifyMap[v.ParentId] = append(classifyMap[v.ParentId], v)
  301. }
  302. }
  303. //组装三级分类
  304. for key, classify := range classifyList {
  305. subList, ok := classifyMap[classify.ExcelClassifyId]
  306. if ok {
  307. classifyList[key].Children = append(classifyList[key].Children, subList...)
  308. }
  309. }
  310. nodeAll := make([]*excel.ExcelClassifyItems, 0)
  311. for _, v := range classifyList {
  312. if v.ParentId == 0 {
  313. nodeAll = append(nodeAll, v)
  314. }
  315. }
  316. resp := response2.ExcelClassifyListResp{
  317. AllNodes: nodeAll,
  318. }
  319. br.Ret = 200
  320. br.Success = true
  321. br.Msg = "获取成功"
  322. br.Data = resp
  323. }
  324. // AddExcelClassify
  325. // @Title 新增excel表格分类
  326. // @Description 新增excel表格分类接口
  327. // @Param request body request.AddExcelClassifyReq true "type json string"
  328. // @Success 200 Ret=200 保存成功
  329. // @router /excel_classify/add [post]
  330. func (this *ExcelClassifyController) AddExcelClassify() {
  331. br := new(models.BaseResponse).Init()
  332. defer func() {
  333. this.Data["json"] = br
  334. this.ServeJSON()
  335. }()
  336. var req request.AddExcelClassifyReq
  337. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  338. if err != nil {
  339. br.Msg = "参数解析异常!"
  340. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  341. return
  342. }
  343. if req.ExcelClassifyName == "" {
  344. br.Msg = "请输入分类名称"
  345. br.IsSendEmail = false
  346. return
  347. }
  348. if req.ParentId < 0 {
  349. br.Msg = "参数错误"
  350. br.IsSendEmail = false
  351. return
  352. }
  353. source := req.Source
  354. if source <= 0 {
  355. source = utils.EXCEL_DEFAULT
  356. }
  357. // 获取同级分类下存在同名分类的数量
  358. var count int
  359. if source == utils.CUSTOM_ANALYSIS_TABLE {
  360. // 自定义分析分类私有化了
  361. ct, e := excel.GetCustomAnalysisExcelClassifyCount(req.ExcelClassifyName, req.ParentId, source, this.SysUser.AdminId)
  362. if e != nil {
  363. br.Msg = "操作失败"
  364. br.ErrMsg = fmt.Sprintf("获取自定义分析同级分类下同名分类失败, %v", e)
  365. return
  366. }
  367. count = ct
  368. } else {
  369. ct, e := excel.GetExcelClassifyCount(req.ExcelClassifyName, req.ParentId, source)
  370. if e != nil {
  371. br.Msg = "操作失败"
  372. br.ErrMsg = fmt.Sprintf("获取同级分类下同名分类失败, %v", e)
  373. return
  374. }
  375. count = ct
  376. }
  377. if count > 0 {
  378. br.Msg = "分类名称已存在,请重新输入"
  379. br.IsSendEmail = false
  380. return
  381. }
  382. //获取该层级下最大的排序数
  383. maxSort, err := excel2.GetExcelClassifyMaxSort(req.ParentId, req.Source)
  384. if err != nil {
  385. br.Msg = "获取失败"
  386. br.ErrMsg = "查询排序信息失败,Err:" + err.Error()
  387. return
  388. }
  389. level := 1
  390. // 查询父级分类是否存在
  391. if req.ParentId > 0 {
  392. var parent *excel.ExcelClassify
  393. parent, err = excel.GetExcelClassifyById(req.ParentId)
  394. if err != nil {
  395. if err.Error() == utils.ErrNoRow() {
  396. br.Msg = "父级分类不存在"
  397. return
  398. }
  399. br.Msg = "获取失败"
  400. br.ErrMsg = "查询父级分类信息失败,Err:" + err.Error()
  401. return
  402. }
  403. level = parent.Level + 1
  404. }
  405. // 入库
  406. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  407. classify := &excel.ExcelClassify{
  408. //ExcelClassifyId: 0,
  409. ExcelClassifyName: req.ExcelClassifyName,
  410. ParentId: req.ParentId,
  411. Source: source,
  412. SysUserId: this.SysUser.AdminId,
  413. SysUserRealName: this.SysUser.RealName,
  414. Level: level,
  415. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  416. Sort: maxSort + 1,
  417. CreateTime: time.Now(),
  418. ModifyTime: time.Now(),
  419. }
  420. _, err = excel.AddExcelClassify(classify)
  421. if err != nil {
  422. br.Msg = "保存分类失败"
  423. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  424. return
  425. }
  426. // 继承分类权限
  427. {
  428. go data_manage_permission.InheritParentClassify(6, source, classify.ExcelClassifyId, classify.ParentId, classify.ExcelClassifyName)
  429. }
  430. br.Ret = 200
  431. br.Msg = "保存成功"
  432. br.Success = true
  433. }
  434. // EditExcelClassify
  435. // @Title 修改excel表格分类
  436. // @Description 修改excel表格分类
  437. // @Param request body request.EditExcelClassifyReq true "type json string"
  438. // @Success 200 Ret=200 修改成功
  439. // @router /excel_classify/edit [post]
  440. func (this *ExcelClassifyController) EditExcelClassify() {
  441. br := new(models.BaseResponse).Init()
  442. defer func() {
  443. this.Data["json"] = br
  444. this.ServeJSON()
  445. }()
  446. var req request.EditExcelClassifyReq
  447. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  448. if err != nil {
  449. br.Msg = "参数解析异常!"
  450. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  451. return
  452. }
  453. if req.ExcelClassifyName == "" {
  454. br.Msg = "请输入分类名称"
  455. br.IsSendEmail = false
  456. return
  457. }
  458. if req.ExcelClassifyId <= 0 {
  459. br.Msg = "参数错误"
  460. br.IsSendEmail = false
  461. return
  462. }
  463. item, err := excel.GetExcelClassifyById(req.ExcelClassifyId)
  464. if err != nil {
  465. br.Msg = "保存失败"
  466. br.Msg = "获取分类信息失败,Err:" + err.Error()
  467. return
  468. }
  469. // 名字一致的话,直接返回成功
  470. if item.ExcelClassifyName == req.ExcelClassifyName {
  471. br.Ret = 200
  472. br.Msg = "保存成功"
  473. br.Success = true
  474. return
  475. }
  476. // 获取同级分类下存在同名分类的数量
  477. count, err := excel.GetExcelClassifyCount(req.ExcelClassifyName, item.ParentId, item.Source)
  478. if err != nil {
  479. br.Msg = "判断名称是否已存在失败"
  480. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  481. return
  482. }
  483. if count > 0 {
  484. br.Msg = "分类名称已存在,请重新输入"
  485. br.IsSendEmail = false
  486. return
  487. }
  488. // 修改分类名称
  489. item.ExcelClassifyName = req.ExcelClassifyName
  490. err = item.Update([]string{"ExcelClassifyName"})
  491. if err != nil {
  492. br.Msg = "保存失败"
  493. br.ErrMsg = "保存失败,Err:" + err.Error()
  494. return
  495. }
  496. br.Ret = 200
  497. br.Msg = "修改成功"
  498. br.Success = true
  499. br.IsAddLog = true
  500. }
  501. // DeleteExcelClassifyCheck
  502. // @Title 删除表格检测接口
  503. // @Description 删除表格检测接口
  504. // @Param request body request.ExcelClassifyDeleteCheckReq true "type json string"
  505. // @Success 200 {object} response.ExcelClassifyDeleteCheckResp
  506. // @router /excel_classify/delete/check [post]
  507. func (this *ExcelClassifyController) DeleteExcelClassifyCheck() {
  508. br := new(models.BaseResponse).Init()
  509. defer func() {
  510. this.Data["json"] = br
  511. this.ServeJSON()
  512. }()
  513. var req request.ExcelClassifyDeleteCheckReq
  514. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  515. if err != nil {
  516. br.Msg = "参数解析异常!"
  517. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  518. return
  519. }
  520. if req.ExcelClassifyId < 0 && req.ExcelInfoId <= 0 {
  521. br.Msg = "参数错误"
  522. br.IsSendEmail = false
  523. return
  524. }
  525. var deleteStatus int
  526. var tipsMsg string
  527. // 校验是否存在该分类
  528. ExcelClassifyInfo, err := excel.GetExcelClassifyById(req.ExcelClassifyId)
  529. if err != nil {
  530. if err.Error() == utils.ErrNoRow() {
  531. br.Msg = "该分类不存在"
  532. br.ErrMsg = "该分类不存在"
  533. return
  534. }
  535. br.Msg = "删除失败"
  536. br.ErrMsg = "查询该分类失败,Err:" + err.Error()
  537. return
  538. }
  539. if ExcelClassifyInfo == nil {
  540. br.Msg = "该分类不存在"
  541. br.ErrMsg = "该分类不存在"
  542. br.IsSendEmail = false
  543. return
  544. }
  545. //删除分类
  546. if req.ExcelClassifyId > 0 && req.ExcelInfoId == 0 {
  547. //判断表格分类下,是否含有表格
  548. count, err := excel.GetExcelInfoCountByClassifyId(req.ExcelClassifyId)
  549. if err != nil {
  550. br.Msg = "删除失败"
  551. br.ErrMsg = "分类下是否含有指标失败,Err:" + err.Error()
  552. return
  553. }
  554. if count > 0 {
  555. deleteStatus = 1
  556. tipsMsg = "该分类下关联表格不可删除"
  557. } else {
  558. childClassify, e := excel.GetChildClassifyById(req.ExcelClassifyId)
  559. if e != nil && e.Error() != utils.ErrNoRow() {
  560. br.Msg = "获取失败"
  561. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  562. return
  563. }
  564. if len(childClassify) > 0 {
  565. var classifyIds []int
  566. for _, v := range childClassify {
  567. classifyIds = append(classifyIds, v.ExcelClassifyId)
  568. }
  569. condition := fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  570. var pars []interface{}
  571. pars = append(pars, classifyIds)
  572. childCount, err := excel.GetExcelInfoCountByCondition(condition, pars)
  573. if err != nil && err.Error() != utils.ErrNoRow() {
  574. br.Msg = "删除失败"
  575. br.ErrMsg = "查询分类下表格数量失败,Err:" + err.Error()
  576. return
  577. }
  578. if childCount > 0 {
  579. deleteStatus = 1
  580. tipsMsg = "该分类下关联表格不可删除"
  581. }
  582. }
  583. }
  584. }
  585. //if deleteStatus != 1 && req.ExcelInfoId == 0 {
  586. // classifyCount, err := data_manage.GetExcelClassifyCountByClassifyId(req.ExcelClassifyId)
  587. // if err != nil && err.Error() != utils.ErrNoRow() {
  588. // br.Msg = "删除失败"
  589. // br.ErrMsg = "分类下是否含有表格失败,Err:" + err.Error()
  590. // return
  591. // }
  592. // if classifyCount > 0 {
  593. // deleteStatus = 2
  594. // tipsMsg = "确认删除当前目录及包含的子目录吗?"
  595. // }
  596. //}
  597. if deleteStatus == 0 {
  598. tipsMsg = "可删除,进行删除操作"
  599. }
  600. resp := response2.ExcelClassifyDeleteCheckResp{
  601. DeleteStatus: deleteStatus,
  602. TipsMsg: tipsMsg,
  603. }
  604. br.Ret = 200
  605. br.Msg = "检测成功"
  606. br.Success = true
  607. br.Data = resp
  608. }
  609. // DeleteExcelClassify
  610. // @Title 删除表格分类/表格
  611. // @Description 删除表格分类/表格接口
  612. // @Param request body request.DeleteExcelClassifyReq true "type json string"
  613. // @Success 200 Ret=200 删除成功
  614. // @router /excel_classify/delete [post]
  615. func (this *ExcelClassifyController) DeleteExcelClassify() {
  616. br := new(models.BaseResponse).Init()
  617. defer func() {
  618. this.Data["json"] = br
  619. this.ServeJSON()
  620. }()
  621. sysUser := this.SysUser
  622. if sysUser == nil {
  623. br.Msg = "请登录"
  624. br.ErrMsg = "请登录,SysUser Is Empty"
  625. br.Ret = 408
  626. return
  627. }
  628. var req request.DeleteExcelClassifyReq
  629. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  630. if err != nil {
  631. br.Msg = "参数解析异常!"
  632. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  633. return
  634. }
  635. if req.ExcelClassifyId < 0 && req.ExcelInfoId <= 0 {
  636. br.Msg = "参数错误"
  637. br.IsSendEmail = false
  638. return
  639. }
  640. //删除分类
  641. if req.ExcelClassifyId > 0 && req.ExcelInfoId == 0 {
  642. //判断是否含有指标
  643. count, err := excel.GetExcelInfoCountByClassifyId(req.ExcelClassifyId)
  644. if err != nil && err.Error() != utils.ErrNoRow() {
  645. br.Msg = "删除失败"
  646. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  647. return
  648. }
  649. if count > 0 {
  650. br.Msg = "该目录下存在关联ETA表格,不可删除"
  651. br.IsSendEmail = false
  652. return
  653. } else {
  654. childClassify, e := excel.GetChildClassifyById(req.ExcelClassifyId)
  655. if e != nil && e.Error() != utils.ErrNoRow() {
  656. br.Msg = "获取失败"
  657. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  658. return
  659. }
  660. if len(childClassify) > 0 {
  661. var classifyIds []int
  662. for _, v := range childClassify {
  663. classifyIds = append(classifyIds, v.ExcelClassifyId)
  664. }
  665. condition := fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  666. var pars []interface{}
  667. pars = append(pars, classifyIds)
  668. childCount, err := excel.GetExcelInfoCountByCondition(condition, pars)
  669. if err != nil && err.Error() != utils.ErrNoRow() {
  670. br.Msg = "删除失败"
  671. br.ErrMsg = "查询分类下表格数量失败,Err:" + err.Error()
  672. return
  673. }
  674. if childCount > 0 {
  675. br.Msg = "该目录下存在关联ETA表格,不可删除"
  676. br.IsSendEmail = false
  677. return
  678. }
  679. }
  680. }
  681. classifyItem, err := excel.GetExcelClassifyById(req.ExcelClassifyId)
  682. if err != nil {
  683. br.Msg = "删除失败"
  684. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  685. if err.Error() == utils.ErrNoRow() {
  686. br.Msg = "分类不存在"
  687. br.ErrMsg = "分类不存在"
  688. }
  689. return
  690. }
  691. classifyItem.IsDelete = 1
  692. err = classifyItem.Update([]string{"IsDelete"})
  693. if err != nil {
  694. br.Msg = "删除失败"
  695. br.ErrMsg = "删除失败,Err:" + err.Error()
  696. return
  697. }
  698. }
  699. resp := response2.AddExcelInfoResp{}
  700. //删除表格
  701. if req.ExcelInfoId > 0 {
  702. excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
  703. if err != nil {
  704. if err.Error() == utils.ErrNoRow() {
  705. br.Msg = "表格已删除,请刷新页面"
  706. br.ErrMsg = "表格已删除,请刷新页面"
  707. return
  708. } else {
  709. br.Msg = "删除失败"
  710. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  711. return
  712. }
  713. }
  714. if excelInfo == nil {
  715. br.Msg = "表格已删除,请刷新页面"
  716. br.IsSendEmail = false
  717. return
  718. }
  719. // 删除excel
  720. err, errMsg, isSendEmail := excel2.Delete(excelInfo, sysUser)
  721. if err != nil {
  722. br.Msg = "删除失败"
  723. if errMsg != `` {
  724. br.Msg = errMsg
  725. }
  726. br.ErrMsg = "删除失败,Err:" + err.Error()
  727. br.IsSendEmail = isSendEmail
  728. return
  729. }
  730. // 返回下一个表格的信息
  731. {
  732. var nextItem *excel.ExcelInfo
  733. var condition string
  734. var pars []interface{}
  735. condition += " AND excel_classify_id=? "
  736. pars = append(pars, excelInfo.ExcelClassifyId)
  737. if excelInfo.Source == utils.BALANCE_TABLE {
  738. condition += " AND parent_id=0 AND balance_type=0 "
  739. }
  740. condition += " AND (sort>? OR (sort=? AND excel_info_id<?) ) "
  741. pars = append(pars, excelInfo.Sort, excelInfo.Sort, excelInfo.ExcelInfoId)
  742. nextItem, err = excel.GetNextExcelInfoByCondition(condition, pars)
  743. if err != nil && err.Error() != utils.ErrNoRow() {
  744. br.Msg = "删除失败"
  745. br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  746. return
  747. }
  748. // 如果没找到,那么查找下一个分类的第一个表格
  749. if nextItem == nil {
  750. currClassifyInfo, err := excel.GetExcelClassifyById(excelInfo.ExcelClassifyId)
  751. if err != nil && err.Error() != utils.ErrNoRow() {
  752. br.Msg = "删除失败"
  753. br.ErrMsg = "获取当前表格分类信息失败,Err:" + err.Error()
  754. return
  755. }
  756. nextItem, err = excel.GetNextExcelInfo(excelInfo.ExcelClassifyId, currClassifyInfo.Sort, currClassifyInfo.Source)
  757. if err != nil && err.Error() != utils.ErrNoRow() {
  758. br.Msg = "删除失败"
  759. br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  760. return
  761. }
  762. }
  763. // 如果找到下一个表格了,那么就返回
  764. if nextItem != nil {
  765. resp = response2.AddExcelInfoResp{
  766. ExcelInfoId: nextItem.ExcelInfoId,
  767. UniqueCode: nextItem.UniqueCode,
  768. }
  769. }
  770. }
  771. //新增操作日志
  772. //{
  773. // ExcelLog := new(data_manage.ExcelInfoLog)
  774. // ExcelLog.ExcelName = ExcelInfo.ExcelName
  775. // ExcelLog.ExcelInfoId = req.ExcelInfoId
  776. // ExcelLog.ExcelClassifyId = ExcelInfo.ExcelClassifyId
  777. // ExcelLog.SysUserId = sysUser.AdminId
  778. // ExcelLog.SysUserRealName = sysUser.RealName
  779. // ExcelLog.UniqueCode = ExcelInfo.UniqueCode
  780. // ExcelLog.CreateTime = time.Now()
  781. // ExcelLog.Content = string(this.Ctx.Input.RequestBody)
  782. // ExcelLog.Status = "删除表格"
  783. // ExcelLog.Method = this.Ctx.Input.URI()
  784. // go data_manage.AddExcelInfoLog(ExcelLog)
  785. //}
  786. }
  787. br.Ret = 200
  788. br.Msg = "删除成功"
  789. br.Success = true
  790. br.Data = resp
  791. br.IsAddLog = true
  792. }
  793. // ExcelClassifyMove
  794. // @Title 表格分类移动接口
  795. // @Description 表格分类移动接口
  796. // @Success 200 {object} request.MoveExcelClassifyReq
  797. // @router /excel_classify/move [post]
  798. func (this *ExcelClassifyController) ExcelClassifyMove() {
  799. br := new(models.BaseResponse).Init()
  800. defer func() {
  801. this.Data["json"] = br
  802. this.ServeJSON()
  803. }()
  804. sysUser := this.SysUser
  805. if sysUser == nil {
  806. br.Msg = "请登录"
  807. br.ErrMsg = "请登录,SysUser Is Empty"
  808. br.Ret = 408
  809. return
  810. }
  811. var req request.MoveExcelClassifyReq
  812. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  813. if err != nil {
  814. br.Msg = "参数解析异常!"
  815. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  816. return
  817. }
  818. if req.ClassifyId <= 0 && req.ExcelInfoId <= 0 {
  819. br.Msg = "参数错误"
  820. br.ErrMsg = "请选择拖动目标,分类目录或者指标"
  821. return
  822. }
  823. //判断分类是否存在
  824. err, errMsg := excel2.MoveExcelClassify(req)
  825. if err != nil {
  826. br.Msg = err.Error()
  827. br.ErrMsg = errMsg
  828. return
  829. }
  830. br.Ret = 200
  831. br.Success = true
  832. br.IsAddLog = true
  833. br.Msg = "移动成功"
  834. }