booth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package parking
  2. import (
  3. "errors"
  4. "server/dao"
  5. "server/global"
  6. "server/model/parking/request"
  7. )
  8. type boothService struct{}
  9. func NewBoothService() *boothService {
  10. return &boothService{}
  11. }
  12. // CreateBooth 创建岗亭
  13. func (s *boothService) CreateBooth(req request.BoothCreate) error {
  14. // 检查岗亭编码是否已存在
  15. var existingBooth dao.Booth
  16. result := global.GVA_DB.Where("booth_code = ?", req.BoothCode).First(&existingBooth)
  17. if result.RowsAffected > 0 {
  18. return errors.New("岗亭编码已存在")
  19. }
  20. booth := dao.Booth{
  21. BoothCode: req.BoothCode,
  22. BoothName: req.BoothName,
  23. IPAddress: req.IPAddress,
  24. Description: req.Description,
  25. ParkingLotID: req.ParkingLotID,
  26. }
  27. return global.GVA_DB.Create(&booth).Error
  28. }
  29. // UpdateBooth 更新岗亭
  30. func (s *boothService) UpdateBooth(req request.BoothUpdate) error {
  31. var booth dao.Booth
  32. result := global.GVA_DB.Preload("Channels").Preload("Channels.UHFReaders").First(&booth, req.ID)
  33. if result.RowsAffected == 0 {
  34. return errors.New("岗亭不存在")
  35. }
  36. // 检查岗亭编码是否被其他岗亭使用
  37. var existingBooth dao.Booth
  38. result = global.GVA_DB.Where("booth_code = ? AND id != ?", req.BoothCode, req.ID).First(&existingBooth)
  39. if result.RowsAffected > 0 {
  40. return errors.New("岗亭编码已被其他岗亭使用")
  41. }
  42. booth.BoothCode = req.BoothCode
  43. booth.BoothName = req.BoothName
  44. booth.IPAddress = req.IPAddress
  45. booth.Description = req.Description
  46. booth.ParkingLotID = req.ParkingLotID
  47. // 修改岗亭的区域id,那么它下面的子级关系的区域id都要改
  48. for _, channel := range booth.Channels {
  49. global.GVA_DB.Model(&channel).Where("id = ?", channel.ID).Update("parking_lot_id", req.ParkingLotID)
  50. for _, reader := range channel.UHFReaders {
  51. global.GVA_DB.Model(&reader).Where("id = ?", channel.ID).Update("parking_lot_id", req.ParkingLotID)
  52. }
  53. }
  54. return global.GVA_DB.Save(&booth).Error
  55. }
  56. // GetBoothByID 根据ID获取岗亭
  57. func (s *boothService) GetBoothByID(id uint) (dao.Booth, error) {
  58. var booth dao.Booth
  59. result := global.GVA_DB.First(&booth, id)
  60. if result.RowsAffected == 0 {
  61. return dao.Booth{}, errors.New("岗亭不存在")
  62. }
  63. return booth, nil
  64. }
  65. // GetBoothByCode 根据编码获取岗亭
  66. func (s *boothService) GetBoothByCode(code string) (dao.Booth, error) {
  67. var booth dao.Booth
  68. result := global.GVA_DB.Where("booth_code = ?", code).First(&booth)
  69. if result.RowsAffected == 0 {
  70. return dao.Booth{}, errors.New("岗亭不存在")
  71. }
  72. return booth, nil
  73. }
  74. func (s *boothService) QueryAllBooths() ([]dao.Booth, error) {
  75. var booths []dao.Booth
  76. err := global.GVA_DB.Find(&booths).Error
  77. if err != nil {
  78. return []dao.Booth{}, err
  79. }
  80. return booths, nil
  81. }
  82. // ListBooths 获取岗亭列表
  83. func (s *boothService) ListBooths(req request.BoothQuery) (int64, []dao.Booth, error) {
  84. var booths []dao.Booth
  85. var total int64
  86. query := global.GVA_DB.Model(&dao.Booth{})
  87. if req.BoothCode != "" {
  88. query = query.Where("booth_code LIKE ?", "%"+req.BoothCode+"%")
  89. }
  90. if req.BoothName != "" {
  91. query = query.Where("booth_name LIKE ?", "%"+req.BoothName+"%")
  92. }
  93. // 计算总数
  94. query.Count(&total)
  95. // 分页查询
  96. offset := (req.Page - 1) * req.PageSize
  97. result := query.Offset(offset).Limit(req.PageSize).Preload("ParkingLot").Find(&booths)
  98. if result.Error != nil {
  99. return 0, nil, result.Error
  100. }
  101. return total, booths, nil
  102. }
  103. // DeleteBooth 删除岗亭
  104. func (s *boothService) DeleteBooth(id uint) error {
  105. result := global.GVA_DB.Delete(&dao.Booth{}, id)
  106. if result.RowsAffected == 0 {
  107. return errors.New("岗亭不存在")
  108. }
  109. return result.Error
  110. }