booth.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  26. return global.GVA_DB.Create(&booth).Error
  27. }
  28. // UpdateBooth 更新岗亭
  29. func (s *boothService) UpdateBooth(req request.BoothUpdate) error {
  30. var booth dao.Booth
  31. result := global.GVA_DB.First(&booth, req.ID)
  32. if result.RowsAffected == 0 {
  33. return errors.New("岗亭不存在")
  34. }
  35. // 检查岗亭编码是否被其他岗亭使用
  36. var existingBooth dao.Booth
  37. result = global.GVA_DB.Where("booth_code = ? AND id != ?", req.BoothCode, req.ID).First(&existingBooth)
  38. if result.RowsAffected > 0 {
  39. return errors.New("岗亭编码已被其他岗亭使用")
  40. }
  41. booth.BoothCode = req.BoothCode
  42. booth.BoothName = req.BoothName
  43. booth.IPAddress = req.IPAddress
  44. booth.Description = req.Description
  45. return global.GVA_DB.Save(&booth).Error
  46. }
  47. // GetBoothByID 根据ID获取岗亭
  48. func (s *boothService) GetBoothByID(id uint) (dao.Booth, error) {
  49. var booth dao.Booth
  50. result := global.GVA_DB.First(&booth, id)
  51. if result.RowsAffected == 0 {
  52. return dao.Booth{}, errors.New("岗亭不存在")
  53. }
  54. return booth, nil
  55. }
  56. // GetBoothByCode 根据编码获取岗亭
  57. func (s *boothService) GetBoothByCode(code string) (dao.Booth, error) {
  58. var booth dao.Booth
  59. result := global.GVA_DB.Where("booth_code = ?", code).First(&booth)
  60. if result.RowsAffected == 0 {
  61. return dao.Booth{}, errors.New("岗亭不存在")
  62. }
  63. return booth, nil
  64. }
  65. // ListBooths 获取岗亭列表
  66. func (s *boothService) ListBooths(req request.BoothQuery) (int64, []dao.Booth, error) {
  67. var booths []dao.Booth
  68. var total int64
  69. query := global.GVA_DB.Model(&dao.Booth{})
  70. if req.BoothCode != "" {
  71. query = query.Where("booth_code LIKE ?", "%"+req.BoothCode+"%")
  72. }
  73. if req.BoothName != "" {
  74. query = query.Where("booth_name LIKE ?", "%"+req.BoothName+"%")
  75. }
  76. // 计算总数
  77. query.Count(&total)
  78. // 分页查询
  79. offset := (req.Page - 1) * req.PageSize
  80. result := query.Offset(offset).Limit(req.PageSize).Find(&booths)
  81. if result.Error != nil {
  82. return 0, nil, result.Error
  83. }
  84. return total, booths, nil
  85. }
  86. // DeleteBooth 删除岗亭
  87. func (s *boothService) DeleteBooth(id uint) error {
  88. result := global.GVA_DB.Delete(&dao.Booth{}, id)
  89. if result.RowsAffected == 0 {
  90. return errors.New("岗亭不存在")
  91. }
  92. return result.Error
  93. }