department.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package v1
  2. import (
  3. "git.getensh.com/common/gopkgs/logger"
  4. "git.getensh.com/common/gopkgs/tasker/httptasker"
  5. "git.getensh.com/common/gopkgs/util"
  6. "github.com/gin-gonic/gin"
  7. "go.uber.org/zap"
  8. "net/http"
  9. "property-applete-gateway/errors"
  10. param_v1 "property-applete-gateway/param/v1"
  11. "property-applete-gateway/pb"
  12. "property-applete-gateway/pb/v1"
  13. "property-applete-gateway/utils"
  14. )
  15. //
  16. // @Summary 添加部门
  17. // @Description 添加部门
  18. // @Tags 部门
  19. // @Accept json
  20. // @Produce json
  21. // @Param token header string true "token"
  22. // @Param body body v1.DepartmentAddBody true "信息"
  23. // @Success 200 {object} v1.DepartmentAddResponse
  24. // @Failure 500 {object} base.HTTPError
  25. // @Router /api/v1/department [post]
  26. func (c *Controller) DepartmentAdd(ctx *gin.Context) {
  27. // 解析参数
  28. req := &param_v1.DepartmentAddRequest{}
  29. parseParamTask := func() error {
  30. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.DepartmentAddBody)
  31. if err != nil {
  32. logger.Error("func",
  33. zap.String("call", "util.ShouldBind"),
  34. zap.String("error", err.Error()))
  35. return errors.ParamsError
  36. }
  37. return nil
  38. }
  39. // 业务处理
  40. handleServiceTask := func() error {
  41. tokenInfo, err := utils.GetSubjectValue(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. // 响应数据
  46. resp := param_v1.DepartmentAddResponse{}
  47. rpcReq := &v1.DepartmentAddRequest{
  48. GardenId:tokenInfo.GardenId,
  49. DepartmentName:req.DepartmentName,
  50. Desc:req.Desc,
  51. }
  52. rpcRsp, err := pb.System.DepartmentAdd(ctx, rpcReq)
  53. if err != nil {
  54. s, _ := json.MarshalToString(req)
  55. logger.Error("func",
  56. zap.String("call", "pb.System.DepartmentAdd"),
  57. zap.String("params", s),
  58. zap.String("error", err.Error()))
  59. return errors.ErrorTransForm(err)
  60. }
  61. resp.Data = *rpcRsp
  62. ctx.JSON(http.StatusOK, resp)
  63. logReq := OperationLogRequest{
  64. Module:ModuleDepartment,
  65. Action:ActionDepartmentAdd,
  66. Origin:nil,
  67. Target:req.DepartmentAddBody,
  68. UserName:tokenInfo.UserName,
  69. Uid:tokenInfo.Uid,
  70. Cid:tokenInfo.Cid,
  71. GardenId:tokenInfo.GardenId,
  72. }
  73. go OperationLogAdd(&logReq)
  74. return nil
  75. }
  76. // 执行任务
  77. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  78. }
  79. //
  80. // @Summary 更改部门
  81. // @Description 更改部门
  82. // @Tags 部门
  83. // @Accept json
  84. // @Produce json
  85. // @Param token header string true "token"
  86. // @Param body body v1.DepartmentUpdateBody true "信息"
  87. // @Success 200 {object} v1.DepartmentUpdateResponse
  88. // @Failure 500 {object} base.HTTPError
  89. // @Router /api/v1/department [put]
  90. func (c *Controller) DepartmentUpdate(ctx *gin.Context) {
  91. // 解析参数
  92. req := &param_v1.DepartmentUpdateRequest{}
  93. parseParamTask := func() error {
  94. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.DepartmentUpdateBody)
  95. if err != nil {
  96. logger.Error("func",
  97. zap.String("call", "util.ShouldBind"),
  98. zap.String("error", err.Error()))
  99. return errors.ParamsError
  100. }
  101. return nil
  102. }
  103. // 业务处理
  104. handleServiceTask := func() error {
  105. tokenInfo, err := utils.GetSubjectValue(ctx)
  106. if err != nil {
  107. return err
  108. }
  109. // 响应数据
  110. resp := param_v1.DepartmentUpdateResponse{}
  111. rpcReq := &v1.DepartmentUpdateRequest{
  112. GardenId:tokenInfo.GardenId,
  113. Id:req.Id,
  114. DepartmentName:req.DepartmentName,
  115. Desc:req.Desc,
  116. }
  117. rpcRsp, err := pb.System.DepartmentUpdate(ctx, rpcReq)
  118. if err != nil {
  119. s, _ := json.MarshalToString(req)
  120. logger.Error("func",
  121. zap.String("call", "pb.System.DepartmentUpdate"),
  122. zap.String("params", s),
  123. zap.String("error", err.Error()))
  124. return errors.ErrorTransForm(err)
  125. }
  126. ctx.JSON(http.StatusOK, resp)
  127. logReq := OperationLogRequest{
  128. Module:ModuleDepartment,
  129. Action:ActionDepartmentUpdate,
  130. Origin:rpcRsp.Origin,
  131. Target:req.DepartmentUpdateBody,
  132. UserName:tokenInfo.UserName,
  133. Uid:tokenInfo.Uid,
  134. Cid:tokenInfo.Cid,
  135. GardenId:tokenInfo.GardenId,
  136. }
  137. go OperationLogAdd(&logReq)
  138. return nil
  139. }
  140. // 执行任务
  141. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  142. }
  143. //
  144. // @Summary 删除部门
  145. // @Description 删除部门
  146. // @Tags 部门
  147. // @Accept json
  148. // @Produce json
  149. // @Param token header string true "token"
  150. // @Param id query int true "id"
  151. // @Success 200 {object} v1.DepartmentDelResponse
  152. // @Failure 500 {object} base.HTTPError
  153. // @Router /api/v1/department [delete]
  154. func (c *Controller) DepartmentDel(ctx *gin.Context) {
  155. // 解析参数
  156. req := &param_v1.DepartmentDelRequest{}
  157. parseParamTask := func() error {
  158. err := util.ShouldBind(ctx, &req.Header, nil, &req.DepartmentDelQuery, nil)
  159. if err != nil {
  160. logger.Error("func",
  161. zap.String("call", "util.ShouldBind"),
  162. zap.String("error", err.Error()))
  163. return errors.ParamsError
  164. }
  165. return nil
  166. }
  167. // 业务处理
  168. handleServiceTask := func() error {
  169. tokenInfo, err := utils.GetSubjectValue(ctx)
  170. if err != nil {
  171. return err
  172. }
  173. // 响应数据
  174. resp := param_v1.DepartmentDelResponse{}
  175. rpcReq := &v1.DepartmentDelRequest{
  176. GardenId:tokenInfo.GardenId,
  177. Id:req.Id,
  178. }
  179. rpcRsp, err := pb.System.DepartmentDel(ctx, rpcReq)
  180. if err != nil {
  181. s, _ := json.MarshalToString(req)
  182. logger.Error("func",
  183. zap.String("call", "pb.System.DepartmentDel"),
  184. zap.String("params", s),
  185. zap.String("error", err.Error()))
  186. return errors.ErrorTransForm(err)
  187. }
  188. ctx.JSON(http.StatusOK, resp)
  189. logReq := OperationLogRequest{
  190. Module:ModuleDepartment,
  191. Action:ActionDepartmentDel,
  192. Origin:rpcRsp.Origin,
  193. Target:nil,
  194. UserName:tokenInfo.UserName,
  195. Uid:tokenInfo.Uid,
  196. Cid:tokenInfo.Cid,
  197. GardenId:tokenInfo.GardenId,
  198. }
  199. go OperationLogAdd(&logReq)
  200. return nil
  201. }
  202. // 执行任务
  203. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  204. }
  205. //
  206. // @Summary 部门列表
  207. // @Description 部门列表
  208. // @Tags 部门
  209. // @Accept json
  210. // @Produce json
  211. // @Param page query int false " "
  212. // @Param page_size query int false " "
  213. // @Param token header string true "token"
  214. // @Success 200 {object} v1.DepartmentListResponse
  215. // @Failure 500 {object} base.HTTPError
  216. // @Router /api/v1/department [get]
  217. func (c *Controller) DepartmentList(ctx *gin.Context) {
  218. // 解析参数
  219. req := &param_v1.DepartmentListRequest{}
  220. parseParamTask := func() error {
  221. err := util.ShouldBind(ctx, &req.Header, nil, &req.DepartmentListQuery, nil)
  222. if err != nil {
  223. logger.Error("func",
  224. zap.String("call", "util.ShouldBind"),
  225. zap.String("error", err.Error()))
  226. return errors.ParamsError
  227. }
  228. return nil
  229. }
  230. // 业务处理
  231. handleServiceTask := func() error {
  232. tokenInfo, err := utils.GetSubjectValue(ctx)
  233. if err != nil {
  234. return err
  235. }
  236. // 响应数据
  237. resp := param_v1.DepartmentListResponse{}
  238. rpcReq := &v1.DepartmentListRequest{
  239. GardenId:tokenInfo.GardenId,
  240. }
  241. rpcRsp, err := pb.System.DepartmentList(ctx, rpcReq)
  242. if err != nil {
  243. s, _ := json.MarshalToString(req)
  244. logger.Error("func",
  245. zap.String("call", "pb.System.DepartmentList"),
  246. zap.String("params", s),
  247. zap.String("error", err.Error()))
  248. return errors.ErrorTransForm(err)
  249. }
  250. if rpcRsp.List == nil {
  251. rpcRsp.List = make([]*v1.DepartmentItem, 0)
  252. }
  253. resp.Data = *rpcRsp
  254. ctx.JSON(http.StatusOK, resp)
  255. return nil
  256. }
  257. // 执行任务
  258. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  259. }