editList.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package announcement_management
  2. import (
  3. "adm-management/errors"
  4. v1 "adm-management/pb/v1"
  5. "context"
  6. "reflect"
  7. "encoding/json"
  8. "fmt"
  9. "git.getensh.com/common/gopkgsv2/database"
  10. "git.getensh.com/common/gopkgsv2/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. )
  15. type Edit struct {
  16. ModelNo string `gorm:"column:model_no" json:"model_no" form:"model_no"`
  17. VehicleBatch string `gorm:"column:vehicle_batch" json:"vehicle_batch" form:"vehicle_batch"`
  18. BrandName string `gorm:"column:brand_name" json:"brand_name" form:"brand_name"`
  19. VehicleTypeDetail string `gorm:"column:vehicle_type_detail" json:"vehicle_type_detail" form:"vehicle_type_detail"`
  20. ApprovedLoad string `gorm:"column:approved_load" json:"approved_load" form:"approved_load"`
  21. GrossMass string `gorm:"column:gross_mass" json:"gross_mass" form:"gross_mass"`
  22. UnladenMass string `gorm:"column:unladen_mass" json:"unladen_mass" form:"unladen_mass"`
  23. FuelTypeDetail string `gorm:"column:fuel_type_detail" json:"fuel_type_detail" form:"fuel_type_detail"`
  24. AxleNumber string `gorm:"column:axle_number" json:"axle_number" form:"axle_number"`
  25. EmissionStandard string `gorm:"column:emission_standard" json:"emission_standard" form:"emission_standard"`
  26. Wheelbase string `gorm:"column:wheelbase" json:"wheelbase" form:"wheelbase"`
  27. AxleWeight string `gorm:"column:axle_weight" json:"axle_weight" form:"axle_weight"`
  28. NumberOfLeafSprings string `gorm:"column:number_of_leaf_springs" json:"number_of_leaf_springs" form:"number_of_leaf_springs"`
  29. TyreNumber string `gorm:"column:tyre_number" json:"tyre_number" form:"tyre_number"`
  30. TyreSize string `gorm:"column:tyre_size" json:"tyre_size" form:"tyre_size"`
  31. ApproachingDepartureAngle string `gorm:"column:approaching_departure_angle" json:"approaching_departure_angle" form:"approaching_departure_angle"`
  32. FrontBackSuspention string `gorm:"column:front_back_suspention" json:"front_back_suspention" form:"front_back_suspention"`
  33. FrontWheelDistance string `gorm:"column:front_wheel_distance" json:"front_wheel_distance" form:"front_wheel_distance"`
  34. BackWheelDistance string `gorm:"column:back_wheel_distance" json:"back_wheel_distance" form:"back_wheel_distance"`
  35. VinRules string `gorm:"column:vin_rules" json:"vin_rules" form:"vin_rules"`
  36. Long string `gorm:"column:long" json:"long" form:"long"`
  37. Wide string `gorm:"column:wide" json:"wide" form:"wide"`
  38. High string `gorm:"column:high" json:"high" form:"high"`
  39. CargoLong string `gorm:"column:cargo_long" json:"cargo_long" form:"cargo_long"`
  40. CargoWide string `gorm:"column:cargo_wide" json:"cargo_wide" form:"cargo_wide"`
  41. CargoHigh string `gorm:"column:cargo_high" json:"cargo_high" form:"cargo_high"`
  42. TopSpeed string `gorm:"column:top_speed" json:"top_speed" form:"top_speed"`
  43. ApprovedNumber string `gorm:"column:approved_number" json:"approved_number" form:"approved_number"`
  44. NumberOfPassengers string `gorm:"column:number_of_passengers" json:"number_of_passengers" form:"number_of_passengers"`
  45. SteeringMode string `gorm:"column:steering_mode" json:"steering_mode" form:"steering_mode"`
  46. TractionMass string `gorm:"column:traction_mass" json:"traction_mass" form:"traction_mass"`
  47. LoadMassCoefficient string `gorm:"column:load_mass_coefficient" json:"load_mass_coefficient" form:"load_mass_coefficient"`
  48. SemitrailerSaddleMaxAllowGrossMass string `gorm:"column:semitrailer_saddle_max_allow_gross_mass" json:"semitrailer_saddle_max_allow_gross_mass" form:"semitrailer_saddle_max_allow_gross_mass"`
  49. CompanyName string `gorm:"column:company_name" json:"company_name" form:"company_name"`
  50. CompanyAddress string `gorm:"column:company_address" json:"company_address" form:"company_address"`
  51. }
  52. type field struct {
  53. Eng string `gorm:"column:field" json:"eng" form:"eng"`
  54. Chi string `gorm:"column:desc" json:"chi" form:"chi"`
  55. }
  56. func EditList(ctx context.Context, req *v1.EditRequest) (reply *v1.EditReply, err error) {
  57. defer func() {
  58. if r := recover(); r != nil {
  59. err = fmt.Errorf("%+v", r)
  60. e := &status.Status{}
  61. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  62. logger.Error("err",
  63. zap.String("system_err", err.Error()),
  64. zap.Stack("stacktrace"))
  65. }
  66. }
  67. }()
  68. reply = &v1.EditReply{}
  69. db := database.DB()
  70. detail := Edit{}
  71. err = db.Raw("SELECT model_no, vehicle_batch, brand_name, vehicle_type_detail, approved_load, gross_mass, unladen_mass, fuel_type_detail, axle_number, emission_standard, wheelbase, axle_weight, number_of_leaf_springs, tyre_number, tyre_size, approaching_departure_angle, front_back_suspention, front_wheel_distance, back_wheel_distance, vin_rules, `long`, wide, high, cargo_long, cargo_wide, cargo_high, top_speed, approved_number, number_of_passengers, steering_mode, traction_mass, load_mass_coefficient, semitrailer_saddle_max_allow_gross_mass, company_name, company_address FROM db_adm_dws.t_adm_dws16 where vehicle_batch = ? and model_no = ?", req.VehicleBatch, req.ModelNo).Find(&detail).Error
  72. if err != nil {
  73. if err == gorm.ErrRecordNotFound {
  74. return reply, nil
  75. }
  76. return reply, errors.SystemError
  77. }
  78. var list []*v1.EditList
  79. t := reflect.TypeOf(detail)
  80. v := reflect.ValueOf(detail)
  81. var fields []field
  82. err = db.Raw("select field, `desc` from db_adm_management.t_adm_field_management where field in ('model_no', 'vehicle_batch', 'brand_name', 'vehicle_type_detail', 'approved_load', 'gross_mass', 'unladen_mass', 'fuel_type_detail', 'axle_number', 'emission_standard', 'wheelbase', 'axle_weight', 'number_of_leaf_springs', 'tyre_number', 'tyre_size', 'approaching_departure_angle', 'front_back_suspention', 'front_wheel_distance', 'back_wheel_distance', 'vin_rules', 'long', 'wide', 'high', 'cargo_long', 'cargo_wide', 'cargo_high', 'top_speed', 'approved_number', 'number_of_passengers', 'steering_mode', 'traction_mass', 'load_mass_coefficient', 'semitrailer_saddle_max_allow_gross_mass', 'company_name', 'company_address')").Find(&fields).Error
  83. maps := make(map[string]string)
  84. for _, v := range fields {
  85. maps[v.Eng] = v.Chi
  86. }
  87. for i := 0; i < t.NumField(); i++ {
  88. list = append(list, &v1.EditList{
  89. Key: t.Field(i).Tag.Get("json"),
  90. Desc: maps[t.Field(i).Tag.Get("json")],
  91. Value: v.Field(i).Interface().(string),
  92. })
  93. }
  94. reply.List = list
  95. return reply, nil
  96. }