excel_classify.go 26 KB

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