sys_post.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package controller
  2. import (
  3. "Medical_OAuth/app/admin/model"
  4. "Medical_OAuth/app/admin/service"
  5. "Medical_OAuth/app/admin/service/dto"
  6. "Medical_OAuth/common/actions"
  7. "github.com/gin-gonic/gin"
  8. "github.com/gin-gonic/gin/binding"
  9. "gogs.baozhida.cn/zoie/OAuth-core/api"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  11. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  12. )
  13. type SysPostController struct {
  14. api.Api
  15. }
  16. // GetPage 获取岗位列表
  17. // @Summary 获取岗位列表
  18. // @Description 获取岗位列表
  19. // @Tags 岗位
  20. // @Param postName query string false "岗位名称"
  21. // @Param status query int false "状态 1-停用 2-正常"
  22. // @Param sort query int false "排序 ASC-升序 DESC-降序"
  23. // @Param pageSize query int false "页条数"
  24. // @Param pageIndex query int false "页码"
  25. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysPost}} "{"code": 200, "data": [...]}"
  26. // @Router /api/post [get]
  27. // @Security Bearer
  28. func (e SysPostController) GetPage(c *gin.Context) {
  29. s := service.SysPost{}
  30. req := dto.SysPostPageReq{}
  31. err := e.MakeContext(c).
  32. MakeOrm().
  33. Bind(&req, binding.Form).
  34. MakeService(&s.Service).
  35. Errors
  36. if err != nil {
  37. e.Logger.Error(err)
  38. e.Error(500, err, err.Error())
  39. return
  40. }
  41. list := make([]model.SysPost, 0)
  42. var count int64
  43. //数据权限检查
  44. p := actions.GetPermissionFromContext(c)
  45. err = s.GetPage(&req, &list, &count, p)
  46. if err != nil {
  47. e.Error(500, err, err.Error())
  48. return
  49. }
  50. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  51. }
  52. // Get 通过id获取岗位
  53. // @Summary 通过id获取岗位
  54. // @Description 通过id获取岗位
  55. // @Tags 岗位
  56. // @Param id path int true "岗位id"
  57. // @Success 200 {object} response.Response{data=model.SysPost} "{"code": 200, "data": [...]}"
  58. // @Router /api/post/{id} [get]
  59. // @Security Bearer
  60. func (e SysPostController) Get(c *gin.Context) {
  61. s := service.SysPost{}
  62. req := dto.SysPostGetReq{}
  63. err := e.MakeContext(c).
  64. MakeOrm().
  65. Bind(&req, nil).
  66. MakeService(&s.Service).
  67. Errors
  68. if err != nil {
  69. e.Logger.Error(err)
  70. e.Error(500, err, err.Error())
  71. return
  72. }
  73. var object model.SysPost
  74. //数据权限检查
  75. p := actions.GetPermissionFromContext(c)
  76. err = s.Get(&req, &object, p)
  77. if err != nil {
  78. e.Error(500, err, err.Error())
  79. return
  80. }
  81. e.OK(object, "查询成功")
  82. }
  83. // Insert 添加岗位
  84. // @Summary 添加岗位
  85. // @Description 添加岗位
  86. // @Tags 岗位
  87. // @Accept application/json
  88. // @Product application/json
  89. // @Param data body dto.SysPostInsertReq true "data"
  90. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  91. // @Router /api/post [post]
  92. // @Security Bearer
  93. func (e SysPostController) Insert(c *gin.Context) {
  94. s := service.SysPost{}
  95. req := dto.SysPostInsertReq{}
  96. err := e.MakeContext(c).
  97. MakeOrm().
  98. Bind(&req, binding.JSON).
  99. MakeService(&s.Service).
  100. Errors
  101. if err != nil {
  102. e.Logger.Error(err)
  103. e.Error(500, err, err.Error())
  104. return
  105. }
  106. req.SetCreateBy(user.GetUserId(c))
  107. err = s.Insert(&req)
  108. if err != nil {
  109. e.Error(500, err, err.Error())
  110. return
  111. }
  112. e.OK(req.GetId(), "创建成功")
  113. }
  114. // Update 修改岗位
  115. // @Summary 修改岗位
  116. // @Description 修改岗位
  117. // @Tags 岗位
  118. // @Accept application/json
  119. // @Product application/json
  120. // @Param id path int true "岗位id"
  121. // @Param data body dto.SysPostUpdateReq true "body"
  122. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  123. // @Router /api/post/{id} [put]
  124. // @Security Bearer
  125. func (e SysPostController) Update(c *gin.Context) {
  126. s := service.SysPost{}
  127. req := dto.SysPostUpdateReq{}
  128. err := e.MakeContext(c).
  129. MakeOrm().
  130. Bind(&req, binding.JSON, nil).
  131. MakeService(&s.Service).
  132. Errors
  133. if err != nil {
  134. e.Logger.Error(err)
  135. e.Error(500, err, err.Error())
  136. return
  137. }
  138. req.SetUpdateBy(user.GetUserId(c))
  139. err = s.Update(&req)
  140. if err != nil {
  141. e.Error(500, err, err.Error())
  142. return
  143. }
  144. e.OK(req.GetId(), "更新成功")
  145. }
  146. // Delete 删除岗位
  147. // @Summary 删除岗位
  148. // @Description 删除岗位
  149. // @Tags 岗位
  150. // @Accept application/json
  151. // @Product application/json
  152. // @Param id body dto.SysPostDeleteReq true "请求参数"
  153. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  154. // @Router /api/post [delete]
  155. // @Security Bearer
  156. func (e SysPostController) Delete(c *gin.Context) {
  157. s := service.SysPost{}
  158. req := dto.SysPostDeleteReq{}
  159. userSvc := service.SysUser{}
  160. err := e.MakeContext(c).
  161. MakeOrm().
  162. Bind(&req, binding.JSON).
  163. MakeService(&s.Service).
  164. MakeService(&userSvc.Service).
  165. Errors
  166. if err != nil {
  167. e.Logger.Error(err)
  168. e.Error(500, err, err.Error())
  169. return
  170. }
  171. userReq := dto.SysUserGetCountReq{PostIds: req.Ids}
  172. var count int64
  173. err = userSvc.GetCount(&userReq, &count)
  174. if err != nil {
  175. e.Error(500, err, err.Error())
  176. return
  177. }
  178. if count > 0 {
  179. e.Error(500, err, "有用户关联,禁止删除!")
  180. return
  181. }
  182. req.SetUpdateBy(user.GetUserId(c))
  183. err = s.Remove(&req)
  184. if err != nil {
  185. e.Error(500, err, err.Error())
  186. return
  187. }
  188. e.OK(req.GetId(), "删除成功")
  189. }