dhcp.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package dao
  2. import (
  3. "dhcp/global"
  4. "dhcp/model"
  5. )
  6. // 初始化数据库指针
  7. var db = global.DB
  8. func AddDHCP(dhcp model.DHCP) error {
  9. var dp model.DHCP
  10. tx := db.Model(&model.DHCP{}).Where("mac_address = ?", dhcp.MACAddress).First(dp)
  11. if tx.Error != nil {
  12. return tx.Error
  13. }
  14. if dp.Id == 0 {
  15. tx = db.Create(&dhcp)
  16. } else {
  17. tx = db.Model(&model.DHCP{}).Where("mac_address = ?", dhcp.MACAddress).Updates(dhcp)
  18. }
  19. if tx.Error != nil {
  20. return tx.Error
  21. }
  22. return nil
  23. }
  24. func GetDHCPByMac(macAddress string) (model.DHCP, error) {
  25. var dhcp model.DHCP
  26. tx := db.Model(&model.DHCP{}).Where("mac_address = ?", macAddress).First(&dhcp)
  27. if tx.Error != nil {
  28. return dhcp, tx.Error
  29. }
  30. return dhcp, nil
  31. }
  32. func GetDHCP() ([]model.DHCP, error) {
  33. var dhcp []model.DHCP
  34. tx := db.Model(&model.DHCP{}).Find(&dhcp)
  35. if tx.Error != nil {
  36. return nil, tx.Error
  37. }
  38. return dhcp, nil
  39. }
  40. func DeleteDHCP(macAddress string) error {
  41. tx := db.Model(&model.DHCP{}).Where("mac_address = ?", macAddress).Delete(&model.DHCP{})
  42. if tx.Error != nil {
  43. return tx.Error
  44. }
  45. return nil
  46. }
  47. func UpdateDHCP(dhcp model.DHCP) error {
  48. tx := db.Model(&model.DHCP{}).Where("mac_address = ?", dhcp.MACAddress).Updates(dhcp)
  49. if tx.Error != nil {
  50. return tx.Error
  51. }
  52. return nil
  53. }