zone.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "cp-organization-management-gateway/consts"
  6. "cp-organization-management-gateway/errors"
  7. param_v1 "cp-organization-management-gateway/param/v1"
  8. "cp-organization-management-gateway/pb"
  9. "cp-organization-management-gateway/pb/v1"
  10. "cp-organization-management-gateway/utils"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "github.com/jaryhe/gopkgs/tasker/httptasker"
  13. "github.com/jaryhe/gopkgs/util"
  14. "net/http"
  15. "github.com/gin-gonic/gin"
  16. "go.uber.org/zap"
  17. )
  18. func handleZoneList(list []*v1.ZoneItem) ([]*v1.ZoneItem) {
  19. if len(list) == 0 {
  20. list = make([]*v1.ZoneItem, 0)
  21. return list
  22. }
  23. for i, v := range list {
  24. list[i].Childs = handleZoneList(v.Childs)
  25. }
  26. return list
  27. }
  28. //
  29. // @Summary 区域列表
  30. // @Description 区域列表
  31. // @Tags 区域
  32. // @Accept json
  33. // @Produce json
  34. // @Param token header string true "token"
  35. // @Success 200 {object} v1.ZoneListResponse
  36. // @Failure 500 {object} base.HTTPError
  37. // @Router /api/v1/zone/list [get]
  38. func (c *Controller) ZoneList(ctx *gin.Context) {
  39. // 解析参数
  40. req := &param_v1.ZoneListRequest{}
  41. parseParamTask := func() error {
  42. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  43. if err != nil {
  44. logger.Error("func",
  45. zap.String("call", "util.ShouldBind"),
  46. zap.String("error", err.Error()))
  47. return errors.ParamsError
  48. }
  49. return nil
  50. }
  51. // 业务处理
  52. handleServiceTask := func() error {
  53. // 响应数据
  54. tokenInfo, err := utils.GetTokeInfo(ctx)
  55. if err != nil {
  56. return err
  57. }
  58. resp := param_v1.ZoneListResponse{}
  59. rpcReq := &v1.ZoneListRequest{
  60. OrganizationCode:tokenInfo.OrganizationCode,
  61. Uid:tokenInfo.Uid,
  62. }
  63. rpcRsp, err := pb.Organization.ZoneList(ctx, rpcReq)
  64. if err != nil {
  65. s, _ := json.MarshalToString(req)
  66. logger.Error("func",
  67. zap.String("call", "pb.Organization.ZoneList"),
  68. zap.String("params", s),
  69. zap.String("error", err.Error()))
  70. return errors.ErrorTransForm(err)
  71. }
  72. rpcRsp.List = handleZoneList(rpcRsp.List)
  73. resp.Data = *rpcRsp
  74. ctx.JSON(http.StatusOK, resp)
  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 zone_code path string true " "
  88. // @Success 200 {object} v1.ZoneDelResponse
  89. // @Failure 500 {object} base.HTTPError
  90. // @Router /api/v1/zone/{zone_code} [delete]
  91. func (c *Controller) ZoneDel(ctx *gin.Context) {
  92. // 解析参数
  93. req := &param_v1.ZoneDelRequest{}
  94. parseParamTask := func() error {
  95. err := util.ShouldBind(ctx, &req.Header, &req.ZoneDelPath, nil, nil)
  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. // 响应数据
  107. tokenInfo, err := utils.GetTokeInfo(ctx)
  108. if err != nil {
  109. return err
  110. }
  111. resp := param_v1.ZoneDelResponse{}
  112. rpcReq := &v1.ZoneDelRequest{
  113. OrganizationCode:tokenInfo.OrganizationCode,
  114. Uid:tokenInfo.Uid,
  115. ZoneCode:req.ZoneCode,
  116. }
  117. rpcRsp, err := pb.Organization.ZoneDel(ctx, rpcReq)
  118. if err != nil {
  119. s, _ := json.MarshalToString(req)
  120. logger.Error("func",
  121. zap.String("call", "pb.Organization.ZoneDel"),
  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:consts.OperationModuleZone,
  129. Action:consts.OperationActionZoneDel,
  130. Origin:rpcRsp.Zones,
  131. Target:req.ZoneDelPath,
  132. UserName:tokenInfo.Username,
  133. Uid:tokenInfo.Uid,
  134. OrganizationCode:tokenInfo.OrganizationCode,
  135. }
  136. OperationLogAdd(&logReq)
  137. return nil
  138. }
  139. // 执行任务
  140. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  141. }
  142. //
  143. // @Summary 添加区域
  144. // @Description 添加区域
  145. // @Tags 区域
  146. // @Accept json
  147. // @Produce json
  148. // @Param token header string true "token"
  149. // @Param body body v1.ZoneAddBody true " "
  150. // @Success 200 {object} v1.ZoneAddResponse
  151. // @Failure 500 {object} base.HTTPError
  152. // @Router /api/v1/zone [post]
  153. func (c *Controller) ZoneAdd(ctx *gin.Context) {
  154. // 解析参数
  155. req := &param_v1.ZoneAddRequest{}
  156. parseParamTask := func() error {
  157. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ZoneAddBody)
  158. if err != nil {
  159. logger.Error("func",
  160. zap.String("call", "util.ShouldBind"),
  161. zap.String("error", err.Error()))
  162. return errors.ParamsError
  163. }
  164. return nil
  165. }
  166. // 业务处理
  167. handleServiceTask := func() error {
  168. // 响应数据
  169. tokenInfo, err := utils.GetTokeInfo(ctx)
  170. if err != nil {
  171. return err
  172. }
  173. resp := param_v1.ZoneAddResponse{}
  174. rpcReq := &v1.ZoneAddRequest{
  175. OrganizationCode:tokenInfo.OrganizationCode,
  176. Uid:tokenInfo.Uid,
  177. ParentZoneCode:req.ParentZoneCode,
  178. ZoneName:req.ZoneName,
  179. }
  180. rpcRsp, err := pb.Organization.ZoneAdd(ctx, rpcReq)
  181. if err != nil {
  182. s, _ := json.MarshalToString(req)
  183. logger.Error("func",
  184. zap.String("call", "pb.Organization.ZoneAdd"),
  185. zap.String("params", s),
  186. zap.String("error", err.Error()))
  187. return errors.ErrorTransForm(err)
  188. }
  189. resp.Data = *rpcRsp
  190. ctx.JSON(http.StatusOK, resp)
  191. logReq := OperationLogRequest{
  192. Module:consts.OperationModuleZone,
  193. Action:consts.OperationActionZoneAdd,
  194. Origin:nil,
  195. Target:req.ZoneAddBody,
  196. UserName:tokenInfo.Username,
  197. Uid:tokenInfo.Uid,
  198. OrganizationCode:tokenInfo.OrganizationCode,
  199. }
  200. 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 token header string true "token"
  213. // @Param body body v1.ZoneUpdateBody true " "
  214. // @Success 200 {object} v1.ZoneUpdateResponse
  215. // @Failure 500 {object} base.HTTPError
  216. // @Router /api/v1/zone [put]
  217. func (c *Controller) ZoneUpdate(ctx *gin.Context) {
  218. // 解析参数
  219. req := &param_v1.ZoneUpdateRequest{}
  220. parseParamTask := func() error {
  221. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ZoneUpdateBody)
  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. // 响应数据
  233. tokenInfo, err := utils.GetTokeInfo(ctx)
  234. if err != nil {
  235. return err
  236. }
  237. resp := param_v1.ZoneUpdateResponse{}
  238. rpcReq := &v1.ZoneUpdateRequest{
  239. OrganizationCode:tokenInfo.OrganizationCode,
  240. Uid:tokenInfo.Uid,
  241. ZoneName:req.ZoneName,
  242. ZoneCode:req.ZoneCode,
  243. ParentZoneCode:req.ParentZoneCode,
  244. }
  245. rpcRsp, err := pb.Organization.ZoneUpdate(ctx, rpcReq)
  246. if err != nil {
  247. s, _ := json.MarshalToString(req)
  248. logger.Error("func",
  249. zap.String("call", "pb.Organization.ZoneUpdate"),
  250. zap.String("params", s),
  251. zap.String("error", err.Error()))
  252. return errors.ErrorTransForm(err)
  253. }
  254. ctx.JSON(http.StatusOK, resp)
  255. logReq := OperationLogRequest{
  256. Module:consts.OperationModuleZone,
  257. Action:consts.OperationActionZoneUpdate,
  258. Origin:rpcRsp.Origin,
  259. Target:rpcRsp.Target,
  260. UserName:tokenInfo.Username,
  261. Uid:tokenInfo.Uid,
  262. OrganizationCode:tokenInfo.OrganizationCode,
  263. }
  264. OperationLogAdd(&logReq)
  265. return nil
  266. }
  267. // 执行任务
  268. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  269. }