project.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. "github.com/gin-gonic/gin"
  6. "github.com/jaryhe/gopkgs/logger"
  7. "github.com/jaryhe/gopkgs/tasker/httptasker"
  8. "github.com/jaryhe/gopkgs/util"
  9. "go.uber.org/zap"
  10. "net/http"
  11. "smart-enterprise-management-gateway/errors"
  12. param_v1 "smart-enterprise-management-gateway/param/v1"
  13. "smart-enterprise-management-gateway/pb"
  14. "smart-enterprise-management-gateway/pb/v1"
  15. "smart-enterprise-management-gateway/utils"
  16. "strconv"
  17. "strings"
  18. )
  19. // 项目列表
  20. // @Summary 项目列表
  21. // @Description 项目列表
  22. // @Tags project
  23. // @Accept json
  24. // @Produce json
  25. // @Param token header string true " "
  26. // @Param page query int true " "
  27. // @Param filter query string false " "
  28. // @Param page_size query int false "小于0时代表获取所有"
  29. // @Success 200 {object} v1.ProjectListResponse
  30. // @Failure 500 {object} base.HTTPError
  31. // @Router /api/v1/project/list [get]
  32. func (c *Controller) ProjectList(ctx *gin.Context) {
  33. // 解析参数
  34. req := &param_v1.ProjectListRequest{}
  35. var loginUid int64
  36. var userName string
  37. parseParamTask := func() error {
  38. err := util.ShouldBind(ctx, &req.Header, nil, &req.ProjectListQuery, nil)
  39. if err != nil {
  40. logger.Error("func",
  41. zap.String("call", "util.ShouldBind"),
  42. zap.String("error", err.Error()))
  43. return errors.ParamsError
  44. }
  45. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  46. return nil
  47. }
  48. // 业务处理
  49. handleServiceTask := func() error {
  50. // 响应数据
  51. resp := param_v1.ProjectListResponse{}
  52. rpcReq := &v1.ProjectListRequest{
  53. Page:req.Page,
  54. Filter:req.Filter,
  55. Cid:loginUid,
  56. PageSize:req.PageSize,
  57. }
  58. if req.FilterStatus != "" {
  59. array := strings.Split(req.FilterStatus, ",")
  60. for _, v := range array {
  61. value, _ := strconv.Atoi(v)
  62. rpcReq.FilterStatus = append(rpcReq.FilterStatus, int32(value))
  63. }
  64. }
  65. reply, err := pb.Enterprise.ProjectList(ctx, rpcReq)
  66. if err != nil {
  67. s, _ := json.MarshalToString(req)
  68. logger.Error("func",
  69. zap.String("call", "ProjectList"),
  70. zap.String("params", s),
  71. zap.String("error", err.Error()))
  72. return errors.ErrorTransForm(err)
  73. }
  74. resp.Data = *reply
  75. if resp.Data.List == nil {
  76. resp.Data.List = make([]*v1.ProjectItem, 0)
  77. }
  78. ctx.JSON(http.StatusOK, resp)
  79. return nil
  80. }
  81. // 执行任务
  82. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  83. }
  84. // 添加项目
  85. // @Summary 添加项目
  86. // @Description 添加项目
  87. // @Tags project
  88. // @Accept json
  89. // @Produce json
  90. // @Param token header string true " "
  91. // @Param body body v1.ProjectAddBody true " "
  92. // @Success 200 {object} v1.ProjectListResponse
  93. // @Failure 500 {object} base.HTTPError
  94. // @Router /api/v1/project [post]
  95. func (c *Controller) ProjectAdd(ctx *gin.Context) {
  96. // 解析参数
  97. req := &param_v1.ProjectAddRequest{}
  98. var loginUid int64
  99. var userName string
  100. parseParamTask := func() error {
  101. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ProjectAddBody)
  102. if err != nil {
  103. logger.Error("func",
  104. zap.String("call", "util.ShouldBind"),
  105. zap.String("error", err.Error()))
  106. return errors.ParamsError
  107. }
  108. loginUid,userName, err = utils.GetJwtIdFromCtx(ctx)
  109. return nil
  110. }
  111. // 业务处理
  112. var projectId int64
  113. handleServiceTask := func() error {
  114. // 响应数据
  115. resp := param_v1.ProjectAddResponse{}
  116. rpcReq := &v1.ProjectAddRequest{}
  117. utils.StructCopy(rpcReq, &req.ProjectAddBody, "")
  118. rpcReq.CompanyId = loginUid
  119. reply, err := pb.Enterprise.ProjectAdd(ctx, rpcReq)
  120. if err != nil {
  121. s, _ := json.MarshalToString(req)
  122. logger.Error("func",
  123. zap.String("call", "ProjectAdd"),
  124. zap.String("params", s),
  125. zap.String("error", err.Error()))
  126. return errors.ErrorTransForm(err)
  127. }
  128. projectId = reply.Id
  129. ctx.JSON(http.StatusOK, resp)
  130. return nil
  131. }
  132. // 执行任务
  133. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  134. s, _ := json.MarshalToString(req)
  135. utils.LogWrite("项目申请", loginUid, userName, s, err, projectId)
  136. }
  137. // 项目账号列表
  138. // @Summary 项目账号列表
  139. // @Description 项目账号列表
  140. // @Tags project
  141. // @Accept json
  142. // @Produce json
  143. // @Param token header string true " "
  144. // @Param page query int true " "
  145. // @Param filter query string false " "
  146. // @Success 200 {object} v1.ProjectUserListResponse
  147. // @Failure 500 {object} base.HTTPError
  148. // @Router /api/v1/project/user_list [get]
  149. func (c *Controller) ProjectUserList(ctx *gin.Context) {
  150. // 解析参数
  151. req := &param_v1.ProjectUserListRequest{}
  152. var loginUid int64
  153. var userName string
  154. parseParamTask := func() error {
  155. err := util.ShouldBind(ctx, &req.Header, nil, &req.ProjectUserListQuery, 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. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  163. return nil
  164. }
  165. // 业务处理
  166. handleServiceTask := func() error {
  167. // 响应数据
  168. resp := param_v1.ProjectUserListResponse{}
  169. rpcReq := &v1.ProjectUserListRequest{
  170. Page:req.Page,
  171. Filter:req.Filter,
  172. Cid:loginUid,
  173. }
  174. reply, err := pb.Enterprise.ProjectUserList(ctx, rpcReq)
  175. if err != nil {
  176. s, _ := json.MarshalToString(req)
  177. logger.Error("func",
  178. zap.String("call", "ProjectUserList"),
  179. zap.String("params", s),
  180. zap.String("error", err.Error()))
  181. return errors.ErrorTransForm(err)
  182. }
  183. resp.Data = *reply
  184. if resp.Data.List == nil {
  185. resp.Data.List = make([]*v1.ProjectUserItem, 0)
  186. }
  187. ctx.JSON(http.StatusOK, resp)
  188. return nil
  189. }
  190. // 执行任务
  191. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  192. }
  193. // 起停用项目账号
  194. // @Summary 起停用项目账号
  195. // @Description 起停用项目账号
  196. // @Tags project
  197. // @Accept json
  198. // @Produce json
  199. // @Param token header string true " "
  200. // @Param body body v1.EnableProjectUserBody true " "
  201. // @Success 200 {object} v1.EnableProjectUserResponse
  202. // @Failure 500 {object} base.HTTPError
  203. // @Router /api/v1/project/user [put]
  204. func (c *Controller) EnableProjectUser(ctx *gin.Context) {
  205. // 解析参数
  206. req := &param_v1.EnableProjectUserRequest{}
  207. var loginUid int64
  208. var userName string
  209. parseParamTask := func() error {
  210. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.EnableProjectUserBody)
  211. if err != nil {
  212. logger.Error("func",
  213. zap.String("call", "util.ShouldBind"),
  214. zap.String("error", err.Error()))
  215. return errors.ParamsError
  216. }
  217. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  218. return nil
  219. }
  220. // 业务处理
  221. var projectId int64
  222. handleServiceTask := func() error {
  223. // 响应数据
  224. resp := param_v1.EnableProjectUserResponse{}
  225. rpcReq := &v1.EnableProjectUserRequest{
  226. Id:req.Id,
  227. Enable:req.Enable,
  228. }
  229. reply, err := pb.Enterprise.EnableProjectUser(ctx, rpcReq)
  230. if err != nil {
  231. s, _ := json.MarshalToString(req)
  232. logger.Error("func",
  233. zap.String("call", "EnableProjectUser"),
  234. zap.String("params", s),
  235. zap.String("error", err.Error()))
  236. return errors.ErrorTransForm(err)
  237. }
  238. projectId = reply.ProjectId
  239. ctx.JSON(http.StatusOK, resp)
  240. return nil
  241. }
  242. // 执行任务
  243. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  244. s, _ := json.MarshalToString(req)
  245. utils.LogWrite("起停用项目账号", loginUid, userName, s, err, projectId)
  246. }
  247. // 重置项目账号密码
  248. // @Summary 重置项目账号密码
  249. // @Description 重置项目账号密码
  250. // @Tags project
  251. // @Accept json
  252. // @Produce json
  253. // @Param token header string true " "
  254. // @Param body body v1.ProjectUserPasswordResetBody true " "
  255. // @Success 200 {object} v1.ProjectUserPasswordResetResponse
  256. // @Failure 500 {object} base.HTTPError
  257. // @Router /api/v1/project/password [put]
  258. func (c *Controller) ProjectUserPasswordReset(ctx *gin.Context) {
  259. // 解析参数
  260. req := &param_v1.ProjectUserPasswordResetRequest{}
  261. var loginUid int64
  262. var userName string
  263. parseParamTask := func() error {
  264. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ProjectUserPasswordResetBody)
  265. if err != nil {
  266. logger.Error("func",
  267. zap.String("call", "util.ShouldBind"),
  268. zap.String("error", err.Error()))
  269. return errors.ParamsError
  270. }
  271. loginUid,userName, err = utils.GetJwtIdFromCtx(ctx)
  272. return nil
  273. }
  274. // 业务处理
  275. var projectId int64
  276. handleServiceTask := func() error {
  277. // 响应数据
  278. resp := param_v1.ProjectUserPasswordResetResponse{}
  279. rpcReq := &v1.ProjectUserPasswordResetRequest{}
  280. rpcReq.Password = req.Password
  281. rpcReq.Id = req.Id
  282. _, err := pb.Enterprise.ProjectUserPasswordReset(ctx, rpcReq)
  283. if err != nil {
  284. s, _ := json.MarshalToString(req)
  285. logger.Error("func",
  286. zap.String("call", "ProjectUserPasswordReset"),
  287. zap.String("params", s),
  288. zap.String("error", err.Error()))
  289. return errors.ErrorTransForm(err)
  290. }
  291. ctx.JSON(http.StatusOK, resp)
  292. return nil
  293. }
  294. // 执行任务
  295. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  296. s, _ := json.MarshalToString(req)
  297. utils.LogWrite("重置项目账号密码", loginUid, userName, s, err, projectId)
  298. }
  299. // 变更项目
  300. // @Summary变更项目
  301. // @Description 变更项目
  302. // @Tags project
  303. // @Accept json
  304. // @Produce json
  305. // @Param token header string true " "
  306. // @Param body body v1.ProjectUpdateBody true " "
  307. // @Success 200 {object} v1.ProjectUpdateResponse
  308. // @Failure 500 {object} base.HTTPError
  309. // @Router /api/v1/project [put]
  310. func (c *Controller) ProjectUpdate(ctx *gin.Context) {
  311. // 解析参数
  312. req := &param_v1.ProjectUpdateRequest{}
  313. var loginUid int64
  314. var userName string
  315. parseParamTask := func() error {
  316. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ProjectUpdateBody)
  317. if err != nil {
  318. logger.Error("func",
  319. zap.String("call", "util.ShouldBind"),
  320. zap.String("error", err.Error()))
  321. return errors.ParamsError
  322. }
  323. loginUid,userName, err = utils.GetJwtIdFromCtx(ctx)
  324. return nil
  325. }
  326. // 业务处理
  327. handleServiceTask := func() error {
  328. // 响应数据
  329. resp := param_v1.ProjectUpdateResponse{}
  330. rpcReq := &v1.ProjectUpdateRequest{}
  331. utils.StructCopy(rpcReq, &req.ProjectUpdateBody, "")
  332. _, err := pb.Enterprise.ProjectUpdate(ctx, rpcReq)
  333. if err != nil {
  334. s, _ := json.MarshalToString(req)
  335. logger.Error("func",
  336. zap.String("call", "ProjectUpdate"),
  337. zap.String("params", s),
  338. zap.String("error", err.Error()))
  339. return errors.ErrorTransForm(err)
  340. }
  341. ctx.JSON(http.StatusOK, resp)
  342. return nil
  343. }
  344. // 执行任务
  345. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  346. s, _ := json.MarshalToString(req)
  347. utils.LogWrite("项目变更", loginUid, userName, s, err, req.Id)
  348. }
  349. // 获取省市区
  350. // @Summary获取省市区
  351. // @Description 获取省市区
  352. // @Tags project
  353. // @Accept json
  354. // @Produce json
  355. // @Success 200 {object} v1.ProjectDistrictResponse
  356. // @Failure 500 {object} base.HTTPError
  357. // @Router /api/v1/project/district [get]
  358. func (c *Controller) ProjectDistrict(ctx *gin.Context) {
  359. // 解析参数
  360. req := &param_v1.ProjectDistrictRequest{}
  361. // 业务处理
  362. handleServiceTask := func() error {
  363. // 响应数据
  364. resp := param_v1.ProjectDistrictResponse{}
  365. rpcReq := &v1.ProjectDistrictRequest{}
  366. rpcResp, err := pb.Enterprise.ProjectDistrict(ctx, rpcReq)
  367. if err != nil {
  368. s, _ := json.MarshalToString(req)
  369. logger.Error("func",
  370. zap.String("call", "ProjectDistrict"),
  371. zap.String("params", s),
  372. zap.String("error", err.Error()))
  373. return errors.ErrorTransForm(err)
  374. }
  375. if rpcResp.List == nil {
  376. rpcResp.List = []*v1.ProjectDistrictProvince{}
  377. }
  378. for i, v := range rpcResp.List {
  379. if v.List == nil {
  380. rpcResp.List[i].List = []*v1.ProjectDistrictCity{}
  381. continue
  382. }
  383. for j, z := range rpcResp.List[i].List {
  384. if z.List == nil {
  385. rpcResp.List[i].List[j].List = []*v1.ProjectDistrictZone{}
  386. }
  387. }
  388. }
  389. resp.Data = *rpcResp
  390. ctx.JSON(http.StatusOK, resp)
  391. return nil
  392. }
  393. // 执行任务
  394. httptasker.Exec(ctx, handleServiceTask)
  395. }
  396. // 项目详情
  397. // @Summary 项目详情
  398. // @Description 项目详情
  399. // @Tags project
  400. // @Accept json
  401. // @Produce json
  402. // @Param token header string true " "
  403. // @Param id path int64 true " "
  404. // @Success 200 {object} v1.ProjectInfoResponse
  405. // @Failure 500 {object} base.HTTPError
  406. // @Router /api/v1/project/info/{id} [get]
  407. func (c *Controller) ProjectInfo(ctx *gin.Context) {
  408. // 解析参数
  409. req := &param_v1.ProjectInfoRequest{}
  410. var loginUid int64
  411. var userName string
  412. parseParamTask := func() error {
  413. err := util.ShouldBind(ctx, &req.Header, &req.ProjectInfoPath, nil, nil)
  414. if err != nil {
  415. logger.Error("func",
  416. zap.String("call", "util.ShouldBind"),
  417. zap.String("error", err.Error()))
  418. return errors.ParamsError
  419. }
  420. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  421. return nil
  422. }
  423. // 业务处理
  424. handleServiceTask := func() error {
  425. // 响应数据
  426. resp := param_v1.ProjectInfoResponse{}
  427. rpcReq := &v1.ProjectInfoRequest{
  428. Id:req.Id,
  429. }
  430. reply, err := pb.Enterprise.ProjectInfo(ctx, rpcReq)
  431. if err != nil {
  432. s, _ := json.MarshalToString(req)
  433. logger.Error("func",
  434. zap.String("call", "ProjectInfo"),
  435. zap.String("params", s),
  436. zap.String("error", err.Error()))
  437. return errors.ErrorTransForm(err)
  438. }
  439. resp.Data = *reply
  440. ctx.JSON(http.StatusOK, resp)
  441. return nil
  442. }
  443. // 执行任务
  444. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  445. }
  446. // 项目删除
  447. // @Summary 项目删除
  448. // @Description 项目删除
  449. // @Tags project
  450. // @Accept json
  451. // @Produce json
  452. // @Param token header string true " "
  453. // @Param id path int64 true " "
  454. // @Success 200 {object} v1.ProjectDelResponse
  455. // @Failure 500 {object} base.HTTPError
  456. // @Router /api/v1/project/{id} [delete]
  457. func (c *Controller) ProjectDel(ctx *gin.Context) {
  458. // 解析参数
  459. req := &param_v1.ProjectDelRequest{}
  460. var loginUid int64
  461. var userName string
  462. parseParamTask := func() error {
  463. err := util.ShouldBind(ctx, &req.Header, &req.ProjectDelPath, nil, nil)
  464. if err != nil {
  465. logger.Error("func",
  466. zap.String("call", "util.ShouldBind"),
  467. zap.String("error", err.Error()))
  468. return errors.ParamsError
  469. }
  470. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  471. return nil
  472. }
  473. origin := ""
  474. // 业务处理
  475. handleServiceTask := func() error {
  476. // 响应数据
  477. resp := param_v1.ProjectDelResponse{}
  478. rpcReq := &v1.ProjectDelRequest{
  479. Id:req.Id,
  480. }
  481. reply, err := pb.Enterprise.ProjectDel(ctx, rpcReq)
  482. if err != nil {
  483. s, _ := json.MarshalToString(req)
  484. logger.Error("func",
  485. zap.String("call", "ProjectDel"),
  486. zap.String("params", s),
  487. zap.String("error", err.Error()))
  488. return errors.ErrorTransForm(err)
  489. }
  490. origin = reply.Origin
  491. ctx.JSON(http.StatusOK, resp)
  492. return nil
  493. }
  494. // 执行任务
  495. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  496. utils.LogWrite("项目删除", loginUid, userName, origin, err, req.Id)
  497. }
  498. //
  499. // @Summary 项目完工
  500. // @Description 项目完工
  501. // @Tags project
  502. // @Accept json
  503. // @Produce json
  504. // @Param token header string true " "
  505. // @Param id path int64 true " "
  506. // @Success 200 {object} v1.ProjectFinishResponse
  507. // @Failure 500 {object} base.HTTPError
  508. // @Router /api/v1/project/finish/{id} [put]
  509. func (c *Controller) ProjectFinish(ctx *gin.Context) {
  510. // 解析参数
  511. req := &param_v1.ProjectFinishRequest{}
  512. var loginUid int64
  513. var userName string
  514. parseParamTask := func() error {
  515. err := util.ShouldBind(ctx, &req.Header, &req.ProjectFinishPath, nil, nil)
  516. if err != nil {
  517. logger.Error("func",
  518. zap.String("call", "util.ShouldBind"),
  519. zap.String("error", err.Error()))
  520. return errors.ParamsError
  521. }
  522. loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
  523. return nil
  524. }
  525. // 业务处理
  526. handleServiceTask := func() error {
  527. // 响应数据
  528. resp := param_v1.ProjectFinishResponse{}
  529. rpcReq := &v1.ProjectFinishRequest{
  530. Id:req.Id,
  531. }
  532. _, err := pb.Enterprise.ProjectFinish(ctx, rpcReq)
  533. if err != nil {
  534. s, _ := json.MarshalToString(req)
  535. logger.Error("func",
  536. zap.String("call", "ProjectFinish"),
  537. zap.String("params", s),
  538. zap.String("error", err.Error()))
  539. return errors.ErrorTransForm(err)
  540. }
  541. ctx.JSON(http.StatusOK, resp)
  542. return nil
  543. }
  544. // 执行任务
  545. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  546. utils.LogWrite("项目完工", loginUid, userName, "", err, req.Id)
  547. }