suggestion_order_del.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package suggestion
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/statistic"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "strings"
  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 checkSuggestionOrderDelParam(req *pb_v1.SuggestionOrderDelRequest) error {
  21. switch {
  22. case req.Id == 0:
  23. return status.Error(10003, "id 不能为空")
  24. case req.GardenId == 0:
  25. return status.Error(10003, "小区不能为空")
  26. }
  27. return nil
  28. }
  29. func SuggestionOrderDel(ctx context.Context, req *pb_v1.SuggestionOrderDelRequest) (reply *pb_v1.SuggestionOrderDelReply, err error) {
  30. reply = &pb_v1.SuggestionOrderDelReply{}
  31. // 捕获各个task中的异常并返回给调用者
  32. defer func() {
  33. if r := recover(); r != nil {
  34. err = fmt.Errorf("%+v", r)
  35. e := &status.Status{}
  36. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  37. logger.Error("err",
  38. zap.String("system_err", err.Error()),
  39. zap.Stack("stacktrace"))
  40. }
  41. }
  42. }()
  43. // 参数检查
  44. err = checkSuggestionOrderDelParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. // 获取旧数据
  50. old := dbmodel.NewSuggestionOrder(dbname)
  51. where := map[string]interface{}{
  52. "id":req.Id,
  53. }
  54. err = old.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, errors.DataBaseError
  57. }
  58. if old.ID == 0 {
  59. return nil, errors.ErrRecordNotFound
  60. }
  61. if old.Status == SuggestionOrderStatusSend {
  62. return nil, status.Error(10003, "已派单,不能删除")
  63. }
  64. if req.HouseholdUid > 0 && old.HouseholdUid != req.HouseholdUid{
  65. return nil, status.Error(10003,"您无法进行该操作")
  66. }
  67. reply.Origin = &pb_v1.SuggestionOrderUpdateRequest{
  68. GardenId:req.GardenId,
  69. Id:req.Id,
  70. // 分类id
  71. SuggestionType:old.SuggestionType,
  72. // 报修人
  73. ApplyPeople:old.ApplyPeople,
  74. // 报修人电话
  75. ApplyPeoplePhone:old.ApplyPeoplePhone,
  76. HouseholdUid:old.HouseholdUid,
  77. // 报修内容
  78. ApplyContent:old.ApplyContent,
  79. // 报修图片
  80. ApplyPic:strings.Split(old.ApplyPic, ";"),
  81. }
  82. // 删除数据
  83. repairOrder := dbmodel.NewSuggestionOrder(dbname)
  84. where = map[string]interface{}{
  85. "id":req.Id,
  86. }
  87. db := database.DB().Begin()
  88. err = repairOrder.Delete(db, where)
  89. if err != nil {
  90. db.Rollback()
  91. return nil, errors.DataBaseError
  92. }
  93. statisticReq := pb_v1.RepairStatisticSetRequest{
  94. HandleType:statistic.HandleTypeSuggestion,
  95. TotalIncrease:int64(-1),
  96. GardenId:req.GardenId,
  97. }
  98. if old.Status == SuggestionOrderStatusFinish {
  99. statisticReq.FinishIncrease = -1
  100. }
  101. err = statistic.RepairStatisticSetWithDb(&statisticReq, db)
  102. if err != nil {
  103. db.Rollback()
  104. return nil, err
  105. }
  106. db.Commit()
  107. return reply, nil
  108. }