department.go 7.1 KB

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