choose.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package maintainance
  2. import (
  3. "adm-vehicle-style/errors"
  4. "adm-vehicle-style/model"
  5. v1 "adm-vehicle-style/pb/v1"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. "gorm.io/gorm"
  15. )
  16. func ChooseTemplateDetail(ctx context.Context, req *v1.ChooseTemplateDetailRequest) (reply *v1.EmptyReply, err error) {
  17. reply = &v1.EmptyReply{}
  18. defer func() {
  19. if r := recover(); r != nil {
  20. err = fmt.Errorf("%+v", r)
  21. e := &status.Status{}
  22. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  23. logger.Error("err",
  24. zap.String("system_err", err.Error()),
  25. zap.Stack("stacktrace"))
  26. }
  27. }
  28. }()
  29. var (
  30. tx = database.DB().Begin()
  31. )
  32. defer func() {
  33. if err != nil {
  34. tx.Rollback()
  35. return
  36. }
  37. tx.Commit()
  38. }()
  39. // 删除原有维保手册
  40. if err := model.NewMaintainDetailModel().Delete(tx.Where("style_id = ?", req.StyleId)); err != nil {
  41. return reply, errors.SystemError
  42. }
  43. if err := model.NewMaintainTitleModel().Delete(tx.Where("style_id = ?", req.StyleId)); err != nil {
  44. return reply, errors.SystemError
  45. }
  46. // 查询模板详情
  47. list, err := model.NewMaintainDetailTemplateModel().List(database.DB().Where("tid = ?", req.Tid))
  48. if err != nil {
  49. if err == gorm.ErrRecordNotFound {
  50. return reply, errors.DataNotExistError
  51. }
  52. return reply, errors.SystemError
  53. }
  54. inserts := make([]model.GdMaintainDetail, 0, len(list))
  55. for _, v := range list {
  56. inserts = append(inserts, model.GdMaintainDetail{
  57. StyleId: req.StyleId,
  58. ItemId: v.ItemId,
  59. StartMile: v.StartMile,
  60. MileCycle: v.MileCycle,
  61. StartDate: v.StartDate,
  62. DateCycle: v.DateCycle,
  63. CreatedAt: time.Now(),
  64. UpdatedAt: time.Now(),
  65. })
  66. }
  67. if err := model.NewMaintainDetailModel().Insert(tx, inserts); err != nil {
  68. return reply, errors.SystemError
  69. }
  70. // 查询模板标题
  71. title, err := model.NewMaintenanceTitleTemplateModel().Get(database.DB().Where("tid = ?", req.Tid))
  72. if err != nil {
  73. if err == gorm.ErrRecordNotFound {
  74. return reply, errors.DataNotExistError
  75. }
  76. return reply, errors.SystemError
  77. }
  78. // 没有title
  79. insert := &model.GdMaintainTitle{
  80. StartMile: title.StartMile,
  81. StartDate: title.StartDate,
  82. StyleId: req.StyleId,
  83. MaintainMileMinCycle: title.MaintainMileMinCycle,
  84. MaintainDateMinCycle: title.MaintainDateMinCycle,
  85. RepairCycle: title.RepairCycle,
  86. WashCycle: title.WashCycle,
  87. CreatedAt: time.Now(),
  88. UpdatedAt: time.Now(),
  89. }
  90. if err := model.NewMaintainTitleModel().Insert(tx, insert); err != nil {
  91. return reply, errors.SystemError
  92. }
  93. return reply, nil
  94. }