organization.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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-system-management-gateway/consts"
  6. "cp-system-management-gateway/errors"
  7. param_v1 "cp-system-management-gateway/param/v1"
  8. "cp-system-management-gateway/pb"
  9. "cp-system-management-gateway/pb/v1"
  10. "cp-system-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. //
  19. // @Summary 创建机构
  20. // @Description 创建机构
  21. // @Tags 机构
  22. // @Accept json
  23. // @Produce json
  24. // @Param token header string true "token"
  25. // @Param body body v1.CreateOrganizationBody true "机构信息"
  26. // @Success 200 {object} v1.CreateOrganizationResponse
  27. // @Failure 500 {object} base.HTTPError
  28. // @Router /api/v1/organization [post]
  29. func (c *Controller) CreateOrganization(ctx *gin.Context) {
  30. // 解析参数
  31. req := &param_v1.CreateOrganizationRequest{}
  32. parseParamTask := func() error {
  33. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.CreateOrganizationBody)
  34. if err != nil {
  35. logger.Error("func",
  36. zap.String("call", "util.ShouldBind"),
  37. zap.String("error", err.Error()))
  38. return errors.ParamsError
  39. }
  40. return nil
  41. }
  42. // 业务处理
  43. handleServiceTask := func() error {
  44. // 响应数据
  45. resp := param_v1.CreateOrganizationResponse{}
  46. rpcReq := &v1.CreateOrganizationRequest{
  47. Month:req.Month,
  48. OrganizationName:req.OrganizationName,
  49. IsDisable:req.IsDisable,
  50. }
  51. rpcRsp, err := pb.System.CreateOrganization(ctx, rpcReq)
  52. if err != nil {
  53. s, _ := json.MarshalToString(req)
  54. logger.Error("func",
  55. zap.String("call", "pb.System.CreateOrganization"),
  56. zap.String("params", s),
  57. zap.String("error", err.Error()))
  58. return errors.ErrorTransForm(err)
  59. }
  60. resp.Data.OrganizationCode = rpcRsp.OrganizationCode
  61. ctx.JSON(http.StatusOK, resp)
  62. // 操作日志
  63. loginUid, userName, _ := utils.GetJwtIdFromCtx(ctx)
  64. logReq := OperationLogRequest{
  65. Module:consts.OperationModuleOrganization,
  66. Action:consts.OperationActionOrganizationAdd,
  67. Origin:nil,
  68. Target:req.CreateOrganizationBody,
  69. UserName:userName,
  70. Uid:loginUid,
  71. }
  72. OperationLogAdd(&logReq)
  73. return nil
  74. }
  75. // 执行任务
  76. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  77. }
  78. //
  79. // @Summary 修改机构
  80. // @Description 修改机构
  81. // @Tags 机构
  82. // @Accept json
  83. // @Produce json
  84. // @Param token header string true "token"
  85. // @Param body body v1.UpdateOrganizationBody true "机构信息"
  86. // @Success 200 {object} v1.UpdateOrganizationResponse
  87. // @Failure 500 {object} base.HTTPError
  88. // @Router /api/v1/organization [put]
  89. func (c *Controller) UpdateOrganization(ctx *gin.Context) {
  90. // 解析参数
  91. req := &param_v1.UpdateOrganizationRequest{}
  92. parseParamTask := func() error {
  93. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UpdateOrganizationBody)
  94. if err != nil {
  95. logger.Error("func",
  96. zap.String("call", "util.ShouldBind"),
  97. zap.String("error", err.Error()))
  98. return errors.ParamsError
  99. }
  100. return nil
  101. }
  102. // 业务处理
  103. handleServiceTask := func() error {
  104. // 响应数据
  105. resp := param_v1.UpdateOrganizationResponse{}
  106. rpcReq := &v1.UpdateOrganizationRequest{
  107. Month:req.Month,
  108. OrganizationName:req.OrganizationName,
  109. IsDisable:req.IsDisable,
  110. OrganizationCode:req.OrganizationCode,
  111. }
  112. origin, err := pb.System.UpdateOrganization(ctx, rpcReq)
  113. if err != nil {
  114. s, _ := json.MarshalToString(req)
  115. logger.Error("func",
  116. zap.String("call", "pb.System.UpdateOrganization"),
  117. zap.String("params", s),
  118. zap.String("error", err.Error()))
  119. return errors.ErrorTransForm(err)
  120. }
  121. ctx.JSON(http.StatusOK, resp)
  122. // 操作日志
  123. loginUid, userName, _ := utils.GetJwtIdFromCtx(ctx)
  124. logReq := OperationLogRequest{
  125. Module:consts.OperationModuleOrganization,
  126. Action:consts.OperationActionOrganizationUpdate,
  127. Origin:origin.Origin,
  128. Target:req.UpdateOrganizationBody,
  129. UserName:userName,
  130. Uid:loginUid,
  131. }
  132. OperationLogAdd(&logReq)
  133. return nil
  134. }
  135. // 执行任务
  136. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  137. }
  138. //
  139. // @Summary 获取机构列表
  140. // @Description 获取机构列表
  141. // @Tags 机构
  142. // @Accept json
  143. // @Produce json
  144. // @Param token header string true "token"
  145. // @Param filter query string false "过滤"
  146. // @Param page query int64 false " "
  147. // @Param page_size query int64 false " "
  148. // @Success 200 {object} v1.OrganizationListResponse
  149. // @Failure 500 {object} base.HTTPError
  150. // @Router /api/v1/organization/list [get]
  151. func (c *Controller) OrganizationList(ctx *gin.Context) {
  152. // 解析参数
  153. req := &param_v1.OrganizationListRequest{}
  154. parseParamTask := func() error {
  155. err := util.ShouldBind(ctx, &req.Header, nil, &req.OrganizationListQuery, nil)
  156. if err != nil {
  157. logger.Error("func",
  158. zap.String("call", "util.ShouldBind"),
  159. zap.String("error", err.Error()))
  160. return errors.ParamsError
  161. }
  162. return nil
  163. }
  164. // 业务处理
  165. handleServiceTask := func() error {
  166. // 响应数据
  167. resp := param_v1.OrganizationListResponse{}
  168. rpcReq := &v1.OrganizationListRequest{
  169. Page:req.Page,
  170. PageSize:req.PageSize,
  171. Filter:req.Filter,
  172. }
  173. rpcRsp, err := pb.System.OrganizationList(ctx, rpcReq)
  174. if err != nil {
  175. s, _ := json.MarshalToString(req)
  176. logger.Error("func",
  177. zap.String("call", "pb.System.OrganizationList"),
  178. zap.String("params", s),
  179. zap.String("error", err.Error()))
  180. return errors.ErrorTransForm(err)
  181. }
  182. if rpcRsp.List == nil {
  183. rpcRsp.List = make([]*v1.Organization, 0)
  184. }
  185. resp.Data = *rpcRsp
  186. ctx.JSON(http.StatusOK, resp)
  187. return nil
  188. }
  189. // 执行任务
  190. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  191. }
  192. //
  193. // @Summary 创建机构超级管理员
  194. // @Description 创建机构超级管理员
  195. // @Tags 机构
  196. // @Accept json
  197. // @Produce json
  198. // @Param token header string true "token"
  199. // @Param body body v1.CreateOrganizationUserBody true "机构信息"
  200. // @Success 200 {object} v1.CreateOrganizationUserResponse
  201. // @Failure 500 {object} base.HTTPError
  202. // @Router /api/v1/organization/user [post]
  203. func (c *Controller) CreateOrganizationUser(ctx *gin.Context) {
  204. // 解析参数
  205. req := &param_v1.CreateOrganizationUserRequest{}
  206. parseParamTask := func() error {
  207. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.CreateOrganizationUserBody)
  208. if err != nil {
  209. logger.Error("func",
  210. zap.String("call", "util.ShouldBind"),
  211. zap.String("error", err.Error()))
  212. return errors.ParamsError
  213. }
  214. return nil
  215. }
  216. // 业务处理
  217. handleServiceTask := func() error {
  218. // 响应数据
  219. resp := param_v1.CreateOrganizationUserResponse{}
  220. rpcReq := &v1.CreateOrganizationUserRequest{
  221. OrganizationCode:req.OrganizationCode,
  222. Username:req.Username,
  223. Password:req.Password,
  224. Email:req.Email,
  225. Phone:req.Phone,
  226. }
  227. rpcRsp, err := pb.System.CreateOrganizationUser(ctx, rpcReq)
  228. if err != nil {
  229. s, _ := json.MarshalToString(req)
  230. logger.Error("func",
  231. zap.String("call", "pb.System.CreateOrganizationUser"),
  232. zap.String("params", s),
  233. zap.String("error", err.Error()))
  234. return errors.ErrorTransForm(err)
  235. }
  236. resp.Data.Uid = rpcRsp.Uid
  237. ctx.JSON(http.StatusOK, resp)
  238. // 操作日志
  239. loginUid, userName, _ := utils.GetJwtIdFromCtx(ctx)
  240. logReq := OperationLogRequest{
  241. Module:consts.OperationModuleOrganization,
  242. Action:consts.OperationActionOrganizationUserAdd,
  243. Origin:nil,
  244. Target:req.CreateOrganizationUserBody,
  245. UserName:userName,
  246. Uid:loginUid,
  247. }
  248. OperationLogAdd(&logReq)
  249. return nil
  250. }
  251. // 执行任务
  252. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  253. }
  254. //
  255. // @Summary 修改机构超级管理员
  256. // @Description 修改机构超级管理员
  257. // @Tags 机构
  258. // @Accept json
  259. // @Produce json
  260. // @Param token header string true "token"
  261. // @Param body body v1.OrganizationUserUpdateBody true " "
  262. // @Success 200 {object} v1.OrganizationUserUpdateResponse
  263. // @Failure 500 {object} base.HTTPError
  264. // @Router /api/v1/organization/user [put]
  265. func (c *Controller) OrganizationUserUpdate(ctx *gin.Context) {
  266. // 解析参数
  267. req := &param_v1.OrganizationUserUpdateRequest{}
  268. parseParamTask := func() error {
  269. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.OrganizationUserUpdateBody)
  270. if err != nil {
  271. logger.Error("func",
  272. zap.String("call", "util.ShouldBind"),
  273. zap.String("error", err.Error()))
  274. return errors.ParamsError
  275. }
  276. return nil
  277. }
  278. // 业务处理
  279. handleServiceTask := func() error {
  280. // 响应数据
  281. resp := param_v1.OrganizationUserUpdateResponse{}
  282. rpcReq := &v1.OrganizationUserUpdateRequest{
  283. OrganizationCode:req.OrganizationCode,
  284. Username:req.Username,
  285. Email:req.Email,
  286. Phone:req.Phone,
  287. Id:req.Id,
  288. Password:req.Password,
  289. }
  290. origin, err := pb.System.OrganizationUserUpdate(ctx, rpcReq)
  291. if err != nil {
  292. s, _ := json.MarshalToString(req)
  293. logger.Error("func",
  294. zap.String("call", "pb.System.OrganizationUserUpdate"),
  295. zap.String("params", s),
  296. zap.String("error", err.Error()))
  297. return errors.ErrorTransForm(err)
  298. }
  299. ctx.JSON(http.StatusOK, resp)
  300. // 操作日志
  301. loginUid, userName, _ := utils.GetJwtIdFromCtx(ctx)
  302. origin.Origin.Password = "******"
  303. req.OrganizationUserUpdateBody.Password = "******"
  304. logReq := OperationLogRequest{
  305. Module:consts.OperationModuleOrganization,
  306. Action:consts.OperationActionOrganizationUserUpdate,
  307. Origin:origin.Origin,
  308. Target:req.OrganizationUserUpdateBody,
  309. UserName:userName,
  310. Uid:loginUid,
  311. }
  312. OperationLogAdd(&logReq)
  313. return nil
  314. }
  315. // 执行任务
  316. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  317. }
  318. /*
  319. //
  320. // @Summary 修改机构超级管理员密码
  321. // @Description 修改机构超级管理员密码
  322. // @Tags 机构
  323. // @Accept json
  324. // @Produce json
  325. // @Param token header string true "token"
  326. // @Param body body v1.OrganizationUserResetPasswordBody true " "
  327. // @Success 200 {object} v1.OrganizationUserResetPasswordResponse
  328. // @Failure 500 {object} base.HTTPError
  329. // @Router /api/v1/organization/user_password [put]
  330. func (c *Controller) OrganizationUserResetPassword(ctx *gin.Context) {
  331. // 解析参数
  332. req := &param_v1.OrganizationUserResetPasswordRequest{}
  333. parseParamTask := func() error {
  334. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.OrganizationUserResetPasswordBody)
  335. if err != nil {
  336. logger.Error("func",
  337. zap.String("call", "util.ShouldBind"),
  338. zap.String("error", err.Error()))
  339. return errors.ParamsError
  340. }
  341. return nil
  342. }
  343. // 业务处理
  344. handleServiceTask := func() error {
  345. // 响应数据
  346. resp := param_v1.OrganizationUserResetPasswordResponse{}
  347. rpcReq := &v1.OrganizationUserResetPasswordRequest{
  348. OrganizationCode:req.OrganizationCode,
  349. Id:req.Id,
  350. Password:req.Password,
  351. }
  352. _, err := pb.System.OrganizationUserResetPassword(ctx, rpcReq)
  353. if err != nil {
  354. s, _ := json.MarshalToString(req)
  355. logger.Error("func",
  356. zap.String("call", "pb.System.OrganizationUserResetPassword"),
  357. zap.String("params", s),
  358. zap.String("error", err.Error()))
  359. return errors.ErrorTransForm(err)
  360. }
  361. ctx.JSON(http.StatusOK, resp)
  362. loginUid, userName, _ := utils.GetJwtIdFromCtx(ctx)
  363. req.Password = "******"
  364. logReq := OperationLogRequest{
  365. Module:consts.OperationModuleOrganization,
  366. Action:consts.OperationActionOrganizationUserResetPassword,
  367. Origin:nil,
  368. Target:req.OrganizationUserResetPasswordBody,
  369. UserName:userName,
  370. Uid:loginUid,
  371. }
  372. OperationLogAdd(&logReq)
  373. return nil
  374. }
  375. // 执行任务
  376. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  377. }
  378. */
  379. //
  380. // @Summary 获取机构超级管理员列表
  381. // @Description 获取机构超级管理员列表
  382. // @Tags 机构
  383. // @Accept json
  384. // @Produce json
  385. // @Param token header string true "token"
  386. // @Param page query int64 false " "
  387. // @Param page_size query int64 false " "
  388. // @Param organization_code query string true "机构代码"
  389. // @Success 200 {object} v1.OrganizationUserListResponse
  390. // @Failure 500 {object} base.HTTPError
  391. // @Router /api/v1/organization/user_list [get]
  392. func (c *Controller) OrganizationUserList(ctx *gin.Context) {
  393. // 解析参数
  394. req := &param_v1.OrganizationUserListRequest{}
  395. parseParamTask := func() error {
  396. err := util.ShouldBind(ctx, &req.Header, nil, &req.OrganizationUserListQuery, nil)
  397. if err != nil {
  398. logger.Error("func",
  399. zap.String("call", "util.ShouldBind"),
  400. zap.String("error", err.Error()))
  401. return errors.ParamsError
  402. }
  403. return nil
  404. }
  405. // 业务处理
  406. handleServiceTask := func() error {
  407. // 响应数据
  408. resp := param_v1.OrganizationUserListResponse{}
  409. rpcReq := &v1.OrganizationUserListRequest{
  410. Page:req.Page,
  411. PageSize:req.PageSize,
  412. OrganizationCode:req.OrganizationCode,
  413. }
  414. rpcRsp, err := pb.System.OrganizationUserList(ctx, rpcReq)
  415. if err != nil {
  416. s, _ := json.MarshalToString(req)
  417. logger.Error("func",
  418. zap.String("call", "pb.System.OrganizationUserList"),
  419. zap.String("params", s),
  420. zap.String("error", err.Error()))
  421. return errors.ErrorTransForm(err)
  422. }
  423. if rpcRsp.List == nil {
  424. rpcRsp.List = make([]*v1.OrganizationUserItem, 0)
  425. }
  426. resp.Data = *rpcRsp
  427. ctx.JSON(http.StatusOK, resp)
  428. return nil
  429. }
  430. // 执行任务
  431. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  432. }