repair_order_del.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "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 checkRepairOrderDelParam(req *pb_v1.RepairOrderDelRequest) 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 RepairOrderDel(ctx context.Context, req *pb_v1.RepairOrderDelRequest) (reply *pb_v1.RepairOrderDelReply, err error) {
  30. reply = &pb_v1.RepairOrderDelReply{}
  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 = checkRepairOrderDelParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. // 获取旧数据
  50. old := dbmodel.NewRepairOrder(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 == RepairOrderStatusSend {
  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.RepairOrderUpdateRequest{
  68. GardenId:req.GardenId,
  69. Id:req.Id,
  70. // 分类id
  71. ClassId:old.ClassId,
  72. // 报修人
  73. ApplyPeople:old.ApplyPeople,
  74. // 报修人电话
  75. ApplyPeoplePhone:old.ApplyPeoplePhone,
  76. // 预约时间
  77. Appointment: old.Appointment.Unix(),
  78. // 房屋id
  79. HouseId:old.HouseId,
  80. HouseholdUid:old.HouseholdUid,
  81. // 报修内容
  82. ApplyContent:old.ApplyContent,
  83. // 报修图片
  84. ApplyPic:strings.Split(old.ApplyPic, ";"),
  85. }
  86. // 删除数据
  87. repairOrder := dbmodel.NewRepairOrder(dbname)
  88. where = map[string]interface{}{
  89. "id":req.Id,
  90. }
  91. db := database.DB().Begin()
  92. err = repairOrder.Delete(db, where)
  93. if err != nil {
  94. db.Rollback()
  95. return nil, errors.DataBaseError
  96. }
  97. statisticReq := pb_v1.RepairStatisticSetRequest{
  98. HandleType:statistic.HandleTypeRepair,
  99. TotalIncrease:int64(-1),
  100. GardenId:req.GardenId,
  101. }
  102. if old.Status == RepairOrderStatusFinish {
  103. statisticReq.FinishIncrease = -1
  104. }
  105. err = statistic.RepairStatisticSetWithDb(&statisticReq, db)
  106. if err != nil {
  107. db.Rollback()
  108. return nil, err
  109. }
  110. db.Commit()
  111. return reply, nil
  112. }