repair_class_update.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package repair
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "strings"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkRepairClassUpdateParam(req *pb_v1.RepairClassUpdateRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Name == "":
  25. return status.Error(10003, "名称不能为空")
  26. case req.Id == 0:
  27. return status.Error(10003, "id 不能为空")
  28. }
  29. return nil
  30. }
  31. func RepairClassUpdate(ctx context.Context, req *pb_v1.RepairClassUpdateRequest) (reply *pb_v1.RepairClassUpdateReply, err error) {
  32. reply = &pb_v1.RepairClassUpdateReply{}
  33. // 捕获各个task中的异常并返回给调用者
  34. defer func() {
  35. if r := recover(); r != nil {
  36. err = fmt.Errorf("%+v", r)
  37. e := &status.Status{}
  38. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  39. logger.Error("err",
  40. zap.String("system_err", err.Error()),
  41. zap.Stack("stacktrace"))
  42. }
  43. }
  44. }()
  45. // 参数检查
  46. err = checkRepairClassUpdateParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. // 获取旧数据
  52. old := dbmodel.NewRepairClass(dbname)
  53. where := map[string]interface{}{
  54. "id":req.Id,
  55. }
  56. err = old.Find(database.DB(), where)
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return nil, errors.DataBaseError
  59. }
  60. if old.ID == 0 {
  61. return nil, errors.ErrRecordNotFound
  62. }
  63. reply.Origin = &pb_v1.RepairClassUpdateRequest{
  64. GardenId:req.GardenId,
  65. Id:req.Id,
  66. Name:req.Name,
  67. Enable:false,
  68. }
  69. if old.Enable == 1 {
  70. reply.Origin.Enable = true
  71. }
  72. if old.Enable == 1 && !req.Enable {
  73. repairOrder := dbmodel.NewRepairOrder(dbname)
  74. where := map[string]interface{}{
  75. "status in":[]int32{RepairOrderStatusSend, RepairOrderStatusCreate},
  76. "class_id":req.Id,
  77. }
  78. count, err := repairOrder.Count(database.DB(), where, nil)
  79. if err != nil {
  80. return nil, errors.DataBaseError
  81. }
  82. if count > 0 {
  83. return nil, status.Error(10003, "有工单未处理,不能禁用")
  84. }
  85. }
  86. // 更新新数据
  87. now := time.Now()
  88. class := dbmodel.NewRepairClass(dbname)
  89. enable := 1
  90. if !req.Enable {
  91. enable = 2
  92. }
  93. values := map[string]interface{}{
  94. "name":req.Name,
  95. "enable":enable,
  96. "updated_at":now,
  97. "area":req.Area,
  98. }
  99. where = map[string]interface{}{
  100. "id":req.Id,
  101. }
  102. err = class.Update(database.DB(), where, values)
  103. if err != nil {
  104. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  105. return nil, status.Error(10003, "分类名已存在")
  106. }
  107. return nil, errors.DataBaseError
  108. }
  109. return reply, nil
  110. }