application_set.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package cpackage
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. dbmodel "property-common/model"
  9. pb_v1 "property-common/pb/v1"
  10. "time"
  11. "git.getensh.com/common/gopkgs/database"
  12. "git.getensh.com/common/gopkgs/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "property-common/errors"
  16. )
  17. func checkPackageApplicationSetParam(req *pb_v1.PackageApplicationSetRequest) error {
  18. switch {
  19. case req.Id == 0:
  20. return status.Error(10003, "id不能为空")
  21. }
  22. return nil
  23. }
  24. //
  25. func PackageApplicationSet(ctx context.Context, req *pb_v1.PackageApplicationSetRequest) (reply *pb_v1.PackageApplicationSetReply, err error) {
  26. reply = &pb_v1.PackageApplicationSetReply{}
  27. // 捕获各个task中的异常并返回给调用者
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("%+v", r)
  31. e := &status.Status{}
  32. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  33. logger.Error("err",
  34. zap.String("system_err", err.Error()),
  35. zap.Stack("stacktrace"))
  36. }
  37. }
  38. }()
  39. // 参数检查
  40. err = checkPackageApplicationSetParam(req)
  41. if err != nil {
  42. return nil, err
  43. }
  44. now := time.Now()
  45. where := map[string]interface{}{
  46. "package_id": req.Id,
  47. }
  48. array := make([]dbmodel.TPackageApplication, len(req.ApplicationIds))
  49. for i, v := range req.ApplicationIds {
  50. array[i].PackageId = req.Id
  51. array[i].ApplicationId = v
  52. array[i].CreatedAt = now
  53. array[i].UpdatedAt = now
  54. }
  55. db := database.DB().Begin()
  56. p := dbmodel.TPackageApplication{}
  57. err = p.Delete(db, where)
  58. if err != nil {
  59. db.Rollback()
  60. return nil, errors.DataBaseError
  61. }
  62. if len(array) > 0 {
  63. err = p.InsertMulti(db, &array)
  64. if err != nil {
  65. db.Rollback()
  66. return nil, errors.DataBaseError
  67. }
  68. }
  69. db.Commit()
  70. return reply, nil
  71. }