dept.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package handlers
  2. import (
  3. "net/http"
  4. "easydo-echo_win7/middleware"
  5. "easydo-echo_win7/models"
  6. "easydo-echo_win7/services"
  7. "easydo-echo_win7/utils"
  8. "github.com/labstack/echo/v4"
  9. )
  10. func Add_dept_to_routes(e *echo.Echo) {
  11. group := e.Group("/sysDept")
  12. group.Use(middleware.AuthMiddleware)
  13. group.POST("/getList", deptGetList)
  14. group.POST("/save", deptSave)
  15. group.POST("/update", deptUpdate)
  16. group.POST("/remove", deptRemove)
  17. }
  18. func deptGetList(c echo.Context) error {
  19. var paramMap map[string]interface{}
  20. if err := c.Bind(&paramMap); err != nil {
  21. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  22. }
  23. result, err := services.JdbcClient.GetJdbcList(paramMap, models.SysDept{})
  24. if err != nil {
  25. utils.PrintSqlErr(err)
  26. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  27. }
  28. list := utils.ConvertInterface[[]models.SysDept](result)
  29. if len(list) == 0 {
  30. return c.JSON(http.StatusOK, []models.SysDept{})
  31. }
  32. return c.JSON(http.StatusOK, list)
  33. }
  34. func deptSave(c echo.Context) error {
  35. dept := new(models.SysDept)
  36. if err := c.Bind(dept); err != nil {
  37. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  38. }
  39. err := services.JdbcClient.JdbcInsert(dept)
  40. if err != nil {
  41. utils.PrintSqlErr(err)
  42. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  43. }
  44. return c.JSON(http.StatusOK, dept)
  45. }
  46. func deptUpdate(c echo.Context) error {
  47. dept := new(models.SysDept)
  48. if err := c.Bind(dept); err != nil {
  49. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  50. }
  51. err := services.JdbcClient.JdbcUpdateById(dept)
  52. if err != nil {
  53. utils.PrintSqlErr(err)
  54. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  55. }
  56. return c.JSON(http.StatusOK, dept)
  57. }
  58. func deptRemove(c echo.Context) error {
  59. dept := new(models.SysDept)
  60. if err := c.Bind(dept); err != nil {
  61. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  62. }
  63. deptParam := new(models.SysDept)
  64. deptParam.Pid = dept.ID
  65. count, err := services.JdbcClient.GetJdbcCount(deptParam)
  66. if err != nil {
  67. utils.PrintSqlErr(err)
  68. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  69. }
  70. if count > 0 {
  71. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("存在下级部门,无法删除", ""))
  72. }
  73. err = services.JdbcClient.JdbcRemoveById(dept)
  74. if err != nil {
  75. utils.PrintSqlErr(err)
  76. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  77. }
  78. return c.JSON(http.StatusOK, dept)
  79. }